示例#1
0
 public FieldNode(IdNode identifier, TypeNode type, bool isStatic, EncapsulationNode encapsulation,
                  VariableInitializerNode assigner, Token token)
 {
     this.identifier    = identifier;
     this.type          = type;
     this.isStatic      = isStatic;
     this.encapsulation = encapsulation;
     this.assigner      = assigner;
     this.token         = token;
 }
示例#2
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (GetDefaultValue(fieldType, symbolSet) is object defaultValue)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(hasInitializer: true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }
示例#3
0
        private FieldSymbol BuildField(FieldDeclarationNode fieldNode, TypeSymbol typeSymbol)
        {
            TypeSymbol fieldType = typeSymbol.SymbolSet.ResolveType(fieldNode.Type, _symbolTable, typeSymbol);

            Debug.Assert(fieldType != null);

            if (fieldType != null)
            {
                FieldSymbol symbol = new FieldSymbol(fieldNode.Name, typeSymbol, fieldType);
                BuildMemberDetails(symbol, typeSymbol, fieldNode, fieldNode.Attributes);

                if (fieldNode.Initializers.Count != 0)
                {
                    VariableInitializerNode initializer = (VariableInitializerNode)fieldNode.Initializers[0];

                    if ((initializer.Value != null) && (initializer.Value.NodeType != ParseNodeType.Literal))
                    {
                        symbol.SetImplementationState(/* hasInitializer */ true);
                    }
                }

                if (fieldNode.NodeType == ParseNodeType.ConstFieldDeclaration)
                {
                    Debug.Assert(fieldNode.Initializers.Count == 1);

                    VariableInitializerNode initializer = (VariableInitializerNode)fieldNode.Initializers[0];
                    if ((initializer.Value != null) && (initializer.Value.NodeType == ParseNodeType.Literal))
                    {
                        symbol.SetConstant();
                        symbol.Value = ((LiteralToken)initializer.Value.Token).LiteralValue;
                    }

                    // TODO: Handle other constant cases that can be evaluated at compile
                    //       time (eg. combining enum flags)
                }

                return(symbol);
            }

            return(null);
        }
示例#4
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                object defaultValue = null;

                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (fieldType.Type == SymbolType.Enumeration)
                {
                    // The default for named values is null, so this only applies to
                    // regular enum types

                    EnumerationSymbol enumType = (EnumerationSymbol)fieldType;

                    if (enumType.UseNamedValues == false)
                    {
                        defaultValue = 0;
                    }
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Integer) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedInteger) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Long) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedLong) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Short) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedShort) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Byte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.SignedByte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Double) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Single) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Decimal))
                {
                    defaultValue = 0;
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Boolean))
                {
                    defaultValue = false;
                }

                if (defaultValue != null)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(/* hasInitializer */ true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }