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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
/*
* cs_misc.tc - Semantic analysis and codegen for C# miscellaneous node types.
*
* Copyright (C) 2001 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Perform semantic analysis for the "typeof" operator.
*/
ILNode_SemAnalysis(ILNode_TypeOf)
{
CSSemValue value;
/* Get the semantic value for the sub-expression */
value = ILNode_SemAnalysisType(node->expr, info, &(node->expr));
/* Check that the argument is of the correct kind */
if(CSSemIsType(value))
{
if(ILType_IsPrimitive(CSSemGetType(value)))
{
node->type = ILType_FromClass
(ILTypeToClass(info, CSSemGetType(value)));
}
else
{
node->type = CSSemGetType(value);
}
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid argument to `typeof' operator");
node->type = ILFindSystemType(info, "Int32");
}
/* The type of the expression is "System.Type" */
CSSemSetRValue(value, ILFindSystemType(info, "Type"));
return value;
}
/*
* Get the type of the "typeof" operator.
*/
ILNode_GetType(ILNode_TypeOf)
{
return ILMachineType_ObjectRef;
}
/*
* Generate value code for the "typeof" operator.
*/
ILNode_GenValue(ILNode_TypeOf)
{
ILGenTypeToken(info, IL_OP_LDTOKEN, node->type);
ILGenAdjust(info, 1);
ILGenCallByName(info,
"class [.library]System.Type "
"[.library]System.Type::GetTypeFromHandle"
"(valuetype [.library]System.RuntimeTypeHandle)");
return ILMachineType_ObjectRef;
}
/*
* Generate Java value code for the "typeof" operator.
*/
JavaGenValue(ILNode_TypeOf)
{
const char *name;
/* Use the "System.Type.GetType" method to resolve the type,
because the JVM does not have an equivalent to "ldtoken" */
name = CSTypeToName(node->type);
JavaGenStringConst(info, name, strlen(name));
JavaGenCallByName(info, "System/String", "__FromJavaString",
"(Ljava/lang/String;)LSystem/String;");
JavaGenAdjust(info, 1);
JavaGenCallByName(info, "System/Type", "GetType",
"(LSystem/String;)LSystem/Type;");
/* The type of the whole expression is "object reference" */
return ILMachineType_ObjectRef;
}
/*
* Get the machine type for the "sizeof" operator.
*/
ILNode_GetType(ILNode_SizeOf)
{
return ILMachineType_Int32;
}
/*
* Generate value code for the "sizeof" operator.
*/
ILNode_GenValue(ILNode_SizeOf)
{
if(node->size != 0)
{
ILGenInt32(info, node->size);
}
else
{
ILClass *classInfo = ILTypeToClass(info, node->type);
ILGenClassToken(info, IL_OP_PREFIX + IL_PREFIX_OP_SIZEOF, classInfo);
}
ILGenAdjust(info, 1);
return ILMachineType_Int32;
}
/*
* Evaluate the constant value for the "sizeof" operator.
*/
ILNode_EvalConst(ILNode_SizeOf)
{
if(node->size != 0)
{
value->valueType = ILMachineType_Int32;
value->un.i4Value = node->size;
return 1;
}
else
{
/* Non-primitive types must be evaluated at runtime */
return 0;
}
}
/*
* Generate Java value code for the "sizeof" operator.
*/
JavaGenValue(ILNode_SizeOf)
{
/* Only called for primitive types */
JavaGenInt32(info, node->size);
JavaGenAdjust(info, 1);
return ILMachineType_Int32;
}
%{
/*
* Get the size of a type if it is static. Returns zero if it isn't.
*/
static ILUInt32 GetPrimitiveTypeSize(ILType *type)
{
if(ILType_IsPrimitive(type))
{
switch(ILType_ToElement(type))
{
case IL_META_ELEMTYPE_BOOLEAN:
case IL_META_ELEMTYPE_I1:
case IL_META_ELEMTYPE_U1: return 1;
case IL_META_ELEMTYPE_CHAR:
case IL_META_ELEMTYPE_I2:
case IL_META_ELEMTYPE_U2: return 2;
case IL_META_ELEMTYPE_I4:
case IL_META_ELEMTYPE_U4:
case IL_META_ELEMTYPE_R4: return 4;
case IL_META_ELEMTYPE_I8:
case IL_META_ELEMTYPE_U8:
case IL_META_ELEMTYPE_R8: return 8;
}
}
return 0;
}
%}
/*
* Perform semantic analysis for the "sizeof" operator.
*/
ILNode_SemAnalysis(ILNode_SizeOf)
{
CSSemValue value;
/* Get the semantic value for the sub-expression */
value = ILNode_SemAnalysis(node->expr, info, &(node->expr));
/* sizeof(primitive) is safe in C# 2.0 */
if(!ILType_IsPrimitive(CSSemGetType(value)))
{
/* Print an error or warning for this construct if necessary */
CCUnsafeMessage(info, (ILNode *)node, "unsafe `sizeof' operator");
}
/* Convert native types into their value type forms */
if(CSSemIsType(value))
{
if(CSSemGetType(value) == ILType_Int)
{
CSSemSetType(value, ILFindSystemType(info, "IntPtr"));
}
else if(CSSemGetType(value) == ILType_UInt)
{
CSSemSetType(value, ILFindSystemType(info, "UIntPtr"));
}
}
/* Check the semantic value */
if(!CSSemIsType(value))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid argument to `sizeof' operator");
node->type = ILType_Int32;
}
else if(!ILType_IsPrimitive(CSSemGetType(value)) &&
!ILType_IsValueType(CSSemGetType(value)) &&
!ILType_IsPointer(CSSemGetType(value)))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"cannot take the size of a managed type");
node->type = ILType_Int32;
}
else if(!ILType_IsPrimitive(CSSemGetType(value)) && info->outputIsJava)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`sizeof' disallowed when compiling to Java bytecode");
node->type = ILType_Int32;
}
else if(CSSemGetType(value) == ILType_Void)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`void' type is not allowed in this context");
node->type = ILType_Int32;
}
else if(ILType_IsPointer(CSSemGetType(value)))
{
/* Pointers always have the same size as a native integer */
node->type = ILType_Int;
}
else
{
node->type = CSSemGetType(value);
}
/* Pre-compute the size if this is a primitive type */
node->size = GetPrimitiveTypeSize(node->type);
/* Return the type information to the caller */
CSSemSetRValue(value, ILType_Int32);
return value;
}
/*
* Get the machine type for the "stackalloc" operator.
*/
ILNode_GetType(ILNode_StackAlloc)
{
if(info->outputIsJava)
{
return ILMachineType_ObjectRef;
}
else
{
return ILMachineType_UnmanagedPtr;
}
}
/*
* Generate value code for the "stackalloc" operator.
*/
ILNode_GenValue(ILNode_StackAlloc)
{
ILNode_GenValue(node->expr2, info);
ILGenSimple(info, IL_PREFIX_OP_LOCALLOC + IL_OP_PREFIX);
return ILMachineType_UnmanagedPtr;
}
/*
* Generate Java value code for the "stackalloc" operator.
*/
JavaGenValue(ILNode_StackAlloc)
{
/* TODO */
return ILMachineType_ObjectRef;
}
/*
* Perform semantic analysis for the "stackalloc" operator.
*/
ILNode_SemAnalysis(ILNode_StackAlloc)
{
ILNode *tempNode;
ILType *type;
CSSemValue value;
/* Print an error or warning for this construct if necessary */
CCUnsafeMessage(info, (ILNode *)node, "unsafe `stackalloc' operator");
/* Perform semantic analysis on the sub-expressions */
/* The first must be a type */
type = CSSemType(node->expr1, info, &(node->expr1));
/* The second one must be an integer expression */
value = ILNode_SemAnalysis(node->expr2, info, &(node->expr2));
if(!CSSemIsValue(value))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid element number count in stackalloc expression");
}
else
{
if(!ILCoerce(info, node->expr2, &node->expr2,
CSSemGetType(value), ILType_Int32, 1))
{
CCErrorOnLine(yygetfilename(node->expr2), yygetlinenum(node->expr2),
"incompatible types in stackalloc element number specifier: "
"no conversion from `%s' to `int'",
CSTypeToName(CSSemGetType(value)));
}
}
/*
* Replace expr1 and expr2 with semguards so that they will not get
* semanalyzed again
*/
node->expr2 = ILNode_SemGuard_create(node->expr2, value);
CSSemSetType(value, type);
tempNode = ILNode_SemGuard_create(node->expr1, value);
tempNode = ILNode_SizeOf_create(tempNode);
node->expr2 = ILNode_Mul_create(tempNode, node->expr2);
/* Now semanalyze the final expression again */
value = ILNode_SemAnalysis(node->expr2, info, &(node->expr2));
/* The result has the type "pointer to type" */
type = ILTypeCreateRef(info->context, IL_TYPE_COMPLEX_PTR, type);
if(!type)
{
CCOutOfMemory();
}
CSSemSetRValue(value, type);
return value;
}
ILNode_SemAnalysis(ILNode_NewExpression)
{
CSSemValue value;
ILNode_ListIter iter;
ILNode *child;
ILNode *expr;
ILNode **parentNode;
ILNode **parentOfExpr;
ILEvalValue evalValue;
int numDimensions = 0;
int dim;
ILType *elemType;
/* Determine if the array creation is normal or initializer-based */
if(node->indexes)
{
if(yyisa(node->type, ILNode_ArrayType))
{
/* Create the final array type with the rank specified by the
number of expressions. */
node->type =
ILNode_ArrayType_create(node->type, CountArgList(node->indexes));
}
else
{
/* Build the full type node, by combining the element
type, indexed dimensions, and the rank specifiers */
node->type =
ILNode_ArrayType_create(node->type, CountArgList(node->indexes));
ILNode_ListIter_Init(&iter, node->rank);
while((child = ILNode_ListIter_Next(&iter)) != 0)
{
node->type = ILNode_ArrayType_create
(node->type, ((ILNode_TypeSuffix *)child)->count);
}
}
/* Perform semantic analysis on the array type */
node->arrayType = CSSemType(node->type, info, &(node->type));
/* Perform semantic analysis on the array dimensions */
child = node->indexes;
parentOfExpr = &(node->indexes);
while(yyisa(child, ILNode_ArgList))
{
++numDimensions;
expr = ((ILNode_ArgList *)child)->expr2;
parentNode = &(((ILNode_ArgList *)child)->expr2);
if(CSSemExpectValue(expr, info, parentNode, &value))
{
if(!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_Int32,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_UInt32,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_Int64,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_UInt64,1))
{
CCErrorOnLine
(yygetfilename(expr), yygetlinenum(expr),
"incompatible types in dimension specifier: "
"no conversion from `%s' to `int', `uint', "
"`long', or `ulong'",
CSTypeToName(CSSemGetType(value)));
}
}
else
{
CCErrorOnLine(yygetfilename(expr), yygetlinenum(expr),
"invalid dimension in array creation expression");
}
parentOfExpr = &(((ILNode_ArgList *)child)->expr1);
child = *parentOfExpr;
}
++numDimensions;
expr = child;
parentNode = parentOfExpr;
if(CSSemExpectValue(expr, info, parentNode, &value))
{
if(!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_Int32,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_UInt32,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_Int64,1) &&
!ILCoerce(info, *parentNode, parentNode,
CSSemGetType(value), ILType_UInt64,1))
{
CCErrorOnLine
(yygetfilename(expr), yygetlinenum(expr),
"incompatible types in dimension specifier: "
"no conversion from `%s' to `int', `uint', "
"`long', or `ulong'",
CSTypeToName(CSSemGetType(value)));
}
}
else
{
CCErrorOnLine(yygetfilename(expr), yygetlinenum(expr),
"invalid dimension in array creation expression");
}
}
else
{
/* Perform semantic analysis on the array type, which is
assumed to already have rank specifiers */
node->arrayType = CSSemType(node->type, info, &(node->type));
/* Check that the supplied type is actually an array */
if(!ILType_IsArray(node->arrayType))
{
CCErrorOnLine(yygetfilename(node->type), yygetlinenum(node->type),
"array initializer used with non-array type");
CSSemSetRValue(value, node->arrayType);
return value;
}
}
/* Check that the element type has a class form associated with it.
If we are compiling in a "no floating point" profile, the
"System.Single" and "System.Double" classes won't exist, which
will lead to problems during the code generation phase */
elemType = ILTypeGetElemType(node->arrayType);
if(!ILTypeToClass(info, elemType))
{
CCErrorOnLine(yygetfilename(node->type), yygetlinenum(node->type),
"array of `%s' used in a profile that cannot support it",
CSTypeToName(elemType));
}
/* Handle array initializers */
if(node->arrayInitializer)
{
/* Wrap the initializer in an "ILNode_ArrayInit" node */
node->arrayInitializer =
ILNode_ArrayInit_create(node->arrayInitializer);
/* Validate the initializer's shape against the actual type,
and then coerce all of the elements to their final types */
if(!ILArrayInitShapeOK(info, node->arrayInitializer, node->arrayType))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"incorrect pattern of elements in array initializer");
}
else
{
CoerceArrayInit(info, node->arrayInitializer,
&(node->arrayInitializer),
ILTypeGetElemType(node->arrayType));
}
/* If we have indices, then they must be constant and match
the sizes of the initializer dimensions */
if(node->indexes)
{
dim = numDimensions;
child = node->indexes;
while(yyisa(child, ILNode_ArgList))
{
expr = ((ILNode_ArgList *)child)->expr2;
if(!ILNode_EvalConst(expr, info, &evalValue) ||
!ILGenCastConst(info, &evalValue, evalValue.valueType,
ILMachineType_Int64) ||
evalValue.un.i8Value !=
(ILInt64)ILGetArrayInitLength
(((ILNode_ArrayInit *)(node->arrayInitializer))
->expr, dim - 1))
{
CCErrorOnLine(yygetfilename(expr), yygetlinenum(expr),
"dimension %d does not match initializer", dim);
}
child = (((ILNode_ArgList *)child)->expr1);
--dim;
}
expr = child;
if(!ILNode_EvalConst(expr, info, &evalValue) ||
!ILGenCastConst(info, &evalValue, evalValue.valueType,
ILMachineType_Int64) ||
evalValue.un.i8Value !=
(ILInt64)ILGetArrayInitLength
(((ILNode_ArrayInit *)(node->arrayInitializer))
->expr, dim - 1))
{
CCErrorOnLine(yygetfilename(expr), yygetlinenum(expr),
"dimension %d does not match initializer", dim);
}
}
/* Replace the current node with the initializer node */
*parent = node->arrayInitializer;
}
/* Done */
CSSemSetRValue(value, node->arrayType);
return value;
}
/*
* Perform semantic analysis for the "semantic guard" expression.
* This is an "as-is" node that has already been analysed in a
* previous pass and should not be analysed again. This usually
* happens when field initializers are turned into statements
* within static constructors.
*/
ILNode_SemAnalysis(ILNode_SemGuard)
{
return node->value;
}
/*
* Perform semantic analysis for an array initialization expression.
*/
ILNode_SemAnalysis(ILNode_ArrayInit)
{
if(node->arrayType)
{
/* We've already visited this node, so return its type */
CSSemValue value;
CSSemSetRValue(value, node->arrayType);
return value;
}
else
{
/* We haven't visited this node, so it has been used in
an incorrect context. Initializers can be used on
fields and variables using regular assignment, or in
array creation expressions */
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
_("internal error in array initializer"));
return CSSemValueDefault;
}
}
/*
* Perform semantic analysis on a "make reference" operator.
*/
ILNode_SemAnalysis(ILNode_MakeRefAny)
{
CSSemValue value;
/* Cannot use this operator with Java output */
if(info->outputIsJava)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`__makeref' disallowed when compiling to Java bytecode");
}
/* Perform semantic analysis on the sub-expression
which must be an l-value. S-values are not allowed
because we have no way of knowing if some future
code may try to read the value via the typedref */
value = ILNode_SemAnalysis(node->expr, info, &(node->expr));
if(!CSSemIsLValue(value) ||
yyisa(node->expr, ILNode_LValueNoRef))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid lvalue in `__makeref' expression");
node->type = ILType_Int32;
}
else
{
node->type = CSSemGetType(value);
}
/* The type of "__makeref" expressions is always ILType_TypedRef */
CSSemSetRValue(value, ILType_TypedRef);
return value;
}
/*
* Perform semantic analysis on a "get reference type" operator.
*/
ILNode_SemAnalysis(ILNode_RefType)
{
CSSemValue value;
/* Cannot use this operator with Java output */
if(info->outputIsJava)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`__reftype' disallowed when compiling to Java bytecode");
}
/* Perform semantic analysis on the sub-expression, which
must be ILType_TypedRef */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value) ||
CSSemGetType(value) != ILType_TypedRef)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid argument to `__reftype'");
}
/* The result is "System.Type" */
CSSemSetRValue(value, ILFindSystemType(info, "Type"));
return value;
}
/*
* Perform semantic analysis on a "get reference value" operator.
*/
ILNode_SemAnalysis(ILNode_RefValue)
{
CSSemValue value;
/* Cannot use this operator with Java output */
if(info->outputIsJava)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`__refvalue' disallowed when compiling to Java bytecode");
}
/* Perform semantic analysis on the first sub-expression,
which must be ILType_TypedRef */
if(!CSSemExpectValue(node->expr1, info, &(node->expr1), &value) ||
CSSemGetType(value) != ILType_TypedRef)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid first argument to `__refvalue'");
}
/* Perform semantic analysis on the second sub-expression,
which must be a type */
node->type = CSSemType(node->expr2, info, &(node->expr2));
/* The result is an r-value of the supplied type */
CSSemSetRValue(value, node->type);
return value;
}
ILNode_SemAnalysis(ILNode_BaseDestructor)
{
ILNode_MethodDeclaration *caller;
ILType *type;
caller= (ILNode_MethodDeclaration *)(info->currentMethod);
if(!caller)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"cannot call base destructors outside a method ");
return CSSemValueDefault;
}
type = ILType_FromClass(ILMethod_Owner(caller->methodInfo));
if(ILTypeIsObjectClass(type))
{
return CSSemValueDefault;
}
return ILNode_SemAnalysis(node->destructor,info,&(node->destructor));
}
/*
* Generate value code for the "stackalloc" operator.
*/
ILNode_GenDiscard(ILNode_StackAlloc)
{
/* For handling side effects only */
ILNode_GenDiscard(node->expr2, info);
}
ILNode_GenDiscard(ILNode_BaseDestructor)
{
ILNode_GenDiscard(node->destructor,info);
}
|