⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symbol.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------------
// Symbol Information
// Contains definitions of all Symbol Table entries.
// All classes for a symbol table entry are appended with 'Entry'
// Class organization:
// SymEntry
// + NamespaceEntry
//      + ImportedNamespaceEntry - namespace imported from external assembly
//      + UserNamespaceEntry -namespace defined in our sources
// + TypeEntry - raw type (both Classes & primitives)
// + ExpEntry - can be used in an expression, has a TypeEntry
//      + MethodExpEntry - for a specific method
//      + VarExpEntry
//          + LocalVarExpEntry
//          + ParamVarExpEntry         
//      + FieldExpEntry
//      + PropertyExpEntry
//
// + MethodHeaderEntry - used to index all methods of a given name
//-----------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Xml;

using Blue.Public;
using Log = Blue.Log;
using Modifiers = AST.Modifiers;

namespace SymbolEngine
{
#region SymEntry base class for all Symbols
    //-----------------------------------------------------------------------------
    // Base class for symbol table entries
    //-----------------------------------------------------------------------------
    
    /// <summary>
    /// The base class for all symbols.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public abstract class SymEntry
    {
        public string m_strName; // name of this symbol
        
        public string Name
        {
            get { return m_strName; }
        }
        
        // Write out this Entry's name, attributes,     
        public abstract void Dump(XmlWriter o, bool fRecursive);
        
        
        // Debugging support - Validate each symbol entry
        // Allow recursive flag so that we don't get stuck in an infinite loop
        public virtual void DebugCheck(ISemanticResolver s) { }
        
        
        // Shortcut helper functions.
        static public void PrintError(SymbolError.SymbolErrorException e)
        {    
            Blue.Driver.StdErrorLog.PrintError(e);
        }
        static public void ThrowError(SymbolError.SymbolErrorException e)
        {    
            Blue.Driver.StdErrorLog.ThrowError(e);
        }    
    }
#endregion

#region Class for LabelEntry symbol (for gotos)
    //-----------------------------------------------------------------------------
    // Labels (for gotos)
    //-----------------------------------------------------------------------------
    public class LabelEntry : SymEntry
    {        
        // Used by a labelstatement
        public LabelEntry(string stName, AST.LabelStatement node)
        {
            Debug.Assert(node != null);
            Debug.Assert(stName != null);

            m_strName = stName;
            m_node = node;
        }


        public override void Dump(XmlWriter o, bool fRecursive)
        {
            o.WriteStartElement("LabelEntry");
            o.WriteAttributeString("name", m_strName);
            o.WriteEndElement();
        }

        AST.LabelStatement m_node;
        public AST.LabelStatement Node
        {
            get { return m_node; }
        }

        // Opaque object used by codegen
        object m_objCookie;
        public object CodegenCookie
        {
            get { return m_objCookie; }
            set { m_objCookie = value; }
        }        
    }
#endregion

#region Namespace Symbols
    //-----------------------------------------------------------------------------
    // Namespace entries
    //-----------------------------------------------------------------------------
    public abstract class NamespaceEntry : SymEntry
    {  
        // Scope for children
        protected Scope m_scope;
        public Scope ChildScope
        {
            get { return m_scope; }
        }

        protected string m_stFullName;
        public string FullName
        {   
            get { return m_stFullName; }
        }
    }
    
    //-----------------------------------------------------------------------------
    // Represent an imported namespace
    //-----------------------------------------------------------------------------
    public class ImportedNamespaceEntry : NamespaceEntry
    {
        // assembly we're imported from?
                
        public ImportedNamespaceEntry(string stNamespace, string stFullName)
        {
            m_strName = stNamespace;
            m_stFullName = stFullName;
            m_scope = new Scope("imported_namespace:" + stFullName, null, null);
        }
    
        // Write out this Entry's name, attributes, (and if fRecursive, then nested scope too)
        public override void Dump(XmlWriter o, bool fRecursive)
        {
            o.WriteStartElement("ImportedNamespace");
            o.WriteAttributeString("name", this.m_strName);
            if (fRecursive && (m_scope != null)) 
            {
                m_scope.Dump(o, true);
            }
            o.WriteEndElement();        
        }
        
        public override string ToString()
        {
            return "ImportedNamespaceSym:" + m_strName;        
        }        
    }
    
    //-----------------------------------------------------------------------------
    // represent a user namespace
    //-----------------------------------------------------------------------------
    public class UserNamespaceEntry : NamespaceEntry
    {
#region Construction    
        // Global namespace has "" name.
        public UserNamespaceEntry(AST.NamespaceDecl node, string stFullName)
        {
            Debug.Assert(node != null);
            Debug.Assert(stFullName != null);
            
            m_strName = node.Name;
            m_stFullName = stFullName;

            // Create the real scope for the namespace. This scope just has the symbols,
            // but it can't be attached to a particular node or lexical parent
            // since it's shared by all blocks for that namespace.
            if (IsGlobal)
            {
                // Add filename as a debugging hint
                string stFilename = node.Location.Filename;
                m_scope = new Scope("global " + stFilename, null, null);
            } 
            else 
            {
                m_scope = new Scope("user_namespace:" + node.Name, null, null);
            }
            //m_node = node;
            m_node = null;
        }
#endregion        

#region Properties & Data
        public bool IsGlobal
        {
            get { return m_strName == ""; }
        }
        
