return AddToContainer (ds, ds.Basename);
}
+ protected virtual void RemoveMemberType (DeclSpace ds)
+ {
+ RemoveFromContainer (ds.Basename);
+ }
+
public void AddConstant (Const constant)
{
if (!AddMember (constant))
return tc;
}
+ public virtual void RemoveTypeContainer (TypeContainer next_part)
+ {
+ if (types != null)
+ types.Remove (next_part);
+ RemoveMemberType (next_part);
+ }
+
public void AddDelegate (Delegate d)
{
if (!AddMemberType (d))
public void AddConstructor (Constructor c)
{
- if (c.Name != MemberName.Name) {
- Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
- }
-
bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
if (!AddToContainer (c, is_static ?
ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
public void ResolveFieldInitializers (EmitContext ec)
{
- // Field initializers are tricky for partial classes. They have to
- // share same costructor (block) but they have they own resolve scope.
- DeclSpace orig = ec.DeclContainer;
-
if (partial_parts != null) {
foreach (TypeContainer part in partial_parts) {
- ec.DeclContainer = part;
part.DoResolveFieldInitializers (ec);
}
}
- ec.DeclContainer = PartialContainer;
DoResolveFieldInitializers (ec);
- ec.DeclContainer = orig;
}
void DoResolveFieldInitializers (EmitContext ec)
if (initialized_static_fields == null)
return;
- bool has_complex_initializer = false;
- using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
- foreach (FieldInitializer fi in initialized_static_fields) {
- fi.ResolveStatement (ec);
- if (!fi.IsComplexInitializer)
- continue;
-
- has_complex_initializer = true;
+ bool has_complex_initializer = !RootContext.Optimize;
+ int i;
+ ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
+ for (i = 0; i < initialized_static_fields.Count; ++i) {
+ FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
+ ExpressionStatement s = fi.ResolveStatement (ec);
+ if (s == null) {
+ s = EmptyExpressionStatement.Instance;
+ } else if (fi.IsComplexInitializer) {
+ has_complex_initializer |= true;
}
+ init [i] = s;
+ }
+
+ for (i = 0; i < initialized_static_fields.Count; ++i) {
+ FieldInitializer fi = (FieldInitializer) 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 && RootContext.Optimize) {
- for (int i = 0; i < initialized_static_fields.Count; ++i) {
- FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
- if (fi.IsDefaultInitializer) {
- initialized_static_fields.RemoveAt (i);
- --i;
- }
- }
- }
+ //
+ if (!has_complex_initializer && fi.IsDefaultInitializer)
+ continue;
+
+ ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
}
return;
if (initialized_fields == null)
return;
- using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
- for (int i = 0; i < initialized_fields.Count; ++i) {
- FieldInitializer fi = (FieldInitializer) initialized_fields [i];
- fi.ResolveStatement (ec);
- if (fi.IsDefaultInitializer && RootContext.Optimize) {
- // Field is re-initialized to its default value => removed
- initialized_fields.RemoveAt (i);
- --i;
- }
- }
+ for (int i = 0; i < initialized_fields.Count; ++i) {
+ FieldInitializer fi = (FieldInitializer) initialized_fields [i];
+ ExpressionStatement s = fi.ResolveStatement (ec);
+ if (s == null)
+ continue;
+
+ //
+ // Field is re-initialized to its default value => removed
+ //
+ if (fi.IsDefaultInitializer && RootContext.Optimize)
+ continue;
+
+ ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
}
}
}
current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
-
- foreach (TypeParameter type_param in TypeParameters)
- if (!type_param.CheckDependencies ()) {
- error = true;
- return false;
- }
-
- if (current_type != null) {
- current_type = current_type.ResolveAsTypeTerminal (this, false);
- if (current_type == null) {
- error = true;
- return false;
- }
-
- CurrentType = current_type.Type;
+ current_type = current_type.ResolveAsTypeTerminal (this, false);
+ if (current_type == null) {
+ error = true;
+ return false;
}
+ CurrentType = current_type.Type;
return true;
}
return true;
}
- public static void Error_KeywordNotAllowed (Location loc)
- {
- Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
- }
-
/// <summary>
/// Populates our TypeBuilder with fields and methods
/// </summary>
- public override bool DefineMembers ()
+ public override bool Define ()
{
if (members_defined)
return members_defined_ok;
- if (!base.DefineMembers ())
- return false;
-
members_defined_ok = DoDefineMembers ();
members_defined = true;
}
}
- public override bool Define ()
- {
- CheckProtectedModifier ();
- return true;
- }
-
public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
{
return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
{
ArrayList members = new ArrayList ();
- DefineMembers ();
+ Define ();
if (methods != null) {
int len = methods.Count;
set { caching_flags |= Flags.HasExplicitLayout; }
}
+ public bool HasStructLayout {
+ get { return (caching_flags & Flags.HasStructLayout) != 0; }
+ set { caching_flags |= Flags.HasStructLayout; }
+ }
+
//
// Return the nested type with name @name. Ensures that the nested type
// is defined if necessary. Do _not_ use this when you have a MemberCache handy.
if ((f.caching_flags & Flags.IsAssigned) != 0)
continue;
+
+ //
+ // Don't be pendatic over serializable attributes
+ //
+ if (f.OptAttributes != null || PartialContainer.HasStructLayout)
+ continue;
Constant c = New.Constantify (f.MemberType);
Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
return;
}
- if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
- PartialContainer.HasExplicitLayout = true;
+ if (a.Type == TypeManager.struct_layout_attribute_type) {
+ PartialContainer.HasStructLayout = true;
+
+ if (a.GetLayoutKindValue () == LayoutKind.Explicit)
+ PartialContainer.HasExplicitLayout = true;
}
base.ApplyAttributeBuilder (a, cb);
}
Constructor c = new Constructor (this, MemberName.Name, mods,
- Parameters.EmptyReadOnlyParameters,
+ null, Parameters.EmptyReadOnlyParameters,
new GeneratedBaseInitializer (Location),
Location);
public override bool Define ()
{
- if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer)
- DefineDefaultConstructor (true);
+ CheckProtectedModifier ();
+
+ base.Define ();
if (default_static_constructor != null)
default_static_constructor.Define ();
- return base.Define ();
+ return true;
}
public override void Emit ()
{
+ if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer) {
+ DefineDefaultConstructor (true);
+ default_static_constructor.Define ();
+ }
+
base.Emit ();
if (declarative_security != null) {
continue;
}
- if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
- m.CheckProtectedModifier ();
- continue;
- }
-
if (m is Indexer) {
Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
continue;
base.DefineContainerMembers (list);
}
- public override TypeBuilder DefineType ()
+ public override bool Define ()
{
if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
- return null;
}
if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
- return null;
}
- return base.DefineType ();
+ return base.Define ();
}
protected override bool DoDefineMembers ()
this.ModFlags |= Modifiers.SEALED;
}
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ base.ApplyAttributeBuilder (a, cb);
+
+ //
+ // When struct constains fixed fixed and struct layout has explicitly
+ // set CharSet, its value has to be propagated to compiler generated
+ // fixed field types
+ //
+ if (a.Type == TypeManager.struct_layout_attribute_type && Fields != null && a.HasField ("CharSet")) {
+ for (int i = 0; i < Fields.Count; ++i) {
+ FixedField ff = Fields [i] as FixedField;
+ if (ff != null)
+ ff.SetCharSet (TypeBuilder.Attributes);
+ }
+ }
+ }
+
public override AttributeTargets AttributeTargets {
get {
return AttributeTargets.Struct;
}
}
- const TypeAttributes DefaultTypeAttributes =
- TypeAttributes.SequentialLayout |
- TypeAttributes.Sealed |
- TypeAttributes.BeforeFieldInit;
-
-
protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
{
TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
//
protected override TypeAttributes TypeAttr {
get {
+ const TypeAttributes DefaultTypeAttributes =
+ TypeAttributes.SequentialLayout |
+ TypeAttributes.Sealed |
+ TypeAttributes.BeforeFieldInit;
+
return base.TypeAttr | DefaultTypeAttributes;
}
}
}
}
- const TypeAttributes DefaultTypeAttributes =
- TypeAttributes.AutoLayout |
- TypeAttributes.Abstract |
- TypeAttributes.Interface;
-
protected override TypeAttributes TypeAttr {
get {
+ const TypeAttributes DefaultTypeAttributes =
+ TypeAttributes.AutoLayout |
+ TypeAttributes.Abstract |
+ TypeAttributes.Interface;
+
return base.TypeAttr | DefaultTypeAttributes;
}
}
}
if ((ModFlags & Modifiers.NEW) == 0) {
- if (this is Method && conflict_symbol is MethodBase)
+ if (this is MethodOrOperator && conflict_symbol.MemberType == MemberTypes.Method)
return true;
Report.SymbolRelatedToPreviousError (conflict_symbol);
Report.SymbolRelatedToPreviousError (t);
if (this is Indexer)
Report.Error (55, Location,
- "Inconsistent accessibility: parameter type `" +
- TypeManager.CSharpName (t) + "' is less " +
- "accessible than indexer `" + GetSignatureForError () + "'");
+ "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
+ TypeManager.CSharpName (t), GetSignatureForError ());
else if (this is Operator)
Report.Error (57, Location,
- "Inconsistent accessibility: parameter type `" +
- TypeManager.CSharpName (t) + "' is less " +
- "accessible than operator `" + GetSignatureForError () + "'");
+ "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
+ TypeManager.CSharpName (t), GetSignatureForError ());
else
Report.Error (51, Location,
"Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
if (!CheckBase ())
return false;
- if (block != null && block.IsIterator && !(Parent is IteratorStorey))
+ if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
+ //
+ // Current method is turned into automatically generated
+ // wrapper which creates an instance of iterator
+ //
Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
+ ModFlags |= Modifiers.DEBUGGER_HIDDEN;
+ }
if (IsPartialDefinition) {
caching_flags &= ~Flags.Excluded_Undetected;
if (!base.Define ())
return false;
- if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
+ if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
Error1599 (Location, ReturnType);
return false;
}
// The spec claims that static is not permitted, but
// my very own code has static constructors.
//
- public Constructor (DeclSpace parent, string name, int mod, Parameters args,
+ public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, Parameters args,
ConstructorInitializer init, Location loc)
: base (parent, null, null, mod, AllowedModifiers,
- new MemberName (name, loc), null, args)
+ new MemberName (name, loc), attrs, args)
{
Initializer = init;
}
if (Report.Errors > 0)
return;
- if (emit_field_initializers)
- Parent.PartialContainer.EmitFieldInitializers (ec);
-
ec.EmitResolvedTopBlock (block, unreachable);
}
protected bool IsTypePermitted ()
{
- if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
+ if (TypeManager.IsSpecialType (MemberType)) {
Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
return false;
}
if (TypeManager.IsGenericParameter (MemberType))
return true;
- if (MemberType == TypeManager.void_type) {
- // TODO: wrong location
- Expression.Error_VoidInvalidInTheContext (Location);
- return false;
- }
-
if (MemberType.IsSealed && MemberType.IsAbstract) {
Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
return false;
Expression size_expr, Attributes attrs, Location loc):
base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
{
- if (RootContext.Version == LanguageVersion.ISO_1)
+ if (RootContext.Version < LanguageVersion.ISO_2)
Report.FeatureIsNotAvailable (loc, "fixed size buffers");
this.size_expr = size_expr;
// Create nested fixed buffer container
string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
- fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
+ fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, CodeGen.Module.DefaultCharSetType |
TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
buffer_size *= type_size;
EmitFieldSize (buffer_size);
+
+#if GMCS_SOURCE
+ //
+ // Emit compiler generated fixed buffer type attribute
+ //
+ CustomAttributeBuilder cab = TypeManager.unsafe_value_type_attr;
+ if (cab == null) {
+ Type attr_type = TypeManager.CoreLookupType (
+ "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute", Kind.Class, true);
+
+ if (attr_type != null) {
+ ConstructorInfo ci = TypeManager.GetPredefinedConstructor (attr_type, Location);
+ if (ci != null) {
+ cab = new CustomAttributeBuilder (ci, new object [0]);
+ TypeManager.unsafe_value_type_attr = cab;
+ }
+ }
+ }
+
+ if (cab != null)
+ fixed_buffer_type.SetCustomAttribute (cab);
+#endif
base.Emit ();
}
}
}
+ public void SetCharSet (TypeAttributes ta)
+ {
+ TypeAttributes cta = fixed_buffer_type.Attributes;
+ if ((cta & TypeAttributes.UnicodeClass) != (ta & TypeAttributes.UnicodeClass))
+ SetTypeBuilderCharSet ((cta & ~TypeAttributes.AutoClass) | TypeAttributes.UnicodeClass);
+ else if ((cta & TypeAttributes.AutoClass) != (ta & TypeAttributes.AutoClass))
+ SetTypeBuilderCharSet ((cta & ~TypeAttributes.UnicodeClass) | TypeAttributes.AutoClass);
+ else if (cta == 0 && ta != 0)
+ SetTypeBuilderCharSet (cta & ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass));
+ }
+
+ void SetTypeBuilderCharSet (TypeAttributes ta)
+ {
+ MethodInfo mi = typeof (TypeBuilder).GetMethod ("SetCharSet", BindingFlags.Instance | BindingFlags.NonPublic);
+ if (mi == null) {
+ Report.RuntimeMissingSupport (Location, "TypeBuilder::SetCharSet");
+ } else {
+ mi.Invoke (fixed_buffer_type, new object [] { ta });
+ }
+ }
+
#region IFixedField Members
public FieldInfo Element {
if (initializer != null) {
((TypeContainer) Parent).RegisterFieldForInitialization (this,
- new FieldInitializer (FieldBuilder, initializer));
+ new FieldInitializer (FieldBuilder, initializer, this));
} else {
if (Parent.PartialContainer.Kind == Kind.Struct)
CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
}
((TypeContainer) Parent).RegisterFieldForInitialization (this,
- new FieldInitializer (FieldBuilder, Initializer));
+ new FieldInitializer (FieldBuilder, Initializer, this));
}
return true;
else if (OperatorType == OpType.Implicit)
Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
- if (MemberType == TypeManager.void_type) {
- Report.Error (590, Location, "User-defined operators cannot return void");
- return false;
- }
-
Type declaring_type = MethodData.DeclaringType;
Type return_type = MemberType;
Type first_arg_type = ParameterTypes [0];
public OpType GetMatchingOperator ()
{
switch (OperatorType) {
- case OpType.Equality:
- return OpType.Inequality;
- case OpType.Inequality:
- return OpType.Equality;
- case OpType.True:
- return OpType.False;
- case OpType.False:
- return OpType.True;
- case OpType.GreaterThan:
- return OpType.LessThan;
- case OpType.LessThan:
- return OpType.GreaterThan;
- case OpType.GreaterThanOrEqual:
- return OpType.LessThanOrEqual;
- case OpType.LessThanOrEqual:
- return OpType.GreaterThanOrEqual;
- default:
- return OpType.TOP;
+ case OpType.Equality:
+ return OpType.Inequality;
+ case OpType.Inequality:
+ return OpType.Equality;
+ case OpType.True:
+ return OpType.False;
+ case OpType.False:
+ return OpType.True;
+ case OpType.GreaterThan:
+ return OpType.LessThan;
+ case OpType.LessThan:
+ return OpType.GreaterThan;
+ case OpType.GreaterThanOrEqual:
+ return OpType.LessThanOrEqual;
+ case OpType.LessThanOrEqual:
+ return OpType.GreaterThanOrEqual;
+ default:
+ return OpType.TOP;
}
}