Beispiel #1
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;
        }
 public ExpressionBuilder(ILocalSymbolTable symbolTable, FieldSymbol fieldContext, IErrorHandler errorHandler, CompilerOptions options) {
     _symbolTable = symbolTable;
     _symbolContext = fieldContext;
     _classContext = ((ClassSymbol)fieldContext.Parent).PrimaryPartialClass;
     _symbolSet = fieldContext.SymbolSet;
     _errorHandler = errorHandler;
     _options = options;
 }
        private static void GenerateFieldComment(ScriptTextWriter writer, FieldSymbol fieldSymbol)
        {
            writer.Write("/// <field name=\"{0}\"", fieldSymbol.GeneratedName);

            GenerateTypeAttributes(writer, fieldSymbol.AssociatedType);

            if ((fieldSymbol.Visibility & MemberVisibility.Static) != 0) {
                writer.Write(" static=\"true\"");
            }

            writer.WriteLine(">");

            GenerateFormattedComment(writer, fieldSymbol.Documentation);

            writer.WriteLine("/// </field>");
        }
Beispiel #4
0
        private static void GenerateField(ScriptGenerator generator, string typeName, FieldSymbol fieldSymbol) {
            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = true;
            if ((fieldSymbol.Visibility & MemberVisibility.Static) != 0) {
                instanceMember = false;
                writer.Write(typeName);
                writer.Write(".");
            }

            writer.Write(fieldSymbol.GeneratedName);
            if (instanceMember) {
                writer.WriteTrimmed(": ");
            }
            else {
                writer.WriteTrimmed(" = ");
            }
            CodeGenerator.GenerateScript(generator, fieldSymbol);

            if (instanceMember == false) {
                writer.Write(";");
                writer.WriteNewLine();
            }
        }
Beispiel #5
0
        private void BuildCode(FieldSymbol fieldSymbol)
        {
            ImplementationBuilder implBuilder = new ImplementationBuilder(_options, _errorHandler);
            fieldSymbol.AddImplementation(implBuilder.BuildField(fieldSymbol));

            _implementations.Add(fieldSymbol.Implementation);
        }
Beispiel #6
0
 public static void GenerateScript(ScriptGenerator generator, FieldSymbol symbol) {
     GenerateImplementationScript(generator, symbol, symbol.Implementation);
 }
Beispiel #7
0
        private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList<TypeSymbol> typeArguments)
        {
            TypeSymbol parentType = (TypeSymbol)templateMember.Parent;
            TypeSymbol instanceAssociatedType;

            if (templateMember.AssociatedType.Type == SymbolType.GenericParameter) {
                GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType;
                instanceAssociatedType = typeArguments[genericParameter.Index];
            }
            else {
                instanceAssociatedType = typeArguments[0];
            }

            if (templateMember.Type == SymbolType.Indexer) {
                IndexerSymbol templateIndexer = (IndexerSymbol)templateMember;
                IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType);

                if (templateIndexer.UseScriptIndexer) {
                    instanceIndexer.SetScriptIndexer();
                }
                instanceIndexer.SetVisibility(templateIndexer.Visibility);

                return instanceIndexer;
            }
            else if (templateMember.Type == SymbolType.Property) {
                PropertySymbol templateProperty = (PropertySymbol)templateMember;
                PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType);

                if (templateProperty.IsTransformed) {
                    instanceProperty.SetTransformedName(templateProperty.GeneratedName);
                }
                instanceProperty.SetNameCasing(templateProperty.IsCasePreserved);
                instanceProperty.SetVisibility(templateProperty.Visibility);

                return instanceProperty;
            }
            else if (templateMember.Type == SymbolType.Field) {
                FieldSymbol templateField = (FieldSymbol)templateMember;
                FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType);

                if (templateField.IsTransformed) {
                    instanceField.SetTransformedName(templateField.GeneratedName);
                }
                instanceField.SetNameCasing(templateField.IsCasePreserved);
                instanceField.SetVisibility(templateField.Visibility);

                return instanceField;
            }
            else if (templateMember.Type == SymbolType.Method) {
                MethodSymbol templateMethod = (MethodSymbol)templateMember;
                MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType);

                if (templateMethod.IsAliased) {
                    instanceMethod.SetAlias(templateMethod.Alias);
                }
                else if (templateMethod.IsTransformed) {
                    instanceMethod.SetTransformedName(templateMethod.GeneratedName);
                }
                if (templateMethod.SkipGeneration) {
                    instanceMethod.SetSkipGeneration();
                }
                if (templateMethod.InterfaceMember != null) {
                    instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember);
                }
                instanceMethod.SetNameCasing(templateMethod.IsCasePreserved);
                instanceMethod.SetVisibility(templateMethod.Visibility);

                return instanceMethod;
            }

            Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'.");
            return null;
        }
