-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
SourceTypeSymbol.cs
627 lines (522 loc) · 27.5 KB
/
SourceTypeSymbol.cs
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
using Microsoft.CodeAnalysis;
using Pchp.CodeAnalysis.CodeGen;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using static Devsense.PHP.Syntax.Name;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeGen;
using Pchp.CodeAnalysis.Emit;
using Peachpie.CodeAnalysis.Utilities;
namespace Pchp.CodeAnalysis.Symbols
{
partial class SourceTypeSymbol
{
internal void SynthesizeInit(Emit.PEModuleBuilder module, DiagnosticBag diagnostics)
{
if (!IsInterface)
{
module.EmitBootstrap(this);
}
// .cctor
EmitFieldsCctor(module);
// __statics.Init
((SynthesizedStaticFieldsHolder)this.StaticsContainer)?.EmitCtors(module);
// IPhpCallable { Invoke, ToPhpValue }
EmitPhpCallable(module, diagnostics);
// IDisposable { Dispose }
EmitDisposable(module, diagnostics);
// .ctor
EmitPhpCtors(this.InstanceConstructors, module, diagnostics);
// System.ToString
EmitToString(module);
// trait methods
EmitTraitImplementations(module);
}
void EmitFieldsCctor(Emit.PEModuleBuilder module)
{
// list app static fields
var sflds = GetMembers().OfType<IPhpPropertySymbol>()
.Where(f => f.FieldKind == PhpPropertyKind.AppStaticField)
.ToList();
if (sflds.Count != 0)
{
// emit initialization of app static fields
// note, their initializers do not have Context available, since they are not bound to a Context
var cctor = module.GetStaticCtorBuilder(this);
lock (cctor)
{
using (var cg = new CodeGenerator(cctor, module, DiagnosticBag.GetInstance(), module.Compilation.Options.OptimizationLevel, false, this, null, thisPlace: null)
{
CallerType = this,
ContainingFile = this.ContainingFile,
})
{
foreach (var f in sflds)
{
Debug.Assert(f.RequiresContext == false);
Debug.Assert(f.ContainingStaticsHolder == null);
f.EmitInit(cg);
}
}
}
}
}
void EmitPhpCallable(Emit.PEModuleBuilder module, DiagnosticBag diagnostics)
{
var __invoke = TryGetMagicInvoke();
if (__invoke == null || (__invoke.OverriddenMethod != null && __invoke.OverriddenMethod.ContainingType.TypeKind != TypeKind.Interface))
{
return;
}
//
// IPhpCallable.Invoke(Context <ctx>, PhpVaue[] arguments)
//
var invoke = new SynthesizedMethodSymbol(this, "IPhpCallable.Invoke", false, true, DeclaringCompilation.CoreTypes.PhpValue, isfinal: false)
{
ExplicitOverride = (MethodSymbol)DeclaringCompilation.CoreTypes.IPhpCallable.Symbol.GetMembers("Invoke").Single(),
ForwardedCall = __invoke,
};
invoke.SetParameters(
new SpecialParameterSymbol(invoke, DeclaringCompilation.CoreTypes.Context, SpecialParameterSymbol.ContextName, 0),
new SynthesizedParameterSymbol(invoke, ArrayTypeSymbol.CreateSZArray(ContainingAssembly, DeclaringCompilation.CoreTypes.PhpValue.Symbol), 1, RefKind.None, name: "arguments", isParams: true));
module.SetMethodBody(invoke, MethodGenerator.GenerateMethodBody(module, invoke, il =>
{
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, new ParamPlace(invoke.Parameters[0]), new ArgPlace(this, 0))
{
CallerType = this,
};
cg.EmitRet(cg.EmitForwardCall(__invoke, invoke, callvirt: true));
}, null, diagnostics, false));
module.SynthesizedManager.AddMethod(this, invoke);
//
// IPhpCallable.ToPhpValue()
//
var tophpvalue = new SynthesizedMethodSymbol(this, "IPhpCallable.ToPhpValue", false, true, DeclaringCompilation.CoreTypes.PhpValue, isfinal: false)
{
ExplicitOverride = (MethodSymbol)DeclaringCompilation.CoreTypes.IPhpCallable.Symbol.GetMembers("ToPhpValue").Single(),
};
//
module.SetMethodBody(tophpvalue, MethodGenerator.GenerateMethodBody(module, tophpvalue, il =>
{
var thisPlace = new ArgPlace(this, 0);
var ctxPlace = new FieldPlace(thisPlace, this.ContextStore, module);
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, ctxPlace, thisPlace);
// return PhpValue.FromClass(this)
cg.EmitThis();
cg.EmitRet(cg.EmitCall(ILOpCode.Call, cg.CoreMethods.PhpValue.FromClass_Object));
}, null, diagnostics, false));
module.SynthesizedManager.AddMethod(this, tophpvalue);
}
void EmitDisposable(Emit.PEModuleBuilder module, DiagnosticBag diagnostics)
{
var __destruct = TryGetDestruct();
if (__destruct == null || (__destruct.OverriddenMethod != null && __destruct.OverriddenMethod.ContainingType.TypeKind != TypeKind.Interface))
{
// already implemented in a base class
return;
}
//
// IDisposable.Dispose()
//
var dispose = new SynthesizedMethodSymbol(this, "IDisposable.Dispose", false, true, DeclaringCompilation.GetSpecialType(SpecialType.System_Void), isfinal: true)
{
ExplicitOverride = (MethodSymbol)DeclaringCompilation.GetSpecialTypeMember(SpecialMember.System_IDisposable__Dispose),
ForwardedCall = __destruct,
};
module.SetMethodBody(dispose, MethodGenerator.GenerateMethodBody(module, dispose, il =>
{
var thisPlace = new ArgPlace(this, 0);
var ctxPlace = new FieldPlace(thisPlace, this.ContextStore, module);
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, ctxPlace, thisPlace)
{
CallerType = this,
};
// private bool <>b_disposed;
var disposedField = cg.Module.SynthesizedManager.GetOrCreateSynthesizedField(this, DeclaringCompilation.CoreTypes.Boolean, WellKnownPchpNames.SynthesizedDisposedFieldName, Accessibility.Private, false, false, false);
var disposedPlace = new FieldPlace(thisPlace, disposedField, cg.Module);
// if (<>b_disposed) return;
var lblContinue = new object();
disposedPlace.EmitLoad(cg.Builder);
cg.Builder.EmitBranch(ILOpCode.Brfalse, lblContinue);
cg.EmitRet(DeclaringCompilation.CoreTypes.Void);
cg.Builder.MarkLabel(lblContinue);
// <>b_disposed = true;
disposedPlace.EmitStorePrepare(cg.Builder);
cg.Builder.EmitBoolConstant(true);
disposedPlace.EmitStore(cg.Builder);
// __destruct()
cg.EmitPop(cg.EmitForwardCall(__destruct, dispose, callvirt: true));
// .ret
cg.EmitRet(DeclaringCompilation.GetSpecialType(SpecialType.System_Void));
}, null, diagnostics, false));
module.SynthesizedManager.AddMethod(this, dispose);
//
// Finalize()
//
var finalize = new SynthesizedFinalizeSymbol(this)
{
ForwardedCall = dispose,
};
Debug.Assert(finalize.OverriddenMethod != null);
module.SetMethodBody(finalize, MethodGenerator.GenerateMethodBody(module, finalize, il =>
{
var thisPlace = new ArgPlace(this, 0);
var ctxPlace = new FieldPlace(thisPlace, this.ContextStore, module);
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, ctxPlace, thisPlace)
{
CallerType = this,
};
//
cg.Builder.OpenLocalScope(ScopeType.TryCatchFinally);
// try {
cg.Builder.OpenLocalScope(ScopeType.Try);
// Dispose()
cg.EmitPop(cg.EmitForwardCall(dispose, finalize, callvirt: false));
//if (cg.EmitPdbSequencePoints) cg.Builder.EmitOpCode(ILOpCode.Nop);
// }
cg.Builder.CloseLocalScope();
// finally {
cg.Builder.OpenLocalScope(ScopeType.Finally);
// base.Finalize()
thisPlace.EmitLoad(cg.Builder);
cg.EmitCall(ILOpCode.Call, finalize.ExplicitOverride);
//if (cg.EmitPdbSequencePoints) cg.Builder.EmitOpCode(ILOpCode.Nop);
// }
cg.Builder.CloseLocalScope();
cg.Builder.CloseLocalScope();
// .ret
cg.EmitRet(DeclaringCompilation.GetSpecialType(SpecialType.System_Void));
}, null, diagnostics, false));
module.SynthesizedManager.AddMethod(this, finalize);
}
void EmitPhpCtors(ImmutableArray<MethodSymbol> instancectors, Emit.PEModuleBuilder module, DiagnosticBag diagnostics)
{
foreach (SynthesizedPhpCtorSymbol ctor in instancectors)
{
module.SetMethodBody(ctor, MethodGenerator.GenerateMethodBody(module, ctor, il =>
{
if (ctor is SynthesizedParameterlessPhpCtorSymbol)
{
EmitParameterlessCtor(ctor, il, module, diagnostics);
return;
}
Debug.Assert(SpecialParameterSymbol.IsContextParameter(ctor.Parameters[0]));
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, new ParamPlace(ctor.Parameters[0]), new ArgPlace(this, 0))
{
CallerType = this,
ContainingFile = ContainingFile,
};
Debug.Assert(ctor.BaseCtor != null);
// base..ctor or this..ctor
cg.EmitPop(cg.EmitForwardCall(ctor.BaseCtor, ctor));
if (ctor.PhpConstructor == null)
{
// initialize <ctx> field, if field is declared within this type
var ctxField = this.ContextStore;
if (ctxField != null && object.ReferenceEquals((object)ctxField.ContainingType, this))
{
var ctxFieldPlace = new FieldPlace(cg.ThisPlaceOpt, ctxField, module);
// Debug.Assert(<ctx> != null)
cg.EmitDebugAssertNotNull(cg.ContextPlaceOpt, "Context cannot be null.");
// <this>.<ctx> = <ctx>
ctxFieldPlace.EmitStorePrepare(il);
cg.EmitLoadContext();
ctxFieldPlace.EmitStore(il);
}
// trait specific:
if (ctor is SynthesizedPhpTraitCtorSymbol tctor)
{
EmitTraitCtorInit(cg, tctor);
}
// trait instances:
foreach (var t in this.TraitUses)
{
EmitTraitInstanceInit(cg, ctor, t);
}
// initialize instance fields:
foreach (var f in this.GetMembers().OfType<IPhpPropertySymbol>().Where(f => f.FieldKind == PhpPropertyKind.InstanceField))
{
Debug.Assert(f.ContainingStaticsHolder == null);
f.EmitInit(cg);
}
}
else
{
Debug.Assert(ctor.BaseCtor.ContainingType == this);
// this.__construct
cg.EmitPop(cg.EmitForwardCall(ctor.PhpConstructor, ctor));
}
// ret
Debug.Assert(ctor.ReturnsVoid);
cg.EmitRet(ctor.ReturnType);
}, null, diagnostics, false));
}
}
void EmitParameterlessCtor(SynthesizedPhpCtorSymbol ctor, ILBuilder il, PEModuleBuilder module, DiagnosticBag diagnostics)
{
Debug.Assert(ctor.BaseCtor != null);
Debug.Assert(ctor.PhpConstructor == null);
// Pchp.Core.Utilities.ContextExtensions.CurrentContext
var extensions = (NamedTypeSymbol)module.Compilation.GetTypeByMetadataName("Pchp.Core.Utilities.ContextExtensions");
if (extensions.IsErrorTypeOrNull()) throw new InvalidOperationException("Class Pchp.Core.Utilities.ContextExtensions cannot be resolved.");
var ctxfield = extensions.LookupMember<PropertySymbol>("CurrentContext");
if (ctxfield == null) throw new InvalidOperationException("Property ContextExtensions.DefaultContext cannot be resolved.");
Debug.Assert(ctxfield.GetMethod.IsStatic);
//
var cg = new CodeGenerator(il, module, diagnostics, module.Compilation.Options.OptimizationLevel, false, this, new PropertyPlace(null, ctxfield), new ArgPlace(this, 0))
{
CallerType = this,
ContainingFile = ContainingFile,
};
// this( ContextExtensions.CurrentContext )
cg.EmitPop(cg.EmitForwardCall(ctor.BaseCtor, ctor));
// ret
Debug.Assert(ctor.ReturnsVoid);
cg.EmitRet(ctor.ReturnType);
}
static void EmitTraitCtorInit(CodeGenerator cg, SynthesizedPhpTraitCtorSymbol tctor)
{
var il = cg.Builder;
// this.<>this = @this
var thisFieldPlace = new FieldPlace(cg.ThisPlaceOpt, tctor.ContainingType.RealThisField, module: cg.Module);
thisFieldPlace.EmitStorePrepare(il);
tctor.ThisParameter.EmitLoad(il);
thisFieldPlace.EmitStore(il);
}
void EmitTraitInstanceInit(CodeGenerator cg, SynthesizedPhpCtorSymbol ctor, TraitUse t)
{
// Template: this.<>trait_T = new T(ctx, this, self)
// PLACE: this.<>trait_T
var instancePlace = new FieldPlace(cg.ThisPlaceOpt, t.TraitInstanceField, module: cg.Module);
// .ctor(Context, object @this, RuntimeTypeHandle self)
var tctor = t.Symbol.InstanceConstructors[0];
Debug.Assert(tctor.ParameterCount == 2);
Debug.Assert(tctor.Parameters[0].Type == cg.CoreTypes.Context);
//Debug.Assert(tctor.Parameters[1].Type == TSelfParameter);
// using trait in trait?
var ctort = ctor as SynthesizedPhpTraitCtorSymbol;
//
instancePlace.EmitStorePrepare(cg.Builder);
// Context:
cg.EmitLoadContext();
// this:
if (ctort != null) ctort.ThisParameter.EmitLoad(cg.Builder); // this is passed from caller
else cg.EmitThis();
// new T<TSelf>(...)
cg.EmitCall(ILOpCode.Newobj, tctor);
instancePlace.EmitStore(cg.Builder);
}
void EmitToString(Emit.PEModuleBuilder module)
{
if (this.IsInterface || this.IsTrait)
{
return;
}
var __tostring = this.GetMembersByPhpName(SpecialMethodNames.Tostring.Value).OfType<MethodSymbol>().FirstOrDefault();
if (__tostring != null) // implement ToString if: there is __toString() function
{
// lookup base string ToString()
var overriden = this.LookupMember<MethodSymbol>(
WellKnownMemberNames.ObjectToString,
m => OverrideHelper.SignaturesMatch(m, (MethodSymbol)DeclaringCompilation.GetSpecialTypeMember(SpecialMember.System_Object__ToString)));
Debug.Assert(overriden != null);
if (overriden == null || overriden.IsSealed || overriden.ContainingType == this)
{
// cannot be overriden
return;
}
// public sealed override string ToString()
var tostring = new SynthesizedMethodSymbol(this, WellKnownMemberNames.ObjectToString, false, true, DeclaringCompilation.CoreTypes.String, Accessibility.Public, isfinal: false, phphidden: true)
{
ExplicitOverride = overriden,
ForwardedCall = __tostring,
};
module.SetMethodBody(tostring, MethodGenerator.GenerateMethodBody(module, tostring, il =>
{
var thisPlace = new ArgPlace(this, 0);
var cg = new CodeGenerator(il, module, DiagnosticBag.GetInstance(), module.Compilation.Options.OptimizationLevel, false, this, new FieldPlace(thisPlace, this.ContextStore, module), thisPlace);
if (__tostring != null)
{
// __tostring().ToString()
cg.EmitConvert(cg.EmitForwardCall(__tostring, tostring, callvirt: true), 0, tostring.ReturnType);
}
else
{
// PhpException.ObjectToStringNotSupported(this)
cg.EmitThis();
cg.EmitPop(cg.EmitCall(ILOpCode.Call, cg.CoreTypes.PhpException.Method("ObjectToStringNotSupported", cg.CoreTypes.Object)));
// return ""
cg.Builder.EmitStringConstant(string.Empty);
}
cg.EmitRet(tostring.ReturnType);
}, null, DiagnosticBag.GetInstance(), false));
module.SynthesizedManager.AddMethod(this, tostring);
}
}
void EmitTraitImplementations(Emit.PEModuleBuilder module)
{
foreach (var t in TraitUses)
{
foreach (var m in t.GetMembers().OfType<SynthesizedMethodSymbol>())
{
Debug.Assert(m.ForwardedCall != null);
module.SetMethodBody(m, MethodGenerator.GenerateMethodBody(module, m, il =>
{
IPlace thisPlace = null;
IPlace traitInstancePlace = null;
IPlace ctxPlace;
if (m.IsStatic)
{
// Template: return TRAIT.method(...)
Debug.Assert(SpecialParameterSymbol.IsContextParameter(m.Parameters[0]));
ctxPlace = new ParamPlace(m.Parameters[0]);
}
else
{
// Template: return this.<>trait.method(...)
thisPlace = new ArgPlace(this, 0); // this
ctxPlace = new FieldPlace(thisPlace, this.ContextStore, module); // this.<ctx>
traitInstancePlace = new FieldPlace(thisPlace, t.TraitInstanceField, module); // this.<>trait
}
using (var cg = new CodeGenerator(il, module, DiagnosticBag.GetInstance(), module.Compilation.Options.OptimizationLevel, false, this, ctxPlace, thisPlace)
{
CallerType = this,
})
{
var forwarded_type = cg.EmitForwardCall(m.ForwardedCall, m, thisPlaceExplicit: traitInstancePlace);
var target_type = m.ReturnType;
cg.EmitConvert(forwarded_type, 0, target_type); // always (forwarded_type === target_type)
cg.EmitRet(target_type);
}
}, null, DiagnosticBag.GetInstance(), false));
module.SynthesizedManager.AddMethod(this, m);
// ghost stubs: // TODO: resolve this already in SourceTypeSymbol.GetMembers(), now it does not get overloaded properly
var ps = m.Parameters;
for (int i = 0; i < ps.Length; i++)
{
if (ps[i] is IPhpValue p && p.Initializer != null && ps[i].ExplicitDefaultConstantValue == null) // => ConstantValue couldn't be resolved for optional parameter
{
// create ghost stub foo(p0, .. pi-1) => foo(p0, .. , pN)
GhostMethodBuilder.CreateGhostOverload(m, module, DiagnosticBag.GetInstance(), i);
}
}
}
}
}
/// <summary>
/// Collects methods that has to be overriden and matches with this declaration.
/// Missing overrides are reported, needed ghost stubs are synthesized.
/// </summary>
public void FinalizeMethodTable(Emit.PEModuleBuilder module, DiagnosticBag diagnostics)
{
// creates ghost stubs for overrides that do not match the signature
foreach (var info in this.ResolveOverrides(diagnostics))
{
// note: unresolved abstracts already reported by ResolveOverrides
// is ghost stub needed?
if (ReferenceEquals(info.OverrideCandidate?.ContainingType, this) || // candidate not matching exactly the signature in this type
info.ImplementsInterface) // explicitly implement the interface
{
if (info.HasOverride)
{
if (info.ImplementsInterface && info.Override != null && info.Override.IsVirtual)
{
// create explicit override only if the interface method is implemented with a class method that does not implement the interface
/*
* interface I { foo } // METHOD
* class X { foo } // OVERRIDE
* class Y : X, I { explicit I.foo override } // GHOST
*/
if (info.Override.ContainingType.ImplementsInterface(info.Method.ContainingType))
{
/* => X implements I */
// explicit method override is not needed
continue;
}
}
/*
* class A {
* TReturn1 foo(A, B);
* }
* class B : A {
* TReturn2 foo(A2, B2);
*
* // SYNTHESIZED GHOST:
* override TReturn foo(A, B){ return (TReturn)foo((A2)A, (B2)B);
* }
*/
// override method with a ghost that calls the override
(info.Override ?? info.OverrideCandidate).CreateGhostOverload(
this, module, diagnostics,
info.Method.ReturnType, info.Method.Parameters,
phphidden: true, explicitOverride: info.Method);
}
else
{
// synthesize abstract method implementing unresolved interface member
if (info.IsUnresolvedAbstract && info.ImplementsInterface && this.IsAbstract && !this.IsInterface)
{
var method = info.Method;
Debug.Assert(!method.IsStatic);
Debug.Assert(method.DeclaredAccessibility != Accessibility.Private);
Debug.Assert(method.ContainingType.IsInterface);
// Template: abstract function {name}({parameters})
var ghost = new SynthesizedMethodSymbol(this, method.RoutineName,
isstatic: false, isvirtual: true, isabstract: true, isfinal: false,
returnType: method.ReturnType,
accessibility: method.DeclaredAccessibility);
ghost.SetParameters(method.Parameters.Select(p => SynthesizedParameterSymbol.Create(ghost, p)).ToArray());
module.SynthesizedManager.AddMethod(this, ghost);
}
}
}
// setup synthesized methods explicit override as resolved
if (info.Override is SynthesizedMethodSymbol sm && sm.ExplicitOverride == null && sm.ContainingType == this)
{
sm.ExplicitOverride = info.Method;
}
}
// synthesized field accessors:
foreach (var srcf in GetMembers().OfType<SourceFieldSymbol>())
{
var paccessor = srcf.FieldAccessorProperty;
if (paccessor != null)
{
GenerateFieldAccessorProperty(module, diagnostics, srcf, paccessor);
}
}
}
void GenerateFieldAccessorProperty(Emit.PEModuleBuilder module, DiagnosticBag diagnostics, SourceFieldSymbol srcf, PropertySymbol paccessor)
{
//
module.SynthesizedManager.AddProperty(this, paccessor);
//
var get_body = MethodGenerator.GenerateMethodBody(module, paccessor.GetMethod, (il) =>
{
// Template: return field;
var place = new FieldPlace(new ArgPlace(this, 0), srcf.OverridenDefinition, module);
place.EmitLoad(il);
il.EmitRet(false);
}, null, diagnostics, false);
module.SetMethodBody(paccessor.GetMethod, get_body);
module.SynthesizedManager.AddMethod(this, paccessor.GetMethod);
//
var set_body = MethodGenerator.GenerateMethodBody(module, paccessor.SetMethod, (il) =>
{
// Template: field = value;
var place = new FieldPlace(new ArgPlace(this, 0), srcf.OverridenDefinition, module);
place.EmitStorePrepare(il);
new ArgPlace(this, 1).EmitLoad(il);
place.EmitStore(il);
il.EmitRet(true);
}, null, diagnostics, false);
module.SetMethodBody(paccessor.SetMethod, set_body);
module.SynthesizedManager.AddMethod(this, paccessor.SetMethod);
}
}
}