📄 class.cs
字号:
public bool AddClassOrStruct (TypeContainer c) { if (!AddToTypeContainer (c)) return false; types.Add (c); return true; } public void AddDelegate (Delegate d) { if (!AddToTypeContainer (d)) return; if (delegates == null) delegates = new MemberCoreArrayList (); delegates.Add (d); } public void AddMethod (Method method) { if (!AddToMemberContainer (method)) return; if (methods == null) methods = new MethodArrayList (this); if (method.MemberName.Left != null) methods.Insert (0, method); else methods.Add (method); } public void AddConstructor (Constructor c) { if (c.Name != Basename) { Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type"); } bool is_static = (c.ModFlags & Modifiers.STATIC) != 0; if (is_static){ if (default_static_constructor != null) { Report.SymbolRelatedToPreviousError (default_static_constructor); Report.Error (111, c.Location, Error111, c.GetSignatureForError ()); return; } default_static_constructor = c; } else { if (c.IsDefault ()){ if (default_constructor != null) { Report.SymbolRelatedToPreviousError (default_constructor); Report.Error (111, c.Location, Error111, c.Location, c.GetSignatureForError ()); return; } default_constructor = c; } if (instance_constructors == null) instance_constructors = new MemberCoreArrayList (); instance_constructors.Add (c); } } internal static string Error111 { get { return "`{0}' is already defined. Rename this member or use different parameter types"; } } public bool AddInterface (TypeContainer iface) { if (!AddToTypeContainer (iface)) return false; if (interfaces == null) { interfaces = new MemberCoreArrayList (); } interfaces.Add (iface); return true; } public void AddField (FieldMember field) { if (!AddToMemberContainer (field)) return; if (fields == null) fields = new MemberCoreArrayList (); fields.Add (field); if (field.HasInitializer) RegisterFieldForInitialization (field); if ((field.ModFlags & Modifiers.STATIC) != 0) return; if (first_nonstatic_field == null) { first_nonstatic_field = field; return; } if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent && RootContext.WarningLevel >= 3) { Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent); Report.Warning (282, field.Location, "struct instance field `{0}' found in different declaration from instance field `{1}'", field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ()); } } public void AddProperty (Property prop) { if (!AddToMemberContainer (prop) || !AddToMemberContainer (prop.Get) || !AddToMemberContainer (prop.Set)) return; if (properties == null) properties = new MemberCoreArrayList (); if (prop.MemberName.Left != null) properties.Insert (0, prop); else properties.Add (prop); } public void AddEvent (Event e) { if (!AddToMemberContainer (e)) return; if (e is EventProperty) { if (!AddToMemberContainer (e.Add)) return; if (!AddToMemberContainer (e.Remove)) return; } if (events == null) events = new MemberCoreArrayList (); events.Add (e); } /// <summary> /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute /// </summary> public void AddIndexer (Indexer i) { if (indexers == null) indexers = new IndexerArrayList (this); if (i.IsExplicitImpl) indexers.Insert (0, i); else indexers.Add (i); } public void AddOperator (Operator op) { if (!AddToMemberContainer (op)) return; if (operators == null) operators = new OperatorArrayList (this); operators.Add (op); } public void AddIterator (Iterator i) { if (iterators == null) iterators = new ArrayList (); iterators.Add (i); } public void AddType (TypeContainer tc) { types.Add (tc); } public void AddPart (ClassPart part) { if (parts == null) parts = new ArrayList (); parts.Add (part); } public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb) { if (a.Type == TypeManager.default_member_type) { if (Indexers != null) { Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer"); return; } } base.ApplyAttributeBuilder (a, cb); } public override AttributeTargets AttributeTargets { get { switch (Kind) { case Kind.Class: return AttributeTargets.Class; case Kind.Struct: return AttributeTargets.Struct; case Kind.Interface: return AttributeTargets.Interface; default: throw new NotSupportedException (); } } } public ArrayList Types { get { return types; } } public MethodArrayList Methods { get { return methods; } } public ArrayList Constants { get { return constants; } } public ArrayList Interfaces { get { return interfaces; } } public ArrayList Iterators { get { return iterators; } } public string Base { get { return base_class_name; } } public ArrayList Bases { get { return type_bases; } set { type_bases = value; } } public ArrayList Fields { get { return fields; } } public ArrayList InstanceConstructors { get { return instance_constructors; } } public ArrayList Properties { get { return properties; } } public ArrayList Events { get { return events; } } public ArrayList Enums { get { return enums; } } public ArrayList Indexers { get { return indexers; } } public ArrayList Operators { get { return operators; } } public ArrayList Delegates { get { return delegates; } } public ArrayList Parts { get { return parts; } } protected override TypeAttributes TypeAttr { get { return Modifiers.TypeAttr (ModFlags, this) | base.TypeAttr; } } public string IndexerName { get { return indexers == null ? DefaultIndexerName : indexers.IndexerName; } } public bool IsComImport { get { if (OptAttributes == null) return false; return OptAttributes.Contains (TypeManager.comimport_attr_type, EmitContext); } } public virtual void RegisterFieldForInitialization (FieldMember field) { if ((field.ModFlags & Modifiers.STATIC) != 0){ if (initialized_static_fields == null) initialized_static_fields = new ArrayList (); initialized_static_fields.Add (field); } else { if (initialized_fields == null) initialized_fields = new ArrayList (); initialized_fields.Add (field); } } bool CanElideInitializer (Type field_type, Constant c) { if (field_type == c.Type) return true; if (TypeManager.IsValueType (field_type) || TypeManager.HasElementType (field_type)) return false; // Reference type with null initializer. return c.Type == TypeManager.null_type; } // // Emits the instance field initializers // public virtual bool EmitFieldInitializers (EmitContext ec) { ArrayList fields; Expression instance_expr; if (ec.IsStatic){ fields = initialized_static_fields; instance_expr = null; } else { fields = initialized_fields; instance_expr = new This (Location.Null).Resolve (ec); } if (fields == null) return true; foreach (FieldMember f in fields){ Expression e = f.GetInitializerExpression (ec); if (e == null) return false; Location l = f.Location; FieldExpr fe = new FieldExpr (f.FieldBuilder, l, true); fe.InstanceExpression = instance_expr; ExpressionStatement a = new Assign (fe, e, l); a = a.ResolveStatement (ec); if (a == null) return false; Constant c = e as Constant; if (c != null && c.IsDefaultValue && CanElideInitializer (f.MemberType, c)) continue; a.EmitStatement (ec); } return true; } // // Defines the default constructors // protected void DefineDefaultConstructor (bool is_static) { Constructor c; // The default constructor is public // If the class is abstract, the default constructor is protected // The default static constructor is private int mods = Modifiers.PUBLIC; if (is_static) mods = Modifiers.STATIC | Modifiers.PRIVATE; else if ((ModFlags & Modifiers.ABSTRACT) != 0) mods = Modifiers.PROTECTED; TypeContainer constructor_parent = this; if (Parts != null) constructor_parent = (TypeContainer) Parts [0]; c = new Constructor (constructor_parent, Basename, mods, Parameters.EmptyReadOnlyParameters, new GeneratedBaseInitializer (Location), Location); AddConstructor (c); c.Block = new ToplevelBlock (null, Location); } /// <remarks> /// The pending methods that need to be implemented // (interfaces or abstract methods) /// </remarks> public PendingImplementation Pending; public abstract PendingImplementation GetPendingImplementations (); TypeExpr[] GetPartialBases (out TypeExpr base_class) { ArrayList ifaces = new ArrayList (); base_class = null; Location base_loc = Location.Null; foreach (ClassPart part in parts) { TypeExpr new_base_class; TypeExpr[] new_ifaces; new_ifaces = part.GetClassBases (out new_base_class); if (new_ifaces == null && new_base_class != null) return null; if ((base_class != null) && (new_base_class != null) && !base_class.Equals (new_base_class)) { Report.Error (263, part.Location, "Partial declarations of `{0}' must " + "not specify different base classes", Name); if (!base_loc.IsNull) Report.LocationOfPreviousError (base_loc); return null; } if ((base_class == null) && (new_base_class != null)) { base_class = new_base_class; base_loc = part.Location; } if (new_ifaces == null) continue; foreach (TypeExpr iface in new_ifaces) { bool found = false; foreach (TypeExpr old_iface in ifaces) { if (old_iface.Equals (iface)) { found = true; break; } } if (!found) ifaces.Add (iface); } } TypeExpr[] retval = new TypeExpr [ifaces.Count]; ifaces.CopyTo (retval, 0); return retval; } TypeExpr[] GetNormalBases (out TypeExpr base_class) { base_class = null; int count = Bases.Count; int start = 0, i, j; if (Kind == Kind.Class){ TypeExpr name = ResolveBaseTypeExpr ( (Expression) Bases [0], false, Location);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -