public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression) { if (IsPartialPart) PartialContainer.RegisterFieldForInitialization (field, expression); if ((field.ModFlags & Modifiers.STATIC) != 0){ if (initialized_static_fields == null) { HasStaticFieldInitializer = true; initialized_static_fields = new List<FieldInitializer> (4); } initialized_static_fields.Add (expression); } else { if (initialized_fields == null) initialized_fields = new List<FieldInitializer> (4); initialized_fields.Add (expression); } }
public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression) { if ((field.ModFlags & Modifiers.STATIC) == 0) { Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers", field.GetSignatureForError ()); return; } base.RegisterFieldForInitialization (field, expression); }
public void ResolveFieldInitializers (BlockContext ec) { Debug.Assert (!IsPartialPart); if (ec.IsStatic) { if (initialized_static_fields == null) return; bool has_complex_initializer = !ec.Module.Compiler.Settings.Optimize; int i; ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count]; for (i = 0; i < initialized_static_fields.Count; ++i) { FieldInitializer fi = initialized_static_fields [i]; ExpressionStatement s = fi.ResolveStatement (ec); if (s == null) { s = EmptyExpressionStatement.Instance; } else if (!fi.IsSideEffectFree) { has_complex_initializer = true; } init [i] = s; } for (i = 0; i < initialized_static_fields.Count; ++i) { FieldInitializer fi = initialized_static_fields [i]; // // Need special check to not optimize code like this // static int a = b = 5; // static int b = 0; // if (!has_complex_initializer && fi.IsDefaultInitializer) continue; ec.AssignmentInfoOffset += fi.AssignmentOffset; ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i])); } return; } if (initialized_fields == null) return; for (int i = 0; i < initialized_fields.Count; ++i) { FieldInitializer fi = initialized_fields [i]; // // Clone before resolving otherwise when field initializer is needed // in more than 1 constructor any resolve after the initial one would // only took the resolved expression which is problem for expressions // that generate extra expressions or code during Resolve phase // var cloned = fi.Clone (new CloneContext ()); ExpressionStatement s = fi.ResolveStatement (ec); if (s == null) { initialized_fields [i] = new FieldInitializer (fi.Field, ErrorExpression.Instance, Location.Null); continue; } // // Field is re-initialized to its default value => removed // if (fi.IsDefaultInitializer && Kind != MemberKind.Struct && ec.Module.Compiler.Settings.Optimize) continue; ec.AssignmentInfoOffset += fi.AssignmentOffset; ec.CurrentBlock.AddScopeStatement (new StatementExpression (s)); initialized_fields [i] = (FieldInitializer) cloned; } }
public void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression) { if (IsPartialPart) PartialContainer.RegisterFieldForInitialization (field, expression); if ((field.ModFlags & Modifiers.STATIC) != 0){ if (initialized_static_fields == null) { HasStaticFieldInitializer = true; initialized_static_fields = new List<FieldInitializer> (4); } initialized_static_fields.Add (expression); } else { if (Kind == MemberKind.Struct) { if (Compiler.Settings.Version != LanguageVersion.Experimental) { Report.Error (573, expression.Location, "'{0}': Structs cannot have instance property or field initializers", GetSignatureForError ()); } } if (initialized_fields == null) initialized_fields = new List<FieldInitializer> (4); initialized_fields.Add (expression); } }
public void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression) { if (IsPartialPart) PartialContainer.RegisterFieldForInitialization (field, expression); if ((field.ModFlags & Modifiers.STATIC) != 0){ if (initialized_static_fields == null) { HasStaticFieldInitializer = true; initialized_static_fields = new List<FieldInitializer> (4); } initialized_static_fields.Add (expression); } else { if (Kind == MemberKind.Struct && Compiler.Settings.Version < LanguageVersion.V_6) { Report.FeatureIsNotAvailable (Compiler, expression.Location, "struct instance member initializer"); } if (initialized_fields == null) initialized_fields = new List<FieldInitializer> (4); initialized_fields.Add (expression); } }
public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression) { if ((field.ModFlags & Modifiers.STATIC) == 0 && !HasExplicitConstructor ()) { Report.Error (8054, field.Location, "`{0}': Structs without explicit constructors cannot contain members with initializers", field.GetSignatureForError ()); return; } base.RegisterFieldForInitialization (field, expression); }