public abstract class TypeContainer : MemberCore
{
public readonly MemberKind Kind;
- public readonly string Basename;
protected List<TypeContainer> containers;
: base (parent, name, attrs)
{
this.Kind = kind;
- if (name != null)
- this.Basename = name.Basename;
-
defined_names = new Dictionary<string, MemberCore> ();
}
public virtual void AddPartial (TypeDefinition next_part)
{
MemberCore mc;
- (PartialContainer ?? this).defined_names.TryGetValue (next_part.Basename, out mc);
+ (PartialContainer ?? this).defined_names.TryGetValue (next_part.MemberName.Basename, out mc);
AddPartial (next_part, mc as TypeDefinition);
}
containers.Remove (cont);
var tc = Parent == Module ? Module : this;
- tc.defined_names.Remove (cont.Basename);
+ tc.defined_names.Remove (cont.MemberName.Basename);
}
public virtual void VerifyMembers ()
public int AnonymousMethodsCounter;
public int MethodGroupsCounter;
- static readonly string[] attribute_targets = new string[] { "type" };
+ static readonly string[] attribute_targets = new [] { "type" };
+ static readonly string[] attribute_targets_primary = new [] { "type", "method" };
/// <remarks>
/// The pending methods that need to be implemented
public ParametersCompiled PrimaryConstructorParameters { get; set; }
+ public Arguments PrimaryConstructorBaseArguments { get; set; }
+
+ public Location PrimaryConstructorBaseArgumentsStart { get; set; }
+
public TypeParameters TypeParametersAll {
get {
return all_type_parameters;
public override string[] ValidAttributeTargets {
get {
- return attribute_targets;
+ return PrimaryConstructorParameters != null ? attribute_targets_primary : attribute_targets;
}
}
public override void AddTypeContainer (TypeContainer tc)
{
- AddNameToContainer (tc, tc.Basename);
+ AddNameToContainer (tc, tc.MemberName.Basename);
base.AddTypeContainer (tc);
}
if (symbol is TypeParameter) {
Report.Error (692, symbol.Location,
"Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
- } else if (symbol is PrimaryConstructorField && mc is TypeParameter) {
- Report.Error (9003, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as type parameter `{1}'",
- symbol.Parent.GetSignatureForError (), symbol.GetSignatureForError ());
} else {
Report.Error (102, symbol.Location,
"The type `{0}' already contains a definition for `{1}'",
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
+ if (a.Target == AttributeTargets.Method) {
+ foreach (var m in members) {
+ var c = m as Constructor;
+ if (c == null)
+ continue;
+
+ if (c.IsPrimaryConstructor) {
+ c.ApplyAttributeBuilder (a, ctor, cdata, pa);
+ return;
+ }
+ }
+
+ throw new InternalErrorException ();
+ }
+
if (has_normal_indexers && a.Type == pa.DefaultMember) {
Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
return;
}
}
- public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
+ public void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
{
if (IsPartialPart)
PartialContainer.RegisterFieldForInitialization (field, expression);
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);
//
// Field is re-initialized to its default value => removed
//
- if (fi.IsDefaultInitializer && ec.Module.Compiler.Settings.Optimize)
+ if (fi.IsDefaultInitializer && Kind != MemberKind.Struct && ec.Module.Compiler.Settings.Optimize)
continue;
ec.AssignmentInfoOffset += fi.AssignmentOffset;
CreateMetadataName (sb);
TypeBuilder = Module.CreateBuilder (sb.ToString (), TypeAttr, type_size);
} else {
- TypeBuilder = parent_def.TypeBuilder.DefineNestedType (Basename, TypeAttr, null, type_size);
+ TypeBuilder = parent_def.TypeBuilder.DefineNestedType (MemberName.Basename, TypeAttr, null, type_size);
}
if (DeclaringAssembly.Importer != null)
public SourceMethodBuilder CreateMethodSymbolEntry ()
{
- if (Module.DeclaringAssembly.SymbolWriter == null)
+ if (Module.DeclaringAssembly.SymbolWriter == null || (ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
return null;
var source_file = GetCompilationSourceFile ();
if (PrimaryConstructorParameters != null) {
if (PartialContainer.PrimaryConstructorParameters != null) {
- Report.Error (9001, Location, "Only one part of a partial type can declare primary constructor parameters");
+ Report.Error (8036, Location, "Only one part of a partial type can declare primary constructor parameters");
} else {
PartialContainer.PrimaryConstructorParameters = PrimaryConstructorParameters;
}
}
if (iface_exprs != null) {
+ if (!PrimaryConstructorBaseArgumentsStart.IsNull) {
+ Report.Error (8049, PrimaryConstructorBaseArgumentsStart, "Implemented interfaces cannot have arguments");
+ }
+
foreach (var iface_type in iface_exprs) {
// Prevents a crash, the interface might not have been resolved: 442144
if (iface_type == null)
// defined after current container
//
if (class_partial_parts != null) {
- foreach (var pp in class_partial_parts)
+ foreach (var pp in class_partial_parts) {
+ if (pp.PrimaryConstructorBaseArguments != null)
+ PrimaryConstructorBaseArguments = pp.PrimaryConstructorBaseArguments;
+
pp.DoDefineBaseType ();
+ }
}
{
base.RemoveContainer (cont);
Members.Remove (cont);
- Cache.Remove (cont.Basename);
+ Cache.Remove (cont.MemberName.Basename);
}
protected virtual bool DoResolveTypeParameters ()
{
- var tparams = CurrentTypeParameters;
+ var tparams = MemberName.TypeParameters;
if (tparams == null)
return true;
error = true;
return false;
}
+
+ if (IsPartialPart) {
+ var pc_tp = PartialContainer.CurrentTypeParameters [i];
+
+ tp.Create (spec, this);
+ tp.Define (pc_tp);
+
+ if (tp.OptAttributes != null) {
+ if (pc_tp.OptAttributes == null)
+ pc_tp.OptAttributes = tp.OptAttributes;
+ else
+ pc_tp.OptAttributes.Attrs.AddRange (tp.OptAttributes.Attrs);
+ }
+ }
}
if (IsPartialPart) {
{
}
- public Arguments PrimaryConstructorBaseArguments { get; set; }
+ public ToplevelBlock PrimaryConstructorBlock { get; set; }
protected override TypeAttributes TypeAttr {
get {
return;
}
- if (symbol is PrimaryConstructorField) {
- Report.Error (9004, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as containing type",
- symbol.Parent.GetSignatureForError ());
- return;
- }
-
InterfaceMemberBase imb = symbol as InterfaceMemberBase;
if (imb == null || !imb.IsExplicitImpl) {
Report.SymbolRelatedToPreviousError (this);
// The default static constructor is private
Modifiers mods;
+ ParametersCompiled parameters = null;
if (is_static) {
mods = Modifiers.STATIC | Modifiers.PRIVATE;
+ parameters = ParametersCompiled.EmptyReadOnlyParameters;
} else {
mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
+ parameters = PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters;
}
- var c = new Constructor (this, MemberName.Name, mods, null, PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters, Location);
+ var c = new Constructor (this, MemberName.Name, mods, null, parameters, Location);
if (Kind == MemberKind.Class)
c.Initializer = new GeneratedBaseInitializer (Location, PrimaryConstructorBaseArguments);
- if (PrimaryConstructorParameters != null)
+ if (PrimaryConstructorParameters != null && !is_static) {
c.IsPrimaryConstructor = true;
+ c.caching_flags |= Flags.MethodOverloadsExist;
+ }
AddConstructor (c, true);
- c.Block = new ToplevelBlock (Compiler, c.ParameterInfo, Location) {
- IsCompilerGenerated = true
- };
+ if (PrimaryConstructorBlock == null) {
+ c.Block = new ToplevelBlock (Compiler, parameters, Location) {
+ IsCompilerGenerated = true
+ };
+ } else {
+ c.Block = PrimaryConstructorBlock;
+ }
return c;
}
CheckProtectedModifier ();
if (PrimaryConstructorParameters != null) {
- foreach (Parameter p in PrimaryConstructorParameters.FixedParameters) {
- if ((p.ModFlags & Parameter.Modifier.RefOutMask) != 0)
- continue;
- var f = new PrimaryConstructorField (this, p);
- AddField (f);
+ foreach (Parameter p in PrimaryConstructorParameters.FixedParameters) {
+ if (p.Name == MemberName.Name) {
+ Report.Error (8039, p.Location, "Primary constructor of type `{0}' has parameter of same name as containing type",
+ GetSignatureForError ());
+ }
- generated_primary_constructor.Block.AddStatement (
- new StatementExpression (new PrimaryConstructorAssign (f, p), p.Location));
+ if (CurrentTypeParameters != null) {
+ for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
+ var tp = CurrentTypeParameters [i];
+ if (p.Name == tp.Name) {
+ Report.Error (8038, p.Location, "Primary constructor of type `{0}' has parameter of same name as type parameter `{1}'",
+ GetSignatureForError (), p.GetSignatureForError ());
+ }
+ }
+ }
}
}
protected override bool DoDefineMembers ()
{
- if (PrimaryConstructorParameters != null)
+ var res = base.DoDefineMembers ();
+
+ if (PrimaryConstructorParameters != null || (initialized_fields != null && !HasUserDefaultConstructor ())) {
generated_primary_constructor = DefineDefaultConstructor (false);
+ generated_primary_constructor.Define ();
+ }
- return base.DoDefineMembers ();
+ return res;
}
public override void Emit ()
base.Emit ();
}
+ bool HasUserDefaultConstructor ()
+ {
+ foreach (var m in PartialContainer.Members) {
+ var c = m as Constructor;
+ if (c == null)
+ continue;
+
+ if (!c.IsStatic && c.ParameterInfo.IsEmpty)
+ return true;
+ }
+
+ return false;
+ }
+
public override bool IsUnmanagedType ()
{
if (has_unmanaged_check_done)
base_type = Compiler.BuiltinTypes.ValueType;
return ifaces;
}
-
- 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);
- }
-
}
/// <summary>