Пример #1
0
        internal static void AssertSymbolInTable(string text, int sid, bool duplicate, ISymbolTable symbolTable)
        {
            if (text == null)
            {
                Assert.IsNull(symbolTable.FindKnownSymbol(sid));
                return;
            }

            if (sid != SymbolToken.UnknownSid)
            {
                Assert.AreEqual(text, symbolTable.FindKnownSymbol(sid));
            }

            if (duplicate)
            {
                return;
            }

            Assert.AreEqual(sid, symbolTable.FindSymbol(text));
            var token = symbolTable.Find(text);

            Assert.AreEqual(sid, token.Sid);
            Assert.AreEqual(text, token.Text);

            token = symbolTable.Intern(text);
            Assert.AreEqual(sid, token.Sid);
            Assert.AreEqual(text, token.Text);
        }
Пример #2
0
        public Symbol FindSymbol(string name, Symbol context, SymbolFilter filter)
        {
            Debug.Assert(string.IsNullOrEmpty(name) == false);

            Symbol symbol = null;

            if (this.IsAnonymousType && (filter & SymbolFilter.Types) == 0)
            {
                var anonymousProperty = new PropertySymbol(name, this, this);
                anonymousProperty.SetNameCasing(preserveCase: true);
                return(anonymousProperty);
            }

            if ((filter & SymbolFilter.Types) != 0)
            {
                symbol = GetNestedType(name, context, filter)
                         ?? GetGenericParamaterType(name, context, filter);
            }

            if (symbol == null && (filter & SymbolFilter.Members) != 0)
            {
                SymbolFilter baseFilter = filter | SymbolFilter.ExcludeParent;

                symbol = GetMember(name)
                         ?? FindSymbolFromBase(name, context, symbol, baseFilter);

                if (symbol != null && symbol.MatchFilter(filter) == false)
                {
                    symbol = null;
                }
            }

            if (symbol == null && !filter.HasFlag(SymbolFilter.ExcludeParent))
            {
                if (parentSymbolTable != null)
                {
                    symbol = parentSymbolTable.FindSymbol(name, context, filter);
                }
                else if (context?.Parent is ISymbolTable symbolTable)
                {
                    symbolTable.FindSymbol(name, context, filter);
                }
            }

            return(symbol);
        }
Пример #3
0
        Symbol ISymbolTable.FindSymbol(string name, Symbol context)
        {
            Symbol symbol = null;

            if (_localTable.ContainsKey(name))
            {
                symbol = _localTable[name];
            }

            if (symbol == null)
            {
                Debug.Assert(_parentSymbolTable != null);
                symbol = _parentSymbolTable.FindSymbol(name, context);
            }

            return(symbol);
        }
Пример #4
0
        Symbol ISymbolTable.FindSymbol(string name, Symbol context, SymbolFilter filter)
        {
            Debug.Assert(string.IsNullOrEmpty(name) == false);
            Debug.Assert(context != null);

            Symbol symbol = null;

            if ((filter & SymbolFilter.Members) != 0)
            {
                SymbolFilter baseFilter = filter | SymbolFilter.ExcludeParent;

                symbol = GetMember(name);

                if (symbol == null)
                {
                    TypeSymbol baseType   = GetBaseType();
                    TypeSymbol objectType =
                        (TypeSymbol)((ISymbolTable)SymbolSet.SystemNamespace).FindSymbol("Object", null,
                                                                                         SymbolFilter.Types);

                    if (baseType == null && this != objectType)
                    {
                        baseType = objectType;
                    }

                    if (baseType != null)
                    {
                        symbol = ((ISymbolTable)baseType).FindSymbol(name, context, baseFilter);
                    }
                }

                if (symbol != null && symbol.MatchFilter(filter) == false)
                {
                    symbol = null;
                }
            }

            if (symbol == null && parentSymbolTable != null &&
                (filter & SymbolFilter.ExcludeParent) == 0)
            {
                symbol = parentSymbolTable.FindSymbol(name, context, filter);
            }

            return(symbol);
        }
