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

📄 symbolerrors.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// General place to put errors relating to symbol Resolution
// These errors can come from anywhere during Resolution (including any
// AST node, a scope, or the Semantic Checker).
//-----------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Reflection;

using AST;
using SymbolEngine;

using StringBuilder = System.Text.StringBuilder;

/// <summary>
/// The <c>SymbolError</c> class contains the error codes for all errors occuring durring
/// symbol resolution as well as type-safe static methods to provide the exceptions for
/// each type of error.
/// </summary>
public class SymbolError
{
    //-----------------------------------------------------------------------------
    // Syntax error code specific to Checker
    //-----------------------------------------------------------------------------
    public enum Code
    {
        cUndefinedSymbol,       // a symbol is undefined
        cSymbolAlreadyDefined,  // a symbol is already defined
        cTypeMismatch,          // two types are incompatible
        cIllegalImportAssembly, // Some problem in importing an assembly
        cMissingAsmReference,   // expected the following assembly reference
        
        cShadowCatchHandlers,   // A catch handler is shadowing a previous handler
        cLabelAlreadyDefined,   // A label is already defined
        cBadSymbolType,         // the symbol exists, but is not the type it should be
        cMustBeInsideLoop,      // 'break' & 'continue' must exist inside a loop
        
        cOnlySingleInheritence, // Only support single inheritence
        cNoReturnTypeExpected,  // A void function is trying to return something
        
        cAmbiguousMethod,       // can't resolve an overload
        cMethodNotDefined,      // The method is not defined
        cNoAcceptableOverload,  // The method exists, but there's no proper overload

        cCircularReference,     // Type A derives from B, and B derives from A
        
        cNoParamsOnStaticCtor,  // A static constructor can't have any parameters
        cNotValidLHS,           // The expression is not valid on the LHS
        
        cNotYetImplemented,     // The feature is not yet implemented
                
        cNoFieldInitForStructs, // structs can't have instance field initializers
        cNoAcceptableOperator,  // No operator is defined to takes the given args
        cAsOpOnlyOnRefTypes,    // The as operator can only be used on Reference types
        cBadTypeIfExp,          // The args on an ?: operator have incompatible types
        cMissingInterfaceMethod,// An class is missing a method from a base interface
        cIMethodMustBePublic,   // Methods implementing interfaces must be public
        
        cSymbolNotInNamespace,  // the symbol is not defined in the given namespace
        cSymbolNotInType,       // the symbol is not defined in the given type
        cClassMustBeAbstract,   // Class must be abstract because it has abstract members
        cNoMethodToOverload,
        cCantOverrideFinal,
        cCantOverrideNonVirtual,
        cVisibilityMismatch,
        cMustDeriveFromInterface,
        cNoEventOnRHS,
        cMustBeCompileTimeConstant,
        cNewArrayBoundsMismatch,
        cNoAcceptableIndexer,
        cBaseAccessCantBeStatic,
    }
    
    //-----------------------------------------------------------------------------
    // When we get a symbol error, we want to throw an exception
    //-----------------------------------------------------------------------------
    public class SymbolErrorException : ErrorException
    {
        // @dogfood - if a parameter is of type 'code', then that gets confused with
        // the ErrorException get_Code Property from our base class.
        // For now, use a fully-qualified name. But we should fix this.
        //internal SymbolErrorException(Code c, FileRange location, string s) : 
        internal SymbolErrorException(SymbolError.Code c, FileRange location, string s) : 
            base (c, location, s)
        {
            // All Symbol errors will come through this body.
        }
    }
    
    //-----------------------------------------------------------------------------
    // The symbol is undefined    
    //-----------------------------------------------------------------------------
    public static SymbolErrorException UndefinedSymbol(Identifier id)
    {
        return new SymbolErrorException(
            Code.cUndefinedSymbol,
            id.Location, 
            "'" + id.Text + "' is undefined."
        );
    }
       
    //-----------------------------------------------------------------------------            
    // The expression is not valid on the lefthand side
    //-----------------------------------------------------------------------------
    public static SymbolErrorException NotValidLHS(FileRange location)
    {
        return new SymbolErrorException(
            Code.cNotValidLHS,
            location,
            "This expression is not a valid Left-Hand-Side expression"
            );    
    }
    
    //-----------------------------------------------------------------------------
    // The specified feature is not yet implemented
    //-----------------------------------------------------------------------------
    public static SymbolErrorException NotYetImpl(FileRange location, string stHint)
    {
        return new SymbolErrorException(
            Code.cNotYetImplemented,
            location,
            "'" + stHint + "' not implemented."
            );
    }
    

    //-----------------------------------------------------------------------------
    // A static constructor can't have any paramters
    //-----------------------------------------------------------------------------
    public static SymbolErrorException NoParamsOnStaticCtor(AST.MethodDecl m)
    {
        return new SymbolErrorException(
            Code.cNoParamsOnStaticCtor,
            m.Location,
            "A static constructor can't have any parameters"
            );
    }

    //-----------------------------------------------------------------------------
    // Something went wrong went importing an assembly.
    // Given the assembly we tried to import and a hint with more information.
    //-----------------------------------------------------------------------------
    public static SymbolErrorException IllegalAssembly(Assembly asm, string stHint)
    {
        return new SymbolErrorException(
            Code.cIllegalImportAssembly,
            null, 
            "Error importing '" + asm.ToString() + "'. " + stHint
            );
    }
    
    //-----------------------------------------------------------------------------
    // To import type t, we expect an explicit import for assembly stFilename
    //-----------------------------------------------------------------------------
    public static SymbolErrorException MissingAsmReference(Type t, string stFilename)
    {
        return new SymbolErrorException(
            Code.cMissingAsmReference,
            null,
            "In order to use type '" + t.ToString() + "', you must explicitly reference assembly '"+stFilename + "'");
    }
    
    //-----------------------------------------------------------------------------
    // Type Mismatch
    //-----------------------------------------------------------------------------
    public static SymbolErrorException TypeMismatch(System.Type tFrom, System.Type tTo, FileRange location)
    {
        return new SymbolErrorException(
            Code.cTypeMismatch,
            location,
            "Can't convert from '" + tFrom.ToString() + "' to '" + tTo.ToString() + "'"
        );
    }
    
    //-----------------------------------------------------------------------------
    // @todo - this should be a parser error
    // Helper functions for errors that occur during resolution
    //-----------------------------------------------------------------------------
    public static SymbolErrorException NoFieldInitForStructs(FieldDecl f)
    {
        return new SymbolErrorException(
            Code.cNoFieldInitForStructs,
            f.Location,
            "Structs can't have field intializers for instance fields"
            );  
    }
    
    //-----------------------------------------------------------------------------
    // Overload for binary & unary
    //-----------------------------------------------------------------------------
    public static SymbolErrorException NoAcceptableOperator(        
        FileRange location,
        System.Type tLeft, 
        System.Type tRight,
        BinaryExp.BinaryOp op
        )
    {
        return new SymbolErrorException(
            Code.cNoAcceptableOperator,
            location,
            "No binary operator '" + op +  
            "' that takes arguments '" + tLeft.ToString() + "' and '" + tRight.ToString() + "'.");
            
    }   
         
    //-----------------------------------------------------------------------------
    // Shadow
    //-----------------------------------------------------------------------------
    public static SymbolErrorException ShadowCatchHandlers(        
        FileRange location,
        System.Type tCatchType,
        System.Type tPrevCatchType)
    {
        return new SymbolErrorException(
            Code.cShadowCatchHandlers, location, 
            "Catch handler for type '"+ tCatchType.ToString()+
            "' is inaccessible because of a previous handler of type '" +
            tPrevCatchType.ToString() + "'");
    }
    
    //-----------------------------------------------------------------------------
    // Limits on 'As' operator
    //-----------------------------------------------------------------------------
    public static SymbolErrorException AsOpOnlyOnRefTypes(        
        FileRange location    
        )
    {
        return new SymbolErrorException(Code.cAsOpOnlyOnRefTypes, location,
            "The 'as' operator can only be used on reference types, not value types.");
    }

    //-----------------------------------------------------------------------------
    // Bad types for the ?: operator
    //-----------------------------------------------------------------------------
    public static SymbolErrorException BadTypeIfExp(        
        IfExp e
        )
    {
        Type t = e.TrueExp.CLRType;
        Type f = e.FalseExp.CLRType;
        
        return new SymbolErrorException(
            Code.cBadTypeIfExp, e.Location,
            "Type of '?:' operator can't be determined because there's no implicit conversion between '" + t + "' and '" + f + "'."
            );
    }
    
    //-----------------------------------------------------------------------------
    // Missing an method inherited from an interface
    //-----------------------------------------------------------------------------
    public static SymbolErrorException MissingInterfaceMethod(        
        FileRange location,
        MethodExpEntry mInterface,
        TypeEntry tClass        
        )
    {
        string stClass = tClass.FullName;
        string stMethod = mInterface.PrettyDecoratedName;
        string stInterface = mInterface.SymbolClass.FullName;
        
        return new SymbolErrorException(
            Code.cMissingInterfaceMethod, 
            location,
            "The type '"+stClass+"' does not implement method '"+
            stMethod + "' from interface '" + stInterface + "'");
    }
    
    //-----------------------------------------------------------------------------
    // The methdo must be public because it is implementing an interface.
    //-----------------------------------------------------------------------------
    public static SymbolErrorException IMethodMustBePublic(        
        FileRange location,
        MethodExpEntry mInterface,
        TypeEntry tClass
        
        )
    {
        string stClass = tClass.FullName;
        string stMethod = mInterface.PrettyDecoratedName;
        string stInterface = mInterface.SymbolClass.FullName;
        
        return new SymbolErrorException(
            Code.cIMethodMustBePublic, 
            location,
            "The method '" + stMethod + "' must be public to implement interface '" + stInterface + "'");
    }
    
    //-----------------------------------------------------------------------------
    // The symbol is undefined in the namespace.
    //-----------------------------------------------------------------------------
    public static SymbolErrorException UndefinedSymbolInNamespace(
        NamespaceEntry n,
        Identifier idMissingSymbol
        )
    {
        return new SymbolErrorException(
            Code.cSymbolNotInNamespace,
            idMissingSymbol.Location,
            "'" + idMissingSymbol.Text + "' is not defined in the namespace '" + 
            n.FullName + "'. (Are you missing an assembly reference?)");
    }
    
    //-----------------------------------------------------------------------------
    // The symbol is undefined in the type
    //-----------------------------------------------------------------------------
    public static SymbolErrorException UndefinedSymbolInType(                
        TypeEntry t,
        Identifier idMissingSymbol
        )
    {

⌨️ 快捷键说明

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