예제 #1
0
        void GenerateBinding(GenerateTypeInfo typeInfo, Analyze.Binding binding)
        {
            if (binding.Expression != null)
            {
                Analyze.Expressions.Literal literal;

                // literal -> static field
                if ((literal = binding.Expression as Analyze.Expressions.Literal) != null)
                {
                    GenerateLiteralBinding(typeInfo, binding, literal);
                }
            }
        }
예제 #2
0
        void GenerateLiteralBinding(GenerateTypeInfo typeInfo, Analyze.Binding binding, Analyze.Expressions.Literal literal)
        {
            if (literal.ResolvedType == null)
            {
                Unit.AddError(new GeneratorError
                {
                    Message   = string.Format(ErrorMessages.E_0017_Generator_UnresolvedType, ApteridError.Truncate(literal.SyntaxNode.Text)),
                    ErrorNode = literal.SyntaxNode
                });
                return;
            }

            var atts = FieldAttributes.Static | FieldAttributes.InitOnly;

            atts |= binding.IsPublic ? FieldAttributes.Public : FieldAttributes.Private;
            var field = typeInfo.TypeBuilder.DefineField(binding.Name.Name, literal.ResolvedType.CLRType, atts);

            typeInfo.Bindings.Add(literal, field);

            if (literal.Value == null)
            {
                if (field.FieldType.IsValueType)
                {
                    field.SetConstant(Activator.CreateInstance(field.FieldType));
                }
                else
                {
                    field.SetConstant(null);
                }
            }
            else
            {
                typeInfo.StaticFieldsToInit.Add(new FieldInitInfo
                {
                    Node    = binding.SyntaxNode,
                    Field   = field,
                    GenLoad = il => GenerateLoadLiteral(il, literal, field.FieldType)
                });
            }
        }