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

📄 ast.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 5 页
字号:
        
        o.WriteEndElement();
    }

    // Debugging check
    public override void DebugCheck(ISemanticResolver s)
    {   
        //EnterNamespace(s);
        
        Debug.Assert(m_arUsingDirectives != null);
        Debug.Assert(m_arNestedNamespaces != null);
        //Debug.Assert(m_arClasses != null);
        
        /*
        foreach(ClassDecl c in m_arClasses)
        {
            c.DebugCheck(s);
        }
        */

        foreach(NamespaceDecl n in this.m_arNestedNamespaces)
        {
            n.DebugCheck(s);
        }
        
        foreach(TypeDeclBase e in m_arTypes)
            e.DebugCheck(s);

        foreach(UsingDirective u in this.m_arUsingDirectives)
        {
            u.DebugCheck(s);
        }

        //ExitNamespace(s);
    }
#endregion    
}


#region Base class for all type declarations
//-----------------------------------------------------------------------------
// Base for type declarations
//-----------------------------------------------------------------------------
public abstract class TypeDeclBase : Node
{    
#region Resolution
    // Resolution is the act of changing strings (in a parse tree) into symbols.
    // Symbols are associated with the appropriate object in the System.Reflection]
    // namespace.
    public abstract void ResolveTypesAsBlueStub(
        ISemanticResolver s,
        string stNamespace, // namespace that a type goes in. Includes nested classes.
        Scope scopeParent   // our Lexical parent's scope that we should add ourselves too.
    );
    
    public abstract void ResolveTypesAsCLR(
        ISemanticResolver s,
        ICLRtypeProvider provider
    );
    
    public abstract void ResolveMemberDecls(
        ISemanticResolver s,
        ICLRtypeProvider provider
    );
    
    public abstract void ResolveBodies(ISemanticResolver s);
    
    public abstract void ReportClass(ArrayList alClasses);
#endregion
    
#region Generate    
    // Generate the body for this type
    public abstract void GenerateType(Blue.CodeGen.EmitCodeGen gen);
    
    // Get the CLR type that this node represents. Useful for codegen.
    public abstract System.Type CLRType
    {
        get;
    }
#endregion
} // end TypeDeclBase

#endregion // Type Decl base





#region Delegate type declaration
//-----------------------------------------------------------------------------
// Delegates
//-----------------------------------------------------------------------------
public class DelegateDecl : TypeDeclBase
{
#region Construction    
    public DelegateDecl(
        Identifier      idName,
        TypeSig         tRetType,
        ParamVarDecl[]  arParams,        
        Modifiers       mods
    )
    {
        Debug.Assert(idName != null);
        Debug.Assert(tRetType != null);
        Debug.Assert(arParams != null);
                
        m_idName    = idName;
        m_tRetType  = tRetType;
        m_arParams  = arParams;
        m_mods      = mods;
        
        // Implied sealed
        m_mods.SetSealed();
    }    
#endregion Construction

#region Properties & Data
    Identifier      m_idName;    
    TypeSig         m_tRetType;
    ParamVarDecl[]  m_arParams;        
    Modifiers       m_mods;
    
    ClassDecl       m_nodeProxy;
    //TypeEntry       m_symbol;
#endregion

#region Resolution
    // Helper function
    // Given a symbol for a delegate, get the params for it
    static public System.Type [] GetParams(TypeEntry t)
    {
        // We can do this by looking up the invoke function
        MethodHeaderEntry header = t.LookupMethodHeader("Invoke");
        MethodExpEntry m = header.GetFirstMethod();
        
        Debug.Assert(header.GetNextMethod(m) == null, "Delegate should only have 1 Invoke() method");
        
        // Get parameters off this method
    
        System.Type [] al = m.ParamTypes(false);
        return al;
    }
    
    // Is this CLR type a delegate?
    public static bool IsDelegate(System.Type t)
    {
        // Determine by base type..
        return 
            (t != null) && 
            (t.BaseType == typeof(System.MulticastDelegate));
    }

