コード例 #1
0
ファイル: FunctionGenerator.cs プロジェクト: ronsaldo/chela
        private void GenerateStaticFieldInitializations(AstNode node, Function ctor, Structure building)
        {
            // Get the static initialization fields.
            List<FieldDeclaration> staticInitedField = new List<FieldDeclaration> ();
            bool isUnsafe = false;
            foreach(FieldDeclaration decl in building.GetFieldInitializations())
            {
                Variable variable = decl.GetVariable();
                if(variable.IsStatic())
                {
                    staticInitedField.Add(decl);
                    if(variable.IsUnsafe())
                        isUnsafe = true;
                }
            }

            // If there aren't field to initialize, don't create the static constructor.
            if(staticInitedField.Count == 0)
                return;

            // Check if unsafe its required.
            if(isUnsafe && !ctor.IsUnsafe())
                Error(node, "static constructor must be unsafe due to implicit unsafe static field initialization.");

            // Initialize the static fields.
            GenerateStaticFieldInitializations(node, staticInitedField);
        }
コード例 #2
0
ファイル: FunctionGenerator.cs プロジェクト: ronsaldo/chela
        private void GenerateFieldInitializations(AstNode node, Structure building)
        {
            foreach(FieldDeclaration decl in building.GetFieldInitializations())
            {
                // Ignore static fields.
                FieldVariable field = (FieldVariable)decl.GetVariable();
                if(field.IsStatic())
                    continue;

                // Load self.
                builder.CreateLoadArg(0);

                // Visit the value expression.
                Expression value = decl.GetDefaultValue();
                value.Accept(this);

                // Cast the value type.
                IChelaType valueType = value.GetNodeType();
                IChelaType coercionType = decl.GetCoercionType();
                if(valueType != coercionType)
                    Cast(decl, value.GetNodeValue(), valueType, coercionType);

                // Store the value.
                builder.CreateStoreField(field);
            }
        }