Пример #5
0
        private void BuildTypeInheritance(ClassSymbol classSymbol)
        {
            if (classSymbol.PrimaryPartialClass != classSymbol)
            {
                // Don't build type inheritance for non-primary partial classes.
                return;
            }

            CustomTypeNode customTypeNode = (CustomTypeNode)classSymbol.ParseContext;

            if ((customTypeNode.BaseTypes != null) && (customTypeNode.BaseTypes.Count != 0))
            {
                ClassSymbol            baseClass  = null;
                List <InterfaceSymbol> interfaces = null;

                foreach (NameNode node in customTypeNode.BaseTypes)
                {
                    TypeSymbol baseTypeSymbol = (TypeSymbol)_symbolTable.FindSymbol(node.Name, classSymbol, SymbolFilter.Types);
                    Debug.Assert(baseTypeSymbol != null);

                    if (baseTypeSymbol.Type == SymbolType.Class)
                    {
                        Debug.Assert(baseClass == null);
                        baseClass = (ClassSymbol)baseTypeSymbol;
                    }
                    else
                    {
                        Debug.Assert(baseTypeSymbol.Type == SymbolType.Interface);

                        if (interfaces == null)
                        {
                            interfaces = new List <InterfaceSymbol>();
                        }
                        interfaces.Add((InterfaceSymbol)baseTypeSymbol);
                    }
                }

                if ((baseClass != null) || (interfaces != null))
                {
                    classSymbol.SetInheritance(baseClass, interfaces);
                }
            }
        }
Пример #6
0
        Symbol ISymbolTable.FindSymbol(string name, Symbol context, SymbolFilter filter)
        {
            Symbol symbol = null;

            if ((filter & SymbolFilter.Locals) != 0)
            {
                if (localTable.ContainsKey(name))
                {
                    symbol = localTable[name];
                }
            }

            if (symbol == null)
            {
                Debug.Assert(parentSymbolTable != null);
                symbol = parentSymbolTable.FindSymbol(name, context, filter);
            }

            return(symbol);
        }
Пример #7
0
        public Symbol FindSymbol(string name, Symbol context, SymbolFilter filter)
        {
            Debug.Assert(string.IsNullOrEmpty(name) == false);

            Symbol symbol = null;

            if ((filter & SymbolFilter.Types) != 0)
            {
                symbol = GetNestedType(name, context, filter);
            }

            if (symbol == null && (filter & SymbolFilter.Members) != 0)
            {
                SymbolFilter baseFilter = filter | SymbolFilter.ExcludeParent;

                symbol = GetMember(name)
                         ?? FindSymbolFromBase(name, context, symbol, baseFilter);

                if (symbol != null && symbol.MatchFilter(filter) == false)
                {
                    symbol = null;
                }
            }

            if (symbol == null && !filter.HasFlag(SymbolFilter.ExcludeParent))
            {
                if (parentSymbolTable != null)
                {
                    symbol = parentSymbolTable.FindSymbol(name, context, filter);
                }
                else if (context?.Parent is ISymbolTable symbolTable)
                {
                    symbolTable.FindSymbol(name, context, filter);
                }
            }

            return(symbol);
        }
Пример #8
0
        Symbol ISymbolTable.FindSymbol(string name, Symbol context)
        {
            Debug.Assert(string.IsNullOrEmpty(name) == false);
            Debug.Assert(context != null);

            Symbol symbol = GetMember(name);

            if (symbol == null)
            {
                var baseType = GetBaseType();
                if (baseType != null)
                {
                    symbol = ((ISymbolTable)baseType).FindSymbol(name, context);
                }
            }

            if (symbol == null && _parentSymbolTable != null)
            {
                symbol = _parentSymbolTable.FindSymbol(name, context);
            }

            return(symbol);
        }
Пример #9
0
        public TypeSymbol ResolveType(TypeSyntax node, ISymbolTable symbolTable, Symbol contextSymbol)
        {
            if (node is PredefinedTypeSyntax)
            {
                return(ResolveIntrinsicType((PredefinedTypeSyntax)node, symbolTable, contextSymbol));
            }
            //else if (node is ArrayTypeNode)
            //{
            //    ArrayTypeNode arrayTypeNode = (ArrayTypeNode)node;

            //    TypeSymbol itemTypeSymbol = ResolveType(arrayTypeNode.BaseType, symbolTable, contextSymbol);
            //    Debug.Assert(itemTypeSymbol != null);

            //    return CreateArrayTypeSymbol(itemTypeSymbol);
            //}
            else
            {
                Debug.Assert(node is IdentifierNameSyntax);
                var nameNode = (IdentifierNameSyntax)node;

                return((TypeSymbol)symbolTable.FindSymbol(nameNode.Name.Text, contextSymbol));
            }
        }
Пример #10
0
        public TypeSymbol ResolveType(ParseNode node, ISymbolTable symbolTable, Symbol contextSymbol)
        {
            if (node is IntrinsicTypeNode)
            {
                IntrinsicType intrinsicType = IntrinsicType.Integer;

                IntrinsicTypeNode intrinsicTypeNode = (IntrinsicTypeNode)node;

                switch (intrinsicTypeNode.Type)
                {
                case TokenType.Object:
                    intrinsicType = IntrinsicType.Object;

                    break;

                case TokenType.Bool:
                    intrinsicType = IntrinsicType.Boolean;

                    break;

                case TokenType.String:
                case TokenType.Char:
                    intrinsicType = IntrinsicType.String;

                    break;

                case TokenType.Int:
                    intrinsicType = IntrinsicType.Integer;

                    break;

                case TokenType.UInt:
                    intrinsicType = IntrinsicType.UnsignedInteger;

                    break;

                case TokenType.Long:
                    intrinsicType = IntrinsicType.Long;

                    break;

                case TokenType.ULong:
                    intrinsicType = IntrinsicType.UnsignedLong;

                    break;

                case TokenType.Short:
                    intrinsicType = IntrinsicType.Short;

                    break;

                case TokenType.UShort:
                    intrinsicType = IntrinsicType.UnsignedShort;

                    break;

                case TokenType.Byte:
                    intrinsicType = IntrinsicType.Byte;

                    break;

                case TokenType.SByte:
                    intrinsicType = IntrinsicType.SignedByte;

                    break;

                case TokenType.Float:
                    intrinsicType = IntrinsicType.Single;

                    break;

                case TokenType.Decimal:
                    intrinsicType = IntrinsicType.Decimal;

                    break;

                case TokenType.Double:
                    intrinsicType = IntrinsicType.Double;

                    break;

                case TokenType.Delegate:
                    intrinsicType = IntrinsicType.Delegate;

                    break;

                case TokenType.Void:
                    intrinsicType = IntrinsicType.Void;

                    break;
                }

                TypeSymbol typeSymbol = ResolveIntrinsicType(intrinsicType);

                if (intrinsicTypeNode.IsNullable)
                {
                    TypeSymbol nullableType = ResolveIntrinsicType(IntrinsicType.Nullable);
                    typeSymbol = CreateGenericTypeSymbol(nullableType, new List <TypeSymbol> {
                        typeSymbol
                    });
                }

                return(typeSymbol);
            }

            if (node is ArrayTypeNode)
            {
                ArrayTypeNode arrayTypeNode = (ArrayTypeNode)node;

                TypeSymbol itemTypeSymbol = ResolveType(arrayTypeNode.BaseType, symbolTable, contextSymbol);
                Debug.Assert(itemTypeSymbol != null);

                return(CreateArrayTypeSymbol(itemTypeSymbol));
            }

            if (node is AtomicNameNode atomicNameNode)
            {
                TypeSymbol typeSymbol = ResolveAtomicNameNodeType(atomicNameNode, symbolTable, contextSymbol);
                if (typeSymbol != null)
                {
                    return(typeSymbol);
                }
            }

            if (node is GenericNameNode)
            {
                GenericNameNode genericNameNode = (GenericNameNode)node;
                string          genericTypeName = genericNameNode.Name + "`" + genericNameNode.TypeArguments.Count;
                TypeSymbol      templateType    =
                    (TypeSymbol)symbolTable.FindSymbol(genericTypeName, contextSymbol, SymbolFilter.Types);

                List <TypeSymbol> typeArguments = new List <TypeSymbol>();

                foreach (ParseNode argNode in genericNameNode.TypeArguments)
                {
                    TypeSymbol argType = ResolveType(argNode, symbolTable, contextSymbol);
                    Debug.Assert(argType != null);
                    typeArguments.Add(argType);
                }

                if (templateType != null)
                {
                    TypeSymbol resolvedSymbol = CreateGenericTypeSymbol(templateType, typeArguments);
                    Debug.Assert(resolvedSymbol != null);

                    return(resolvedSymbol);
                }
            }

            Debug.Assert(node is NameNode);
            NameNode nameNode = (NameNode)node;

            return((TypeSymbol)symbolTable.FindSymbol(nameNode.Name, contextSymbol, SymbolFilter.Types));
        }
Пример #11
0
        public TypeSymbol ResolveType(TypeSyntax node, ISymbolTable symbolTable, Symbol contextSymbol)
        {
            if (node is PredefinedTypeSyntax)
            {
                return ResolveIntrinsicType((PredefinedTypeSyntax) node, symbolTable, contextSymbol);
            }
            //else if (node is ArrayTypeNode)
            //{
            //    ArrayTypeNode arrayTypeNode = (ArrayTypeNode)node;

            //    TypeSymbol itemTypeSymbol = ResolveType(arrayTypeNode.BaseType, symbolTable, contextSymbol);
            //    Debug.Assert(itemTypeSymbol != null);

            //    return CreateArrayTypeSymbol(itemTypeSymbol);
            //}
            else
            {
                Debug.Assert(node is IdentifierNameSyntax);
                var nameNode = (IdentifierNameSyntax) node;

                return (TypeSymbol)symbolTable.FindSymbol(nameNode.Name.Text, contextSymbol);
            }
        }
Пример #12
0
        public TypeSymbol ResolveType(ParseNode node, ISymbolTable symbolTable, Symbol contextSymbol)
        {
            if (node is IntrinsicTypeNode) {
                IntrinsicType intrinsicType = IntrinsicType.Integer;

                IntrinsicTypeNode intrinsicTypeNode = (IntrinsicTypeNode)node;
                switch (intrinsicTypeNode.Type) {
                    case TokenType.Object:
                        intrinsicType = IntrinsicType.Object;
                        break;
                    case TokenType.Bool:
                        intrinsicType = IntrinsicType.Boolean;
                        break;
                    case TokenType.String:
                    case TokenType.Char:
                        intrinsicType = IntrinsicType.String;
                        break;
                    case TokenType.Int:
                        intrinsicType = IntrinsicType.Integer;
                        break;
                    case TokenType.UInt:
                        intrinsicType = IntrinsicType.UnsignedInteger;
                        break;
                    case TokenType.Long:
                        intrinsicType = IntrinsicType.Long;
                        break;
                    case TokenType.ULong:
                        intrinsicType = IntrinsicType.UnsignedLong;
                        break;
                    case TokenType.Short:
                        intrinsicType = IntrinsicType.Short;
                        break;
                    case TokenType.UShort:
                        intrinsicType = IntrinsicType.UnsignedShort;
                        break;
                    case TokenType.Byte:
                        intrinsicType = IntrinsicType.Byte;
                        break;
                    case TokenType.SByte:
                        intrinsicType = IntrinsicType.SignedByte;
                        break;
                    case TokenType.Float:
                        intrinsicType = IntrinsicType.Single;
                        break;
                    case TokenType.Decimal:
                        intrinsicType = IntrinsicType.Decimal;
                        break;
                    case TokenType.Double:
                        intrinsicType = IntrinsicType.Double;
                        break;
                    case TokenType.Delegate:
                        intrinsicType = IntrinsicType.Delegate;
                        break;
                    case TokenType.Void:
                        intrinsicType = IntrinsicType.Void;
                        break;
                }

                TypeSymbol typeSymbol = ResolveIntrinsicType(intrinsicType);

                if (intrinsicTypeNode.IsNullable) {
                    TypeSymbol nullableType = ResolveIntrinsicType(IntrinsicType.Nullable);
                    typeSymbol = CreateGenericTypeSymbol(nullableType, new List<TypeSymbol>() { typeSymbol });
                }

                return typeSymbol;
            }
            else if (node is ArrayTypeNode) {
                ArrayTypeNode arrayTypeNode = (ArrayTypeNode)node;

                TypeSymbol itemTypeSymbol = ResolveType(arrayTypeNode.BaseType, symbolTable, contextSymbol);
                Debug.Assert(itemTypeSymbol != null);

                return CreateArrayTypeSymbol(itemTypeSymbol);
            }
            else if (node is GenericNameNode) {
                GenericNameNode genericNameNode = (GenericNameNode)node;
                string genericTypeName = genericNameNode.Name + "`" + genericNameNode.TypeArguments.Count;
                TypeSymbol templateType = (TypeSymbol)symbolTable.FindSymbol(genericTypeName, contextSymbol, SymbolFilter.Types);

                List<TypeSymbol> typeArguments = new List<TypeSymbol>();
                foreach (ParseNode argNode in genericNameNode.TypeArguments) {
                    TypeSymbol argType = ResolveType(argNode, symbolTable, contextSymbol);
                    typeArguments.Add(argType);
                }

                TypeSymbol resolvedSymbol = CreateGenericTypeSymbol(templateType, typeArguments);
                Debug.Assert(resolvedSymbol != null);

                return resolvedSymbol;
            }
            else {
                Debug.Assert(node is NameNode);
                NameNode nameNode = (NameNode)node;

                return (TypeSymbol)symbolTable.FindSymbol(nameNode.Name, contextSymbol, SymbolFilter.Types);
            }
        }
Пример #13
0
 public static T FindSymbol <T>(this ISymbolTable table, string name, Symbol context, SymbolFilter filter)
     where T : Symbol
 {
     return((T)table.FindSymbol(name, context, filter));
 }