    // Delegates are really just blessed Types.
    void CreateProxyType(ISemanticResolver s)
    {
        Debug.Assert(m_nodeProxy == null, "only create proxy once");
    // The delegate R F(A) (where R is a return type, and A is a parameter list)
    // Can be converted into the type:
    // sealed class F : System.MulticastDelegate {
    //      F(object, native int) { }
    //      BeginInvoke() { }
    //      EndInvoke() { }
    //      R Invoke(A) { }
    // }
        BlockStatement stmtEmpty = new BlockStatement(null, new Statement[0]);
        Modifiers modsPublic = new Modifiers();
        modsPublic.SetPublic();
        
        Modifiers modsVirtual = modsPublic;
        modsVirtual.SetVirtual();
        
    
        //System.Type tNativeInt = typeof(int);
        System.Type tNativeInt = Type.GetType("System.IntPtr");
    
        TypeEntry t_IAsyncResult = s.ResolveCLRTypeToBlueType(typeof(System.IAsyncResult));
        
        // Create the parameters for the BeginInvoke()
        ParamVarDecl [] paramBeginInvoke = new ParamVarDecl[m_arParams.Length + 2];
        m_arParams.CopyTo(paramBeginInvoke, 0);
        paramBeginInvoke[m_arParams.Length]= new ParamVarDecl(
            new Identifier("cb"), 
            new ResolvedTypeSig(typeof(System.AsyncCallback), s), 
            EArgFlow.cIn
        );
        
        paramBeginInvoke[m_arParams.Length + 1] = new ParamVarDecl(
            new Identifier("state"), 
            new ResolvedTypeSig(typeof(System.Object), s), 
            EArgFlow.cIn
        );
    
        m_nodeProxy = new ClassDecl(
            m_idName,
            new TypeSig[] {
                new ResolvedTypeSig(typeof(System.MulticastDelegate), s)
            },
            new MethodDecl[] {
            // Ctor
                new MethodDecl(
                    m_idName, null, new ParamVarDecl[] {
                        new ParamVarDecl(new Identifier("instance"),    new ResolvedTypeSig(typeof(object), s),  EArgFlow.cIn),
                        new ParamVarDecl(new Identifier("func"),        new ResolvedTypeSig(tNativeInt, s),  EArgFlow.cIn)
                    },
                    stmtEmpty, modsPublic
                ),
            // Invoke,
                new MethodDecl(
                    new Identifier("Invoke"),
                    this.m_tRetType,
                    this.m_arParams,
                    stmtEmpty,
                    modsVirtual),
                    
            // Begin Invoke
                new MethodDecl(
                    new Identifier("BeginInvoke"),
                    new ResolvedTypeSig(t_IAsyncResult),
                    paramBeginInvoke,
                    stmtEmpty,
                    modsVirtual),                    
            
            // End Invoke
                new MethodDecl(
                    new Identifier("EndInvoke"),
                    this.m_tRetType,
                    new ParamVarDecl[] {
                        new ParamVarDecl(new Identifier("result"), new ResolvedTypeSig(t_IAsyncResult), EArgFlow.cIn)
                    },
                    stmtEmpty,
                    modsVirtual)
                                            
            },
            new PropertyDecl[0],
            new FieldDecl[0],
            new EventDecl[0],
            new TypeDeclBase[0],
            m_mods,
            true); // isClass
            
    }

    // Resolution functions
    public override void ResolveTypesAsBlueStub(
        ISemanticResolver s,
        string stNamespace, // namespace that a type goes in. Includes nested classes.
        Scope scopeParent
    )
    {
        CreateProxyType(s);
        this.m_nodeProxy.ResolveTypesAsBlueStub(s, stNamespace, scopeParent);
        
        // We can go ahead and resolve the rest because we know that a Delegate
        // doesn't inherit / implement interfaces for other user-defined classes.
        //this.m_nodeProxy.ResolveTypesAsBlueLinks(s);
        //this.m_nodeProxy.ResolveTypesAsBlueFinal(s);
    }
    
    public override void ResolveTypesAsCLR(
        ISemanticResolver s,
        ICLRtypeProvider provider
    )
    {
        this.m_nodeProxy.ResolveTypesAsCLR(s, provider);
    }
    
    public override void ResolveMemberDecls(
        ISemanticResolver s,
        ICLRtypeProvider provider
    )
    {
        this.m_nodeProxy.ResolveMemberDecls(s, provider);
    }
    
    public override void ResolveBodies(ISemanticResolver s)
    {
        this.m_nodeProxy.ResolveBodies(s);
    }
        
    public override void ReportClass(ArrayList alClasses)
    {
        alClasses.Add(this.m_nodeProxy);
    }
    
    // Get the CLR type that this node represents. Useful for codegen.
    public override System.Type CLRType
    {
        get {
            return this.m_nodeProxy.CLRType;
        }
    }
#endregion Resolution 

#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_nodeProxy != null);
        this.m_nodeProxy.DebugCheck(s);
    }
    
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("DelegateDecl");
        this.m_nodeProxy.Dump(o);
        o.WriteEndElement();
    }
#endregion Checks

#region Generate
    public override void GenerateType(Blue.CodeGen.EmitCodeGen gen)
    {
        this.m_nodeProxy.GenerateType(gen);
    }
#endregion Generate
}
#endregion

#region Enums type declaration
    
//-----------------------------------------------------------------------------
// Declare an enum
//-----------------------------------------------------------------------------
public class EnumDecl : TypeDeclBase
{
#region Construction
    public EnumDecl(Identifier idName, FieldDecl[] fields, Modifiers mods)
    {
        Debug.Assert(fields != null);
        m_fields = fields;
        m_idName = idName;
        m_mods = mods;
    }
#endregion

#region Properties & Data
    FieldDecl[] m_fields;
    public FieldDecl[] Fields
    {
        get { return m_fields; }
    }

    Identifier m_idName;
    public Identifier Name
    {
        get { return m_idName; }
    }

    EnumTypeEntry m_symbol;
    public EnumTypeEntry Symbol 
    {
        get { return m_symbol; }
    }
    
    Modifiers m_mods;
#endregion

#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        foreach(FieldDecl f in m_fields)
        {
            f.DebugCheck(s);
        }
    }
    
    public override void Dump(XmlWriter o)
    {
        foreach(FieldDecl f in m_fields)
        {
            f.Dump(o);
        }
    }
#endregion

#region Resolution
    // Semantic checking
    // Stubs can be resolved in any order.
    public override void ResolveTypesAsBlueStub(
        ISemanticResolver s,
        string stNamespace,
        Scope scopeParent
        )
    {
        // Get our name. Nested classes are separated with a '+'
        // Namespaces are separated with a '.'
        string stFullName = (stNamespace == "") ? 
            (Name.Text) :
            (stNamespace + "." + Name.Text);

        // Are we a nested class? 
        if (scopeParent.Node is ClassDecl)
        {
            Debug.Assert(stNamespace != "");
            stFullName = stNamespace + "+" + Name.Text;
        }                    

        // Create a stub sym entry to add to current scope
        m_symbol = new EnumTypeEntry(Name.Text, stFullName, s, m_mods, this, scopeParent);
        scopeParent.AddSymbol(m_symbol);
    }

    public override void ResolveTypesAsCLR(
        ISemanticResolver s,
        ICLRtypeProvider provider
        )
    {
        if (m_symbol.CLRType != null)
            return;
            
        m_symbol.SetCLRType(provider); // enum
                
        s.AddClrResolvedType(this.Symbol);
    }


    public override void ResolveMemberDecls(
        ISemanticResolver s,
        ICLRtypeProvider provider
    )
    {

⌨️ 快捷键说明

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