Beispiel #8
0
 public FieldExpression(Expression objectReference, FieldSymbol field)
     : this(ExpressionType.Field, objectReference, field)
 {
 }
Beispiel #9
0
 protected FieldExpression(ExpressionType type, Expression objectReference, FieldSymbol field)
     : base(type, field.AssociatedType, SymbolFilter.Public | SymbolFilter.InstanceMembers) {
     _field = field;
     _objectReference = objectReference;
 }
Beispiel #10
0
 public FieldExpression(Expression objectReference, FieldSymbol field)
     : this(ExpressionType.Field, objectReference, field) {
 }
Beispiel #11
0
 private void DumpField(FieldSymbol fieldSymbol)
 {
 }
        private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList <TypeSymbol> typeArguments)
        {
            TypeSymbol parentType = (TypeSymbol)templateMember.Parent;
            TypeSymbol instanceAssociatedType;

            if (templateMember.AssociatedType.Type == SymbolType.GenericParameter)
            {
                GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType;
                instanceAssociatedType = typeArguments[genericParameter.Index];
            }
            else
            {
                instanceAssociatedType = typeArguments[0];
            }

            if (templateMember.Type == SymbolType.Indexer)
            {
                IndexerSymbol templateIndexer = (IndexerSymbol)templateMember;
                IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType);

                if (templateIndexer.IsIntrinsic)
                {
                    instanceIndexer.SetIntrinsic();
                }
                instanceIndexer.SetVisibility(templateIndexer.Visibility);

                return(instanceIndexer);
            }
            else if (templateMember.Type == SymbolType.Property)
            {
                PropertySymbol templateProperty = (PropertySymbol)templateMember;
                PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType);

                if (templateProperty.IsTransformed)
                {
                    instanceProperty.SetTransformedName(templateProperty.GeneratedName);
                }
                instanceProperty.SetNameCasing(templateProperty.IsCasePreserved);
                instanceProperty.SetVisibility(templateProperty.Visibility);

                return(instanceProperty);
            }
            else if (templateMember.Type == SymbolType.Field)
            {
                FieldSymbol templateField = (FieldSymbol)templateMember;
                FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType);

                if (templateField.IsTransformed)
                {
                    instanceField.SetTransformedName(templateField.GeneratedName);
                }
                instanceField.SetNameCasing(templateField.IsCasePreserved);
                instanceField.SetVisibility(templateField.Visibility);

                return(instanceField);
            }
            else if (templateMember.Type == SymbolType.Method)
            {
                MethodSymbol templateMethod = (MethodSymbol)templateMember;
                MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType);

                if (templateMethod.IsTransformed)
                {
                    instanceMethod.SetTransformedName(templateMethod.GeneratedName);
                }
                if (templateMethod.InterfaceMember != null)
                {
                    instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember);
                }
                instanceMethod.SetNameCasing(templateMethod.IsCasePreserved);
                instanceMethod.SetVisibility(templateMethod.Visibility);

                return(instanceMethod);
            }

            Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'.");
            return(null);
        }
 private void DumpField(FieldSymbol fieldSymbol)
 {
 }
Beispiel #14
0
 protected FieldExpression(ExpressionType type, Expression objectReference, FieldSymbol field)
     : base(type, field.AssociatedType, SymbolFilter.Public | SymbolFilter.InstanceMembers)
 {
     _field           = field;
     _objectReference = objectReference;
 }
