1 // $Id: definite.cpp,v 1.69 2004/06/02 10:28:06 elliott-oss Exp $
2 //
3 // This software is subject to the terms of the IBM Jikes Compiler
4 // License Agreement available at the following URL:
5 // http://ibm.com/developerworks/opensource/jikes.
6 // Copyright (C) 1996, 2004 IBM Corporation and others. All Rights Reserved.
7 // You must accept the terms of that agreement to use this software.
8 //
9 #include "platform.h"
10 #include "semantic.h"
11 #include "control.h"
12 #include "option.h"
13
14 #ifdef HAVE_JIKES_NAMESPACE
15 namespace Jikes { // Open namespace Jikes block
16 #endif
17
18 //
19 // NOTE: This file is used to determine definite assignment and definite
20 // unassignment rules, per JLS chapter 16. Often, these are abbreviated
21 // da and du. The BitSet type holds a status bit for every variable in
22 // scope. Since many rules operate identically on da and du, the DefinitePair
23 // type is a wrapper for two BitSets. Finally, boolean expressions can
24 // cause different definite assignment status during speculation, so
25 // the DefiniteAssignmentSet is a wrapper for two DefinitePair objects.
26 //
27 // It is a compile-time error if a local variable is accessed that is not
28 // da, and a compile-time error if a blank final is assigned when it is
29 // not du. This code also handles the compile-time error when an assignment
30 // is attempted to an initialized final.
31 //
32 // There are two contexts: expression and statement. In expression context,
33 // we must pass in the current assignment state and return the resultant
34 // state (if different) - this is to allow speculative decisions when
35 // evaluating loop bodies. In statement context, rather than pass the
36 // information around, we store it in the instance variable
37 // *DefinitelyAssignedVariables() for efficiency.
38 //
39
DefinitePair(const DefiniteAssignmentSet & set)40 inline DefinitePair::DefinitePair(const DefiniteAssignmentSet& set)
41 : da_set(set.DASet()),
42 du_set(set.DUSet())
43 {}
44
operator =(const DefiniteAssignmentSet & rhs)45 inline DefinitePair& DefinitePair::operator=(const DefiniteAssignmentSet& rhs)
46 {
47 da_set = rhs.DASet();
48 du_set = rhs.DUSet();
49 return *this;
50 }
51
52
53 //
54 // There are two versions of DefiniteExpression. Call this version if the
55 // expression can be boolean, but there is no need to track the when true
56 // and when false cases separately; or if the expression cannot be boolean.
57 // If the expression can be (or always is) boolean, and the when true and
58 // when false cases matter, call DefiniteBooleanExpression.
59 //
DefiniteExpression(AstExpression * expr,DefinitePair & def_pair)60 void Semantic::DefiniteExpression(AstExpression* expr, DefinitePair& def_pair)
61 {
62 if (expr -> IsConstant()) // A constant expression has no effect on DA/DU.
63 return;
64 DefiniteAssignmentSet* definite = DefiniteBooleanExpression(expr,
65 def_pair);
66 if (definite)
67 {
68 def_pair = *definite;
69 delete definite;
70 }
71 }
72
73 //
74 // See the comments for DefiniteExpression above. If the when true and when
75 // false status differ after this expression, the calling function MUST delete
76 // the returned object to avoid a memory leak.
77 //
DefiniteBooleanExpression(AstExpression * expr,DefinitePair & def_pair)78 DefiniteAssignmentSet* Semantic::DefiniteBooleanExpression(AstExpression* expr,
79 DefinitePair& def_pair)
80 {
81 DefiniteAssignmentSet* definite = NULL;
82
83 //
84 // Is the expression a constant expression of type boolean?
85 // Recall that a constant expression does not contain an
86 // assignment statement.
87 //
88 if (IsConstantTrue(expr))
89 return new DefiniteAssignmentSet(def_pair, *Universe());
90 else if (IsConstantFalse(expr))
91 return new DefiniteAssignmentSet(*Universe(), def_pair);
92 else if (expr -> symbol != control.no_type)
93 definite = (this ->* DefiniteExpr[expr -> kind])(expr, def_pair);
94
95 assert(! definite || expr -> Type() == control.boolean_type);
96 return definite;
97 }
98
99
DefiniteName(AstExpression * expression,DefinitePair & def_pair)100 DefiniteAssignmentSet* Semantic::DefiniteName(AstExpression* expression,
101 DefinitePair& def_pair)
102 {
103 AstName* name = (AstName*) expression;
104
105 if (name -> resolution_opt)
106 return DefiniteBooleanExpression(name -> resolution_opt, def_pair);
107 if (name -> base_opt)
108 DefiniteName(name -> base_opt, def_pair);
109
110 //
111 // Some simple names are undefined. e.g., the simple name in a method call.
112 // Others were generated by the compiler as method shadows, so we know
113 // when and where they will be properly initialized. Qualified names are
114 // always treated as initialized.
115 //
116 VariableSymbol* variable = name -> symbol
117 ? name -> symbol -> VariableCast() : (VariableSymbol*) NULL;
118 if (variable && ! variable -> ACC_SYNTHETIC() && ! name -> base_opt &&
119 (variable -> IsLocal(ThisMethod()) || variable -> IsFinal(ThisType())))
120 {
121 int index = variable -> LocalVariableIndex(this);
122 //
123 // Compile time constants are always da; this matters in switch
124 // blocks, where we might have bypassed the initializer.
125 //
126 if (! def_pair.da_set[index] && ! name -> IsConstant())
127 {
128 ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_ASSIGNED,
129 name -> identifier_token, variable -> Name());
130
131 if (variable -> IsLocal(ThisMethod())) // avoid cascading errors!
132 def_pair.da_set.AddElement(index);
133 }
134 }
135 return NULL;
136 }
137
138
DefiniteArrayAccess(AstExpression * expression,DefinitePair & def_pair)139 DefiniteAssignmentSet* Semantic::DefiniteArrayAccess(AstExpression* expression,
140 DefinitePair& def_pair)
141 {
142 AstArrayAccess* array_access = (AstArrayAccess*) expression;
143 DefiniteExpression(array_access -> base, def_pair);
144 DefiniteExpression(array_access -> expression, def_pair);
145 return NULL;
146 }
147
148
DefiniteMethodInvocation(AstExpression * expression,DefinitePair & def_pair)149 DefiniteAssignmentSet* Semantic::DefiniteMethodInvocation(AstExpression* expression,
150 DefinitePair& def_pair)
151 {
152 AstMethodInvocation* method_call = (AstMethodInvocation*) expression;
153 if (method_call -> base_opt)
154 DefiniteExpression(method_call -> base_opt, def_pair);
155 for (unsigned i = 0; i < method_call -> arguments -> NumArguments(); i++)
156 DefiniteExpression(method_call -> arguments -> Argument(i), def_pair);
157 return NULL;
158 }
159
160
DefiniteClassCreationExpression(AstExpression * expression,DefinitePair & def_pair)161 DefiniteAssignmentSet* Semantic::DefiniteClassCreationExpression(AstExpression* expression,
162 DefinitePair& def_pair)
163 {
164 unsigned i;
165 AstClassCreationExpression* class_creation =
166 (AstClassCreationExpression*) expression;
167 if (class_creation -> resolution_opt)
168 class_creation = class_creation -> resolution_opt;
169 if (class_creation -> base_opt)
170 DefiniteExpression(class_creation -> base_opt, def_pair);
171 for (i = 0; i < class_creation -> arguments -> NumArguments(); i++)
172 DefiniteExpression(class_creation -> arguments -> Argument(i),
173 def_pair);
174 for (i = 0; i < class_creation -> arguments -> NumLocalArguments(); i++)
175 DefiniteExpression(class_creation -> arguments -> LocalArgument(i),
176 def_pair);
177 return NULL;
178 }
179
180
DefiniteArrayCreationExpression(AstExpression * expression,DefinitePair & def_pair)181 DefiniteAssignmentSet* Semantic::DefiniteArrayCreationExpression(AstExpression* expression,
182 DefinitePair& def_pair)
183 {
184 AstArrayCreationExpression* array_creation =
185 (AstArrayCreationExpression*) expression;
186
187 for (unsigned i = 0; i < array_creation -> NumDimExprs(); i++)
188 {
189 AstDimExpr* dim_expr = array_creation -> DimExpr(i);
190 DefiniteExpression(dim_expr -> expression, def_pair);
191 }
192
193 if (array_creation -> array_initializer_opt)
194 DefiniteArrayInitializer(array_creation -> array_initializer_opt,
195 def_pair);
196 return NULL;
197 }
198
199
DefiniteFinal(AstFieldAccess * field_access)200 inline VariableSymbol* Semantic::DefiniteFinal(AstFieldAccess* field_access)
201 {
202 if (field_access -> resolution_opt)
203 field_access = field_access -> resolution_opt -> FieldAccessCast();
204
205 if (field_access)
206 {
207 VariableSymbol* variable = (field_access -> symbol
208 ? field_access -> symbol -> VariableCast()
209 : (VariableSymbol*) NULL);
210 if (variable && variable -> IsFinal(ThisType()))
211 {
212 //
213 // There is exactly one copy of a static variable, so, it's
214 // always the right one. Access via 'this' is also legal.
215 //
216 if (variable -> ACC_STATIC() ||
217 field_access -> base -> ThisExpressionCast())
218 {
219 return variable;
220 }
221 }
222 }
223 return NULL;
224 }
225
226
DefinitePLUSPLUSOrMINUSMINUS(AstExpression * expr,DefinitePair & def_pair)227 DefiniteAssignmentSet* Semantic::DefinitePLUSPLUSOrMINUSMINUS(AstExpression* expr,
228 DefinitePair& def_pair)
229 {
230 DefiniteExpression(expr, def_pair);
231
232 //
233 // JLS2 added ability for parenthesized variable to remain a variable
234 //
235 while (expr -> ParenthesizedExpressionCast())
236 expr = ((AstParenthesizedExpression*) expr) -> expression;
237
238 VariableSymbol* variable = NULL;
239 if (! expr -> ArrayAccessCast()) // some kind of name
240 {
241 MethodSymbol* read_method = NULL;
242 AstName* name = expr -> NameCast();
243 if (name)
244 {
245 if (name -> resolution_opt)
246 read_method = name -> resolution_opt -> symbol -> MethodCast();
247 }
248 else
249 {
250 AstFieldAccess* field_access = expr -> FieldAccessCast();
251 assert(field_access);
252
253 if (field_access -> resolution_opt)
254 read_method =
255 field_access -> resolution_opt -> symbol -> MethodCast();
256 }
257
258 variable = (read_method
259 ? (VariableSymbol*) read_method -> accessed_member
260 : expr -> symbol -> VariableCast());
261 while (variable && variable -> accessed_local)
262 variable = variable -> accessed_local;
263 }
264
265 //
266 // If we have a variable and it is final then...
267 //
268 if (variable && variable -> ACC_FINAL())
269 {
270 if ((variable -> IsLocal(ThisMethod()) ||
271 variable -> IsFinal(ThisType())) &&
272 ! variable -> ACC_SYNTHETIC() &&
273 (*BlankFinals())[variable -> LocalVariableIndex(this)])
274 {
275 ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_UNASSIGNED,
276 expr -> LeftToken(),
277 expr -> RightToken(),
278 variable -> Name());
279 }
280 else
281 {
282 ReportSemError(SemanticError::FINAL_VARIABLE_NOT_BLANK,
283 expr -> LeftToken(),
284 expr -> RightToken(),
285 variable -> Name());
286 }
287
288 // Mark it assigned, to catch further errors.
289 if (variable -> IsFinal(ThisType()) && ! variable -> ACC_SYNTHETIC())
290 def_pair.du_set.RemoveElement(variable -> LocalVariableIndex(this));
291 }
292 return NULL;
293 }
294
295
DefinitePostUnaryExpression(AstExpression * expression,DefinitePair & def_pair)296 DefiniteAssignmentSet* Semantic::DefinitePostUnaryExpression(AstExpression* expression,
297 DefinitePair& def_pair)
298 {
299 AstPostUnaryExpression* postfix_expression =
300 (AstPostUnaryExpression*) expression;
301 return DefinitePLUSPLUSOrMINUSMINUS(postfix_expression -> expression,
302 def_pair);
303 }
304
305
DefiniteNOT(AstExpression * expr,DefinitePair & def_pair)306 DefiniteAssignmentSet* Semantic::DefiniteNOT(AstExpression* expr,
307 DefinitePair& def_pair)
308 {
309 DefiniteAssignmentSet* after_expr = DefiniteBooleanExpression(expr,
310 def_pair);
311 if (after_expr) // is the expression is a complex boolean expression?
312 {
313 DefinitePair temp(after_expr -> true_pair);
314 after_expr -> true_pair = after_expr -> false_pair;
315 after_expr -> false_pair = temp;
316 }
317 return after_expr;
318 }
319
320
321 //
322 // The default pre unary operators are +, -, and ~.
323 // As these operators are not applicable to boolean expressions,
324 // we do not need to invoke DefiniteExpression to process them.
325 //
DefiniteDefaultPreUnaryExpression(AstExpression * expr,DefinitePair & def_pair)326 DefiniteAssignmentSet* Semantic::DefiniteDefaultPreUnaryExpression(AstExpression* expr,
327 DefinitePair& def_pair)
328 {
329 return (this ->* DefiniteExpr[expr -> kind])(expr, def_pair);
330 }
331
332
DefinitePreUnaryExpression(AstExpression * expression,DefinitePair & def_pair)333 DefiniteAssignmentSet* Semantic::DefinitePreUnaryExpression(AstExpression* expression,
334 DefinitePair& def_pair)
335 {
336 AstPreUnaryExpression* prefix_expression =
337 (AstPreUnaryExpression*) expression;
338 return (this ->* DefinitePreUnaryExpr[prefix_expression -> Tag()])
339 (prefix_expression -> expression, def_pair);
340 }
341
342
DefiniteAND_AND(AstBinaryExpression * expr,DefinitePair & def_pair)343 DefiniteAssignmentSet* Semantic::DefiniteAND_AND(AstBinaryExpression* expr,
344 DefinitePair& def_pair)
345 {
346 DefiniteAssignmentSet* after_left =
347 DefiniteBooleanExpression(expr -> left_expression, def_pair);
348 DefinitePair* before_right = NULL;
349 if (after_left)
350 def_pair = after_left -> true_pair;
351 else
352 before_right = new DefinitePair(def_pair);
353
354 DefiniteAssignmentSet* after_right =
355 DefiniteBooleanExpression(expr -> right_expression, def_pair);
356
357 if (after_left)
358 {
359 if (after_right)
360 {
361 after_right -> false_pair *= after_left -> false_pair;
362 delete after_left;
363 }
364 else
365 {
366 after_right = after_left;
367 after_right -> true_pair = def_pair;
368 after_right -> false_pair *= def_pair;
369 }
370 }
371 else
372 {
373 if (! after_right)
374 after_right = new DefiniteAssignmentSet(def_pair);
375
376 after_right -> false_pair *= *before_right;
377 }
378
379 // harmless if NULL
380 delete before_right;
381 return after_right;
382 }
383
384
DefiniteOR_OR(AstBinaryExpression * expr,DefinitePair & def_pair)385 DefiniteAssignmentSet* Semantic::DefiniteOR_OR(AstBinaryExpression* expr,
386 DefinitePair& def_pair)
387 {
388 DefiniteAssignmentSet* after_left =
389 DefiniteBooleanExpression(expr -> left_expression, def_pair);
390 DefinitePair* before_right = NULL;
391 if (after_left)
392 def_pair = after_left -> false_pair;
393 else
394 before_right = new DefinitePair(def_pair);
395
396 DefiniteAssignmentSet* after_right =
397 DefiniteBooleanExpression(expr -> right_expression, def_pair);
398
399 if (after_left)
400 {
401 if (after_right)
402 {
403 after_right -> true_pair *= after_left -> true_pair;
404 delete after_left;
405 }
406 else
407 {
408 after_right = after_left;
409 after_right -> true_pair *= def_pair;
410 after_right -> false_pair = def_pair;
411 }
412 }
413 else
414 {
415 if (! after_right)
416 after_right = new DefiniteAssignmentSet(def_pair);
417
418 after_right -> true_pair *= *before_right;
419 }
420
421 // harmless if NULL
422 delete before_right;
423 return after_right;
424 }
425
426
DefiniteDefaultBinaryExpression(AstBinaryExpression * expr,DefinitePair & def_pair)427 DefiniteAssignmentSet* Semantic::DefiniteDefaultBinaryExpression(AstBinaryExpression* expr,
428 DefinitePair& def_pair)
429 {
430 DefiniteExpression(expr -> left_expression, def_pair);
431 DefiniteExpression(expr -> right_expression, def_pair);
432 return NULL;
433 }
434
435
DefiniteBinaryExpression(AstExpression * expression,DefinitePair & def_pair)436 DefiniteAssignmentSet* Semantic::DefiniteBinaryExpression(AstExpression* expression,
437 DefinitePair& def_pair)
438 {
439 AstBinaryExpression* binary_expression =
440 (AstBinaryExpression*) expression;
441 return (this ->* DefiniteBinaryExpr[binary_expression -> Tag()])
442 (binary_expression, def_pair);
443 }
444
445
DefiniteInstanceofExpression(AstExpression * expression,DefinitePair & def_pair)446 DefiniteAssignmentSet* Semantic::DefiniteInstanceofExpression(AstExpression* expression,
447 DefinitePair& def_pair)
448 {
449 AstInstanceofExpression* expr = (AstInstanceofExpression*) expression;
450 DefiniteExpression(expr -> expression, def_pair);
451 return NULL;
452 }
453
454
DefiniteConditionalExpression(AstExpression * expression,DefinitePair & def_pair)455 DefiniteAssignmentSet* Semantic::DefiniteConditionalExpression(AstExpression* expression,
456 DefinitePair& def_pair)
457 {
458 AstConditionalExpression* conditional_expression =
459 (AstConditionalExpression*) expression;
460
461 DefiniteAssignmentSet* after_condition =
462 DefiniteBooleanExpression(conditional_expression -> test_expression,
463 def_pair);
464 DefinitePair* before_expressions = NULL;
465
466 if (after_condition)
467 def_pair = after_condition -> true_pair;
468 else before_expressions = new DefinitePair(def_pair);
469 DefiniteAssignmentSet* after_true =
470 DefiniteBooleanExpression(conditional_expression -> true_expression,
471 def_pair);
472 DefinitePair* after_true_pair = (after_true ? (DefinitePair*) NULL
473 : new DefinitePair(def_pair));
474
475 if (after_condition)
476 def_pair = after_condition -> false_pair;
477 else def_pair = *before_expressions;
478 DefiniteAssignmentSet* after_false =
479 DefiniteBooleanExpression(conditional_expression -> false_expression,
480 def_pair);
481
482 if (conditional_expression -> Type() == control.boolean_type)
483 {
484 if (! after_true)
485 after_true = new DefiniteAssignmentSet(*after_true_pair);
486
487 if (after_false)
488 {
489 after_true -> true_pair *= after_false -> true_pair;
490 after_true -> false_pair *= after_false -> false_pair;
491 }
492 else
493 {
494 after_true -> true_pair *= def_pair;
495 after_true -> false_pair *= def_pair;
496 }
497 }
498 else
499 {
500 assert(! after_true && ! after_false);
501 def_pair *= *after_true_pair;
502 }
503
504 // harmless if NULL
505 delete after_condition;
506 delete before_expressions;
507 delete after_false;
508 return after_true;
509 }
510
511
DefiniteAssignmentExpression(AstExpression * expression,DefinitePair & def_pair)512 DefiniteAssignmentSet* Semantic::DefiniteAssignmentExpression(AstExpression* expression,
513 DefinitePair& def_pair)
514 {
515 AstAssignmentExpression* assignment_expression =
516 (AstAssignmentExpression*) expression;
517
518 AstCastExpression* casted_left_hand_side =
519 assignment_expression -> left_hand_side -> CastExpressionCast();
520 AstExpression* left_hand_side = casted_left_hand_side
521 ? casted_left_hand_side -> expression
522 : assignment_expression -> left_hand_side;
523 bool simple_name = false;
524 if (left_hand_side -> NameCast())
525 {
526 AstName* name = (AstName*) left_hand_side;
527 simple_name = ! name -> base_opt;
528 }
529 else
530 {
531 AstFieldAccess* field_access = left_hand_side -> FieldAccessCast();
532 if (field_access)
533 {
534 if (field_access -> resolution_opt)
535 left_hand_side = field_access -> resolution_opt;
536 //
537 // Because constructor parameters often shadow field names,
538 // this.name is legal for final instance fields. However, anything
539 // more complex, such as (this).name or Classname.this.name, as
540 // well as Classname.name for static fields or expression.name in
541 // general, will be rejected.
542 // TODO: This is not well-specified in the JLS; rather we are just
543 // following the lead of Sun's javac 1.4.1. Clean this code up when
544 // a decent specification is given.
545 //
546 if (field_access -> base -> ThisExpressionCast())
547 simple_name = ((AstThisExpression*) field_access -> base) ->
548 base_opt == NULL;
549 }
550 }
551
552 VariableSymbol* variable = (left_hand_side -> symbol
553 ? left_hand_side -> symbol -> VariableCast()
554 : (VariableSymbol*) NULL);
555 while (variable && variable -> accessed_local)
556 variable = variable -> accessed_local;
557 int index = 0;
558
559 //
560 // An array access is never considered to be final. Since no variable
561 // is associated with the array access, the testing for the presence of
562 // variable takes care of that possibility.
563 //
564 if (variable)
565 {
566 if (variable -> IsLocal(ThisMethod()) ||
567 variable -> IsFinal(ThisType()))
568 {
569 index = variable -> LocalVariableIndex(this);
570
571 //
572 // If we have a compound assignment then the variable must have
573 // been set prior to such an assignment. otherwise, an error
574 // occurs.
575 //
576 if (! assignment_expression -> SimpleAssignment() &&
577 ! def_pair.da_set[index])
578 {
579 ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_ASSIGNED,
580 left_hand_side -> LeftToken(),
581 left_hand_side -> RightToken(),
582 variable -> Name());
583 }
584 }
585 else if (variable -> ACC_FINAL())
586 {
587 // attempt to assign a value to a final field member!
588 ReportSemError(SemanticError::FINAL_VARIABLE_NOT_BLANK,
589 left_hand_side -> LeftToken(),
590 left_hand_side -> RightToken(),
591 variable -> Name());
592 }
593 }
594
595 //
596 // The left-hand-side of an assignment expression is either a simple name,
597 // a field access or an array access. A simple name does not need further
598 // action, a field access needs to descend into all qualifying expressions,
599 // and an array access needs to descend into the entire expression.
600 //
601 if (! simple_name)
602 {
603 AstFieldAccess* field_access = left_hand_side -> FieldAccessCast();
604 DefiniteExpression((field_access ? field_access -> base
605 : left_hand_side),
606 def_pair);
607 }
608
609 //
610 // JLS2 16.1.7 - The rules for definite assignment of boolean valued
611 // assignments are stricter than they were in JLS1; hence we no longer
612 // consider the when true and when false values separately.
613 //
614 DefiniteExpression(assignment_expression -> expression, def_pair);
615
616 //
617 // Finally, we mark the variable as assigned.
618 //
619 if (variable &&
620 (variable -> IsLocal(ThisMethod()) || variable -> IsFinal(ThisType())))
621 {
622 if (variable -> ACC_FINAL())
623 {
624 //
625 // It is an error to assign finals except for DU blank final. Also,
626 // final fields must be assigned by simple name, or by this.name.
627 //
628 if (! (*BlankFinals())[index] || ! def_pair.du_set[index])
629 {
630 ReportSemError(((*BlankFinals())[index]
631 ? SemanticError::VARIABLE_NOT_DEFINITELY_UNASSIGNED
632 : SemanticError::FINAL_VARIABLE_NOT_BLANK),
633 left_hand_side -> LeftToken(),
634 left_hand_side -> RightToken(),
635 variable -> Name());
636 }
637 else if (variable -> IsFinal(ThisType()) && ! simple_name)
638 {
639 ReportSemError(SemanticError::FINAL_FIELD_ASSIGNMENT_NOT_SIMPLE,
640 left_hand_side -> LeftToken(),
641 left_hand_side -> RightToken(),
642 variable -> Name(),
643 (variable -> ACC_STATIC() ? NULL
644 : variable -> Name()));
645 }
646 else if (! def_pair.da_set[index])
647 {
648 //
649 // If the variable is also DA, then this code is never
650 // executed, so it does not affect loop or try-catch analysis.
651 //
652 if (DefiniteFinalAssignments() -> Size() > 0)
653 {
654 DefiniteFinalAssignments() -> Top().Next() =
655 left_hand_side;
656 }
657 ReachableAssignments() -> AddElement(index);
658 }
659 }
660
661 def_pair.AssignElement(index);
662 }
663 return NULL;
664 }
665
DefiniteParenthesizedExpression(AstExpression * expression,DefinitePair & def_pair)666 DefiniteAssignmentSet* Semantic::DefiniteParenthesizedExpression(AstExpression* expression,
667 DefinitePair& def_pair)
668 {
669 AstParenthesizedExpression* expr =
670 (AstParenthesizedExpression*) expression;
671 return DefiniteBooleanExpression(expr -> expression, def_pair);
672 }
673
DefiniteFieldAccess(AstExpression * expression,DefinitePair & def_pair)674 DefiniteAssignmentSet* Semantic::DefiniteFieldAccess(AstExpression* expression,
675 DefinitePair& def_pair)
676 {
677 AstFieldAccess* expr = (AstFieldAccess*) expression;
678
679 //
680 // TODO: Sun bug 4395322 mentions that DA is underspecified for field
681 // references. In other words, it is legal to read an uninitialized
682 // static value through Classname.fieldname, or an instance value
683 // through this.fieldname. If Sun specifies that this behavior is correct,
684 // this commented code can be removed.
685 //
686 // VariableSymbol* variable = DefiniteFinal(expr);
687 // if (variable)
688 // {
689 // if (! def_pair.da_set[variable -> LocalVariableIndex(this)])
690 // {
691 // ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_ASSIGNED,
692 // expr -> LeftToken(),
693 // expr -> RightToken(),
694 // variable -> Name());
695 // // supress further warnings
696 // def_pair.da_set.AddElement(variable -> LocalVariableIndex(this));
697 // }
698 // }
699 return DefiniteBooleanExpression((expr -> resolution_opt
700 ? expr -> resolution_opt
701 : expr -> base), def_pair);
702 }
703
DefiniteCastExpression(AstExpression * expression,DefinitePair & def_pair)704 DefiniteAssignmentSet* Semantic::DefiniteCastExpression(AstExpression* expression,
705 DefinitePair& def_pair)
706 {
707 AstCastExpression* expr = (AstCastExpression*) expression;
708 return DefiniteBooleanExpression(expr -> expression, def_pair);
709 }
710
711
712 //
713 // Must have two versions, since this can be called in both expression and
714 // statement context.
715 //
DefiniteArrayInitializer(AstArrayInitializer * array_initializer,DefinitePair & def_pair)716 void Semantic::DefiniteArrayInitializer(AstArrayInitializer* array_initializer,
717 DefinitePair& def_pair)
718 {
719 for (unsigned i = 0;
720 i < array_initializer -> NumVariableInitializers(); i++)
721 {
722 AstArrayInitializer* sub_array_initializer = array_initializer ->
723 VariableInitializer(i) -> ArrayInitializerCast();
724
725 if (sub_array_initializer)
726 DefiniteArrayInitializer(sub_array_initializer, def_pair);
727 else
728 {
729 AstExpression* init =
730 (AstExpression*) array_initializer -> VariableInitializer(i);
731 DefiniteExpression(init, def_pair);
732 }
733 }
734 }
735
DefiniteArrayInitializer(AstArrayInitializer * array_initializer)736 inline void Semantic::DefiniteArrayInitializer(AstArrayInitializer* array_initializer)
737 {
738 DefiniteArrayInitializer(array_initializer,
739 *DefinitelyAssignedVariables());
740 }
741
742
DefiniteVariableInitializer(AstVariableDeclarator * variable_declarator)743 inline void Semantic::DefiniteVariableInitializer(AstVariableDeclarator* variable_declarator)
744 {
745 assert(variable_declarator -> variable_initializer_opt);
746 AstExpression* init =
747 variable_declarator -> variable_initializer_opt -> ExpressionCast();
748 if (! init)
749 DefiniteArrayInitializer((AstArrayInitializer*) variable_declarator ->
750 variable_initializer_opt,
751 *DefinitelyAssignedVariables());
752 else
753 DefiniteExpression(init, *DefinitelyAssignedVariables());
754
755 //
756 // Even when initialized by a non-constant, variables declared in a
757 // switch are necessarily blank finals.
758 // TODO: Sun has never given any nice official word on this.
759 //
760 if (DefiniteBlocks() &&
761 DefiniteBlocks() -> TopBlock() -> Tag() == AstBlock::SWITCH &&
762 (! init || ! init -> IsConstant()))
763 {
764 BlankFinals() -> AddElement(variable_declarator -> symbol ->
765 LocalVariableIndex(this));
766 }
767 }
768
769
DefiniteStatement(Ast * ast)770 inline void Semantic::DefiniteStatement(Ast* ast)
771 {
772 (this ->* DefiniteStmt[ast -> kind])(ast);
773 }
774
DefiniteBlockStatements(AstBlock * block_body)775 inline void Semantic::DefiniteBlockStatements(AstBlock* block_body)
776 {
777 for (unsigned i = 0; i < block_body -> NumStatements(); i++)
778 {
779 AstStatement* statement = block_body -> Statement(i);
780 //
781 // As unreachable statements already cause an error, we avoid
782 // them here
783 //
784 if (statement -> is_reachable)
785 DefiniteStatement(statement);
786 else break;
787 }
788 }
789
790
DefiniteBlock(Ast * stmt)791 void Semantic::DefiniteBlock(Ast* stmt)
792 {
793 AstBlock* block_body = (AstBlock*) stmt;
794 unsigned i;
795
796 DefiniteBlocks() -> Push(block_body);
797 DefiniteBlockStatements(block_body);
798
799 #ifdef DUMP
800 if ((control.option.g & JikesOption::VARS) &&
801 block_body -> NumLocallyDefinedVariables() > 0)
802 {
803 Coutput << "(3) At Line "
804 << lex_stream -> Line(block_body -> RightToken())
805 << " the range for the following variables end:" << endl
806 << endl;
807 for (i = 0; i < block_body -> NumLocallyDefinedVariables(); i++)
808 Coutput << " \""
809 << block_body -> LocallyDefinedVariable(i) -> Name()
810 << "\"" << endl;
811 }
812 #endif // DUMP
813 //
814 // Remove all variables that just went out of scope.
815 //
816 for (i = 0; i < block_body -> block_symbol -> NumVariableSymbols(); i++)
817 {
818 VariableSymbol* variable =
819 block_body -> block_symbol -> VariableSym(i);
820 int index = variable -> LocalVariableIndex(this);
821
822 BlankFinals() -> RemoveElement(index);
823 ReachableAssignments() -> RemoveElement(index);
824 DefinitelyAssignedVariables() -> ReclaimElement(index);
825 DefiniteBlocks() ->
826 ContinuePair(DefiniteBlocks() -> Size() - 2).ReclaimElement(index);
827 }
828
829 //
830 // Note that in constructing the Ast, the parser encloses each
831 // labeled statement in its own block... Therefore, only blocks
832 // are labeled.
833 //
834 if (block_body -> label_opt)
835 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopBreakPair();
836
837 DefiniteBlocks() -> Pop();
838 }
839
840
DefiniteLocalClassStatement(Ast * stmt)841 void Semantic::DefiniteLocalClassStatement(Ast* stmt)
842 {
843 AstLocalClassStatement* local_decl = (AstLocalClassStatement*) stmt;
844 TypeSymbol* local_type = local_decl -> declaration -> class_body ->
845 semantic_environment -> Type();
846 assert(local_type -> LocalClassProcessingCompleted());
847 for (unsigned i = 0; i < local_type -> NumConstructorParameters(); i++)
848 {
849 VariableSymbol* var =
850 local_type -> ConstructorParameter(i) -> accessed_local;
851 if (var -> owner == ThisMethod() &&
852 (! DefinitelyAssignedVariables() ->
853 da_set[var -> LocalVariableIndex(this)]))
854 {
855 ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_ASSIGNED,
856 local_decl, var -> Name());
857 }
858 }
859 }
860
861
DefiniteLocalVariableStatement(Ast * stmt)862 void Semantic::DefiniteLocalVariableStatement(Ast* stmt)
863 {
864 AstLocalVariableStatement* local_decl = (AstLocalVariableStatement*) stmt;
865 for (unsigned i = 0; i < local_decl -> NumVariableDeclarators(); i++)
866 {
867 AstVariableDeclarator* variable_declarator =
868 local_decl -> VariableDeclarator(i);
869 VariableSymbol* variable_symbol = variable_declarator -> symbol;
870 if (! variable_symbol)
871 continue;
872 int index = variable_symbol -> LocalVariableIndex(this);
873 if (control.option.g & JikesOption::VARS)
874 {
875 #ifdef DUMP
876 Coutput << "(3.5) Local Variable \"" << variable_symbol -> Name()
877 << " #" << index << "\" is declared at line "
878 << lex_stream -> Line(variable_declarator -> LeftToken())
879 << endl;
880 #endif // DUMP
881 DefiniteBlocks() -> TopBlock() ->
882 AddLocallyDefinedVariable(variable_symbol);
883 }
884
885 if (variable_declarator -> variable_initializer_opt)
886 {
887 DefiniteVariableInitializer(variable_declarator);
888 DefinitelyAssignedVariables() -> AssignElement(index);
889 }
890 else
891 {
892 DefinitelyAssignedVariables() -> ReclaimElement(index);
893 if (variable_symbol -> ACC_FINAL())
894 BlankFinals() -> AddElement(index);
895 }
896 }
897 }
898
899
DefiniteExpressionStatement(Ast * stmt)900 void Semantic::DefiniteExpressionStatement(Ast* stmt)
901 {
902 AstExpressionStatement* expression_statement =
903 (AstExpressionStatement*) stmt;
904
905 DefiniteExpression(expression_statement -> expression,
906 *DefinitelyAssignedVariables());
907 }
908
909
DefiniteSynchronizedStatement(Ast * stmt)910 void Semantic::DefiniteSynchronizedStatement(Ast* stmt)
911 {
912 AstSynchronizedStatement* synchronized_statement =
913 (AstSynchronizedStatement*) stmt;
914
915 DefiniteExpression(synchronized_statement -> expression,
916 *DefinitelyAssignedVariables());
917
918 DefiniteBlock(synchronized_statement -> block);
919 }
920
921
DefiniteIfStatement(Ast * stmt)922 void Semantic::DefiniteIfStatement(Ast* stmt)
923 {
924 AstIfStatement* if_statement = (AstIfStatement*) stmt;
925
926 DefiniteAssignmentSet* after_expr =
927 DefiniteBooleanExpression(if_statement -> expression,
928 *DefinitelyAssignedVariables());
929 DefinitePair* starting_pair =
930 new DefinitePair(*DefinitelyAssignedVariables());
931 if (after_expr)
932 *DefinitelyAssignedVariables() = after_expr -> true_pair;
933
934 //
935 // Recall that the parser ensures that the statements that appear in an
936 // if-statement (both the true and false statement) are enclosed in a
937 // block.
938 //
939 DefiniteBlock(if_statement -> true_statement);
940
941 if (! if_statement -> false_statement_opt) // no else part ?
942 {
943 *DefinitelyAssignedVariables() *= (after_expr
944 ? after_expr -> false_pair
945 : *starting_pair);
946 }
947 else
948 {
949 DefinitePair true_set(*DefinitelyAssignedVariables());
950 *DefinitelyAssignedVariables() = (after_expr
951 ? after_expr -> false_pair
952 : *starting_pair);
953
954 DefiniteBlock(if_statement -> false_statement_opt);
955
956 *DefinitelyAssignedVariables() *= true_set;
957 }
958
959 // harmless if NULL
960 delete starting_pair;
961 }
962
963
DefiniteLoopBody(BitSet & starting_set)964 void Semantic::DefiniteLoopBody(BitSet& starting_set)
965 {
966 //
967 // Find the set of variables that were DU before the loop, but not DU
968 // at the loop end. This requires the loop to have merged in the DU
969 // status before all continue statements.
970 //
971 starting_set -= DefinitelyAssignedVariables() -> du_set;
972
973 for (unsigned i = 0; i < DefiniteFinalAssignments() -> Top().Length(); i++)
974 {
975 AstExpression* name = DefiniteFinalAssignments() -> Top()[i];
976 VariableSymbol* variable = (VariableSymbol*) name -> symbol;
977
978 if (starting_set[variable -> LocalVariableIndex(this)])
979 {
980 ReportSemError(SemanticError::VARIABLE_NOT_DEFINITELY_UNASSIGNED_IN_LOOP,
981 name -> LeftToken(),
982 name -> RightToken(),
983 variable -> Name());
984 }
985 }
986
987 DefiniteFinalAssignments() -> Pop();
988 }
989
990
DefiniteWhileStatement(Ast * stmt)991 void Semantic::DefiniteWhileStatement(Ast* stmt)
992 {
993 AstWhileStatement* while_statement = (AstWhileStatement*) stmt;
994
995 DefiniteFinalAssignments() -> Push();
996
997 BitSet starting_set(DefinitelyAssignedVariables() -> du_set);
998 DefiniteAssignmentSet* after_expr =
999 DefiniteBooleanExpression(while_statement -> expression,
1000 *DefinitelyAssignedVariables());
1001 DefinitePair before_statement(Universe() -> Size());
1002
1003 if (after_expr)
1004 *DefinitelyAssignedVariables() = after_expr -> true_pair;
1005 else before_statement = *DefinitelyAssignedVariables();
1006
1007 //
1008 // We have already given a warning if the statement is unreachable
1009 //
1010 if (while_statement -> statement -> is_reachable)
1011 DefiniteBlock(while_statement -> statement);
1012 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopContinuePair();
1013 DefiniteLoopBody(starting_set);
1014 *DefinitelyAssignedVariables() = DefiniteBlocks() -> TopBreakPair() *
1015 (after_expr ? after_expr -> false_pair : before_statement);
1016
1017 delete after_expr;
1018 }
1019
1020
DefiniteForStatement(Ast * stmt)1021 void Semantic::DefiniteForStatement(Ast* stmt)
1022 {
1023 AstForStatement* for_statement = (AstForStatement*) stmt;
1024 unsigned i;
1025
1026 //
1027 // Note that in constructing the Ast, the parser encloses each
1028 // for-statement in its own block, so that any variables defined in the
1029 // for-init-statements have scope limited to the for loop. Thus, we do
1030 // not need to worry about declaring or reclaiming variables in the
1031 // for-init section in this method.
1032 //
1033 // For example, the following sequence of statements is legal:
1034 //
1035 // for (int i = 0; i < 10; i++);
1036 // for (int i = 10; i < 20; i++);
1037 //
1038 for (i = 0; i < for_statement -> NumForInitStatements(); i++)
1039 DefiniteStatement(for_statement -> ForInitStatement(i));
1040
1041 DefiniteFinalAssignments() -> Push();
1042
1043 BitSet starting_set(DefinitelyAssignedVariables() -> du_set);
1044 DefiniteAssignmentSet* after_end_expression = NULL;
1045 DefinitePair before_statement(Universe() -> Size());
1046
1047 if (for_statement -> end_expression_opt)
1048 after_end_expression =
1049 DefiniteBooleanExpression(for_statement -> end_expression_opt,
1050 *DefinitelyAssignedVariables());
1051
1052 if (after_end_expression)
1053 *DefinitelyAssignedVariables() = after_end_expression -> true_pair;
1054 else before_statement = *DefinitelyAssignedVariables();
1055
1056 //
1057 // We have already given a warning if the statement is unreachable
1058 //
1059 if (for_statement -> statement -> is_reachable)
1060 DefiniteBlock(for_statement -> statement);
1061
1062 //
1063 // Compute the set of variables that are definitely assigned after the
1064 // contained statement and after every continue statement that may exit
1065 // the body of the for statement.
1066 //
1067 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopContinuePair();
1068 for (i = 0; i < for_statement -> NumForUpdateStatements(); i++)
1069 DefiniteExpressionStatement(for_statement -> ForUpdateStatement(i));
1070 DefiniteLoopBody(starting_set);
1071
1072 //
1073 // Compute the set of variables that belongs to both sets below:
1074 //
1075 // . the universe if no condition expression is present;
1076 // otherwise, the set of variables that are DA when
1077 // the condition expression is false.
1078 //
1079 // . the set of variables that are DA before every
1080 // break statement that may exit the for statement.
1081 //
1082 *DefinitelyAssignedVariables() = DefiniteBlocks() -> TopBreakPair() *
1083 (for_statement -> end_expression_opt
1084 ? (after_end_expression ? after_end_expression -> false_pair
1085 : before_statement)
1086 : *Universe());
1087
1088 delete after_end_expression; // harmless if NULL
1089 }
1090
1091
DefiniteForeachStatement(Ast * stmt)1092 void Semantic::DefiniteForeachStatement(Ast* stmt)
1093 {
1094 AstForeachStatement* for_statement = (AstForeachStatement*) stmt;
1095
1096 //
1097 // Note that in constructing the Ast, the parser encloses each
1098 // for-statement in its own block, so that the loop variable defined in
1099 // the formal parameter has scope limited to the for loop. Thus, we do
1100 // not need to worry about declaring or reclaiming variables in the
1101 // for-init section in this method.
1102 //
1103 // For example, the following sequence of statements is legal:
1104 //
1105 // for (int i : new int[0]);
1106 // for (int i : new int[0]);
1107 //
1108 AstVariableDeclarator* variable_declarator =
1109 for_statement -> formal_parameter -> formal_declarator;
1110 VariableSymbol* variable_symbol = variable_declarator -> symbol;
1111 if (variable_symbol)
1112 {
1113 int index = variable_symbol -> LocalVariableIndex(this);
1114 if (control.option.g & JikesOption::VARS)
1115 {
1116 #ifdef DUMP
1117 Coutput << "(3.6) Foreach Variable \"" << variable_symbol -> Name()
1118 << " #" << index << "\" is declared at line "
1119 << lex_stream -> Line(variable_declarator -> LeftToken())
1120 << endl;
1121 #endif // DUMP
1122 DefiniteBlocks() -> TopBlock() ->
1123 AddLocallyDefinedVariable(variable_symbol);
1124 }
1125 DefinitelyAssignedVariables() -> AssignElement(index);
1126 }
1127
1128 DefiniteExpression(for_statement -> expression,
1129 *DefinitelyAssignedVariables());
1130 BitSet starting_set(DefinitelyAssignedVariables() -> du_set);
1131 DefinitePair before_statement(*DefinitelyAssignedVariables());
1132 DefiniteFinalAssignments() -> Push();
1133
1134 //
1135 // We have already given a warning if the statement is unreachable
1136 //
1137 if (for_statement -> statement -> is_reachable)
1138 DefiniteBlock(for_statement -> statement);
1139
1140 //
1141 // Compute the set of variables that are definitely assigned after the
1142 // contained statement and after every continue statement that may exit
1143 // the body of the for statement.
1144 //
1145 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopContinuePair();
1146 DefiniteLoopBody(starting_set);
1147
1148 //
1149 // Compute the set of variables that are DA before every break statement
1150 // that may exit the for statement.
1151 //
1152 *DefinitelyAssignedVariables() =
1153 DefiniteBlocks() -> TopBreakPair() * before_statement;
1154 }
1155
1156
DefiniteDoStatement(Ast * stmt)1157 void Semantic::DefiniteDoStatement(Ast* stmt)
1158 {
1159 AstDoStatement* do_statement = (AstDoStatement*) stmt;
1160
1161 DefiniteFinalAssignments() -> Push();
1162
1163 BitSet starting_set(DefinitelyAssignedVariables() -> du_set);
1164
1165 DefiniteBlock(do_statement -> statement);
1166
1167 DefinitePair after_stmt(*DefinitelyAssignedVariables());
1168 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopContinuePair();
1169 DefiniteAssignmentSet* after_expr =
1170 DefiniteBooleanExpression(do_statement -> expression,
1171 *DefinitelyAssignedVariables());
1172 DefinitePair after_loop(Universe() -> Size());
1173
1174 if (after_expr)
1175 *DefinitelyAssignedVariables() = after_expr -> true_pair;
1176 else after_loop = *DefinitelyAssignedVariables();
1177 DefiniteLoopBody(starting_set);
1178
1179 *DefinitelyAssignedVariables() = DefiniteBlocks() -> TopBreakPair() *
1180 (after_expr ? after_expr -> false_pair : after_loop);
1181
1182 delete after_expr;
1183 }
1184
1185
DefiniteSwitchStatement(Ast * stmt)1186 void Semantic::DefiniteSwitchStatement(Ast* stmt)
1187 {
1188 AstSwitchStatement* switch_statement = (AstSwitchStatement*) stmt;
1189
1190 AstBlock* block_body = switch_statement -> switch_block;
1191 DefiniteBlocks() -> Push(block_body);
1192
1193 DefiniteExpression(switch_statement -> expression,
1194 *DefinitelyAssignedVariables());
1195
1196 DefinitePair starting_pair(*DefinitelyAssignedVariables());
1197
1198 //
1199 // Recall that the parser inserts an empty statement if necessary after
1200 // the last label, so that all SwitchBlockStatementGroups have statements.
1201 // The standard does not allow us to optimize for a constant switch, or
1202 // for enumerating all 256 byte cases with no default.
1203 //
1204 unsigned i;
1205 for (i = 0; i < block_body -> NumStatements(); i++)
1206 {
1207 AstSwitchBlockStatement* switch_block_statement =
1208 (AstSwitchBlockStatement*) block_body -> Statement(i);
1209
1210 *DefinitelyAssignedVariables() *= starting_pair;
1211 DefiniteBlockStatements(switch_block_statement);
1212 }
1213 if (! switch_statement -> DefaultCase())
1214 *DefinitelyAssignedVariables() *= starting_pair;
1215 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> TopBreakPair();
1216
1217 //
1218 // Remove all variables that just went out of scope
1219 //
1220 for (i = 0; i < block_body -> block_symbol -> NumVariableSymbols(); i++)
1221 {
1222 VariableSymbol* variable =
1223 block_body -> block_symbol -> VariableSym(i);
1224 int index = variable -> LocalVariableIndex(this);
1225
1226 BlankFinals() -> RemoveElement(index);
1227 ReachableAssignments() -> RemoveElement(index);
1228 DefinitelyAssignedVariables() -> ReclaimElement(index);
1229 }
1230
1231 DefiniteBlocks() -> Pop();
1232 }
1233
1234
DefiniteBreakStatement(Ast * stmt)1235 void Semantic::DefiniteBreakStatement(Ast* stmt)
1236 {
1237 AstBreakStatement* break_statement = (AstBreakStatement*) stmt;
1238
1239 //
1240 // Compute the set of variables that are definitely assigned prior to
1241 // executing the break, including if the break occurs in a try or catch
1242 // block.
1243 //
1244 if (AbruptFinallyStack().Top() < break_statement -> nesting_level)
1245 {
1246 DefiniteBlocks() -> BreakPair(break_statement -> nesting_level) *=
1247 *DefinitelyAssignedVariables();
1248 }
1249
1250 //
1251 // After execution of a break statement, it is vacuously true
1252 // that every variable has definitely been assigned and no final
1253 // variable has been possibly assigned (as nothing is reachable
1254 // any way).
1255 //
1256 *DefinitelyAssignedVariables() = *Universe();
1257 }
1258
1259
DefiniteContinueStatement(Ast * stmt)1260 void Semantic::DefiniteContinueStatement(Ast* stmt)
1261 {
1262 AstContinueStatement* continue_statement = (AstContinueStatement*) stmt;
1263
1264 //
1265 // Compute the set of variables that are definitely assigned prior to
1266 // executing the continue, including if the continue occurs in a try or
1267 // catch block.
1268 //
1269 if (AbruptFinallyStack().Top() < continue_statement -> nesting_level)
1270 {
1271 DefiniteBlocks() -> ContinuePair(continue_statement ->
1272 nesting_level) *=
1273 *DefinitelyAssignedVariables();
1274 }
1275
1276 //
1277 // After execution of a continue statement, it is vacuously true
1278 // that every variable has definitely been assigned and no final
1279 // variable has been possibly assigned (as nothing is reachable
1280 // any way).
1281 //
1282 *DefinitelyAssignedVariables() = *Universe();
1283 }
1284
1285
DefiniteReturnStatement(Ast * stmt)1286 void Semantic::DefiniteReturnStatement(Ast* stmt)
1287 {
1288 AstReturnStatement* return_statement = (AstReturnStatement*) stmt;
1289
1290 if (return_statement -> expression_opt)
1291 DefiniteExpression(return_statement -> expression_opt,
1292 *DefinitelyAssignedVariables());
1293
1294 if (AbruptFinallyStack().Top() == 0)
1295 DefiniteBlocks() -> ReturnPair() *= *DefinitelyAssignedVariables();
1296
1297 //
1298 // After execution of a return statement, it is vacuously true
1299 // that every variable has definitely been assigned and no final
1300 // variable has been possibly assigned (as nothing is reachable
1301 // any way).
1302 //
1303 *DefinitelyAssignedVariables() = *Universe();
1304 }
1305
1306
DefiniteThrowStatement(Ast * stmt)1307 void Semantic::DefiniteThrowStatement(Ast* stmt)
1308 {
1309 AstThrowStatement* throw_statement = (AstThrowStatement*) stmt;
1310
1311 DefiniteExpression(throw_statement -> expression,
1312 *DefinitelyAssignedVariables());
1313
1314 //
1315 // After execution of a throw statement, it is vacuously true
1316 // that every variable has definitely been assigned and no final
1317 // variable has been possibly assigned (as nothing is reachable
1318 // any way).
1319 //
1320 *DefinitelyAssignedVariables() = *Universe();
1321 }
1322
1323
DefiniteTryStatement(Ast * stmt)1324 void Semantic::DefiniteTryStatement(Ast* stmt)
1325 {
1326 AstTryStatement* try_statement = (AstTryStatement*) stmt;
1327 if (try_statement -> finally_clause_opt &&
1328 (! try_statement -> finally_clause_opt -> block ->
1329 can_complete_normally))
1330 {
1331 AbruptFinallyStack().Push(try_statement -> finally_clause_opt ->
1332 block -> nesting_level);
1333 }
1334
1335 DefinitePair starting_pair(*DefinitelyAssignedVariables());
1336 BitSet already_assigned(*ReachableAssignments());
1337 ReachableAssignments() -> SetEmpty();
1338
1339 DefiniteBlock(try_statement -> block);
1340
1341 BitSet before_catch_finals(starting_pair.du_set - *ReachableAssignments()),
1342 possibly_finals_union(DefinitelyAssignedVariables() -> du_set);
1343 //
1344 // We initialize the variable after_blocks here. It is used to calculate
1345 // intersection of the set of variables that are definitely assigned by
1346 // all the blocks: the try block, all the catch blocks, if any, and the
1347 // finally block, if there is one.
1348 //
1349 BitSet after_blocks(DefinitelyAssignedVariables() -> da_set);
1350
1351 //
1352 // Recall that the body of the catch blocks must not be
1353 // processed within the environment of the associated try whose
1354 // exceptions they are supposed to catch but within the immediate
1355 // enclosing block (which may itself be a try block).
1356 //
1357 for (unsigned i = 0; i < try_statement -> NumCatchClauses(); i++)
1358 {
1359 DefinitelyAssignedVariables() -> da_set = starting_pair.da_set;
1360 DefinitelyAssignedVariables() -> du_set = before_catch_finals;
1361 AstCatchClause* clause = try_statement -> CatchClause(i);
1362
1363 //
1364 // The catch clause parameter must be added, in case it is final.
1365 // It is already initialized.
1366 //
1367 VariableSymbol* variable = clause -> parameter_symbol;
1368 int index = variable -> LocalVariableIndex(this);
1369 DefinitelyAssignedVariables() -> AddElement(index);
1370 if (control.option.g & JikesOption::VARS)
1371 {
1372 #ifdef DUMP
1373 Coutput << "(7) Variable \"" << variable -> Name() << " #"
1374 << index << "\" is defined at line "
1375 << lex_stream -> Line(clause -> formal_parameter -> LeftToken())
1376 << endl;
1377 #endif // DUMP
1378 }
1379 DefiniteBlock(clause -> block);
1380
1381 //
1382 // The parameter goes out of scope. We do not need to remove it from
1383 // reachable assignments, since if it was final, it can't be assigned.
1384 //
1385 DefinitelyAssignedVariables() -> ReclaimElement(index);
1386 #ifdef DUMP
1387 VariableSymbol* variable = clause -> parameter_symbol;
1388 if (control.option.g & JikesOption::VARS)
1389 Coutput << "(8) Variable \"" << variable -> Name() << " #"
1390 << index << "\" goes out of scope at line "
1391 << lex_stream -> Line(clause -> formal_parameter -> RightToken())
1392 << endl;
1393 #endif // DUMP
1394
1395 //
1396 // Process the set of variables that were definitely assigned
1397 // after this catch block
1398 //
1399 possibly_finals_union *= DefinitelyAssignedVariables() -> du_set;
1400 after_blocks *= DefinitelyAssignedVariables() -> da_set;
1401 }
1402
1403 //
1404 // Like the catch clauses, a finally block must not be processed
1405 // in the environment of its associated try block but in the
1406 // environment of its immediate enclosing block.
1407 //
1408 if (try_statement -> finally_clause_opt)
1409 {
1410 if (! try_statement -> finally_clause_opt -> block ->
1411 can_complete_normally)
1412 {
1413 AbruptFinallyStack().Pop();
1414 }
1415 DefinitelyAssignedVariables() -> da_set = starting_pair.da_set;
1416 DefinitelyAssignedVariables() -> du_set =
1417 starting_pair.du_set - *ReachableAssignments();
1418 DefiniteBlock(try_statement -> finally_clause_opt -> block);
1419 DefinitelyAssignedVariables() -> da_set += after_blocks;
1420 }
1421 else
1422 {
1423 DefinitelyAssignedVariables() -> da_set = after_blocks;
1424 DefinitelyAssignedVariables() -> du_set = possibly_finals_union;
1425 }
1426 *ReachableAssignments() += already_assigned;
1427 }
1428
1429
DefiniteAssertStatement(Ast * stmt)1430 void Semantic::DefiniteAssertStatement(Ast* stmt)
1431 {
1432 AstAssertStatement* assert_statement = (AstAssertStatement*) stmt;
1433
1434 //
1435 // Remember what variables were assigned beforehand.
1436 //
1437 DefinitePair before_assert(*DefinitelyAssignedVariables());
1438 DefiniteAssignmentSet* after_condition =
1439 DefiniteBooleanExpression(assert_statement -> condition,
1440 *DefinitelyAssignedVariables());
1441
1442 if (after_condition)
1443 {
1444 //
1445 // The second expression is evaluated only when the first is false
1446 // Don't modify da, but update du, as a variable is DU after the assert
1447 // iff it is DU before the assert and DU after the condition when true.
1448 //
1449 *DefinitelyAssignedVariables() = after_condition -> false_pair;
1450 before_assert.du_set *= after_condition -> true_pair.du_set;
1451 }
1452 else before_assert.du_set *= DefinitelyAssignedVariables() -> du_set;
1453
1454 if (assert_statement -> message_opt)
1455 DefiniteExpression(assert_statement -> message_opt,
1456 *DefinitelyAssignedVariables());
1457
1458 //
1459 // Restore definitely assigned variables to what they were before,
1460 // since asserts may be disabled
1461 //
1462 *DefinitelyAssignedVariables() = before_assert;
1463
1464 // harmless if NULL
1465 delete after_condition;
1466 }
1467
1468
1469 //
1470 // Called only from DefiniteConstructorBody, for this() calls.
1471 //
DefiniteThisCall(AstThisCall * this_call)1472 void Semantic::DefiniteThisCall(AstThisCall* this_call)
1473 {
1474 for (unsigned i = 0; i < this_call -> arguments -> NumArguments(); i++)
1475 DefiniteExpression(this_call -> arguments -> Argument(i),
1476 *DefinitelyAssignedVariables());
1477 }
1478
1479
1480 //
1481 // Called only from DefiniteConstructorBody, for super() calls.
1482 //
DefiniteSuperCall(AstSuperCall * super_call)1483 void Semantic::DefiniteSuperCall(AstSuperCall* super_call)
1484 {
1485 if (super_call -> base_opt)
1486 DefiniteExpression(super_call -> base_opt,
1487 *DefinitelyAssignedVariables());
1488 for (unsigned i = 0; i < super_call -> arguments -> NumArguments(); i++)
1489 DefiniteExpression(super_call -> arguments -> Argument(i),
1490 *DefinitelyAssignedVariables());
1491 }
1492
1493
DefiniteMethodBody(AstMethodDeclaration * method_declaration)1494 void Semantic::DefiniteMethodBody(AstMethodDeclaration* method_declaration)
1495 {
1496 unsigned i;
1497 assert(FinalFields());
1498 AstBlock* block_body = method_declaration -> method_body_opt;
1499 if (! block_body)
1500 return;
1501
1502 #ifdef DUMP
1503 if (control.option.g & JikesOption::VARS)
1504 Coutput << "(9) Processing method \""
1505 << method_declaration -> method_symbol -> Name() << "\" in "
1506 << ThisType() -> ContainingPackageName()
1507 << "/" << ThisType() -> ExternalName() << endl;
1508 #endif // DUMP
1509 int size = block_body -> block_symbol -> max_variable_index +
1510 FinalFields() -> Length();
1511 Universe() -> Resize(size, BitSet::UNIVERSE);
1512 int stack_size = method_declaration -> method_symbol -> max_block_depth;
1513 DefiniteBlocks() = new DefiniteBlockStack(stack_size, size);
1514 DefinitelyAssignedVariables() -> Resize(size);
1515 BlankFinals() -> Resize(size, BitSet::EMPTY);
1516 ReachableAssignments() -> Resize(size, BitSet::EMPTY);
1517
1518 DefiniteBlocks() -> Push(block_body);
1519
1520 AstMethodDeclarator* method_declarator =
1521 method_declaration -> method_declarator;
1522 for (i = 0; i < method_declarator -> NumFormalParameters(); i++)
1523 {
1524 AstVariableDeclarator* formal_declarator =
1525 method_declarator -> FormalParameter(i) -> formal_declarator;
1526 DefinitelyAssignedVariables() ->
1527 AssignElement(formal_declarator -> symbol ->
1528 LocalVariableIndex(this));
1529 #ifdef DUMP
1530 if (control.option.g & JikesOption::VARS)
1531 {
1532 VariableSymbol* variable = formal_declarator -> symbol;
1533 Coutput << "(10) Variable \"" << variable -> Name() << " #"
1534 << variable -> LocalVariableIndex(this)
1535 << "\" is defined at line "
1536 << lex_stream -> Line(formal_declarator -> LeftToken())
1537 << endl;
1538 }
1539 #endif // DUMP
1540 }
1541
1542 DefiniteBlockStatements(block_body);
1543
1544 #ifdef DUMP
1545 if ((control.option.g & JikesOption::VARS) &&
1546 block_body -> NumLocallyDefinedVariables() > 0)
1547 {
1548 Coutput << "(11) At Line "
1549 << lex_stream -> Line(block_body -> RightToken())
1550 << " the range for the following variables end:" << endl
1551 << endl;
1552 for (i = 0; i < block_body -> NumLocallyDefinedVariables(); i++)
1553 Coutput << " \""
1554 << block_body -> LocallyDefinedVariable(i) -> Name()
1555 << "\"" << endl;
1556 }
1557 #endif // DUMP
1558 DefiniteBlocks() -> Pop();
1559
1560 delete DefiniteBlocks();
1561 DefiniteBlocks() = NULL;
1562 size = FinalFields() -> Length();
1563 Universe() -> Resize(size);
1564 DefinitelyAssignedVariables() -> Resize(size);
1565 BlankFinals() -> Resize(size);
1566 ReachableAssignments() -> Resize(size);
1567 }
1568
1569
DefiniteConstructorBody(AstConstructorDeclaration * constructor_declaration)1570 void Semantic::DefiniteConstructorBody(AstConstructorDeclaration* constructor_declaration)
1571 {
1572 unsigned i;
1573 assert(FinalFields());
1574 #ifdef DUMP
1575 if (control.option.g & JikesOption::VARS)
1576 Coutput << "(12) Processing constructor \""
1577 << constructor_declaration -> constructor_symbol -> Name()
1578 << "\" in "
1579 << ThisType() -> ContainingPackageName() << "/"
1580 << ThisType() -> ExternalName() << endl;
1581 #endif // DUMP
1582 AstMethodBody* block_body = constructor_declaration -> constructor_body;
1583
1584 int size = block_body -> block_symbol -> max_variable_index +
1585 FinalFields() -> Length();
1586 Universe() -> Resize(size, BitSet::UNIVERSE);
1587 int stack_size =
1588 constructor_declaration -> constructor_symbol -> max_block_depth;
1589 DefiniteBlocks() = new DefiniteBlockStack(stack_size, size);
1590 DefinitelyAssignedVariables() -> Resize(size);
1591 BlankFinals() -> Resize(size, BitSet::EMPTY);
1592 ReachableAssignments() -> Resize(size, BitSet::EMPTY);
1593
1594 DefiniteBlocks() -> Push(block_body);
1595
1596 AstMethodDeclarator* constructor_declarator =
1597 constructor_declaration -> constructor_declarator;
1598 for (i = 0; i < constructor_declarator -> NumFormalParameters(); i++)
1599 {
1600 AstVariableDeclarator* formal_declarator =
1601 constructor_declarator -> FormalParameter(i) -> formal_declarator;
1602 DefinitelyAssignedVariables() ->
1603 AddElement(formal_declarator -> symbol -> LocalVariableIndex(this));
1604 #ifdef DUMP
1605 if (control.option.g & JikesOption::VARS)
1606 {
1607 VariableSymbol* variable = formal_declarator -> symbol;
1608 Coutput << "(13) Variable \"" << variable -> Name() << " #"
1609 << variable -> LocalVariableIndex(this)
1610 << "\" is defined at line "
1611 << lex_stream -> Line(formal_declarator -> LeftToken())
1612 << endl;
1613 }
1614 #endif // DUMP
1615 }
1616
1617 if (block_body -> explicit_constructor_opt)
1618 {
1619 if (block_body -> explicit_constructor_opt -> ThisCallCast())
1620 DefiniteThisCall((AstThisCall*) block_body ->
1621 explicit_constructor_opt);
1622 else DefiniteSuperCall((AstSuperCall*) block_body ->
1623 explicit_constructor_opt);
1624 }
1625 DefiniteBlockStatements(block_body);
1626
1627 #ifdef DUMP
1628 if ((control.option.g & JikesOption::VARS) &&
1629 block_body -> NumLocallyDefinedVariables() > 0)
1630 {
1631 Coutput << "(14) At Line "
1632 << lex_stream -> Line(block_body -> RightToken())
1633 << " the range for the following variables end:" << endl
1634 << endl;
1635 for (unsigned j = 0; j < block_body -> NumLocallyDefinedVariables(); j++)
1636 Coutput << " \""
1637 << block_body -> LocallyDefinedVariable(j) -> Name()
1638 << "\"" << endl;
1639 }
1640 #endif // DUMP
1641 //
1642 // Compute the set of finals that has definitely been assigned in this
1643 // constructor.
1644 //
1645 *DefinitelyAssignedVariables() *= DefiniteBlocks() -> ReturnPair();
1646 DefiniteBlocks() -> Pop();
1647
1648 delete DefiniteBlocks();
1649 DefiniteBlocks() = NULL;
1650 size = FinalFields() -> Length();
1651 Universe() -> Resize(size);
1652 DefinitelyAssignedVariables() -> Resize(size);
1653 BlankFinals() -> Resize(size);
1654 ReachableAssignments() -> Resize(size);
1655 }
1656
1657
DefiniteBlockInitializer(AstBlock * block_body,int stack_size)1658 void Semantic::DefiniteBlockInitializer(AstBlock* block_body, int stack_size)
1659 {
1660 assert(FinalFields());
1661 #ifdef DUMP
1662 if (control.option.g & JikesOption::VARS)
1663 Coutput << "(15) Processing Initializer block in "
1664 << ThisType() -> ContainingPackageName() << "/"
1665 << ThisType() -> ExternalName() << endl;
1666 #endif // DUMP
1667 int size = block_body -> block_symbol -> max_variable_index +
1668 FinalFields() -> Length();
1669 Universe() -> Resize(size, BitSet::UNIVERSE);
1670 DefiniteBlocks() = new DefiniteBlockStack(stack_size, size);
1671 DefinitelyAssignedVariables() -> Resize(size);
1672 BlankFinals() -> Resize(size, BitSet::EMPTY);
1673 ReachableAssignments() -> Resize(size, BitSet::EMPTY);
1674
1675 DefiniteBlocks() -> Push(block_body);
1676 DefiniteBlockStatements(block_body);
1677
1678 #ifdef DUMP
1679 if ((control.option.g & JikesOption::VARS) &&
1680 block_body -> NumLocallyDefinedVariables() > 0)
1681 {
1682 Coutput << "(16) At Line "
1683 << lex_stream -> Line(block_body -> RightToken())
1684 << " the range for the following variables end:" << endl
1685 << endl;
1686 for (i = 0; i < block_body -> NumLocallyDefinedVariables(); i++)
1687 Coutput << " \""
1688 << block_body -> LocallyDefinedVariable(i) -> Name()
1689 << "\"" << endl;
1690 }
1691 #endif // DUMP
1692 DefiniteBlocks() -> Pop();
1693
1694 delete DefiniteBlocks();
1695 DefiniteBlocks() = NULL;
1696 size = FinalFields() -> Length();
1697 Universe() -> Resize(size);
1698 DefinitelyAssignedVariables() -> Resize(size);
1699 BlankFinals() -> Resize(size);
1700 ReachableAssignments() -> Resize(size);
1701 }
1702
1703
DefiniteFieldInitializer(AstVariableDeclarator * variable_declarator)1704 void Semantic::DefiniteFieldInitializer(AstVariableDeclarator* variable_declarator)
1705 {
1706 assert(FinalFields());
1707
1708 DefiniteVariableInitializer(variable_declarator);
1709 if (variable_declarator -> symbol -> ACC_FINAL())
1710 {
1711 DefinitelyAssignedVariables() ->
1712 AssignElement(variable_declarator -> symbol ->
1713 LocalVariableIndex());
1714 }
1715 }
1716
1717
DefiniteSetup()1718 void Semantic::DefiniteSetup()
1719 {
1720 //
1721 // Compute the set of final variables (all fields in an interface are
1722 // final) in this type. Then process all initializers.
1723 //
1724 assert(! FinalFields());
1725 TypeSymbol* this_type = ThisType();
1726 FinalFields() =
1727 new Tuple<VariableSymbol*> (this_type -> NumVariableSymbols());
1728 int size = 0;
1729 for (unsigned i = 0; i < this_type -> NumVariableSymbols(); i++)
1730 {
1731 VariableSymbol* variable_symbol = this_type -> VariableSym(i);
1732 if (variable_symbol -> ACC_FINAL() &&
1733 ! variable_symbol -> ACC_SYNTHETIC())
1734 {
1735 variable_symbol -> SetLocalVariableIndex(size++);
1736 FinalFields() -> Next() = variable_symbol;
1737 }
1738 }
1739 Universe() = new DefinitePair(size, BitSet::UNIVERSE);
1740 DefiniteFinalAssignments() = new DefiniteFinalAssignmentStack();
1741 DefinitelyAssignedVariables() = new DefinitePair(size);
1742 BlankFinals() = new BitSet(size, BitSet::EMPTY);
1743 ReachableAssignments() = new BitSet(size, BitSet::EMPTY);
1744 for (int j = 0; j < size; j++)
1745 {
1746 VariableSymbol* final_var = (*FinalFields())[j];
1747 if (! final_var -> declarator -> variable_initializer_opt)
1748 BlankFinals() -> AddElement(j);
1749 }
1750 }
1751
1752
DefiniteCleanUp()1753 void Semantic::DefiniteCleanUp()
1754 {
1755 assert (FinalFields());
1756 delete FinalFields();
1757 FinalFields() = NULL;
1758 delete Universe();
1759 delete DefinitelyAssignedVariables();
1760 delete DefiniteFinalAssignments();
1761 delete BlankFinals();
1762 delete ReachableAssignments();
1763 }
1764
1765 #ifdef HAVE_JIKES_NAMESPACE
1766 } // Close namespace Jikes block
1767 #endif
1768