This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
RuleBuilder.cs
594 lines (464 loc) · 17.6 KB
/
RuleBuilder.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
using System;
using System.Reflection;
using System.Collections.Generic;
using System.CodeDom;
using Mono.Build;
using Mono.Build.Bundling;
namespace MBBundleGen {
public class RuleBuilder : TypeExpressedItem {
// note that we do not include argument names / types in the
// fingerprint hash. If the code behind the rule doesn't change, the results
// derived from the rule shouldn't be rebuilt, even if the arguments
// names, types, numbers, etc do vary. At least, it seems like
// that should be the case to me.
//
// Like TemplateBuilder, we are only optionally bound to a structure.
FingerprintHelper fp = new FingerprintHelper ();
public RuleBuilder (string name, NamespaceBuilder ns, CodeLinePragma loc, TypeAttributes attr) :
base (name, ns, loc, attr)
{
BaseClass = RuleClass;
fp.Add (name);
}
// Arguments
readonly static UserType RuleClass = new UserType (typeof (Rule));
static CodeVariableDeclarationStatement BaseNumArgs;
static RuleBuilder ()
{
// @{int base_total = base.NumArguments;}
BaseNumArgs = new CodeVariableDeclarationStatement ();
BaseNumArgs.Name = "base_total";
BaseNumArgs.Type = new CodeTypeReference (typeof (int));
BaseNumArgs.InitExpression =
new CodePropertyReferenceExpression (CDH.Base, "NumArguments");
}
static CodeExpression BasePlusN (int n)
{
CodeBinaryOperatorExpression idexpr = new CodeBinaryOperatorExpression ();
idexpr.Left = new CodeVariableReferenceExpression ("base_total");
idexpr.Operator = CodeBinaryOperatorType.Add;
idexpr.Right = new CodePrimitiveExpression (n);
return idexpr;
}
public class ArgInfo {
public readonly int Idx;
public readonly UserType Type;
public readonly ArgCount Count;
public readonly string Name;
public readonly CodeExpression Flags;
readonly static CodeTypeReference TRArgFlags =
new CodeTypeReference (typeof (ArgFlags));
readonly static CodeTypeReferenceExpression TREArgFlags =
new CodeTypeReferenceExpression (TRArgFlags);
readonly static CodeFieldReferenceExpression Optional =
new CodeFieldReferenceExpression (TREArgFlags, "Optional");
readonly static CodeFieldReferenceExpression Multi =
new CodeFieldReferenceExpression (TREArgFlags, "Multi");
readonly static CodeFieldReferenceExpression Default =
new CodeFieldReferenceExpression (TREArgFlags, "Default");
readonly static CodeFieldReferenceExpression Standard =
new CodeFieldReferenceExpression (TREArgFlags, "Standard");
readonly static CodeFieldReferenceExpression Ordered =
new CodeFieldReferenceExpression (TREArgFlags, "Ordered");
readonly static CodeFieldReferenceExpression DefOrd =
new CodeFieldReferenceExpression (TREArgFlags, "DefaultOrdered");
readonly static CodeBinaryOperatorType Or = CodeBinaryOperatorType.BitwiseOr;
public ArgInfo (int idx, UserType type, ArgCount count, string name,
bool is_default, bool is_ordered)
{
Idx = idx;
Type = type;
Count = count;
Name = name;
switch (count) {
case ArgCount.ZeroOrMore:
Flags = new CodeBinaryOperatorExpression (Optional, Or, Multi);
break;
case ArgCount.OneOrMore:
Flags = Multi;
break;
case ArgCount.Optional:
Flags = Optional;
break;
case ArgCount.Standard:
Flags = Standard;
break;
default:
throw new Exception ("Unsupported ArgCount kind " + Count.ToString ());
}
if (is_ordered) {
Flags = new CodeBinaryOperatorExpression (Flags, Or, Ordered);
if (is_default)
Flags = new CodeBinaryOperatorExpression (Flags, Or, DefOrd);
} else if (is_default) {
Flags = new CodeBinaryOperatorExpression (Flags, Or, Default);
}
}
RuleArgConversion conv;
UserType arg_class;
public bool Resolve (TypeResolveContext trc, bool errors)
{
if (Type.Resolve (trc, errors))
return true;
arg_class = Type.ResultConversion;
if (arg_class == null) {
conv = RuleArgConversion.None;
arg_class = Type;
} else {
// The arg type is some primitive type (bool, string)
// that we must convert from a result wrapper
Type type = Type.AsSystem;
if (type == null)
throw new Exception ("Can't handle convertible user types");
if (type.IsValueType)
conv = RuleArgConversion.ToValueType;
else
conv = RuleArgConversion.ToRefType;
}
return arg_class.Resolve (trc, errors);
}
public void EmitField (CodeTypeDeclaration type)
{
// Field definition expression
CodeTypeReference fieldtype = null;
switch (Count) {
case ArgCount.OneOrMore:
goto case ArgCount.ZeroOrMore;
case ArgCount.ZeroOrMore:
fieldtype = new CodeTypeReference (Type.AsCodeDom, 1);
break;
case ArgCount.Optional:
if (conv != RuleArgConversion.ToValueType)
goto case ArgCount.Standard;
fieldtype = new CodeTypeReference ("System.Nullable`1");
fieldtype.TypeArguments.Add (Type.AsCodeDom);
break;
case ArgCount.Standard:
fieldtype = Type.AsCodeDom;
break;
}
CodeMemberField fieldmem = new CodeMemberField (fieldtype, Name);
fieldmem.Attributes = MemberAttributes.Family;
type.Members.Add (fieldmem);
}
readonly static CodeTypeReferenceExpression TRERule =
new CodeTypeReferenceExpression (CDH.Rule);
public void EmitStatements (CodeMemberMethod list, CodeVariableReferenceExpression sink,
CodeMemberMethod fetch, CodeVariableReferenceExpression source,
CodeMemberMethod clear)
{
CodeExpression idexpr = RuleBuilder.BasePlusN (Idx);
// List
CodeMethodInvokeExpression invoke = CDH.AddBareInvoke (list, sink, "AddArg");
invoke.Parameters.Add (idexpr);
invoke.Parameters.Add (new CodePrimitiveExpression (Name));
invoke.Parameters.Add (new CodeTypeOfExpression (arg_class.AsCodeDom));
invoke.Parameters.Add (Flags);
// Fetch
invoke = new CodeMethodInvokeExpression ();
invoke.Method = new CodeMethodReferenceExpression (source, "GetArgValue");
invoke.Parameters.Add (idexpr);
CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression ();
mref.TargetObject = TRERule;
if (conv == RuleArgConversion.None) {
switch (Count) {
case ArgCount.OneOrMore:
goto case ArgCount.ZeroOrMore;
case ArgCount.ZeroOrMore:
mref.MethodName = "AsArray";
mref.TypeArguments.Add (Type.AsCodeDom);
break;
case ArgCount.Optional:
mref.MethodName = "AsOptional";
mref.TypeArguments.Add (Type.AsCodeDom);
break;
case ArgCount.Standard:
mref.MethodName = "AsSingle";
mref.TypeArguments.Add (Type.AsCodeDom);
break;
}
} else {
mref.TypeArguments.Add (Type.AsCodeDom);
mref.TypeArguments.Add (arg_class.AsCodeDom);
switch (Count) {
case ArgCount.OneOrMore:
goto case ArgCount.ZeroOrMore;
case ArgCount.ZeroOrMore:
mref.MethodName = "AsArrayConv";
break;
case ArgCount.Optional:
if (conv == RuleArgConversion.ToValueType)
mref.MethodName = "AsOptionalValue";
else
mref.MethodName = "AsOptionalRef";
break;
case ArgCount.Standard:
mref.MethodName = "AsSingleConv";
break;
}
}
CodeMethodInvokeExpression inv2 = new CodeMethodInvokeExpression ();
inv2.Method = mref;
inv2.Parameters.Add (invoke);
CodeAssignStatement assg = new CodeAssignStatement (CDH.ThisDot (Name), inv2);
fetch.Statements.Add (assg);
// ClearArgValues. Can't clear standard value types -- eg, 'bool member;'.
// Those we trust will get set again when FetchArgValues is called next time.
if (conv != RuleArgConversion.ToValueType || Count != ArgCount.Standard) {
assg = new CodeAssignStatement (CDH.ThisDot (Name), CDH.Null);
clear.Statements.Add (assg);
}
}
}
List<ArgInfo> arguments = new List<ArgInfo> ();
public void AddArgument (UserType type, ArgCount count, string name, bool is_default,
bool is_ordered)
{
arguments.Add (new ArgInfo (arguments.Count, type, count, name,
is_default, is_ordered));
}
string target_arg_name = null;
bool target_arg_reqd = false;
public void AddTargetArgument (ArgCount count, string name, bool is_default,
bool is_ordered)
{
if (count == ArgCount.Standard)
target_arg_reqd = true;
else if (count == ArgCount.Optional)
target_arg_reqd = false;
else
throw new Exception ("A .target argument cannot be multiple-valued");
if (is_ordered)
throw new Exception ("A .target argument cannot be ordered");
target_arg_name = name;
}
void EmitTargetArg (CodeMemberMethod list, CodeVariableReferenceExpression sink,
CodeMemberMethod fetch, CodeVariableReferenceExpression source,
CodeMemberMethod clear, CodeTypeDeclaration cur_type)
{
// Field definition expression
CodeMemberField fieldmem = new CodeMemberField (CDH.String, target_arg_name);
fieldmem.Attributes = MemberAttributes.Family;
cur_type.Members.Add (fieldmem);
CodeFieldReferenceExpression fieldref = CDH.ThisDot (target_arg_name);
// ListArguments
CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression ();
invoke.Method = new CodeMethodReferenceExpression (sink, "WantTargetName");
invoke.Parameters.Add (new CodePrimitiveExpression (target_arg_reqd));
list.Statements.Add (invoke);
// FetchArgValues
invoke = new CodeMethodInvokeExpression ();
invoke.Method = new CodeMethodReferenceExpression (source, "GetTargetName");
CodeAssignStatement assg = new CodeAssignStatement (fieldref, invoke);
fetch.Statements.Add (assg);
// ClearArgValues
assg = new CodeAssignStatement (fieldref, CDH.Null);
clear.Statements.Add (assg);
}
CodeTypeReference ArgInfoSink = new CodeTypeReference (typeof (IArgInfoSink));
CodeTypeReference ArgValueSource = new CodeTypeReference (typeof (IArgValueSource));
void EmitArguments (CodeTypeDeclaration cur_type)
{
CodeMemberMethod list, fetch, clear;
CodeVariableReferenceExpression sink, source;
// ListArguments prologue
list = new CodeMemberMethod ();
list.Name = "ListArguments";
list.Attributes = MemberAttributes.Public | MemberAttributes.Override;
list.ReturnType = null;
list.Parameters.Add (CDH.Param (ArgInfoSink, "sink"));
CDH.EmitBaseChainVoid (list);
sink = CDH.VRef ("sink");
// FetchArgValues prologue
fetch = new CodeMemberMethod ();
fetch.Name = "FetchArgValues";
fetch.Attributes = MemberAttributes.Public | MemberAttributes.Override;
fetch.ReturnType = null;
fetch.Parameters.Add (CDH.Param (ArgValueSource, "source"));
CDH.EmitBaseChainVoid (fetch);
source = new CodeVariableReferenceExpression ("source");
// ClearArgValues prologue
clear = new CodeMemberMethod ();
clear.Name = "ClearArgValues";
clear.Attributes = MemberAttributes.Public | MemberAttributes.Override;
clear.ReturnType = null;
CDH.EmitBaseChainVoid (clear);
// Per-arg statements
if (target_arg_name != null)
EmitTargetArg (list, sink, fetch, source, clear, cur_type);
if (arguments.Count > 0) {
list.Statements.Add (BaseNumArgs);
fetch.Statements.Add (BaseNumArgs);
}
for (int i = 0; i < arguments.Count; i++) {
arguments[i].EmitStatements (list, sink, fetch, source, clear);
arguments[i].EmitField (cur_type);
}
// No epilogues.
cur_type.Members.Add (list);
cur_type.Members.Add (fetch);
cur_type.Members.Add (clear);
// NumArguments property override
CodeMemberProperty p = new CodeMemberProperty ();
p.Name = "NumArguments";
p.HasGet = true;
p.HasSet = false;
p.Type = new CodeTypeReference (typeof (int));
p.Attributes = MemberAttributes.Public | MemberAttributes.Override;
CodeBinaryOperatorExpression add = new CodeBinaryOperatorExpression ();
add.Left = new CodePropertyReferenceExpression (CDH.Base, "NumArguments");
add.Operator = CodeBinaryOperatorType.Add;
add.Right = new CodePrimitiveExpression (arguments.Count);
p.GetStatements.Add (new CodeMethodReturnStatement (add));
cur_type.Members.Add (p);
}
// The build function
bool bf_manual;
UserType bf_ret_class;
string bf_ret_arg;
string bf_context_arg;
NativeCode bf_code;
public void SetBuildFunc (bool is_manual, UserType ret_class, string ret_arg,
string context_arg, NativeCode code)
{
bf_manual = is_manual;
bf_ret_class = ret_class;
bf_ret_arg = ret_arg;
bf_context_arg = context_arg;
bf_code = code;
fp.Add (code);
}
void EmitBuildFunc (CodeTypeDeclaration cur_type)
{
// FIXME: allow retclass to be 'bool', 'string', etc and do the
// appropriate conversions.
CodeParameterDeclarationExpression p = CDH.Param (CDH.IContext, bf_context_arg);
CodeMemberMethod meth = new CodeMemberMethod ();
meth.Name = "Build";
meth.Attributes = MemberAttributes.Public | MemberAttributes.Override;
meth.ReturnType = CDH.Result;
meth.Parameters.Add (p);
if (bf_manual)
EmitBuildManual (meth);
else
EmitBuildWrapped (cur_type, meth);
cur_type.Members.Add (meth);
// We know this. In Resolve() we check that OverrideResultType is
// not set if we have a build func (which would lead to
// emitting the 'general result type' code twice)
EmitGeneralResult (cur_type, bf_ret_class);
}
void EmitBuildManual (CodeMemberMethod meth)
{
meth.Statements.Add (bf_code.AsStatement);
}
void EmitBuildWrapped (CodeTypeDeclaration cur_type, CodeMemberMethod meth)
{
CodeMethodInvokeExpression inv =
new CodeMethodInvokeExpression (CDH.This, "CreateResultObject",
new CodeExpression[0] {});
CodeCastExpression cast =
new CodeCastExpression (bf_ret_class.AsCodeDom, inv);
CodeVariableDeclarationStatement vd =
new CodeVariableDeclarationStatement (bf_ret_class.AsCodeDom, bf_ret_arg, cast);
meth.Statements.Add (vd);
CodeVariableReferenceExpression r1 = CDH.VRef (bf_ret_arg);
CodeVariableReferenceExpression r2 = CDH.VRef (bf_context_arg);
inv = new CodeMethodInvokeExpression (CDH.This, "BuildImpl",
new CodeExpression[2] { r1, r2 });
meth.Statements.Add (CDH.IfTrueReturnNull (inv));
meth.Statements.Add (new CodeMethodReturnStatement (r1));
// BuildImpl
CodeParameterDeclarationExpression p1 = CDH.Param (bf_ret_class.AsCodeDom, bf_ret_arg);
CodeParameterDeclarationExpression p2 = CDH.Param (CDH.IContext, bf_context_arg);
meth = new CodeMemberMethod ();
meth.Name = "BuildImpl";
meth.Attributes = MemberAttributes.Private;
meth.ReturnType = CDH.Bool;
meth.Parameters.Add (p1);
meth.Parameters.Add (p2);
meth.Statements.Add (bf_code.AsStatement);
cur_type.Members.Add (meth);
}
// Result type management. Static for SourcefileRuleBuilder.
public NativeCode SpecificResultCode = null;
public void EmitSpecificResult (CodeTypeDeclaration ctd)
{
if (SpecificResultCode != null)
EmitSpecificResult (ctd, SpecificResultCode);
}
public static void EmitSpecificResult (CodeTypeDeclaration ctd, NativeCode code)
{
// SpecificResultType property
CodeMemberProperty srt = new CodeMemberProperty ();
srt.Name = "SpecificResultType";
srt.Attributes = MemberAttributes.Public | MemberAttributes.Override;
srt.Type = CDH.Type;
srt.HasGet = true;
srt.HasSet = false;
srt.GetStatements.Add (code.AsStatement);
ctd.Members.Add (srt);
}
public UserType OverrideResultType;
protected void EmitGeneralResult (CodeTypeDeclaration ctd)
{
if (OverrideResultType != null)
EmitGeneralResult (ctd, OverrideResultType);
}
public static void EmitGeneralResult (CodeTypeDeclaration ctd, UserType rtype)
{
CodeMemberProperty grt = new CodeMemberProperty ();
grt.Name = "GeneralResultType";
grt.Attributes = MemberAttributes.Public | MemberAttributes.Override;
grt.Type = CDH.Type;
grt.HasGet = true;
grt.HasSet = false;
CodeExpression rt = new CodeTypeOfExpression (rtype.AsCodeDom);
grt.GetStatements.Add (new CodeMethodReturnStatement (rt));
ctd.Members.Add (grt);
}
// Misc
public override void AddNativeMember (NativeCode nc)
{
base.AddNativeMember (nc);
fp.Add (nc);
}
// General
public override bool Resolve (TypeResolveContext trc, bool errors)
{
bool ret = base.Resolve (trc, errors);
if (bf_ret_class != null) {
// Null if rule doesn't have a build func of its
// own (eg, it inherits)
ret |= bf_ret_class.Resolve (trc, errors);
if (errors && OverrideResultType != null) {
Console.Error.WriteLine ("Can't have a build function and separately " +
"specify a general result type in {0}", ClassName);
return true;
}
}
if (OverrideResultType != null)
ret |= OverrideResultType.Resolve (trc, errors);
for (int i = 0; i < arguments.Count; i++)
ret |= arguments[i].Resolve (trc, errors);
return ret;
}
protected override void Emit (CodeTypeDeclaration ctd)
{
CodeConstructor ctor = EmitEmptyCtor (ctd);
if (SpecificResultCode != null) {
CodeAssignStatement assg = new CodeAssignStatement ();
assg.Left = new CodeFieldReferenceExpression (CDH.This, "specific_varies");
assg.Right = CDH.True;
ctor.Statements.Add (assg);
}
if (bf_ret_class != null)
// Some classes override parameters but not build functions
EmitBuildFunc (ctd);
EmitGeneralResult (ctd);
EmitSpecificResult (ctd);
EmitArguments (ctd);
fp.EmitGetFingerprint (ctd);
}
}
}