        AST.NamespaceDecl m_node;
        public AST.NamespaceDecl Node
        {
            get { return m_node; }
        }
        /*
        public void SetCurrentNode(AST.NamespaceDecl node)
        {
            Debug.Assert((node == null) ^ (m_node == null));        
            m_node = node;
        }
        */
#endregion

#region Checks        
        // Write out this Entry's name, attributes, (and if fRecursive, then nested scope too)
        public override void Dump(XmlWriter o, bool fRecursive)
        {
            o.WriteStartElement("UserNamespace");
            o.WriteAttributeString("name", this.Name);
            o.WriteAttributeString("fullname", this.FullName);
            
            if (fRecursive)             
                m_scope.Dump(o, true);
                
            o.WriteEndElement();        
        }        
        
        public override string ToString()
        {
            return "UserNamespaceSym:" + m_strName;        
        }  
#endregion              
    }

#endregion


#region Classes for ExpEntry symbols (Locals,Params,Methods,Properties)   
    //-----------------------------------------------------------------------------
    // Symbol table entry that can evaluate to a type
    //-----------------------------------------------------------------------------
    public abstract class ExpEntry : SymEntry
    {
        public TypeEntry m_type;
        
        public System.Type CLRType
        {
            get { return m_type.CLRType; }
        }
    }
    
#region Events
    //-----------------------------------------------------------------------------
    // Events
    //-----------------------------------------------------------------------------
    public class EventExpEntry : ExpEntry
    {
        // User defined events
        public EventExpEntry(TypeEntry tClass, AST.EventDecl e)
        {
            Debug.Assert(tClass != null);
            Debug.Assert(e != null);
            
            this.m_strName          = e.Name.Text;
            this.m_type             = e.EventType.BlueType;
            this.m_tClassDefined    = tClass;
            this.m_node             = e;
            this.m_mods             = e.Mods;
        }
        
        
        // Imported events
        public EventExpEntry(
            System.Reflection.EventInfo eInfo,
            ISemanticResolver s
        )
        {   
            Debug.Assert(eInfo != null);
            Debug.Assert(s != null);
            
            this.m_strName = eInfo.Name;
            this.m_tClassDefined = s.ResolveCLRTypeToBlueType(eInfo.DeclaringType);
            this.m_type = s.ResolveCLRTypeToBlueType(eInfo.EventHandlerType);
            
            this.m_node = null;
                     
            System.Reflection.MethodInfo mAdd = eInfo.GetAddMethod();
            System.Reflection.MethodInfo mRemove = eInfo.GetRemoveMethod();
                                        
            SetAddMethod(new MethodExpEntry(s, mAdd));
            SetRemoveMethod(new MethodExpEntry(s, mRemove));
            
            this.m_mods = new Modifiers(mAdd);
        }
        
    
#region Checks
        public override void Dump(XmlWriter o, bool fRecursive)
        {
            o.WriteStartElement("EventExpEntry");
            o.WriteEndElement();
        }
        
        public override string ToString()
        {
            return "EventExpEntry:" + m_strName + " of " + m_type.ToString();
        }
#endregion
    
#region Properties & Data   
        AST.EventDecl m_node;
        public AST.EventDecl Node
        {
            get { return m_node; }
        }        
 
        TypeEntry m_tClassDefined;
        public TypeEntry SymbolClass
        {
            get { return m_tClassDefined; }
        }
        
        public TypeEntry EventType
        {
            get { return this.m_type; }
        }
        
        MethodExpEntry m_methodAdd;
        public MethodExpEntry AddMethod
        {
            get { return m_methodAdd; }
        }
                
        MethodExpEntry m_methodRemove;
        public MethodExpEntry RemoveMethod
        {
            get { return m_methodRemove; }
        }  
        
        FieldExpEntry m_field;
        public FieldExpEntry Field
        {
            get { return m_field; }
        }        
        
        Modifiers m_mods;
        public Modifiers Mods
        {
            get  { return m_mods; }
        }  
        
#endregion
        public void SetAddMethod(MethodExpEntry m)
        {
            Debug.Assert(m_methodAdd == null);
            m_methodAdd = m;
        }
        
        public void SetRemoveMethod(MethodExpEntry m)
        {
            Debug.Assert(m_methodRemove == null);
            m_methodRemove = m;
        }
        
        // An event with a default handler is backed by a field.        
        // This does not have to be set.
        internal void SetDefaultField(FieldExpEntry f)
        {
            m_field = f;
        }
    
        public void SetInfo(ICLRtypeProvider provider)
        {
            //Debug.Assert(m_info == null);
                        
            //m_info = 
            provider.CreateCLREvent(this);            
        }
    } // end class Events
#endregion Events    
    
    
    
#region Property
    //-----------------------------------------------------------------------------
    // Properties
    //-----------------------------------------------------------------------------
    public class PropertyExpEntry : ExpEntry
    {
#region Ctor        
        // Ctor for user-declared properties
        // We'll rip our data from the node
        public PropertyExpEntry (            
            TypeEntry tDefiningClassType,
            AST.PropertyDecl nodeDecl,
            AST.MethodDecl nodeGet,
            AST.MethodDecl nodeSet
            )
        {
            Debug.Assert(nodeDecl != null);
            
            m_strName = nodeDecl.Name.Text;
            m_tClassDefined = tDefiningClassType;
            
            m_node = nodeDecl;
            
            m_type = nodeDecl.BlueType;
            
            m_symbolGet = (nodeGet == null) ? null : nodeGet.Symbol;
            m_symbolSet = (nodeSet == null) ? null : nodeSet.Symbol;
            
            m_mods = nodeDecl.Mods;            
        }
        
        // Ctor for imported properties
        public PropertyExpEntry(        
            ISemanticResolver s,
            System.Reflection.PropertyInfo info
            )
        {
            m_info = info;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -