1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
|
//
// module.cs: keeps a tree representation of the generated code
//
// Authors: Miguel de Icaza (miguel@gnu.org)
// Marek Safar (marek.safar@gmail.com)
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
// Copyright 2003-2008 Novell, Inc.
// Copyright 2011 Xamarin Inc
//
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Mono.CompilerServices.SymbolWriter;
using System.Linq;
#if STATIC
using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
using System.Reflection;
using System.Reflection.Emit;
#endif
namespace Mono.CSharp
{
//
// Module (top-level type) container
//
public sealed class ModuleContainer : TypeContainer
{
#if STATIC
//
// Compiler generated container for static data
//
sealed class StaticDataContainer : CompilerGeneratedContainer
{
readonly Dictionary<int, Struct> size_types;
int fields;
public StaticDataContainer (ModuleContainer module)
: base (module, new MemberName ("<PrivateImplementationDetails>" + module.builder.ModuleVersionId.ToString ("B"), Location.Null),
Modifiers.STATIC | Modifiers.INTERNAL)
{
size_types = new Dictionary<int, Struct> ();
}
public override void CloseContainer ()
{
base.CloseContainer ();
foreach (var entry in size_types) {
entry.Value.CloseContainer ();
}
}
public FieldSpec DefineInitializedData (byte[] data, Location loc)
{
Struct size_type;
if (!size_types.TryGetValue (data.Length, out size_type)) {
//
// Build common type for this data length. We cannot use
// DefineInitializedData because it creates public type,
// and its name is not unique among modules
//
size_type = new Struct (this, new MemberName ("$ArrayType=" + data.Length, loc), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
size_type.CreateContainer ();
size_type.DefineContainer ();
size_types.Add (data.Length, size_type);
// It has to work even if StructLayoutAttribute does not exist
size_type.TypeBuilder.__SetLayout (1, data.Length);
}
var name = "$field-" + fields.ToString ("X");
++fields;
const Modifiers fmod = Modifiers.STATIC | Modifiers.INTERNAL;
var fbuilder = TypeBuilder.DefineField (name, size_type.CurrentType.GetMetaInfo (), ModifiersExtensions.FieldAttr (fmod) | FieldAttributes.HasFieldRVA);
fbuilder.__SetDataAndRVA (data);
return new FieldSpec (CurrentType, null, size_type.CurrentType, fbuilder, fmod);
}
}
StaticDataContainer static_data;
//
// Makes const data field inside internal type container
//
public FieldSpec MakeStaticData (byte[] data, Location loc)
{
if (static_data == null) {
static_data = new StaticDataContainer (this);
static_data.CreateContainer ();
static_data.DefineContainer ();
AddCompilerGeneratedClass (static_data);
}
return static_data.DefineInitializedData (data, loc);
}
#endif
public CharSet? DefaultCharSet;
public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
readonly Dictionary<int, List<AnonymousTypeClass>> anonymous_types;
readonly Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> array_types;
readonly Dictionary<TypeSpec, PointerContainer> pointer_types;
readonly Dictionary<TypeSpec, ReferenceContainer> reference_types;
readonly Dictionary<TypeSpec, MethodSpec> attrs_cache;
readonly Dictionary<TypeSpec, AwaiterDefinition> awaiters;
AssemblyDefinition assembly;
readonly CompilerContext context;
readonly RootNamespace global_ns;
readonly Dictionary<string, RootNamespace> alias_ns;
ModuleBuilder builder;
bool has_extenstion_method;
PredefinedAttributes predefined_attributes;
PredefinedTypes predefined_types;
PredefinedMembers predefined_members;
public Binary.PredefinedOperator[] OperatorsBinaryEqualityLifted;
public Binary.PredefinedOperator[] OperatorsBinaryLifted;
static readonly string[] attribute_targets = new string[] { "assembly", "module" };
public ModuleContainer (CompilerContext context)
: base (null, MemberName.Null, null, 0)
{
this.context = context;
caching_flags &= ~(Flags.Obsolete_Undetected | Flags.Excluded_Undetected);
containers = new List<TypeContainer> ();
anonymous_types = new Dictionary<int, List<AnonymousTypeClass>> ();
global_ns = new GlobalRootNamespace ();
alias_ns = new Dictionary<string, RootNamespace> ();
array_types = new Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ();
pointer_types = new Dictionary<TypeSpec, PointerContainer> ();
reference_types = new Dictionary<TypeSpec, ReferenceContainer> ();
attrs_cache = new Dictionary<TypeSpec, MethodSpec> ();
awaiters = new Dictionary<TypeSpec, AwaiterDefinition> ();
}
#region Properties
internal Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ArrayTypesCache {
get {
return array_types;
}
}
//
// Cache for parameter-less attributes
//
internal Dictionary<TypeSpec, MethodSpec> AttributeConstructorCache {
get {
return attrs_cache;
}
}
public override AttributeTargets AttributeTargets {
get {
return AttributeTargets.Assembly;
}
}
public ModuleBuilder Builder {
get {
return builder;
}
}
public override CompilerContext Compiler {
get {
return context;
}
}
public int CounterAnonymousTypes { get; set; }
public int CounterAnonymousMethods { get; set; }
public int CounterAnonymousContainers { get; set; }
public int CounterSwitchTypes { get; set; }
public AssemblyDefinition DeclaringAssembly {
get {
return assembly;
}
}
internal DocumentationBuilder DocumentationBuilder {
get; set;
}
public override string DocCommentHeader {
get {
throw new NotSupportedException ();
}
}
public Evaluator Evaluator {
get; set;
}
public bool HasDefaultCharSet {
get {
return DefaultCharSet.HasValue;
}
}
public bool HasExtensionMethod {
get {
return has_extenstion_method;
}
set {
has_extenstion_method = value;
}
}
public bool HasTypesFullyDefined {
get; set;
}
//
// Returns module global:: namespace
//
public RootNamespace GlobalRootNamespace {
get {
return global_ns;
}
}
public override ModuleContainer Module {
get {
return this;
}
}
internal Dictionary<TypeSpec, PointerContainer> PointerTypesCache {
get {
return pointer_types;
}
}
internal PredefinedAttributes PredefinedAttributes {
get {
return predefined_attributes;
}
}
internal PredefinedMembers PredefinedMembers {
get {
return predefined_members;
}
}
internal PredefinedTypes PredefinedTypes {
get {
return predefined_types;
}
}
internal Dictionary<TypeSpec, ReferenceContainer> ReferenceTypesCache {
get {
return reference_types;
}
}
public override string[] ValidAttributeTargets {
get {
return attribute_targets;
}
}
#endregion
public override void Accept (StructuralVisitor visitor)
{
visitor.Visit (this);
}
public void AddAnonymousType (AnonymousTypeClass type)
{
List<AnonymousTypeClass> existing;
if (!anonymous_types.TryGetValue (type.Parameters.Count, out existing))
if (existing == null) {
existing = new List<AnonymousTypeClass> ();
anonymous_types.Add (type.Parameters.Count, existing);
}
existing.Add (type);
}
public void AddAttribute (Attribute attr, IMemberContext context)
{
attr.AttachTo (this, context);
if (attributes == null) {
attributes = new Attributes (attr);
return;
}
attributes.AddAttribute (attr);
}
public override void AddTypeContainer (TypeContainer tc)
{
containers.Add (tc);
}
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
if (a.Target == AttributeTargets.Assembly) {
assembly.ApplyAttributeBuilder (a, ctor, cdata, pa);
return;
}
if (a.Type == pa.DefaultCharset) {
switch (a.GetCharSetValue ()) {
case CharSet.Ansi:
case CharSet.None:
break;
case CharSet.Auto:
DefaultCharSet = CharSet.Auto;
DefaultCharSetType = TypeAttributes.AutoClass;
break;
case CharSet.Unicode:
DefaultCharSet = CharSet.Unicode;
DefaultCharSetType = TypeAttributes.UnicodeClass;
break;
default:
Report.Error (1724, a.Location, "Value specified for the argument to `{0}' is not valid",
a.GetSignatureForError ());
break;
}
} else if (a.Type == pa.CLSCompliant) {
Attribute cls = DeclaringAssembly.CLSCompliantAttribute;
if (cls == null) {
Report.Warning (3012, 1, a.Location,
"You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
} else if (DeclaringAssembly.IsCLSCompliant != a.GetBoolean ()) {
Report.SymbolRelatedToPreviousError (cls.Location, cls.GetSignatureForError ());
Report.Warning (3017, 1, a.Location,
"You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
return;
}
}
builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
}
public override void CloseContainer ()
{
if (anonymous_types != null) {
foreach (var atypes in anonymous_types)
foreach (var at in atypes.Value)
at.CloseContainer ();
}
base.CloseContainer ();
}
public TypeBuilder CreateBuilder (string name, TypeAttributes attr, int typeSize)
{
return builder.DefineType (name, attr, null, typeSize);
}
//
// Creates alias global namespace
//
public RootNamespace CreateRootNamespace (string alias)
{
if (alias == global_ns.Alias) {
RootNamespace.Error_GlobalNamespaceRedefined (Report, Location.Null);
return global_ns;
}
RootNamespace rn;
if (!alias_ns.TryGetValue (alias, out rn)) {
rn = new RootNamespace (alias);
alias_ns.Add (alias, rn);
}
return rn;
}
public void Create (AssemblyDefinition assembly, ModuleBuilder moduleBuilder)
{
this.assembly = assembly;
builder = moduleBuilder;
}
public override bool Define ()
{
DefineContainer ();
ExpandBaseInterfaces ();
base.Define ();
HasTypesFullyDefined = true;
return true;
}
public override bool DefineContainer ()
{
DefineNamespace ();
return base.DefineContainer ();
}
public void EnableRedefinition ()
{
is_defined = false;
}
public override void EmitContainer ()
{
if (OptAttributes != null)
OptAttributes.Emit ();
if (Compiler.Settings.Unsafe && !assembly.IsSatelliteAssembly) {
var pa = PredefinedAttributes.UnverifiableCode;
if (pa.IsDefined)
pa.EmitAttribute (builder);
}
foreach (var tc in containers) {
tc.PrepareEmit ();
}
base.EmitContainer ();
if (Compiler.Report.Errors == 0 && !Compiler.Settings.WriteMetadataOnly)
VerifyMembers ();
if (anonymous_types != null) {
foreach (var atypes in anonymous_types)
foreach (var at in atypes.Value)
at.EmitContainer ();
}
}
internal override void GenerateDocComment (DocumentationBuilder builder)
{
foreach (var tc in containers)
tc.GenerateDocComment (builder);
}
public AnonymousTypeClass GetAnonymousType (IList<AnonymousTypeParameter> parameters)
{
List<AnonymousTypeClass> candidates;
if (!anonymous_types.TryGetValue (parameters.Count, out candidates))
return null;
int i;
foreach (AnonymousTypeClass at in candidates) {
for (i = 0; i < parameters.Count; ++i) {
if (!parameters [i].Equals (at.Parameters [i]))
break;
}
if (i == parameters.Count)
return at;
}
return null;
}
//
// Return container with awaiter definition. It never returns null
// but all container member can be null for easier error reporting
//
public AwaiterDefinition GetAwaiter (TypeSpec type)
{
AwaiterDefinition awaiter;
if (awaiters.TryGetValue (type, out awaiter))
return awaiter;
awaiter = new AwaiterDefinition ();
//
// Predefined: bool IsCompleted { get; }
//
awaiter.IsCompleted = MemberCache.FindMember (type, MemberFilter.Property ("IsCompleted", Compiler.BuiltinTypes.Bool),
BindingRestriction.InstanceOnly) as PropertySpec;
//
// Predefined: GetResult ()
//
// The method return type is also result type of await expression
//
awaiter.GetResult = MemberCache.FindMember (type, MemberFilter.Method ("GetResult", 0,
ParametersCompiled.EmptyReadOnlyParameters, null),
BindingRestriction.InstanceOnly) as MethodSpec;
//
// Predefined: INotifyCompletion.OnCompleted (System.Action)
//
var nc = PredefinedTypes.INotifyCompletion;
awaiter.INotifyCompletion = !nc.Define () || type.ImplementsInterface (nc.TypeSpec, false);
awaiters.Add (type, awaiter);
return awaiter;
}
public override void GetCompletionStartingWith (string prefix, List<string> results)
{
var names = Evaluator.GetVarNames ();
results.AddRange (names.Where (l => l.StartsWith (prefix)));
}
public RootNamespace GetRootNamespace (string name)
{
RootNamespace rn;
alias_ns.TryGetValue (name, out rn);
return rn;
}
public override string GetSignatureForError ()
{
return "<module>";
}
public Binary.PredefinedOperator[] GetPredefinedEnumAritmeticOperators (TypeSpec enumType, bool nullable)
{
TypeSpec underlying;
Binary.Operator mask = 0;
if (nullable) {
underlying = Nullable.NullableInfo.GetEnumUnderlyingType (this, enumType);
mask = Binary.Operator.NullableMask;
} else {
underlying = EnumSpec.GetUnderlyingType (enumType);
}
var operators = new[] {
new Binary.PredefinedOperator (enumType, underlying,
mask | Binary.Operator.AdditionMask | Binary.Operator.SubtractionMask | Binary.Operator.DecomposedMask, enumType),
new Binary.PredefinedOperator (underlying, enumType,
mask | Binary.Operator.AdditionMask | Binary.Operator.SubtractionMask | Binary.Operator.DecomposedMask, enumType),
new Binary.PredefinedOperator (enumType, mask | Binary.Operator.SubtractionMask, underlying)
};
return operators;
}
public void InitializePredefinedTypes ()
{
predefined_attributes = new PredefinedAttributes (this);
predefined_types = new PredefinedTypes (this);
predefined_members = new PredefinedMembers (this);
OperatorsBinaryEqualityLifted = Binary.CreateEqualityLiftedOperatorsTable (this);
OperatorsBinaryLifted = Binary.CreateStandardLiftedOperatorsTable (this);
}
public override bool IsClsComplianceRequired ()
{
return DeclaringAssembly.IsCLSCompliant;
}
public Attribute ResolveAssemblyAttribute (PredefinedAttribute a_type)
{
Attribute a = OptAttributes.Search ("assembly", a_type);
if (a != null) {
a.Resolve ();
}
return a;
}
public void SetDeclaringAssembly (AssemblyDefinition assembly)
{
// TODO: This setter is quite ugly but I have not found a way around it yet
this.assembly = assembly;
}
}
}
|