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

📄 objexpast.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 5 页
字号:
//-----------------------------------------------------------------------------
// File: ObjExpAST.cs
//
// Description: 
// all Exp nodes that are used to generate an expression evaluating to an 
// object
//
// See AST.cs for details
//
// Exp-> ObjExp -> <any derived class below>
//
// SimpleObjExp -> id                                       // a single identifier
// DotObjExp -> ObjExp '.' id                               // Field Access
// MethodCallExp -> id '(' param_list ')'                // MethodCall
// MethodCallExp -> ObjExp '.' id '(' param_list ')'     // MethodCall
// ArrayAccessExp -> ObjExp '[' int_exp ']'                    // array access
// CastObjExp -> '(' Type ')' Exp                           // Type Cast
// NewObjExp -> 'new' type '(' param_list ')'               // new - ctor
//-----------------------------------------------------------------------------

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

using SymbolEngine;
using Blue.Public;
using CodeGen = Blue.CodeGen;


namespace AST
{


#region Resolved Exp
//-----------------------------------------------------------------------------
// Expression that represents a namespace
//-----------------------------------------------------------------------------
public class NamespaceExp : Exp
{
    public NamespaceExp(
        SymbolEngine.NamespaceEntry symbol
    )
    {
        m_symbol = symbol;
    }
    
#region Properties & Data
    NamespaceEntry m_symbol;
    public NamespaceEntry Symbol
    {
        get { return m_symbol; }
    }
#endregion

#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);    
    }
    
    public override void Dump(XmlWriter o)
    {
        
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("<N>'{0}'", m_symbol.FullName);   
    }
#endregion

    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return null;
    }
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        return this;
    }
}

//-----------------------------------------------------------------------------
// Type, this is also temporary
//-----------------------------------------------------------------------------
class TypeExp : Exp
{
    public TypeExp(TypeEntry symbol)
    {
        m_symbol = symbol;
    }
    
    TypeEntry m_symbol;
    public TypeEntry Symbol
    {
        get { return m_symbol; }
    } 
    
#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);    
    }
    
    public override void Dump(XmlWriter o)
    {
        
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {   
        sb.Write("({0})", m_symbol.FullName);
    }
#endregion 

#region Resolution
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return m_symbol.CLRType;
    }
    
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        // We can only do the CLR types after we do them for the symbols.
        // But we may refer to a type before we have resolved it to a CLR symbol.
        // (such as when we specify a base class).
        if (m_symbol.CLRType != null)
            CalcCLRType(s);
        return this; 
    }
#endregion    
    
}


//-----------------------------------------------------------------------------
// Local var
//-----------------------------------------------------------------------------
public class LocalExp : Exp
{
    public LocalExp(
        SymbolEngine.LocalVarExpEntry symbol
    )
    {
        m_symbol = symbol;
    }
    
    LocalVarExpEntry m_symbol;
    public LocalVarExpEntry Symbol
    {
        get  { return m_symbol; }
    }  
    
#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);
    }
    
    public override void Dump(XmlWriter o)
    {
    
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("<L>{0}", m_symbol.Name);   
    }
#endregion   

#region Resolution
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return m_symbol.CLRType;
    }
    
    protected override Exp ResolveExpAsLeft(ISemanticResolver s)
    {
        CalcCLRType(s);
        return this; 
    }
    
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        CalcCLRType(s);
        return this; 
    }
#endregion

#region Generate
    public override void GenerateAsLeftPre(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAsLeftPre(this);
    }    
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
    public override void GenerateAsLeftPost(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAsLeftPost(this);
    }
        
    public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAddrOf(this);
    }
#endregion
}

//-----------------------------------------------------------------------------
// Expression refering to a parameter
//-----------------------------------------------------------------------------
public class ParamExp : Exp
{
    public ParamExp(
        SymbolEngine.ParamVarExpEntry symbol
    )
    {
        m_symbol = symbol;
    }
    
    ParamVarExpEntry m_symbol;
    public ParamVarExpEntry Symbol
    {
        get  { return m_symbol; }
    }        
    
#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);
    }
    
    public override void Dump(XmlWriter o)
    {
    
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("<P>{0}", m_symbol.Name);   
    }
#endregion       


#region Resolution
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return m_symbol.CLRType;
    }
    
    protected override Exp ResolveExpAsLeft(ISemanticResolver s)
    {
        CalcCLRType(s);
        return this; 
    }
    
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        CalcCLRType(s);
        return this; 
    }
#endregion

#region Generate
    public override void GenerateAsLeftPre(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAsLeftPre(this);
    }    
    public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
    {
        gen.Generate(this);
    }
    public override void GenerateAsLeftPost(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAsLeftPost(this);
    }    
    
    public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
    {
        gen.GenerateAddrOf(this);
    }
#endregion

}

//-----------------------------------------------------------------------------
// For Properties
//-----------------------------------------------------------------------------
public class PropertyExp : Exp
{
    public PropertyExp(
        PropertyExpEntry symbol,
        Exp expInstance // null for statics
        )
    {
        Debug.Assert(symbol != null);
        m_symbol = symbol;
        m_expInstance = expInstance;
    }

#region Properties & Data
    SymbolEngine.PropertyExpEntry m_symbol;
    public PropertyExpEntry Symbol
    {
        get { return m_symbol; }
    }

    Exp m_expInstance;
    public Exp InstanceExp
    {
        get { return m_expInstance; }    
    }

    public bool IsStatic
    {
        get { return m_expInstance == null; }
    }
#endregion

#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);
        Debug.Assert(m_symbol.IsStatic == IsStatic);
    }

    public override void Dump(XmlWriter o)
    {

    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        if (m_expInstance == null)
            //sb.Write("[static]");
            sb.Write(m_symbol.SymbolClass.FullName);
        else
            m_expInstance.ToSource(sb);
                                
        sb.Write(".<prop>{0}", m_symbol.Name);   
    }
#endregion

#region Resolution
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        return m_symbol.CLRType;
    }

    protected override Exp ResolveExpAsLeft(ISemanticResolver s)
    {
        // Whoever is resolving against us will have to change us to a
        // Set method (because only they have access to the RHS).
       
        CalcCLRType(s);
        return this; 
    }

    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {
        //CalcCLRType(s);
        //return this; 
        // Transform into a get
        Exp eResolved = new MethodCallExp(
            this.InstanceExp, 
            Symbol.SymbolGet, 
            new ArgExp[0], 
            s
        );
        
        Exp.ResolveExpAsRight(ref eResolved, s);
        
        return eResolved;
    }

#endregion

#region Generate
// Properties don't generate code.
// They should be transformed into MethodCalls by that point 
#endregion

}

//-----------------------------------------------------------------------------
// For MethodPointers (used for delegates)
//-----------------------------------------------------------------------------
public class MethodPtrExp : Exp
{
    public MethodPtrExp(
        MethodExpEntry symbol
    )
    {
        Debug.Assert(symbol != null);
        m_symbol = symbol;
    }        
    
#region Properties & Data
    MethodExpEntry m_symbol;
    public MethodExpEntry Symbol
    {
        get { return m_symbol; }
    }    
#endregion    

#region Checks
    public override void DebugCheck(ISemanticResolver s)
    {
        Debug.Assert(m_symbol != null);
    }
    
    public override void Dump(XmlWriter o)
    {
        o.WriteStartElement("MethodPtrExp");
        string stName = TypeEntry.GetDecoratedParams(m_symbol);
        o.WriteAttributeString("name", stName);
        o.WriteEndElement();
    }
    
    public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)    
    {
        sb.Write("<Mptr>'{0}'", m_symbol.PrettyDecoratedName);    
    }
#endregion

#region Resolution
    protected override Type CalcCLRTypeHelper(ISemanticResolver s)
    {
        // This is a pointer, and pointers are ints.
        // Also, we pass this to the delegate ctor, which expects an int
        return Type.GetType("System.IntPtr");
        //return typeof(int);
    }
            
    protected override Exp ResolveExpAsRight(ISemanticResolver s)
    {

⌨️ 快捷键说明

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