Beispiel #15
0
        private void BuildMembers(TypeSymbol typeSymbol) {
            if (typeSymbol.Type == SymbolType.Delegate) {
                DelegateTypeNode delegateNode = (DelegateTypeNode)typeSymbol.ParseContext;

                TypeSymbol returnType = typeSymbol.SymbolSet.ResolveType(delegateNode.ReturnType, _symbolTable, typeSymbol);
                Debug.Assert(returnType != null);

                if (returnType != null) {
                    MethodSymbol invokeMethod = new MethodSymbol("Invoke", typeSymbol, returnType, MemberVisibility.Public);
                    invokeMethod.SetTransformedName(String.Empty);

                    // Mark the method as abstract, as there is no actual implementation of the method
                    // to be generated
                    invokeMethod.SetImplementationState(SymbolImplementationFlags.Abstract);

                    typeSymbol.AddMember(invokeMethod);
                }
                return;
            }

            CustomTypeNode typeNode = (CustomTypeNode)typeSymbol.ParseContext;

            foreach (MemberNode member in typeNode.Members) {
                MemberSymbol memberSymbol = null;
                switch (member.NodeType) {
                    case ParseNodeType.FieldDeclaration:
                    case ParseNodeType.ConstFieldDeclaration:
                        memberSymbol = BuildField((FieldDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.PropertyDeclaration:
                        memberSymbol = BuildPropertyAsField((PropertyDeclarationNode)member, typeSymbol);
                        if (memberSymbol == null) {
                            memberSymbol = BuildProperty((PropertyDeclarationNode)member, typeSymbol);
                        }
                        break;
                    case ParseNodeType.IndexerDeclaration:
                        memberSymbol = BuildIndexer((IndexerDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.ConstructorDeclaration:
                    case ParseNodeType.MethodDeclaration:
                        if ((member.Modifiers & Modifiers.Extern) != 0) {
                            // Extern methods are there for defining overload signatures, so
                            // we just skip them as far as metadata goes. The validator has
                            // taken care of the requirements/constraints around use of extern methods.
                            continue;
                        }
                        memberSymbol = BuildMethod((MethodDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.EventDeclaration:
                        memberSymbol = BuildEvent((EventDeclarationNode)member, typeSymbol);
                        break;
                    case ParseNodeType.EnumerationFieldDeclaration:
                        memberSymbol = BuildEnumField((EnumerationFieldNode)member, typeSymbol);
                        break;
                }

                if (memberSymbol != null) {
                    memberSymbol.SetParseContext(member);

                    if ((typeSymbol.IsApplicationType == false) &&
                        ((memberSymbol.Type == SymbolType.Constructor) ||
                         (typeSymbol.GetMember(memberSymbol.Name) != null))) {
                        // If the type is an imported type, then it is allowed to contain
                        // overloads, and we're simply going to ignore its existence, as long
                        // as one overload has been added to the member table.
                        continue;
                    }

                    typeSymbol.AddMember(memberSymbol);

                    if ((typeSymbol.Type == SymbolType.Class) && (memberSymbol.Type == SymbolType.Event)) {
                        EventSymbol eventSymbol = (EventSymbol)memberSymbol;
                        if (eventSymbol.DefaultImplementation) {
                            // Add a private field that will serve as the backing member
                            // later on in the conversion (eg. in non-event expressions)
                            MemberVisibility visibility = MemberVisibility.PrivateInstance;
                            if ((eventSymbol.Visibility & MemberVisibility.Static) != 0) {
                                visibility |= MemberVisibility.Static;
                            }

                            FieldSymbol fieldSymbol =
                                new FieldSymbol("__" + Utility.CreateCamelCaseName(eventSymbol.Name), typeSymbol,
                                                eventSymbol.AssociatedType);
                            fieldSymbol.SetVisibility(visibility);
                            fieldSymbol.SetParseContext(((EventDeclarationNode)eventSymbol.ParseContext).Field);

                            typeSymbol.AddMember(fieldSymbol);
                        }
                    }
                }
            }
        }
Beispiel #16
0
        private FieldSymbol BuildPropertyAsField(PropertyDeclarationNode propertyNode, TypeSymbol typeSymbol) {
            AttributeNode intrinsicPropertyAttribute = AttributeNode.FindAttribute(propertyNode.Attributes, "IntrinsicProperty");
            if (intrinsicPropertyAttribute == null) {
                return null;
            }

            TypeSymbol fieldType = typeSymbol.SymbolSet.ResolveType(propertyNode.Type, _symbolTable, typeSymbol);
            Debug.Assert(fieldType != null);

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

                string scriptAlias = GetAttributeValue(propertyNode.Attributes, "ScriptAlias");
                if (scriptAlias != null) {
                    symbol.SetAlias(scriptAlias);
                }

                return symbol;
            }

            return null;
        }
        private void ImportFields(TypeSymbol typeSymbol)
        {
            TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference;

            foreach (FieldDefinition field in type.Fields) {
                if (field.IsSpecialName) {
                    continue;
                }
                if (field.IsPrivate || field.IsAssembly || field.IsFamilyAndAssembly) {
                    continue;
                }

                string fieldName = field.Name;

                TypeSymbol fieldType = ResolveType(field.FieldType);
                if (fieldType == null) {
                    continue;
                }

                MemberVisibility visibility = MemberVisibility.PrivateInstance;
                if (field.IsStatic) {
                    visibility |= MemberVisibility.Static;
                }
                if (field.IsPublic) {
                    visibility |= MemberVisibility.Public;
                }
                else if (field.IsFamily || field.IsFamilyOrAssembly) {
                    visibility |= MemberVisibility.Protected;
                }

                FieldSymbol fieldSymbol = new FieldSymbol(fieldName, typeSymbol, fieldType);

                fieldSymbol.SetVisibility(visibility);
                ImportMemberDetails(fieldSymbol, null, field);

                typeSymbol.AddMember(fieldSymbol);
            }
        }
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            _rootScope = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            _currentScope = _rootScope;

            Expression initializerExpression;

            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;
                }

                initializerExpression =
                    new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                          defaultValue);
            }

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

            return new SymbolImplementation(statements, null);
        }
        private void ImportProperties(TypeSymbol typeSymbol)
        {
            TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference;

            foreach (PropertyDefinition property in type.Properties) {
                if (property.IsSpecialName) {
                    continue;
                }
                if (property.GetMethod == null) {
                    continue;
                }
                if (property.GetMethod.IsPrivate || property.GetMethod.IsAssembly || property.GetMethod.IsFamilyAndAssembly) {
                    continue;
                }

                string propertyName = property.Name;
                bool preserveCase = MetadataHelpers.ShouldPreserveCase(property);
                bool intrinsicProperty = MetadataHelpers.ShouldTreatAsIntrinsicProperty(property);

                TypeSymbol propertyType = ResolveType(property.PropertyType);
                if (propertyType == null) {
                    continue;
                }

                PropertySymbol propertySymbol = null;
                if (property.Parameters.Count != 0) {
                    IndexerSymbol indexerSymbol = new IndexerSymbol(typeSymbol, propertyType);
                    ImportMemberDetails(indexerSymbol, property.GetMethod, property);

                    if (intrinsicProperty) {
                        indexerSymbol.SetIntrinsic();
                    }

                    propertySymbol = indexerSymbol;
                    propertySymbol.SetNameCasing(preserveCase);
                }
                else {
                    if (intrinsicProperty) {
                        // Properties marked with this attribute are to be thought of as
                        // fields. If they are read-only, the C# compiler will enforce that,
                        // so we don't have to worry about making them read-write via a field
                        // instead of a property

                        FieldSymbol fieldSymbol = new FieldSymbol(propertyName, typeSymbol, propertyType);
                        ImportMemberDetails(fieldSymbol, property.GetMethod, property);

                        string alias = MetadataHelpers.GetScriptAlias(property);
                        if (String.IsNullOrEmpty(alias) == false) {
                            fieldSymbol.SetAlias(alias);
                        }

                        typeSymbol.AddMember(fieldSymbol);
                    }
                    else {
                        propertySymbol = new PropertySymbol(propertyName, typeSymbol, propertyType);
                        ImportMemberDetails(propertySymbol, property.GetMethod, property);
                    }
                }

                if (propertySymbol != null) {
                    SymbolImplementationFlags implFlags = SymbolImplementationFlags.Regular;
                    if (property.SetMethod == null) {
                        implFlags |= SymbolImplementationFlags.ReadOnly;
                    }
                    if (property.GetMethod.IsAbstract) {
                        implFlags |= SymbolImplementationFlags.Abstract;
                    }
                    propertySymbol.SetImplementationState(implFlags);

                    typeSymbol.AddMember(propertySymbol);
                }
            }
        }