1 // $Id: expr.cpp,v 1.198 2004/05/17 21:55:56 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
10 #include "platform.h"
11 #include "double.h"
12 #include "parser.h"
13 #include "semantic.h"
14 #include "control.h"
15 #include "table.h"
16 #include "tuple.h"
17 #include "spell.h"
18 #include "option.h"
19 #include "stream.h"
20
21 #ifdef HAVE_JIKES_NAMESPACE
22 namespace Jikes { // Open namespace Jikes block
23 #endif
24
25 template <typename T> inline void ExtremaForType(T& min, T& max);
26
ExtremaForType(i4 & min,i4 & max)27 template <> inline void ExtremaForType(i4& min, i4& max)
28 {
29 min = Int::MIN_INT();
30 max = Int::MAX_INT();
31 }
32
ExtremaForType(LongInt & min,LongInt & max)33 template <> inline void ExtremaForType(LongInt& min, LongInt& max)
34 {
35 min = LongInt::MIN_LONG();
36 max = LongInt::MAX_LONG();
37 }
38
ReportOverflow(Semantic * semantic,AstExpression * expr,bool safe)39 inline void ReportOverflow(Semantic* semantic, AstExpression* expr, bool safe)
40 {
41 if (! safe)
42 {
43 semantic -> ReportSemError(SemanticError::CONSTANT_OVERFLOW, expr,
44 expr -> Type() -> Name());
45 }
46 }
47
48
49 template <typename T>
CheckIntegerNegation(Semantic * semantic,AstExpression * expr,const T & x)50 static void CheckIntegerNegation(Semantic* semantic, AstExpression* expr,
51 const T& x)
52 {
53 T min, max;
54 ExtremaForType(min, max);
55 ReportOverflow(semantic, expr, (x != min));
56 }
57
58
59 template <typename T>
CheckIntegerAddition(Semantic * semantic,AstExpression * expr,const T & x,const T & y)60 inline void CheckIntegerAddition(Semantic* semantic, AstExpression* expr,
61 const T& x, const T& y)
62 {
63 const T zero = T(0);
64 T min, max;
65 ExtremaForType(min, max);
66 bool safe = x == zero ||
67 y == zero ||
68 x < zero && y < zero && x >= (min - y) ||
69 x < zero && y > zero ||
70 x > zero && y < zero ||
71 x > zero && y > zero && x <= (max - y);
72 ReportOverflow(semantic, expr, safe);
73 }
74
75
76 template <typename T>
CheckIntegerSubtraction(Semantic * semantic,AstExpression * expr,const T & x,const T & y)77 static void CheckIntegerSubtraction(Semantic* semantic, AstExpression* expr,
78 const T& x, const T& y)
79 {
80 CheckIntegerAddition(semantic, expr, x, T(-y));
81 }
82
83
84 template <typename T>
CheckIntegerMultiplication(Semantic * semantic,AstExpression * expr,const T & x,const T & y)85 static void CheckIntegerMultiplication(Semantic* semantic, AstExpression* expr,
86 const T& x, const T& y)
87 {
88 const T zero = T(0);
89 const T one = T(1);
90 const T minus_one = T(-1);
91 T min, max;
92 ExtremaForType(min, max);
93 bool safe = x > minus_one && x <= one ||
94 y > minus_one && y <= one ||
95 x < zero && y < zero && T(-x) <= max/-y ||
96 x < zero && y > zero && x >= min/y ||
97 x > zero && y < zero && y >= min/x ||
98 x > zero && y > zero && x <= max/y;
99 ReportOverflow(semantic, expr, safe);
100 }
101
102
103 template <typename T>
CheckIntegerDivision(Semantic * semantic,AstExpression * expr,const T & x,const T & y)104 static void CheckIntegerDivision(Semantic* semantic, AstExpression* expr,
105 const T& x, const T& y)
106 {
107 const T zero = T(0);
108 const T minus_one = T(-1);
109 T min, max;
110 ExtremaForType(min, max);
111 bool safe = (y != zero) && !(x == min && y == minus_one);
112 ReportOverflow(semantic, expr, safe);
113 }
114
115
IsIntValueRepresentableInType(AstExpression * expr,const TypeSymbol * type)116 bool Semantic::IsIntValueRepresentableInType(AstExpression* expr,
117 const TypeSymbol* type)
118 {
119 if (! expr -> IsConstant() ||
120 ! control.IsSimpleIntegerValueType(expr -> Type()))
121 {
122 return false;
123 }
124
125 IntLiteralValue* literal = DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
126 return type == control.int_type || type == control.no_type ||
127 (type == control.char_type && (literal -> value >= 0) &&
128 (literal -> value <= 65535)) ||
129 (type == control.byte_type && (literal -> value >= -128) &&
130 (literal -> value <= 127)) ||
131 (type == control.short_type && (literal -> value >= -32768) &&
132 (literal -> value <= 32767));
133 }
134
135
IsConstantTrue(AstExpression * expr)136 bool Semantic::IsConstantTrue(AstExpression* expr)
137 {
138 return expr -> IsConstant() && expr -> Type() == control.boolean_type &&
139 DYNAMIC_CAST<IntLiteralValue*> (expr -> value) -> value;
140 }
141
142
IsConstantFalse(AstExpression * expr)143 bool Semantic::IsConstantFalse(AstExpression* expr)
144 {
145 return expr -> IsConstant() && expr -> Type() == control.boolean_type &&
146 ! DYNAMIC_CAST<IntLiteralValue*> (expr -> value) -> value;
147 }
148
149
150 //
151 // Returns true if source_method is more specific than target_method, which
152 // is defined as the type that declared the method, as well as all method
153 // parameter types, being equal or more specific in the source_method.
154 //
MoreSpecific(MethodSymbol * source_method,MethodSymbol * target_method)155 inline bool Semantic::MoreSpecific(MethodSymbol* source_method,
156 MethodSymbol* target_method)
157 {
158 //
159 // Sun bug 4761586: the declaration type is no longer considered when
160 // looking for the most specific method.
161 //
162 // if (! CanMethodInvocationConvert(target_method -> containing_type,
163 // source_method -> containing_type))
164 // {
165 // return false;
166 // }
167 for (int k = target_method -> NumFormalParameters() - 1; k >= 0; k--)
168 {
169 if (! CanMethodInvocationConvert(target_method -> FormalParameter(k) ->
170 Type(),
171 source_method -> FormalParameter(k) ->
172 Type()))
173 {
174 return false;
175 }
176 }
177 return true;
178 }
179
180
181 //
182 // Returns true if a method is more specific than the current set of maximally
183 // specific methods.
184 //
MoreSpecific(MethodSymbol * method,Tuple<MethodSymbol * > & maximally_specific_method)185 inline bool Semantic::MoreSpecific(MethodSymbol* method,
186 Tuple<MethodSymbol*>& maximally_specific_method)
187 {
188 for (unsigned i = 0; i < maximally_specific_method.Length(); i++)
189 {
190 if (! MoreSpecific(method, maximally_specific_method[i]))
191 return false;
192 }
193 return true;
194 }
195
196
197 //
198 // Returns true if no method in the current set of maximally specific methods
199 // is more specific than the given method, meaning that the given method should
200 // be added to the set.
201 //
NoMethodMoreSpecific(Tuple<MethodSymbol * > & maximally_specific_method,MethodSymbol * method)202 inline bool Semantic::NoMethodMoreSpecific(Tuple<MethodSymbol*>& maximally_specific_method,
203 MethodSymbol* method)
204 {
205 for (unsigned i = 0; i < maximally_specific_method.Length(); i++)
206 {
207 if (MoreSpecific(maximally_specific_method[i], method))
208 return false;
209 }
210 return true;
211 }
212
213
214 //
215 // Returns true if a method is more specific than the current set of maximally
216 // specific methods.
217 //
MoreSpecific(MethodSymbol * method,Tuple<MethodShadowSymbol * > & maximally_specific_method)218 inline bool Semantic::MoreSpecific(MethodSymbol* method,
219 Tuple<MethodShadowSymbol*>& maximally_specific_method)
220 {
221 for (unsigned i = 0; i < maximally_specific_method.Length(); i++)
222 {
223 if (! MoreSpecific(method,
224 maximally_specific_method[i] -> method_symbol))
225 return false;
226 }
227 return true;
228 }
229
230
231 //
232 // Returns true if no method in the current set of maximally specific methods
233 // is more specific than the given method, meaning that the given method should
234 // be added to the set.
235 //
NoMethodMoreSpecific(Tuple<MethodShadowSymbol * > & maximally_specific_method,MethodSymbol * method)236 inline bool Semantic::NoMethodMoreSpecific(Tuple<MethodShadowSymbol*>& maximally_specific_method,
237 MethodSymbol* method)
238 {
239 for (unsigned i = 0; i < maximally_specific_method.Length(); i++)
240 {
241 if (MoreSpecific(maximally_specific_method[i] -> method_symbol, method))
242 return false;
243 }
244 return true;
245 }
246
247
248 //
249 // Creates a new wchar_t[] containing the type of the method or constructor
250 // overload for printing in Report*NotFound. Caller is responsible for
251 // calling delete[] on the result.
252 //
Header(const NameSymbol * name,AstArguments * args)253 wchar_t* Semantic::Header(const NameSymbol* name, AstArguments* args)
254 {
255 unsigned num_arguments = args -> NumArguments();
256 int length = name -> NameLength();
257 for (unsigned i = 0; i < num_arguments; i++)
258 {
259 TypeSymbol* arg_type = args -> Argument(i) -> Type();
260 // '.' after package_name; ',' and ' ' to separate this argument
261 // from the next one
262 length += arg_type -> ContainingPackage() -> PackageNameLength() +
263 arg_type -> ExternalNameLength() + 3;
264 }
265
266 // +1 for (, +1 for ), +1 for '\0'
267 wchar_t* header = new wchar_t[length + 3];
268 wchar_t* s = header;
269 const wchar_t* s2;
270
271 for (s2 = name -> Name(); *s2; s2++)
272 *s++ = *s2;
273 *s++ = U_LEFT_PARENTHESIS;
274 if (num_arguments > 0)
275 {
276 for (unsigned i = 0; i < num_arguments; i++)
277 {
278 TypeSymbol* arg_type = args -> Argument(i) -> Type();
279
280 PackageSymbol* package = arg_type -> ContainingPackage();
281 wchar_t* package_name = package -> PackageName();
282 if (package -> PackageNameLength() > 0 &&
283 package_name[0] != U_DOT)
284 {
285 while (*package_name)
286 {
287 *s++ = (*package_name == U_SLASH ? (wchar_t) U_DOT
288 : *package_name);
289 package_name++;
290 }
291 *s++ = U_DOT;
292 }
293
294 for (s2 = arg_type -> ExternalName(); *s2; s2++)
295 *s++ = (*s2 == U_DOLLAR ? (wchar_t) U_DOT : *s2);
296 *s++ = U_COMMA;
297 *s++ = U_SPACE;
298 }
299
300 s -= 2; // remove the last ',' and ' '
301 }
302 *s++ = U_RIGHT_PARENTHESIS;
303 *s = U_NULL;
304 return header;
305 }
306
307
308 //
309 // Called when no accessible method was found. This checks in order: a hidden
310 // exact match in an enclosing class (for simple names only); an accessible
311 // method of the same name but different parameter types, favoring methods with
312 // the same parameter count; an accessible field by the same name (for no-arg
313 // call only); an inaccessible method in a superclass; a misspelled method
314 // name; a type by the same name; and finally no method was found. The
315 // parameter type should be NULL only if method_call represents a simple name.
316 //
ReportMethodNotFound(AstMethodInvocation * method_call,TypeSymbol * type)317 void Semantic::ReportMethodNotFound(AstMethodInvocation* method_call,
318 TypeSymbol* type)
319 {
320 AstExpression* base = method_call -> base_opt;
321 SemanticEnvironment* env;
322 SemanticEnvironment* top_env = state_stack.Top();
323 assert((base == NULL) == (type == NULL));
324
325 TokenIndex id_token = method_call -> identifier_token;
326 NameSymbol* name_symbol = lex_stream -> NameSymbol(id_token);
327 MethodShadowSymbol* method_shadow;
328
329 //
330 // First, for simple names, search for a hidden method match in an
331 // enclosing class.
332 //
333 for (env = top_env -> previous; ! base && env; env = env -> previous)
334 {
335 Tuple<MethodShadowSymbol*> others(2);
336 SemanticEnvironment* found_other;
337 FindMethodInEnvironment(others, found_other, env, method_call);
338 if (others.Length() > 0)
339 {
340 ReportSemError(SemanticError::HIDDEN_METHOD_IN_ENCLOSING_CLASS,
341 method_call, others[0] -> method_symbol -> Header(),
342 others[0] -> method_symbol -> containing_type -> ContainingPackageName(),
343 others[0] -> method_symbol -> containing_type -> ExternalName());
344 return;
345 }
346 }
347
348 //
349 // Search for an accessible method with different arguments. Favor the
350 // earliest method found with the smallest difference in parameter count.
351 // Since the JVMS limits methods to 255 parameters, we initialize our
352 // difference detection with 255.
353 //
354 MethodSymbol* best_match = NULL;
355 for (env = top_env; env;
356 env = (base ? (SemanticEnvironment*) NULL : env -> previous))
357 {
358 if (! base)
359 type = env -> Type();
360 if (! type -> expanded_method_table)
361 ComputeMethodsClosure(type, id_token);
362 int difference = 255;
363 for (method_shadow = type -> expanded_method_table ->
364 FindMethodShadowSymbol(name_symbol);
365 method_shadow; method_shadow = method_shadow -> next_method)
366 {
367 MethodSymbol* method = method_shadow -> method_symbol;
368
369 if (! method -> IsTyped())
370 method -> ProcessMethodSignature(this, id_token);
371 if (MemberAccessCheck(type, method, base) ||
372 method_shadow -> NumConflicts() > 0)
373 {
374 int diff = method_call -> arguments -> NumArguments() -
375 method -> NumFormalParameters();
376 if (diff < 0)
377 diff = - diff;
378 if (diff < difference)
379 {
380 best_match = method;
381 difference = diff;
382 }
383 }
384 }
385 if (best_match)
386 {
387 wchar_t* header = Header(name_symbol, method_call -> arguments);
388 ReportSemError(SemanticError::METHOD_OVERLOAD_NOT_FOUND,
389 method_call, header,
390 best_match -> containing_type -> ContainingPackageName(),
391 best_match -> containing_type -> ExternalName(),
392 best_match -> Header());
393 delete [] header;
394 return;
395 }
396 }
397
398 //
399 // For a no-arg method, search for an accessible field of the same name.
400 //
401 if (method_call -> arguments -> NumArguments() == 0)
402 {
403 for (env = top_env; env;
404 env = (base ? (SemanticEnvironment*) NULL : env -> previous))
405 {
406 if (! base)
407 type = env -> Type();
408 if (! type -> expanded_field_table)
409 ComputeFieldsClosure(type, id_token);
410 VariableShadowSymbol* variable_shadow = type ->
411 expanded_field_table -> FindVariableShadowSymbol(name_symbol);
412 if (variable_shadow)
413 {
414 VariableSymbol* variable = variable_shadow -> variable_symbol;
415 if (MemberAccessCheck(type, variable))
416 {
417 TypeSymbol* enclosing_type =
418 variable -> owner -> TypeCast();
419 assert(enclosing_type);
420 ReportSemError(SemanticError::FIELD_NOT_METHOD,
421 method_call, variable -> Name(),
422 enclosing_type -> ContainingPackageName(),
423 enclosing_type -> ExternalName());
424 return;
425 }
426 }
427 }
428 }
429
430 //
431 // Check if the method is inaccessible.
432 //
433 for (TypeSymbol* super_type = type;
434 super_type; super_type = super_type -> super)
435 {
436 for (method_shadow = super_type -> expanded_method_table ->
437 FindMethodShadowSymbol(name_symbol);
438 method_shadow; method_shadow = method_shadow -> next_method)
439 {
440 MethodSymbol* method = method_shadow -> method_symbol;
441 if (! method -> IsTyped())
442 method -> ProcessMethodSignature(this, id_token);
443
444 if (method_call -> arguments -> NumArguments() ==
445 method -> NumFormalParameters())
446 {
447 unsigned i;
448 for (i = 0;
449 i < method_call -> arguments -> NumArguments(); i++)
450 {
451 AstExpression* expr =
452 method_call -> arguments -> Argument(i);
453 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(),
454 expr -> Type()))
455 {
456 break;
457 }
458 }
459 if (i == method_call -> arguments -> NumArguments())
460 {
461 //
462 // JLS 9.2: Interfaces do not have protected members,
463 // even though jikes treats interfaces as subtypes of
464 // Object.
465 //
466 if (base && method -> ACC_PROTECTED() &&
467 base -> Type() -> ACC_INTERFACE())
468 {
469 assert(method -> containing_type == control.Object());
470 ReportSemError(SemanticError::PROTECTED_INTERFACE_METHOD_NOT_ACCESSIBLE,
471 method_call, method -> Header());
472 }
473 //
474 // A protected instance method in the superclass is
475 // inaccessible if the base expression is the wrong type.
476 //
477 else if (method -> ACC_PROTECTED() &&
478 ! method -> ACC_STATIC() &&
479 ThisType() -> HasProtectedAccessTo(method -> containing_type))
480 {
481 assert(base);
482 ReportSemError(SemanticError::PROTECTED_INSTANCE_METHOD_NOT_ACCESSIBLE,
483 method_call, method -> Header(),
484 method -> containing_type -> ContainingPackageName(),
485 method -> containing_type -> ExternalName(),
486 ThisType() -> ContainingPackageName(),
487 ThisType() -> ExternalName());
488 }
489 else
490 {
491 ReportSemError(SemanticError::METHOD_NOT_ACCESSIBLE,
492 method_call, method -> Header(),
493 method -> containing_type -> ContainingPackageName(),
494 method -> containing_type -> ExternalName(),
495 method -> AccessString());
496 }
497 return;
498 }
499 }
500 }
501 }
502
503 //
504 // Search for a misspelled method name.
505 //
506 for (env = top_env; env;
507 env = (base ? (SemanticEnvironment*) NULL : env -> previous))
508 {
509 if (! base)
510 type = env -> Type();
511 best_match = FindMisspelledMethodName(type, method_call, name_symbol);
512 if (best_match)
513 {
514 ReportSemError(SemanticError::METHOD_NAME_MISSPELLED,
515 method_call, name_symbol -> Name(),
516 type -> ContainingPackageName(),
517 type -> ExternalName(), best_match -> Name());
518 return;
519 }
520 }
521 //
522 // Search for a type of the same name.
523 //
524 if (FindType(id_token))
525 ReportSemError(SemanticError::TYPE_NOT_METHOD, method_call,
526 name_symbol -> Name());
527 //
528 // Give up. We didn't find it.
529 //
530 else
531 {
532 if (! base)
533 type = ThisType();
534 wchar_t* header = Header(name_symbol, method_call -> arguments);
535 ReportSemError(SemanticError::METHOD_NOT_FOUND, method_call,
536 header, type -> ContainingPackageName(),
537 type -> ExternalName());
538 delete [] header;
539 }
540 }
541
542
543 //
544 // Called when no accessible constructor was found. This checks in order: an
545 // accessible method of the same name but different parameters, favoring
546 // constructors with the same parameter count; an inaccessible constructor;
547 // an accessible method with the same name as the type; and finally no
548 // constructor was found.
549 //
ReportConstructorNotFound(Ast * ast,TypeSymbol * type)550 void Semantic::ReportConstructorNotFound(Ast* ast, TypeSymbol* type)
551 {
552 AstClassCreationExpression* class_creation =
553 ast -> ClassCreationExpressionCast();
554 AstSuperCall* super_call = ast -> SuperCallCast();
555 AstArguments* args;
556 TokenIndex left_tok;
557
558 if (class_creation)
559 {
560 args = class_creation -> arguments;
561 left_tok = class_creation -> new_token;
562 if (class_creation -> class_body_opt)
563 class_creation = NULL;
564 }
565 else if (super_call)
566 {
567 args = super_call -> arguments;
568 left_tok = super_call -> super_token;
569 }
570 else
571 {
572 AstThisCall* this_call = ast -> ThisCallCast();
573 assert(this_call);
574 args = this_call -> arguments;
575 left_tok = this_call -> this_token;
576 }
577 unsigned num_arguments = args -> NumArguments();
578 TokenIndex right_tok = args -> right_parenthesis_token;
579
580 //
581 // Search for an accessible constructor with different arguments. Favor
582 // the earliest ctor found with the smallest difference in parameter count.
583 // Since the JVMS limits methods to 255 parameters, we initialize our
584 // difference detection with 255.
585 //
586 MethodSymbol* best_match = NULL;
587 MethodSymbol* ctor;
588 int difference = 255;
589 for (ctor = type -> FindMethodSymbol(control.init_name_symbol);
590 ctor; ctor = ctor -> next_method)
591 {
592 if (ConstructorAccessCheck(ctor, ! class_creation))
593 {
594 int diff = num_arguments - ctor -> NumFormalParameters();
595 if (diff < 0)
596 diff = - diff;
597 if (diff < difference)
598 {
599 best_match = ctor;
600 difference = diff;
601 }
602 }
603 }
604 if (best_match)
605 {
606 wchar_t* header = Header(type -> Identity(), args);
607 ReportSemError(SemanticError::CONSTRUCTOR_OVERLOAD_NOT_FOUND, ast,
608 header, type -> ContainingPackageName(),
609 type -> ExternalName(), best_match -> Header());
610 delete [] header;
611 return;
612 }
613
614 //
615 // Check if the constructor is inaccessible.
616 //
617 for (ctor = type -> FindMethodSymbol(control.init_name_symbol);
618 ctor; ctor = ctor -> next_method)
619 {
620 if (num_arguments == ctor -> NumFormalParameters())
621 {
622 unsigned i;
623 for (i = 0; i < num_arguments; i++)
624 {
625 AstExpression* expr = args -> Argument(i);
626 if (! CanMethodInvocationConvert(ctor -> FormalParameter(i) -> Type(),
627 expr -> Type()))
628 {
629 break;
630 }
631 }
632 if (i == num_arguments) // found a match?
633 {
634 ReportSemError(SemanticError::CONSTRUCTOR_NOT_ACCESSIBLE, ast,
635 ctor -> Header(),
636 type -> ContainingPackageName(),
637 type -> ExternalName(), ctor -> AccessString());
638 return;
639 }
640 }
641 }
642
643 //
644 // Search for an accessible method with the same name as the type.
645 //
646 MethodSymbol* method;
647 for (method = type -> FindMethodSymbol(type -> Identity());
648 method; method = method -> next_method)
649 {
650 if (! method -> IsTyped())
651 method -> ProcessMethodSignature(this, right_tok);
652
653 if (num_arguments == method -> NumFormalParameters())
654 {
655 unsigned i;
656 for (i = 0; i < num_arguments; i++)
657 {
658 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(),
659 args -> Argument(i) -> Type()))
660 {
661 break;
662 }
663 }
664 if (i == num_arguments)
665 break;
666 }
667 }
668 if (method)
669 {
670 if (method -> declaration)
671 {
672 AstMethodDeclaration* method_declaration =
673 (AstMethodDeclaration*) method -> declaration;
674 FileLocation loc((method -> containing_type ->
675 semantic_environment -> sem -> lex_stream),
676 (method_declaration -> method_declarator ->
677 identifier_token));
678 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
679 left_tok, right_tok, type -> Name(),
680 loc.location);
681 }
682 else
683 {
684 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
685 left_tok, right_tok, type -> Name(),
686 method -> containing_type -> file_location -> location);
687 }
688 return;
689 }
690
691 //
692 // Give up. We didn't find it.
693 //
694 wchar_t* header = Header(type -> Identity(), args);
695 ReportSemError(SemanticError::CONSTRUCTOR_NOT_FOUND, ast, header,
696 type -> ContainingPackageName(), type -> ExternalName());
697 delete [] header;
698 }
699
700
FindConstructor(TypeSymbol * containing_type,Ast * ast,TokenIndex left_tok,TokenIndex right_tok)701 MethodSymbol* Semantic::FindConstructor(TypeSymbol* containing_type, Ast* ast,
702 TokenIndex left_tok,
703 TokenIndex right_tok)
704 {
705 if (containing_type == control.no_type)
706 return NULL;
707
708 //
709 // If this type is anonymous, we have just generated the constructor,
710 // so we know it is the right one.
711 //
712 if (containing_type -> Anonymous())
713 {
714 return containing_type -> declaration -> default_constructor ->
715 constructor_symbol;
716 }
717
718 AstArguments* args;
719 Tuple<MethodSymbol*> constructor_set(2); // Stores constructor overloads.
720
721 AstClassCreationExpression* class_creation =
722 ast -> ClassCreationExpressionCast();
723 AstSuperCall* super_call = ast -> SuperCallCast();
724
725 if (class_creation)
726 {
727 args = class_creation -> arguments;
728 if (class_creation -> class_body_opt)
729 class_creation = NULL;
730 }
731 else if (super_call)
732 args = super_call -> arguments;
733 else
734 {
735 AstThisCall* this_call = ast -> ThisCallCast();
736 assert(this_call);
737 args = this_call -> arguments;
738 }
739
740 unsigned num_arguments = args -> NumArguments();
741 assert(containing_type -> ConstructorMembersProcessed());
742 MethodSymbol* ctor;
743 for (ctor = containing_type -> FindMethodSymbol(control.init_name_symbol);
744 ctor; ctor = ctor -> next_method)
745 {
746 if (! ctor -> IsTyped())
747 ctor -> ProcessMethodSignature(this, right_tok);
748
749 if (num_arguments == ctor -> NumFormalParameters() &&
750 ConstructorAccessCheck(ctor, ! class_creation))
751 {
752 unsigned i;
753 for (i = 0; i < num_arguments; i++)
754 {
755 if (! CanMethodInvocationConvert(ctor -> FormalParameter(i) -> Type(),
756 args -> Argument(i) -> Type()))
757 {
758 break;
759 }
760 }
761 if (i == num_arguments)
762 {
763 if (MoreSpecific(ctor, constructor_set))
764 {
765 constructor_set.Reset();
766 constructor_set.Next() = ctor;
767 }
768 else if (NoMethodMoreSpecific(constructor_set, ctor))
769 constructor_set.Next() = ctor;
770 }
771 }
772 }
773
774 if (constructor_set.Length() == 0)
775 {
776 if (! containing_type -> Bad() || NumErrors() == 0)
777 ReportConstructorNotFound(ast, containing_type);
778 return NULL;
779 }
780 if (constructor_set.Length() > 1)
781 {
782 ReportSemError(SemanticError::AMBIGUOUS_CONSTRUCTOR_INVOCATION,
783 left_tok, right_tok, containing_type -> Name(),
784 constructor_set[0] -> Header(),
785 constructor_set[1] -> Header());
786 }
787
788 ctor = constructor_set[0];
789 if (ctor -> ACC_SYNTHETIC())
790 {
791 ReportSemError(SemanticError::SYNTHETIC_CONSTRUCTOR_INVOCATION,
792 left_tok, right_tok, ctor -> Header(),
793 containing_type -> ContainingPackageName(),
794 containing_type -> ExternalName());
795 }
796
797 //
798 // If this constructor came from a class file, make sure that its throws
799 // clause has been processed.
800 //
801 ctor -> ProcessMethodThrows(this, right_tok);
802
803 if (control.option.deprecation && ctor -> IsDeprecated() &&
804 ! InDeprecatedContext())
805 {
806 ReportSemError(SemanticError::DEPRECATED_CONSTRUCTOR,
807 left_tok, right_tok, ctor -> Header(),
808 ctor -> containing_type -> ContainingPackageName(),
809 ctor -> containing_type -> ExternalName());
810 }
811 return ctor;
812 }
813
814
815 //
816 //
817 //
FindMisspelledVariableName(TypeSymbol * type,AstExpression * expr)818 VariableSymbol* Semantic::FindMisspelledVariableName(TypeSymbol* type,
819 AstExpression* expr)
820 {
821 AstFieldAccess* field_access = expr -> FieldAccessCast();
822 AstName* field_name = expr -> NameCast();
823 AstExpression* base =
824 field_name ? field_name -> base_opt : field_access -> base;
825 VariableSymbol* misspelled_variable = NULL;
826 int index = 0;
827 TokenIndex identifier_token = expr -> RightToken();
828 const wchar_t* name = lex_stream -> NameString(identifier_token);
829
830 for (unsigned k = 0;
831 k < type -> expanded_field_table -> symbol_pool.Length(); k++)
832 {
833 VariableShadowSymbol* variable_shadow =
834 type -> expanded_field_table -> symbol_pool[k];
835 VariableSymbol* variable = variable_shadow -> variable_symbol;
836 if (! variable -> IsTyped())
837 variable -> ProcessVariableSignature(this, identifier_token);
838 if (! MemberAccessCheck(type, variable, base))
839 variable = NULL;
840 for (unsigned i = 0;
841 ! variable && i < variable_shadow -> NumConflicts(); i++)
842 {
843 variable = variable_shadow -> Conflict(i);
844 if (! variable -> IsTyped())
845 variable -> ProcessVariableSignature(this,
846 identifier_token);
847 if (! MemberAccessCheck(type, variable, base))
848 variable = NULL;
849 }
850
851 if (variable)
852 {
853 int new_index = Spell::Index(name, variable -> Name());
854 if (new_index > index)
855 {
856 misspelled_variable = variable;
857 index = new_index;
858 }
859 }
860 }
861
862 int length = wcslen(name);
863 return (length == 3 && index >= 5) ||
864 (length == 4 && index >= 6) ||
865 (length >= 5 && index >= 7)
866 ? misspelled_variable : (VariableSymbol*) NULL;
867 }
868
869 //
870 //
871 //
FindMisspelledMethodName(TypeSymbol * type,AstMethodInvocation * method_call,NameSymbol * name_symbol)872 MethodSymbol* Semantic::FindMisspelledMethodName(TypeSymbol* type,
873 AstMethodInvocation* method_call,
874 NameSymbol* name_symbol)
875 {
876 AstExpression* base = method_call -> base_opt;
877 MethodSymbol* misspelled_method = NULL;
878 int index = 0;
879 TokenIndex identifier_token = method_call -> identifier_token;
880
881 for (unsigned k = 0;
882 k < type -> expanded_method_table -> symbol_pool.Length(); k++)
883 {
884 MethodShadowSymbol* method_shadow =
885 type -> expanded_method_table -> symbol_pool[k];
886 MethodSymbol* method = method_shadow -> method_symbol;
887
888 if (! method -> IsTyped())
889 method -> ProcessMethodSignature(this, identifier_token);
890
891 if ((method_call -> arguments -> NumArguments() ==
892 method -> NumFormalParameters()) &&
893 (MemberAccessCheck(type, method, base) ||
894 method_shadow -> NumConflicts() > 0))
895 {
896 unsigned i;
897 for (i = 0; i < method_call -> arguments -> NumArguments(); i++)
898 {
899 AstExpression* expr = method_call -> arguments -> Argument(i);
900 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(),
901 expr -> Type()))
902 {
903 break;
904 }
905 }
906 if (i == method_call -> arguments -> NumArguments())
907 {
908 int new_index = Spell::Index(name_symbol -> Name(),
909 method -> Name());
910 if (new_index > index)
911 {
912 misspelled_method = method;
913 index = new_index;
914 }
915 }
916 }
917 }
918
919 int length = name_symbol -> NameLength();
920 int num_args = method_call -> arguments -> NumArguments();
921
922 //
923 // If we have a name of length 2, accept >= 30% probality if the function
924 // takes at least one argument. If we have a name of length 3,
925 // accept >= 50% probality if the function takes at least one argument.
926 // Otherwise, if the length of the name is > 3, accept >= 60% probability.
927 //
928 return index < 3 ? (MethodSymbol*) NULL
929 : ((length == 2 && (index >= 3 || num_args > 0)) ||
930 (length == 3 && (index >= 5 || num_args > 0)) ||
931 (length > 3 && (index >= 6 || (index >= 5 && num_args > 0))))
932 ? misspelled_method : (MethodSymbol*) NULL;
933 }
934
935
936 //
937 // Search the type in question for a method. Note that name_symbol is an
938 // optional argument. If it was not passed to this function then its default
939 // value is NULL (see semantic.h) and we assume that the name to search for
940 // is the name specified in the field_access of the method_call.
941 //
FindMethodInType(TypeSymbol * type,AstMethodInvocation * method_call,NameSymbol * name_symbol)942 MethodShadowSymbol* Semantic::FindMethodInType(TypeSymbol* type,
943 AstMethodInvocation* method_call,
944 NameSymbol* name_symbol)
945 {
946 Tuple<MethodShadowSymbol*> method_set(2); // Stores method overloads.
947 AstExpression* base = method_call -> base_opt;
948 TokenIndex id_token = method_call -> identifier_token;
949 assert(base);
950 if (! name_symbol)
951 name_symbol = lex_stream -> NameSymbol(id_token);
952 if (! type -> expanded_method_table)
953 ComputeMethodsClosure(type, id_token);
954
955 //
956 // Here, we ignore any conflicts in a method declaration. If there are
957 // conflicts, they are necessarily abstract methods inherited from
958 // interfaces, so either the original method implements them all, or it
959 // is also abstract and we are free to choose which one to use.
960 //
961 for (MethodShadowSymbol* method_shadow = type -> expanded_method_table ->
962 FindMethodShadowSymbol(name_symbol);
963 method_shadow; method_shadow = method_shadow -> next_method)
964 {
965 MethodSymbol* method = method_shadow -> method_symbol;
966
967 if (! method -> IsTyped())
968 method -> ProcessMethodSignature(this, id_token);
969
970 //
971 // If there are method shadow conflicts, they are necessarily public
972 // abstract methods inherited from interfaces; and we can skip the
973 // member access check because we can always invoke the public version.
974 //
975 if ((method_call -> arguments -> NumArguments() ==
976 method -> NumFormalParameters()) &&
977 (MemberAccessCheck(type, method, base) ||
978 method_shadow -> NumConflicts() > 0))
979 {
980 unsigned i;
981 for (i = 0; i < method_call -> arguments -> NumArguments(); i++)
982 {
983 AstExpression* expr = method_call -> arguments -> Argument(i);
984 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(),
985 expr -> Type()))
986 {
987 break;
988 }
989 }
990 if (i == method_call -> arguments -> NumArguments())
991 {
992 if (MoreSpecific(method, method_set))
993 {
994 method_set.Reset();
995 method_set.Next() = method_shadow;
996 }
997 else if (NoMethodMoreSpecific(method_set, method))
998 method_set.Next() = method_shadow;
999 }
1000 }
1001 }
1002
1003 if (method_set.Length() == 0)
1004 {
1005 ReportMethodNotFound(method_call, type);
1006 return NULL;
1007 }
1008 else if (method_set.Length() > 1)
1009 {
1010 ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
1011 method_call, name_symbol -> Name(),
1012 method_set[0] -> method_symbol -> Header(),
1013 method_set[0] -> method_symbol -> containing_type -> ContainingPackageName(),
1014 method_set[0] -> method_symbol -> containing_type -> ExternalName(),
1015 method_set[1] -> method_symbol -> Header(),
1016 method_set[1] -> method_symbol -> containing_type -> ContainingPackageName(),
1017 method_set[1] -> method_symbol -> containing_type -> ExternalName());
1018 }
1019
1020 MethodSymbol* method = method_set[0] -> method_symbol;
1021 if (method -> ACC_SYNTHETIC())
1022 {
1023 ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
1024 method_call, method -> Header(),
1025 method -> containing_type -> ContainingPackageName(),
1026 method -> containing_type -> ExternalName());
1027 }
1028
1029 //
1030 // If this method came from a class file, make sure that its throws clause
1031 // has been processed.
1032 //
1033 method -> ProcessMethodThrows(this, id_token);
1034
1035 if (control.option.deprecation && method -> IsDeprecated() &&
1036 ! InDeprecatedContext())
1037 {
1038 ReportSemError(SemanticError::DEPRECATED_METHOD, method_call,
1039 method -> Header(),
1040 method -> containing_type -> ContainingPackageName(),
1041 method -> containing_type -> ExternalName());
1042 }
1043 return method_set[0];
1044 }
1045
1046
FindMethodInEnvironment(Tuple<MethodShadowSymbol * > & methods_found,SemanticEnvironment * & where_found,SemanticEnvironment * envstack,AstMethodInvocation * method_call)1047 void Semantic::FindMethodInEnvironment(Tuple<MethodShadowSymbol*>& methods_found,
1048 SemanticEnvironment*& where_found,
1049 SemanticEnvironment* envstack,
1050 AstMethodInvocation* method_call)
1051 {
1052 assert(! method_call -> base_opt);
1053 TokenIndex id_token = method_call -> identifier_token;
1054 NameSymbol* name_symbol = lex_stream -> NameSymbol(id_token);
1055
1056 for (SemanticEnvironment* env = envstack; env; env = env -> previous)
1057 {
1058 TypeSymbol* type = env -> Type();
1059 if (! type -> expanded_method_table)
1060 ComputeMethodsClosure(type, id_token);
1061
1062 methods_found.Reset();
1063 where_found = NULL;
1064
1065 //
1066 // If this environment contained a method with the right name, the
1067 // search stops:
1068 //
1069 // "Class scoping does not influence overloading: if the inner
1070 // class has one print method, the simple method name 'print'
1071 // refers to that method, not any of the ten 'print' methods in
1072 // the enclosing class."
1073 //
1074 MethodShadowSymbol* method_shadow = type -> expanded_method_table ->
1075 FindMethodShadowSymbol(name_symbol);
1076 if (method_shadow)
1077 {
1078 for ( ; method_shadow;
1079 method_shadow = method_shadow -> next_method)
1080 {
1081 MethodSymbol* method = method_shadow -> method_symbol;
1082
1083 if (! method -> IsTyped())
1084 method -> ProcessMethodSignature(this, id_token);
1085
1086 //
1087 // Since type -> IsOwner(this_type()), i.e., type encloses
1088 // this_type(), method is accessible, even if it is private.
1089 //
1090 if (method_call -> arguments -> NumArguments() ==
1091 method -> NumFormalParameters())
1092 {
1093 unsigned i;
1094 for (i = 0; i < method_call -> arguments -> NumArguments(); i++)
1095 {
1096 AstExpression* expr = method_call -> arguments -> Argument(i);
1097 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(),
1098 expr -> Type()))
1099 {
1100 break;
1101 }
1102 }
1103 if (i == method_call -> arguments -> NumArguments())
1104 {
1105 if (MoreSpecific(method, methods_found))
1106 {
1107 methods_found.Reset();
1108 methods_found.Next() = method_shadow;
1109 }
1110 else if (NoMethodMoreSpecific(methods_found, method))
1111 methods_found.Next() = method_shadow;
1112 }
1113 }
1114 }
1115
1116 //
1117 // If a match was found, save the environment
1118 //
1119 where_found = (methods_found.Length() > 0 ? env
1120 : (SemanticEnvironment*) NULL);
1121 break;
1122 }
1123 }
1124 }
1125
1126
FindMethodInEnvironment(SemanticEnvironment * & where_found,AstMethodInvocation * method_call)1127 MethodShadowSymbol* Semantic::FindMethodInEnvironment(SemanticEnvironment*& where_found,
1128 AstMethodInvocation* method_call)
1129 {
1130 Tuple<MethodShadowSymbol*> methods_found(2);
1131 FindMethodInEnvironment(methods_found, where_found, state_stack.Top(),
1132 method_call);
1133 if (methods_found.Length() == 0)
1134 {
1135 ReportMethodNotFound(method_call, NULL);
1136 return NULL;
1137 }
1138 MethodSymbol* method_symbol = methods_found[0] -> method_symbol;
1139 for (unsigned i = 1; i < methods_found.Length(); i++)
1140 {
1141 ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
1142 method_call, method_symbol -> Name(),
1143 methods_found[0] -> method_symbol -> Header(),
1144 method_symbol -> containing_type -> ContainingPackageName(),
1145 method_symbol -> containing_type -> ExternalName(),
1146 methods_found[i] -> method_symbol -> Header(),
1147 methods_found[i] -> method_symbol -> containing_type -> ContainingPackageName(),
1148 methods_found[i] -> method_symbol -> containing_type -> ExternalName());
1149 }
1150
1151 if (method_symbol -> containing_type != where_found -> Type())
1152 {
1153 //
1154 // The method was inherited.
1155 //
1156 if (method_symbol -> ACC_SYNTHETIC())
1157 {
1158 ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
1159 method_call, method_symbol -> Header(),
1160 method_symbol -> containing_type -> ContainingPackageName(),
1161 method_symbol -> containing_type -> ExternalName());
1162 }
1163 else if (control.option.pedantic)
1164 {
1165 //
1166 // Give a pedantic warning if the inherited method shadowed
1167 // a method of the same name within an enclosing lexical scope.
1168 //
1169 Tuple<MethodShadowSymbol*> others(2);
1170 SemanticEnvironment* found_other;
1171 SemanticEnvironment* previous_env = where_found -> previous;
1172 FindMethodInEnvironment(others, found_other, previous_env,
1173 method_call);
1174
1175 if (others.Length() > 0 &&
1176 where_found -> Type() != found_other -> Type())
1177 {
1178 for (unsigned i = 0; i < others.Length(); i++)
1179 {
1180 if (others[i] -> method_symbol != method_symbol &&
1181 (others[i] -> method_symbol -> containing_type ==
1182 found_other -> Type()))
1183 {
1184 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
1185 method_call,
1186 method_symbol -> Name(),
1187 method_symbol -> containing_type -> ContainingPackageName(),
1188 method_symbol -> containing_type -> ExternalName(),
1189 found_other -> Type() -> ContainingPackageName(),
1190 found_other -> Type() -> ExternalName());
1191 break; // emit only one error message
1192 }
1193 }
1194 }
1195 }
1196 }
1197
1198 //
1199 // If this method came from a class file, make sure that its throws
1200 // clause has been processed.
1201 //
1202 method_symbol -> ProcessMethodThrows(this,
1203 method_call -> identifier_token);
1204 if (control.option.deprecation && method_symbol -> IsDeprecated() &&
1205 ! InDeprecatedContext())
1206 {
1207 ReportSemError(SemanticError::DEPRECATED_METHOD,
1208 method_call, method_symbol -> Header(),
1209 method_symbol -> containing_type -> ContainingPackageName(),
1210 method_symbol -> containing_type -> ExternalName());
1211 }
1212 return methods_found[0];
1213 }
1214
1215
1216
1217 //
1218 // Search the type in question for a variable. Note that name_symbol is an
1219 // optional argument. If it was not passed to this function then its default
1220 // value is NULL (see semantic.h) and we assume that the name to search for
1221 // is the last identifier specified in the field_access. Error reporting if
1222 // the field is not found is up to the callee, since for qualified names,
1223 // the name may successfully resolve to a nested type.
1224 //
FindVariableInType(TypeSymbol * type,AstExpression * expr,NameSymbol * name_symbol)1225 VariableSymbol* Semantic::FindVariableInType(TypeSymbol* type,
1226 AstExpression* expr,
1227 NameSymbol* name_symbol)
1228 {
1229 Tuple<VariableSymbol*> variable_set(2); // Stores variable conflicts.
1230 AstFieldAccess* field_access = expr -> FieldAccessCast();
1231 AstName* name = expr -> NameCast();
1232 AstExpression* base = name ? name -> base_opt : field_access -> base;
1233 assert(base);
1234 VariableSymbol* variable;
1235 if (! name_symbol)
1236 name_symbol = lex_stream -> NameSymbol(expr -> RightToken());
1237 if (! type -> expanded_field_table)
1238 ComputeFieldsClosure(type, expr -> RightToken());
1239
1240 //
1241 // Find the accessible fields with the correct name in the type.
1242 //
1243 VariableShadowSymbol* variable_shadow =
1244 type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
1245
1246 if (variable_shadow)
1247 {
1248 variable = variable_shadow -> variable_symbol;
1249 if (! variable -> IsTyped())
1250 variable -> ProcessVariableSignature(this, expr -> RightToken());
1251 if (MemberAccessCheck(type, variable, base))
1252 variable_set.Next() = variable;
1253
1254 for (unsigned i = 0; i < variable_shadow -> NumConflicts(); i++)
1255 {
1256 variable = variable_shadow -> Conflict(i);
1257 if (! variable -> IsTyped())
1258 variable -> ProcessVariableSignature(this,
1259 expr -> RightToken());
1260 if (MemberAccessCheck(type, variable, base))
1261 variable_set.Next() = variable;
1262 }
1263 }
1264
1265 if (variable_set.Length() == 0)
1266 return NULL;
1267 else if (variable_set.Length() > 1)
1268 {
1269 ReportSemError(SemanticError::AMBIGUOUS_FIELD, expr,
1270 name_symbol -> Name(),
1271 variable_set[0] -> ContainingType() -> ContainingPackageName(),
1272 variable_set[0] -> ContainingType() -> ExternalName(),
1273 variable_set[1] -> ContainingType() -> ContainingPackageName(),
1274 variable_set[1] -> ContainingType() -> ExternalName());
1275 }
1276
1277 variable = variable_set[0];
1278 if (variable -> ACC_SYNTHETIC())
1279 {
1280 ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS, expr,
1281 variable -> Name(),
1282 variable -> ContainingType() -> ContainingPackageName(),
1283 variable -> ContainingType() -> ExternalName());
1284 }
1285
1286 if (control.option.deprecation && variable -> IsDeprecated() &&
1287 ! InDeprecatedContext())
1288 {
1289 ReportSemError(SemanticError::DEPRECATED_FIELD, expr,
1290 variable -> Name(),
1291 variable -> ContainingType() -> ContainingPackageName(),
1292 variable -> ContainingType() -> ExternalName());
1293 }
1294 return variable;
1295 }
1296
1297
1298 //
1299 // Called when no accessible variable was found. The access must be one of
1300 // AstFieldAccess or AstSimpleName. This checks in order: an accessible no-arg
1301 // method by the same name, an inaccessible field in a superclass, a
1302 // misspelled field name, a type by the same name, and finally the field was
1303 // not found.
1304 //
ReportVariableNotFound(AstExpression * access,TypeSymbol * type)1305 void Semantic::ReportVariableNotFound(AstExpression* access, TypeSymbol* type)
1306 {
1307 TokenIndex id_token = access -> RightToken();
1308 NameSymbol* name_symbol = lex_stream -> NameSymbol(id_token);
1309 VariableShadowSymbol* variable_shadow;
1310
1311 if (! type -> expanded_field_table)
1312 ComputeFieldsClosure(type, id_token);
1313 if (! type -> expanded_method_table)
1314 ComputeMethodsClosure(type, id_token);
1315
1316 //
1317 // Search for an accessible no-arg method of the same name.
1318 //
1319 MethodShadowSymbol* method_shadow;
1320 for (method_shadow = type -> expanded_method_table ->
1321 FindMethodShadowSymbol(name_symbol);
1322 method_shadow; method_shadow = method_shadow -> next_method)
1323 {
1324 MethodSymbol* method = method_shadow -> method_symbol;
1325
1326 //
1327 // Make sure that method has been fully prepared.
1328 //
1329 if (! method -> IsTyped())
1330 method -> ProcessMethodSignature(this, id_token);
1331
1332 if (method -> NumFormalParameters() == 0 &&
1333 MemberAccessCheck(type, method))
1334 {
1335 ReportSemError(SemanticError::METHOD_NOT_FIELD,
1336 id_token, name_symbol -> Name(),
1337 method -> containing_type -> ContainingPackageName(),
1338 method -> containing_type -> ExternalName());
1339 return;
1340 }
1341 }
1342
1343 //
1344 // Check if the field is inaccessible.
1345 //
1346 for (TypeSymbol* super_type = type;
1347 super_type; super_type = super_type -> super)
1348 {
1349 variable_shadow = super_type -> expanded_field_table ->
1350 FindVariableShadowSymbol(name_symbol);
1351 if (variable_shadow)
1352 {
1353 VariableSymbol* variable = variable_shadow -> variable_symbol;
1354 TypeSymbol* containing_type = variable -> owner -> TypeCast();
1355
1356 //
1357 // A protected instance field in the superclass is inaccessible if
1358 // the base expression is the wrong type.
1359 //
1360 if (variable -> ACC_PROTECTED() &&
1361 ! variable -> ACC_STATIC() &&
1362 ThisType() -> HasProtectedAccessTo(containing_type))
1363 {
1364 ReportSemError(SemanticError::PROTECTED_INSTANCE_FIELD_NOT_ACCESSIBLE,
1365 id_token, name_symbol -> Name(),
1366 containing_type -> ContainingPackageName(),
1367 containing_type -> ExternalName(),
1368 ThisType() -> ContainingPackageName(),
1369 ThisType() -> ExternalName());
1370 }
1371 else
1372 {
1373 ReportSemError(SemanticError::FIELD_NOT_ACCESSIBLE,
1374 id_token, name_symbol -> Name(),
1375 containing_type -> ContainingPackageName(),
1376 containing_type -> ExternalName(),
1377 variable -> AccessString());
1378 }
1379 return;
1380 }
1381 }
1382
1383 //
1384 // Try various possibilities of what the user might have meant.
1385 //
1386 AstName* ast_name = access -> NameCast();
1387 TypeSymbol* inaccessible_type = (! ast_name || ast_name -> base_opt)
1388 ? NULL : FindInaccessibleType(ast_name);
1389 VariableSymbol* variable = FindMisspelledVariableName(type, access);
1390 if (variable)
1391 {
1392 //
1393 // There is a field with a similar name.
1394 //
1395 ReportSemError(SemanticError::FIELD_NAME_MISSPELLED,
1396 id_token, name_symbol -> Name(),
1397 type -> ContainingPackageName(),
1398 type -> ExternalName(),
1399 variable -> Name());
1400 }
1401 else if (FindType(id_token))
1402 {
1403 //
1404 // There is a type or package of the same name.
1405 //
1406 ReportSemError(SemanticError::TYPE_NOT_FIELD,
1407 id_token, name_symbol -> Name());
1408 }
1409 else if (inaccessible_type)
1410 {
1411 //
1412 // There is an inaccessible type of the same name.
1413 //
1414 ReportTypeInaccessible(ast_name, inaccessible_type);
1415 }
1416 else if (access -> symbol && access -> symbol -> PackageCast())
1417 {
1418 ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME,
1419 access, name_symbol -> Name());
1420 }
1421 else
1422 {
1423 //
1424 // Give up. We didn't find it.
1425 //
1426 ReportSemError(SemanticError::FIELD_NOT_FOUND,
1427 id_token, name_symbol -> Name(),
1428 type -> ContainingPackageName(),
1429 type -> ExternalName());
1430 }
1431 }
1432
1433
FindVariableInEnvironment(Tuple<VariableSymbol * > & variables_found,SemanticEnvironment * & where_found,SemanticEnvironment * envstack,NameSymbol * name_symbol,TokenIndex identifier_token)1434 void Semantic::FindVariableInEnvironment(Tuple<VariableSymbol*>& variables_found,
1435 SemanticEnvironment*& where_found,
1436 SemanticEnvironment* envstack,
1437 NameSymbol* name_symbol,
1438 TokenIndex identifier_token)
1439 {
1440 variables_found.Reset();
1441 where_found = (SemanticEnvironment*) NULL;
1442
1443 for (SemanticEnvironment* env = envstack; env; env = env -> previous)
1444 {
1445 VariableSymbol* variable_symbol =
1446 env -> symbol_table.FindVariableSymbol(name_symbol);
1447 if (variable_symbol) // a local variable
1448 {
1449 variables_found.Next() = variable_symbol;
1450 where_found = env;
1451 break;
1452 }
1453
1454 TypeSymbol* type = env -> Type();
1455 if (! type -> expanded_field_table)
1456 ComputeFieldsClosure(type, identifier_token);
1457 VariableShadowSymbol* variable_shadow = type ->
1458 expanded_field_table -> FindVariableShadowSymbol(name_symbol);
1459 if (variable_shadow)
1460 {
1461 //
1462 // Since type -> IsOwner(this_type()), i.e., type encloses
1463 // this_type(), variable_symbol is accessible, even if it is
1464 // private.
1465 //
1466 variables_found.Next() = variable_shadow -> variable_symbol;
1467
1468 //
1469 // Recall that even an inaccessible member x of a super class (or
1470 // interface) S, in addition to not been inherited by a subclass,
1471 // hides all other occurrences of x that may appear in a super
1472 // class (or super interface) of S (see 8.3).
1473 //
1474 for (unsigned i = 0; i < variable_shadow -> NumConflicts(); i++)
1475 variables_found.Next() = variable_shadow -> Conflict(i);
1476 where_found = env;
1477 break;
1478 }
1479 }
1480 }
1481
1482
FindVariableInEnvironment(SemanticEnvironment * & where_found,TokenIndex identifier_token)1483 VariableSymbol* Semantic::FindVariableInEnvironment(SemanticEnvironment*& where_found,
1484 TokenIndex identifier_token)
1485 {
1486 Tuple<VariableSymbol*> variables_found(2);
1487 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
1488 SemanticEnvironment* envstack = state_stack.Top();
1489 FindVariableInEnvironment(variables_found, where_found, envstack,
1490 name_symbol, identifier_token);
1491
1492 VariableSymbol* variable_symbol =
1493 (VariableSymbol*) (variables_found.Length() > 0
1494 ? variables_found[0] : NULL);
1495
1496 if (variable_symbol)
1497 {
1498 if (variable_symbol -> IsLocal()) // a local variable
1499 {
1500 if (where_found != envstack)
1501 {
1502 TypeSymbol* type = envstack -> Type();
1503
1504 if (! variable_symbol -> ACC_FINAL())
1505 {
1506 MethodSymbol* method =
1507 variable_symbol -> owner -> MethodCast();
1508
1509 //
1510 // TODO: What if the method is a constructor ?
1511 // if (method -> Identity() != control.init_symbol &&
1512 // method -> Identity() != control.block_init_symbol &&
1513 // method -> Identity() != control.clinit_symbol)
1514 //
1515 ReportSemError(SemanticError::INNER_CLASS_REFERENCE_TO_NON_FINAL_LOCAL_VARIABLE,
1516 identifier_token,
1517 type -> ContainingPackageName(),
1518 type -> ExternalName(),
1519 lex_stream -> NameString(identifier_token),
1520 method -> ExternalName());
1521 }
1522 else if (! variable_symbol -> initial_value)
1523 {
1524 //
1525 // The variable is not constant, so we need to insert a
1526 // variable shadow in the outermost local class within the
1527 // scope of the variable, and use that shadow instead.
1528 //
1529 variable_symbol = FindLocalVariable(variable_symbol,
1530 envstack -> Type());
1531 TypeSymbol* shadow_owner =
1532 variable_symbol -> ContainingType();
1533 assert(shadow_owner);
1534 where_found = shadow_owner -> semantic_environment;
1535 }
1536 }
1537 }
1538 else if (variable_symbol -> owner != where_found -> Type())
1539 {
1540 //
1541 // The field was inherited.
1542 //
1543 TypeSymbol* type = (TypeSymbol*) variable_symbol -> owner;
1544 if (variable_symbol -> ACC_SYNTHETIC())
1545 {
1546 ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS,
1547 identifier_token,
1548 variable_symbol -> Name(),
1549 type -> ContainingPackageName(),
1550 type -> ExternalName());
1551 }
1552 else if (control.option.pedantic)
1553 {
1554 //
1555 // Give a pedantic warning if the inherited field shadowed
1556 // a field of the same name within an enclosing lexical scope.
1557 //
1558 Tuple<VariableSymbol*> others(2);
1559 SemanticEnvironment* found_other;
1560 SemanticEnvironment* previous_env = where_found -> previous;
1561 FindVariableInEnvironment(others, found_other, previous_env,
1562 name_symbol, identifier_token);
1563
1564 if (others.Length() > 0 &&
1565 where_found -> Type() != found_other -> Type())
1566 {
1567 for (unsigned i = 0; i < others.Length(); i++)
1568 {
1569 if (others[i] != variable_symbol)
1570 {
1571 MethodSymbol* method =
1572 others[i] -> owner -> MethodCast();
1573
1574 if (method)
1575 {
1576 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_LOCAL,
1577 identifier_token,
1578 lex_stream -> NameString(identifier_token),
1579 type -> ContainingPackageName(),
1580 type -> ExternalName(),
1581 method -> Name());
1582 break;
1583 }
1584 else if (others[i] -> owner == found_other -> Type())
1585 {
1586 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
1587 identifier_token,
1588 lex_stream -> NameString(identifier_token),
1589 type -> ContainingPackageName(),
1590 type -> ExternalName(),
1591 found_other -> Type() -> ContainingPackageName(),
1592 found_other -> Type() -> ExternalName());
1593 break;
1594 }
1595 }
1596 }
1597 }
1598 }
1599 }
1600 }
1601
1602 for (unsigned i = 1; i < variables_found.Length(); i++)
1603 {
1604 ReportSemError(SemanticError::AMBIGUOUS_FIELD, identifier_token,
1605 variable_symbol -> Name(),
1606 variable_symbol -> ContainingType() -> ContainingPackageName(),
1607 variable_symbol -> ContainingType() -> ExternalName(),
1608 variables_found[i] -> ContainingType() -> ContainingPackageName(),
1609 variables_found[i] -> ContainingType() -> ExternalName());
1610 }
1611
1612 if (variable_symbol)
1613 {
1614 if (control.option.deprecation && variable_symbol -> IsDeprecated() &&
1615 ! InDeprecatedContext())
1616 {
1617 ReportSemError(SemanticError::DEPRECATED_FIELD, identifier_token,
1618 variable_symbol -> Name(),
1619 variable_symbol -> ContainingType() -> ContainingPackageName(),
1620 variable_symbol -> ContainingType() -> ExternalName());
1621 }
1622
1623 if (! variable_symbol -> IsTyped())
1624 variable_symbol -> ProcessVariableSignature(this, identifier_token);
1625 }
1626 return variable_symbol;
1627 }
1628
1629
1630 //
1631 // Find a variable shadow in the outermost local class within the scope of
1632 // the variable, and return a local variable shadow to it instead.
1633 //
FindLocalVariable(VariableSymbol * local,TypeSymbol * type)1634 VariableSymbol* Semantic::FindLocalVariable(VariableSymbol* local,
1635 TypeSymbol* type)
1636 {
1637 while (local -> accessed_local)
1638 local = local -> accessed_local;
1639 assert(local -> IsLocal());
1640
1641 TypeSymbol* containing_type = local -> ContainingType();
1642 if (type == containing_type)
1643 return local;
1644
1645 while (type && type -> ContainingType() != containing_type)
1646 {
1647 if (! type -> EnclosingType())
1648 {
1649 assert(type -> Anonymous());
1650 break;
1651 }
1652 type = type -> ContainingType();
1653 }
1654 assert(type && type -> IsLocal());
1655 return type -> FindOrInsertLocalShadow(local);
1656 }
1657
1658
1659 //
1660 // Using the this$0 variable, locate the appropriate enclosing instance.
1661 //
FindEnclosingInstance(AstExpression * base,TypeSymbol * environment_type,bool exact)1662 AstExpression* Semantic::FindEnclosingInstance(AstExpression* base,
1663 TypeSymbol* environment_type,
1664 bool exact)
1665 {
1666 TypeSymbol* base_type = base -> Type();
1667 assert(base_type != environment_type &&
1668 base_type -> HasEnclosingInstance(environment_type, exact));
1669 VariableSymbol* this0 = base_type -> EnclosingInstance();
1670 if (! this0)
1671 {
1672 //
1673 // In the case of an anonymous class in an explicit constructor call,
1674 // when the immediate enclosing class is not yet initialized, other
1675 // enclosing classes are not accessible (even though they COULD be
1676 // available through additional constructor parameters) - JLS 8.8.5.1
1677 //
1678 assert(base_type -> Anonymous() && base_type -> IsLocal());
1679 return NULL;
1680 }
1681
1682 TokenIndex tok = base -> RightToken();
1683
1684 AstFieldAccess* field_access =
1685 compilation_unit -> ast_pool -> GenFieldAccess();
1686 field_access -> base = base;
1687 field_access -> identifier_token = tok;
1688 field_access -> symbol = this0;
1689
1690 if (exact ? (this0 -> Type() == environment_type)
1691 : (this0 -> Type() -> IsSubclass(environment_type)))
1692 {
1693 return field_access;
1694 }
1695 return FindEnclosingInstance(field_access, environment_type, exact);
1696 }
1697
1698
1699 //
1700 // Generate access to the correct enclosing instance.
1701 //
CreateAccessToType(Ast * source,TypeSymbol * environment_type)1702 AstExpression* Semantic::CreateAccessToType(Ast* source,
1703 TypeSymbol* environment_type)
1704 {
1705 TypeSymbol* this_type = ThisType();
1706
1707 TokenIndex left_tok;
1708 TokenIndex right_tok;
1709
1710 AstName* variable = source -> NameCast();
1711 AstMethodInvocation* method = source -> MethodInvocationCast();
1712 AstSuperCall* super_call = source -> SuperCallCast();
1713 AstThisExpression* this_expr = source -> ThisExpressionCast();
1714 AstSuperExpression* super_expr = source -> SuperExpressionCast();
1715 AstClassCreationExpression* class_creation =
1716 source -> ClassCreationExpressionCast();
1717 bool exact = false;
1718
1719 if (variable)
1720 {
1721 assert(! variable -> base_opt);
1722 left_tok = right_tok = variable -> identifier_token;
1723 //
1724 // If this type subclasses the enclosing type, then CreateAccess was
1725 // called because the simple name was not inherited into this type
1726 // (ie. the variable is private or else hidden in a superclass). In
1727 // this case, turn on exact enclosing type checking.
1728 //
1729 if (this_type -> IsSubclass(environment_type))
1730 exact = true;
1731 }
1732 else if (method)
1733 {
1734 assert(! method -> base_opt);
1735 left_tok = right_tok = method -> identifier_token;
1736 //
1737 // If this type subclasses the enclosing type, then CreateAccess was
1738 // called because the simple name was not inherited into this type
1739 // (ie. the method is private or else hidden in a superclass). In
1740 // this case, turn on exact enclosing type checking.
1741 //
1742 if (this_type -> IsSubclass(environment_type))
1743 exact = true;
1744 }
1745 else if (class_creation)
1746 left_tok = right_tok = class_creation -> new_token;
1747 else if (super_call)
1748 left_tok = right_tok = super_call -> super_token;
1749 else if (this_expr)
1750 {
1751 assert(this_expr -> base_opt);
1752 left_tok = this_expr -> LeftToken();
1753 right_tok = this_expr -> this_token;
1754 exact = true;
1755 }
1756 else if (super_expr)
1757 {
1758 assert(super_expr -> base_opt);
1759 left_tok = super_expr -> LeftToken();
1760 right_tok = super_expr -> super_token;
1761 exact = true;
1762 }
1763 else assert(false && "create access to invalid expression");
1764
1765 AstExpression* resolution;
1766
1767 if (! this_type -> HasEnclosingInstance(environment_type, exact))
1768 {
1769 ReportSemError((ExplicitConstructorInvocation() &&
1770 this_type -> IsSubclass(environment_type)
1771 ? SemanticError::ENCLOSING_INSTANCE_ACCESS_FROM_CONSTRUCTOR_INVOCATION
1772 : SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE),
1773 left_tok, right_tok,
1774 environment_type -> ContainingPackageName(),
1775 environment_type -> ExternalName());
1776 resolution = compilation_unit -> ast_pool -> GenName(left_tok);
1777 resolution -> symbol = control.no_type;
1778 }
1779 else
1780 {
1781 //
1782 // Collapse everything except qualified this or super to the innermost
1783 // class. Start from the parameter this$0 in an explicit constructor
1784 // invocation, else start from this.
1785 //
1786 if (ExplicitConstructorInvocation())
1787 {
1788 VariableSymbol* variable = LocalSymbolTable().
1789 FindVariableSymbol(control.this_name_symbol);
1790 assert(variable);
1791 resolution = compilation_unit -> ast_pool -> GenName(left_tok);
1792 resolution -> symbol = variable;
1793 }
1794 else
1795 {
1796 resolution =
1797 compilation_unit -> ast_pool -> GenThisExpression(left_tok);
1798 resolution -> symbol = this_type;
1799 }
1800 TypeSymbol* resolved_type = resolution -> Type();
1801 if (resolved_type != environment_type &&
1802 (! resolved_type -> IsSubclass(environment_type) || exact))
1803 {
1804 AstExpression* intermediate =
1805 FindEnclosingInstance(resolution, environment_type, exact);
1806 if (! intermediate)
1807 {
1808 ReportSemError(SemanticError::ENCLOSING_INSTANCE_ACCESS_ACROSS_STATIC_REGION,
1809 left_tok, right_tok,
1810 environment_type -> ContainingPackageName(),
1811 environment_type -> ExternalName());
1812 resolution -> symbol = control.no_type;
1813 }
1814 else resolution = intermediate;
1815 }
1816 }
1817 if (super_expr)
1818 environment_type = environment_type -> super;
1819 return ConvertToType(resolution, environment_type);
1820 }
1821
1822
CreateAccessToScopedVariable(AstName * name,TypeSymbol * environment_type)1823 void Semantic::CreateAccessToScopedVariable(AstName* name,
1824 TypeSymbol* environment_type)
1825 {
1826 assert(! name -> base_opt);
1827 VariableSymbol* variable = (VariableSymbol*) name -> symbol;
1828 assert(variable -> owner -> TypeCast());
1829 AstExpression* access_expression;
1830 if (variable -> ACC_STATIC())
1831 {
1832 access_expression = compilation_unit -> ast_pool ->
1833 GenName(name -> identifier_token);
1834 access_expression -> symbol = environment_type;
1835 }
1836 else
1837 {
1838 AstThisExpression* this_expr = compilation_unit -> ast_pool ->
1839 GenThisExpression(name -> identifier_token);
1840 this_expr -> resolution_opt =
1841 CreateAccessToType(name, environment_type);
1842 this_expr -> symbol = this_expr -> resolution_opt -> symbol;
1843 access_expression = this_expr;
1844 }
1845
1846 if (access_expression -> symbol != control.no_type)
1847 {
1848 TypeSymbol* containing_type = variable -> ContainingType();
1849
1850 if (variable -> ACC_PRIVATE() ||
1851 (variable -> ACC_PROTECTED() &&
1852 ! ProtectedAccessCheck(containing_type)))
1853 {
1854 assert((variable -> ACC_PRIVATE() &&
1855 environment_type == containing_type) ||
1856 (variable -> ACC_PROTECTED() &&
1857 environment_type -> IsSubclass(containing_type)));
1858
1859 TokenIndex loc = name -> identifier_token;
1860 AstArguments* args =
1861 compilation_unit -> ast_pool -> GenArguments(loc, loc);
1862 if (! variable -> ACC_STATIC())
1863 {
1864 // TODO: WARNING: sharing of Ast subtree !!!
1865 args -> AllocateArguments(1);
1866 args -> AddArgument(access_expression);
1867 }
1868
1869 AstMethodInvocation* accessor =
1870 compilation_unit -> ast_pool -> GenMethodInvocation(loc);
1871 accessor -> base_opt = access_expression;
1872 accessor -> arguments = args;
1873 // The default base type of the accessor method is appropriate.
1874 accessor -> symbol =
1875 environment_type -> GetReadAccessMethod(variable);
1876
1877 name -> resolution_opt = accessor;
1878 }
1879 else
1880 {
1881 AstFieldAccess* field_access =
1882 compilation_unit -> ast_pool -> GenFieldAccess();
1883 field_access -> base = access_expression;
1884 field_access -> identifier_token = name -> identifier_token;
1885 field_access -> symbol = variable;
1886
1887 name -> resolution_opt = field_access;
1888 }
1889 }
1890 }
1891
1892
CreateAccessToScopedMethod(AstMethodInvocation * method_call,TypeSymbol * environment_type)1893 void Semantic::CreateAccessToScopedMethod(AstMethodInvocation* method_call,
1894 TypeSymbol* environment_type)
1895 {
1896 assert(environment_type -> IsOwner(ThisType()));
1897 assert(! method_call -> base_opt);
1898 MethodSymbol* method = (MethodSymbol*) method_call -> symbol;
1899 AstExpression* access_expression;
1900 if (method -> ACC_STATIC())
1901 {
1902 access_expression = compilation_unit -> ast_pool ->
1903 GenName(method_call -> identifier_token);
1904 access_expression -> symbol = environment_type;
1905 }
1906 else
1907 {
1908 AstThisExpression* this_expr = compilation_unit -> ast_pool ->
1909 GenThisExpression(method_call -> identifier_token);
1910 this_expr -> resolution_opt =
1911 CreateAccessToType(method_call, environment_type);
1912 this_expr -> symbol = this_expr -> resolution_opt -> symbol;
1913 access_expression = this_expr;
1914 }
1915
1916 if (access_expression -> symbol != control.no_type)
1917 {
1918 method_call -> base_opt = access_expression;
1919 TypeSymbol* containing_type = method -> containing_type;
1920
1921 if (method -> ACC_PRIVATE() ||
1922 (method -> ACC_PROTECTED() &&
1923 ! ProtectedAccessCheck(containing_type)))
1924 {
1925 assert((method -> ACC_PRIVATE() &&
1926 environment_type == containing_type) ||
1927 (method -> ACC_PROTECTED() &&
1928 environment_type -> IsSubclass(containing_type)));
1929
1930 AstArguments* args = compilation_unit -> ast_pool ->
1931 GenArguments(method_call -> arguments -> left_parenthesis_token,
1932 method_call -> arguments -> right_parenthesis_token);
1933 unsigned num_args = method_call -> arguments -> NumArguments();
1934 if (! method -> ACC_STATIC())
1935 {
1936 args -> AllocateArguments(num_args + 1);
1937 args -> AddArgument(access_expression);
1938 }
1939 else args -> AllocateArguments(num_args);
1940 for (unsigned i = 0; i < num_args; i++)
1941 args -> AddArgument(method_call -> arguments -> Argument(i));
1942
1943 AstMethodInvocation* accessor = compilation_unit -> ast_pool ->
1944 GenMethodInvocation(method_call -> identifier_token);
1945 accessor -> base_opt = access_expression;
1946 accessor -> arguments = args;
1947 accessor -> symbol =
1948 // default base type is appropriate
1949 environment_type -> GetReadAccessMethod(method);
1950
1951 method_call -> symbol = method;
1952 method_call -> resolution_opt = accessor;
1953 }
1954 }
1955 }
1956
1957
CheckSimpleName(AstName * name,SemanticEnvironment * where_found)1958 void Semantic::CheckSimpleName(AstName* name, SemanticEnvironment* where_found)
1959 {
1960 VariableSymbol* variable_symbol = name -> symbol -> VariableCast();
1961 assert(variable_symbol && ! name -> base_opt);
1962
1963 if (StaticRegion() && ! ExplicitConstructorInvocation())
1964 {
1965 if (! (variable_symbol -> IsLocal() ||
1966 variable_symbol -> ACC_STATIC()))
1967 {
1968 ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
1969 name -> identifier_token,
1970 lex_stream -> NameString(name -> identifier_token));
1971 }
1972 else if (variable_symbol -> owner -> TypeCast() &&
1973 ! variable_symbol -> IsDeclarationComplete() &&
1974 ! ProcessingSimpleAssignment())
1975 {
1976 ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
1977 name -> identifier_token,
1978 lex_stream -> NameString(name -> identifier_token));
1979 }
1980 }
1981 else if (! variable_symbol -> ACC_STATIC()) // an instance variable?
1982 {
1983 // an instance field member ?
1984 TypeSymbol* containing_type = variable_symbol -> owner -> TypeCast();
1985
1986 // variable must be a field for these next errors to be valid
1987 if (containing_type && ! variable_symbol -> accessed_local)
1988 {
1989 if (containing_type == ThisType() &&
1990 ! variable_symbol -> IsDeclarationComplete() &&
1991 ! ProcessingSimpleAssignment()) // forward reference?
1992 {
1993 ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
1994 name -> identifier_token,
1995 lex_stream -> NameString(name -> identifier_token));
1996 }
1997 else if (ExplicitConstructorInvocation() &&
1998 where_found == state_stack.Top())
1999 {
2000 //
2001 // If the variable in question is an instance variable that is
2002 // declared in this_type (this_type is definitely a class) or
2003 // one of its super classes, then we have an error:
2004 //
2005 ReportSemError(SemanticError::INSTANCE_VARIABLE_IN_EXPLICIT_CONSTRUCTOR,
2006 name -> identifier_token,
2007 lex_stream -> NameString(name -> identifier_token),
2008 containing_type -> Name());
2009 }
2010 }
2011 }
2012 }
2013
2014
ProcessExpressionOrStringConstant(AstExpression * expr)2015 void Semantic::ProcessExpressionOrStringConstant(AstExpression* expr)
2016 {
2017 ProcessExpression(expr);
2018 //
2019 // If the expression is of type String, check whether or not it is
2020 // constant, and if so, compute the result.
2021 //
2022 if (expr -> symbol == control.String() && ! expr -> IsConstant())
2023 control.Utf8_pool.CheckStringConstant(expr);
2024 }
2025
2026
ProcessName(Ast * expr)2027 void Semantic::ProcessName(Ast* expr)
2028 {
2029 AstName* name = (AstName*) expr;
2030 ProcessAmbiguousName(name);
2031 TypeSymbol* type = name -> Type();
2032 if (type == control.no_type)
2033 return; // ProcessAmbiguousName already reported the error
2034 if (! type || name -> symbol -> TypeCast())
2035 {
2036 ReportVariableNotFound(name, ThisType());
2037 name -> symbol = control.no_type;
2038 }
2039 }
2040
2041
2042 //
2043 // Returns true if the type is accessible from the current semantic location.
2044 //
TypeAccessCheck(TypeSymbol * type)2045 bool Semantic::TypeAccessCheck(TypeSymbol* type)
2046 {
2047 // According to JLS 6.6.1, a type T[] is accessible if T is accessible.
2048 if (type -> IsArray())
2049 type = type -> base_type;
2050
2051 //
2052 // Outside a class body, only public types from other packages, or
2053 // non-private types in the current package, are accessible. For a member
2054 // type, as in T1.T2, this does not check that T1 is also accessible; that
2055 // requires additional checks by the caller.
2056 //
2057 assert(this_package);
2058 if (type -> ACC_PUBLIC() ||
2059 (type -> ContainingPackage() == this_package &&
2060 ! type -> ACC_PRIVATE()))
2061 {
2062 return true;
2063 }
2064 if (state_stack.Size() > 0)
2065 {
2066 //
2067 // Inside a class body, all types listed above are accessible.
2068 // Additionally, declared or inherited member types are accessible.
2069 //
2070 TypeSymbol* this_type = ThisType();
2071 assert(this_type -> ContainingPackage() == this_package);
2072 if (this_type -> outermost_type == type -> outermost_type ||
2073 (type -> ACC_PROTECTED() &&
2074 this_type -> HasProtectedAccessTo(type)))
2075 {
2076 return true;
2077 }
2078 }
2079 return false;
2080 }
2081
2082
2083 //
2084 // Returns true if the constructor is accessible. The invocation is used to
2085 // distinguish between different rules for class instance creation and explicit
2086 // constructor invocation.
2087 //
ConstructorAccessCheck(MethodSymbol * constructor,bool explicit_ctor)2088 bool Semantic::ConstructorAccessCheck(MethodSymbol* constructor,
2089 bool explicit_ctor)
2090 {
2091 TypeSymbol* this_type = ThisType();
2092 TypeSymbol* containing_type = constructor -> containing_type;
2093 if (this_type -> outermost_type != containing_type -> outermost_type &&
2094 constructor -> ACC_PRIVATE())
2095 {
2096 return false;
2097 }
2098
2099 //
2100 // Default constructors are not accessible outside the package, and
2101 // protected constructors can only be accessed by a call to super(). This
2102 // includes anonymous classes, where we will later generate a super() call.
2103 //
2104 if (containing_type -> ContainingPackage() != this_package &&
2105 ! constructor -> ACC_PUBLIC())
2106 {
2107 return constructor -> ACC_PROTECTED() && explicit_ctor;
2108 }
2109 return true;
2110 }
2111
2112
2113 //
2114 // Returns true if the field or method member symbol can be accessed from this
2115 // semantic point, when the qualifier of the access is base_type. base
2116 // is the qualifying expression for the access, and is NULL for simple names.
2117 //
MemberAccessCheck(TypeSymbol * base_type,Symbol * symbol,AstExpression * base)2118 bool Semantic::MemberAccessCheck(TypeSymbol* base_type, Symbol* symbol,
2119 AstExpression* base)
2120 {
2121 TypeSymbol* this_type = ThisType();
2122
2123 VariableSymbol* variable_symbol = symbol -> VariableCast();
2124 MethodSymbol* method_symbol = symbol -> MethodCast();
2125 assert(variable_symbol || method_symbol);
2126
2127 AccessFlags* flags = (variable_symbol ? (AccessFlags*) variable_symbol
2128 : (AccessFlags*) method_symbol);
2129 TypeSymbol* containing_type = (variable_symbol
2130 ? variable_symbol -> ContainingType()
2131 : method_symbol -> containing_type);
2132 assert(containing_type);
2133
2134 //
2135 // When this function, MemberAccessCheck is invoked, it is assumed that
2136 // the base type has been checked as follows:
2137 //
2138 // if (! TypeAccessCheck(base_type))
2139 // ReportTypeInaccessible(base, base_type);
2140 //
2141
2142 if (this_type -> outermost_type != containing_type -> outermost_type)
2143 {
2144 if (flags -> ACC_PRIVATE())
2145 return false;
2146 else if (flags -> ACC_PROTECTED())
2147 {
2148 //
2149 // Within the same package, protected is accessible. Super access
2150 // has special priveleges (contrary to JLS2 15.11.2,
2151 // super.name != ((S)this).name; ). JLS2 6.6.2: When packages
2152 // differ, subclasses may access protected static members without
2153 // further restrictions, but accessing instance members requires
2154 // that the qualifier be the subclass or lower.
2155 // JLS 9.2: Interfaces have no protected members.
2156 //
2157 if (base && base -> Type() -> ACC_INTERFACE())
2158 {
2159 // Object has no fields, so this would be the protected
2160 // methods "inherited" into an interface from Object.
2161 assert(method_symbol);
2162 return false;
2163 }
2164 if (containing_type -> ContainingPackage() == this_package ||
2165 (base && base -> SuperExpressionCast()))
2166 {
2167 return true;
2168 }
2169 if (this_type -> HasProtectedAccessTo(containing_type))
2170 {
2171 if (flags -> ACC_STATIC())
2172 return true;
2173 for (SemanticEnvironment* env =
2174 this_type -> semantic_environment;
2175 env; env = env -> previous)
2176 {
2177 if (base_type -> IsSubclass(env -> Type()))
2178 return true;
2179 }
2180 }
2181 return false;
2182 }
2183 else if (! flags -> ACC_PUBLIC() &&
2184 containing_type -> ContainingPackage() != this_package)
2185 {
2186 return false;
2187 }
2188 }
2189 return true;
2190 }
2191
2192
2193 //
2194 // Returns true if the current type can access a protected member declared in
2195 // the containing type, without an accessor method. This does not test
2196 // whether the target type and member are accessible, since those checks are
2197 // assumed to be already done.
2198 //
ProtectedAccessCheck(TypeSymbol * containing_type)2199 bool Semantic::ProtectedAccessCheck(TypeSymbol* containing_type)
2200 {
2201 return ThisType() -> IsSubclass(containing_type) ||
2202 this_package == containing_type -> ContainingPackage();
2203 }
2204
2205
2206 //
2207 // FindVariableMember resolves a qualified field reference. The parameter
2208 // type is the type of the qualifying expression, field_access is the
2209 // expression being resolved.
2210 //
FindVariableMember(TypeSymbol * type,AstExpression * expr)2211 void Semantic::FindVariableMember(TypeSymbol* type, AstExpression* expr)
2212 {
2213 //
2214 // TypeCast() returns true for super, this, and instance creation as
2215 // well as true type names, hence the extra check
2216 //
2217 AstFieldAccess* field_access = expr -> FieldAccessCast();
2218 AstName* name = expr -> NameCast();
2219 AstExpression* base = name ? name -> base_opt : field_access -> base;
2220 TokenIndex id_token = expr -> RightToken();
2221 bool base_is_type = base -> symbol -> TypeCast() && base -> NameCast();
2222
2223 if (type -> Bad())
2224 {
2225 //
2226 // If no error has been detected so far, report this as an error so
2227 // that we don't try to generate code later. On the other hand, if an
2228 // error had been detected prior to this, don't flood the user with
2229 // spurious messages.
2230 //
2231 if (NumErrors() == 0)
2232 ReportVariableNotFound(expr, type);
2233 expr -> symbol = control.no_type;
2234 }
2235 else if (type == control.null_type || type -> Primitive())
2236 {
2237 ReportSemError(SemanticError::TYPE_NOT_REFERENCE, base,
2238 type -> Name());
2239 expr -> symbol = control.no_type;
2240 }
2241 else
2242 {
2243 TypeSymbol* this_type = ThisType();
2244 if (! TypeAccessCheck(type))
2245 {
2246 ReportTypeInaccessible(base, type);
2247 expr -> symbol = control.no_type;
2248 return;
2249 }
2250
2251 VariableSymbol* variable = FindVariableInType(type, expr);
2252 if (variable)
2253 {
2254 assert(variable -> IsTyped());
2255
2256 if (base_is_type && ! variable -> ACC_STATIC())
2257 {
2258 ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
2259 id_token, lex_stream -> NameString(id_token));
2260 expr -> symbol = control.no_type;
2261 return;
2262 }
2263 if (variable -> ACC_STATIC() && ! base_is_type)
2264 {
2265 ReportSemError(SemanticError::CLASS_FIELD_ACCESSED_VIA_INSTANCE,
2266 id_token, lex_stream -> NameString(id_token));
2267 }
2268 //
2269 // If a variable is FINAL, initialized with a constant expression,
2270 // and of the form TypeName.Identifier, we substitute the
2271 // expression here - JLS 15.28. If it is of any other form, we
2272 // still compute the initial value, which will be inlined in
2273 // bytecode, but do not treat the expression as a constant - JLS2
2274 // clarifications.
2275 //
2276 if (variable -> ACC_FINAL())
2277 {
2278 if (! variable -> IsInitialized())
2279 ComputeFinalValue(variable);
2280 if (base_is_type)
2281 {
2282 assert(variable -> IsInitialized());
2283 expr -> value = variable -> initial_value;
2284 }
2285 }
2286
2287 //
2288 // Access to a private or protected variable in or via an enclosing
2289 // type? If the base is a super expression, be sure to start from
2290 // the correct enclosing instance.
2291 //
2292 TypeSymbol* containing_type = variable -> ContainingType();
2293 TypeSymbol* target_type = containing_type;
2294 if (! variable -> ACC_STATIC() && base -> SuperExpressionCast())
2295 {
2296 AstSuperExpression* super_expr = (AstSuperExpression*) base;
2297 if (super_expr -> base_opt)
2298 target_type = super_expr -> base_opt -> symbol;
2299 }
2300 if (this_type != target_type &&
2301 (variable -> ACC_PRIVATE() ||
2302 (variable -> ACC_PROTECTED() &&
2303 (! ProtectedAccessCheck(containing_type) ||
2304 target_type != containing_type))))
2305 {
2306 if (expr -> IsConstant())
2307 expr -> symbol = variable;
2308 else
2309 {
2310 //
2311 // Find the right enclosing class to place the accessor
2312 // method in. For private fields, the containing type; for
2313 // protected fields, an enclosing class which is related
2314 // to the containing type.
2315 //
2316 TypeSymbol* environment_type = containing_type;
2317 if (variable -> ACC_PROTECTED())
2318 {
2319 for (SemanticEnvironment* env =
2320 this_type -> semantic_environment;
2321 env; env = env -> previous)
2322 {
2323 if (env -> Type() -> IsSubclass(target_type))
2324 {
2325 environment_type = env -> Type();
2326 break;
2327 }
2328 }
2329 assert(environment_type != containing_type &&
2330 environment_type != this_type);
2331 }
2332
2333 AstArguments* args =
2334 compilation_unit -> ast_pool -> GenArguments(id_token,
2335 id_token);
2336 if (! variable -> ACC_STATIC())
2337 {
2338 args -> AllocateArguments(1);
2339 args -> AddArgument(base);
2340 }
2341
2342 AstMethodInvocation* accessor = compilation_unit ->
2343 ast_pool -> GenMethodInvocation(id_token);
2344 accessor -> base_opt = base;
2345 accessor -> arguments = args;
2346 accessor -> symbol = environment_type ->
2347 GetReadAccessMethod(variable, base -> Type());
2348
2349 if (name)
2350 name -> resolution_opt = accessor;
2351 else
2352 field_access -> resolution_opt = accessor;
2353 expr -> symbol = accessor -> symbol;
2354 }
2355 }
2356 else
2357 expr -> symbol = variable;
2358 }
2359 else
2360 {
2361 TypeSymbol* inner_type = FindNestedType(type, id_token);
2362 if (inner_type)
2363 {
2364 if (base_is_type)
2365 {
2366 expr -> symbol = inner_type;
2367 if (! TypeAccessCheck(inner_type))
2368 ReportTypeInaccessible(expr, inner_type);
2369 }
2370 else
2371 {
2372 ReportSemError(SemanticError::TYPE_NOT_FIELD, id_token,
2373 lex_stream -> NameString(id_token));
2374 expr -> symbol = control.no_type;
2375 }
2376 }
2377 else
2378 {
2379 ReportVariableNotFound(expr, type);
2380 expr -> symbol = control.no_type;
2381 }
2382 }
2383 }
2384 }
2385
2386 //
2387 // Note that method names are not processed here but by the function
2388 // ProcessMethodName.
2389 //
ProcessAmbiguousName(AstName * name)2390 void Semantic::ProcessAmbiguousName(AstName* name)
2391 {
2392 TypeSymbol* this_type = ThisType();
2393 //
2394 // JLS2 6.5.2: If the ambiguous name is a simple name,...
2395 //
2396 if (! name -> base_opt)
2397 {
2398 TypeSymbol* type;
2399 //
2400 // ... If the Identifier appears within the scope (6.3) if a local
2401 // variable declaration (14.3) or parameter declaration (8.4.1,
2402 // 8.6.1, 14.18) with that name, then the ambiguous name is
2403 // reclassified as an ExpressionName...
2404 //
2405 // ...Otherwise, consider the class or interface C within whose
2406 // declaration the Identifier occurs. If C has one or more fields
2407 // with that name, which may be either declared within it or inherited,
2408 // then the Ambiguous name is reclassified as an ExpressionName....
2409 //
2410 SemanticEnvironment* where_found;
2411 VariableSymbol* variable_symbol =
2412 FindVariableInEnvironment(where_found, name -> identifier_token);
2413 if (variable_symbol)
2414 {
2415 assert(variable_symbol -> IsTyped());
2416
2417 //
2418 // A variable_symbol that is FINAL may have an initial value.
2419 // If variable_symbol is not final then its initial value is NULL.
2420 //
2421 if (variable_symbol -> ACC_FINAL() &&
2422 ! variable_symbol -> IsInitialized())
2423 {
2424 ComputeFinalValue(variable_symbol);
2425 }
2426 name -> value = variable_symbol -> initial_value;
2427 name -> symbol = variable_symbol;
2428
2429 CheckSimpleName(name, where_found);
2430
2431 //
2432 // If the variable belongs to an outer type, add the proper
2433 // pointer dereferences (and method access in the case of a
2434 // private variable) necessary to get to it.
2435 //
2436 if (where_found != state_stack.Top() &&
2437 variable_symbol -> owner -> TypeCast())
2438 {
2439 CreateAccessToScopedVariable(name, where_found -> Type());
2440 }
2441 }
2442 //
2443 // ...Otherwise, if a type of that name is declared in the compilation
2444 // unit (7.3) containing the Identifier, either by a
2445 // single-type-import declaration (7.5.1) or by a class or interface
2446 // type declaration (7.6), then the Ambiguous name is reclassified as
2447 // a TypeName...
2448 //
2449 // ...Otherwise, if a type of that name is declared in another
2450 // compilation unit (7.3) of the package (7.1) of the compilation unit
2451 // containing the Identifier, then the Ambiguous Name is reclassified
2452 // as a TypeName...
2453 //
2454 // ...Otherwise, if a type of that name is declared by exactly one
2455 // type-import-on-demand declaration (7.5.2) of the compilation unit
2456 // containing the Identifier, then the AmbiguousName is reclassified
2457 // as a TypeName
2458 //
2459 // ...Otherwise, if a type of that name is declared by more than one
2460 // type-import-on-demand declaration of the compilation unit
2461 // containing the Identifier, then a compile-time error results.
2462 //
2463 else if ((type = FindType(name -> identifier_token)))
2464 {
2465 name -> symbol = type;
2466 if (control.option.deprecation && type -> IsDeprecated() &&
2467 ! InDeprecatedContext())
2468 {
2469 ReportSemError(SemanticError::DEPRECATED_TYPE,
2470 name -> identifier_token,
2471 type -> ContainingPackageName(),
2472 type -> ExternalName());
2473 }
2474 }
2475 //
2476 // ...Otherwise, the Ambiguous name is reclassified as a PackageName.
2477 // While the JLS claims a later step determines whether or not
2478 // a package of that name actually exists, it is pointless to defer
2479 // the error that long, as a package cannot qualify a method or field
2480 // access, and a subpackage requires the base package to exist.
2481 //
2482 else
2483 {
2484 NameSymbol* name_symbol =
2485 lex_stream -> NameSymbol(name -> identifier_token);
2486 PackageSymbol* package =
2487 control.external_table.FindPackageSymbol(name_symbol);
2488 if (! package)
2489 {
2490 //
2491 // One last check in case the package was not imported.
2492 //
2493 package = control.external_table.InsertPackageSymbol(name_symbol,
2494 NULL);
2495 control.FindPathsToDirectory(package);
2496 }
2497 if (package -> directory.Length())
2498 name -> symbol = package;
2499 else
2500 {
2501 ReportVariableNotFound(name, this_type);
2502 name -> symbol = control.no_type;
2503 }
2504 }
2505 }
2506 //
2507 // ...If the ambiguous name is a qualified name,...
2508 //
2509 else
2510 {
2511 //
2512 // ...First, classify the name or expression to the left of the '.'...
2513 //
2514 AstName* base = name -> base_opt;
2515 ProcessAmbiguousName(base);
2516
2517 TypeSymbol* type = base -> Type();
2518 assert(type || base -> symbol -> PackageCast());
2519
2520 if (type == control.no_type)
2521 {
2522 name -> symbol = control.no_type;
2523 return;
2524 }
2525 PackageSymbol* package = base -> symbol -> PackageCast();
2526 if (package)
2527 {
2528 //
2529 // ... If there is a package whose name is the name to the
2530 // left of the '.' and that package contains a declaration of
2531 // a type whose name is the same as the Identifier, then the
2532 // AmbiguousName is reclassified as a TypeName...
2533 //
2534 NameSymbol* name_symbol =
2535 lex_stream -> NameSymbol(name -> identifier_token);
2536 type = package -> FindTypeSymbol(name_symbol);
2537
2538 if (type)
2539 {
2540 if (type -> SourcePending())
2541 control.ProcessHeaders(type -> file_symbol);
2542 name -> symbol = type;
2543 }
2544 else
2545 {
2546 FileSymbol* file_symbol =
2547 Control::GetFile(control, package, name_symbol);
2548 if (file_symbol)
2549 {
2550 type = ReadType(file_symbol, package, name_symbol,
2551 name -> identifier_token);
2552 name -> symbol = type;
2553 }
2554 //
2555 // ... Otherwise, this AmbiguousName is reclassified as a
2556 // PackageName. While the JLS claims a later step
2557 // determines whether or not a package of that name
2558 // actually exists, it is pointless to defer the error
2559 // that long, as a package cannot qualify a method or field
2560 // access, and a subpackage requires the base package to
2561 // exist.
2562 //
2563 else
2564 {
2565 PackageSymbol* subpackage =
2566 package -> FindPackageSymbol(name_symbol);
2567 if (! subpackage)
2568 {
2569 //
2570 // One last check in case the subpackage was not
2571 // imported.
2572 //
2573 subpackage =
2574 package -> InsertPackageSymbol(name_symbol);
2575 control.FindPathsToDirectory(subpackage);
2576 }
2577 if (subpackage -> directory.Length())
2578 name -> symbol = subpackage;
2579 else
2580 {
2581 ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME,
2582 name, name_symbol -> Name());
2583 name -> symbol = control.no_type;
2584 }
2585 }
2586 }
2587 }
2588 // ...Whether the qualifier is a type name, variable, or method
2589 // call, this is a regular field access
2590 //
2591 else
2592 {
2593 FindVariableMember(type, name);
2594 AddDependence(this_type, type, name -> IsConstant());
2595 }
2596 }
2597 }
2598
2599
ProcessFieldAccess(Ast * expr)2600 void Semantic::ProcessFieldAccess(Ast* expr)
2601 {
2602 AstFieldAccess* field_access = (AstFieldAccess*) expr;
2603 assert(! field_access -> base -> NameCast());
2604 ProcessExpressionOrStringConstant(field_access -> base);
2605
2606 TypeSymbol* type = field_access -> base -> Type();
2607 assert(type);
2608 if (type == control.no_type)
2609 {
2610 field_access -> symbol = control.no_type;
2611 return;
2612 }
2613 FindVariableMember(type, field_access);
2614 AddDependence(ThisType(), type);
2615
2616 if (field_access -> symbol != control.no_type)
2617 {
2618 PackageSymbol* package = field_access -> symbol -> PackageCast();
2619 if (package)
2620 {
2621 ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME,
2622 field_access, package -> PackageName());
2623 field_access -> symbol = control.no_type;
2624 }
2625 else if (field_access -> symbol -> TypeCast())
2626 {
2627 type = (TypeSymbol*) field_access -> symbol;
2628 ReportSemError(SemanticError::TYPE_NOT_FIELD,
2629 field_access, type -> Name());
2630 field_access -> symbol = control.no_type;
2631 }
2632 else
2633 {
2634 //
2635 // Either it's not a variable (an error) or the signature of
2636 // the variable has been typed
2637 //
2638 assert(! field_access -> symbol -> VariableCast() ||
2639 field_access -> symbol -> VariableCast() -> IsTyped());
2640 }
2641 }
2642 }
2643
2644
ProcessCharacterLiteral(Ast * expr)2645 void Semantic::ProcessCharacterLiteral(Ast* expr)
2646 {
2647 AstCharacterLiteral* char_literal = (AstCharacterLiteral*) expr;
2648
2649 LiteralSymbol* literal =
2650 lex_stream -> LiteralSymbol(char_literal -> character_literal_token);
2651
2652 if (! literal -> value)
2653 control.int_pool.FindOrInsertChar(literal);
2654 if (literal -> value == control.BadValue())
2655 char_literal -> symbol = control.no_type;
2656 else
2657 {
2658 char_literal -> value = literal -> value;
2659 char_literal -> symbol = control.char_type;
2660 }
2661 }
2662
2663
ProcessIntegerLiteral(Ast * expr)2664 void Semantic::ProcessIntegerLiteral(Ast* expr)
2665 {
2666 AstIntegerLiteral* int_literal = (AstIntegerLiteral*) expr;
2667
2668 LiteralSymbol* literal =
2669 lex_stream -> LiteralSymbol(int_literal -> integer_literal_token);
2670
2671 if (! literal -> value)
2672 control.int_pool.FindOrInsertInt(literal);
2673 if (literal -> value == control.BadValue())
2674 {
2675 ReportSemError(SemanticError::INVALID_INT_VALUE, int_literal);
2676 int_literal -> symbol = control.no_type;
2677 }
2678 else
2679 {
2680 int_literal -> value = literal -> value;
2681 int_literal -> symbol = control.int_type;
2682 }
2683 }
2684
2685
ProcessLongLiteral(Ast * expr)2686 void Semantic::ProcessLongLiteral(Ast* expr)
2687 {
2688 AstLongLiteral* long_literal = (AstLongLiteral*) expr;
2689
2690 LiteralSymbol* literal =
2691 lex_stream -> LiteralSymbol(long_literal -> long_literal_token);
2692
2693 if (! literal -> value)
2694 control.long_pool.FindOrInsertLong(literal);
2695 if (literal -> value == control.BadValue())
2696 {
2697 ReportSemError(SemanticError::INVALID_LONG_VALUE, long_literal);
2698 long_literal -> symbol = control.no_type;
2699 }
2700 else
2701 {
2702 long_literal -> value = literal -> value;
2703 long_literal -> symbol = control.long_type;
2704 }
2705 }
2706
2707
ProcessFloatLiteral(Ast * expr)2708 void Semantic::ProcessFloatLiteral(Ast* expr)
2709 {
2710 AstFloatLiteral* float_literal = (AstFloatLiteral*) expr;
2711
2712 LiteralSymbol* literal =
2713 lex_stream -> LiteralSymbol(float_literal -> float_literal_token);
2714
2715 if (! literal -> value)
2716 control.float_pool.FindOrInsertFloat(literal);
2717 if (control.option.source < JikesOption::SDK1_5 &&
2718 (literal -> Name()[1] == U_x || literal -> Name()[1] == U_X))
2719 {
2720 ReportSemError(SemanticError::HEX_FLOATING_POINT_UNSUPPORTED,
2721 float_literal);
2722 }
2723 if (literal -> value == control.BadValue())
2724 {
2725 ReportSemError(SemanticError::INVALID_FLOAT_VALUE, float_literal);
2726 float_literal -> symbol = control.no_type;
2727 }
2728 else
2729 {
2730 float_literal -> value = literal -> value;
2731 float_literal -> symbol = control.float_type;
2732 }
2733 }
2734
2735
ProcessDoubleLiteral(Ast * expr)2736 void Semantic::ProcessDoubleLiteral(Ast* expr)
2737 {
2738 AstDoubleLiteral* double_literal = (AstDoubleLiteral*) expr;
2739
2740 LiteralSymbol* literal =
2741 lex_stream -> LiteralSymbol(double_literal -> double_literal_token);
2742
2743 if (! literal -> value)
2744 control.double_pool.FindOrInsertDouble(literal);
2745 if (control.option.source < JikesOption::SDK1_5 &&
2746 (literal -> Name()[1] == U_x || literal -> Name()[1] == U_X))
2747 {
2748 ReportSemError(SemanticError::HEX_FLOATING_POINT_UNSUPPORTED,
2749 double_literal);
2750 }
2751 if (literal -> value == control.BadValue())
2752 {
2753 ReportSemError(SemanticError::INVALID_DOUBLE_VALUE, double_literal);
2754 double_literal -> symbol = control.no_type;
2755 }
2756 else
2757 {
2758 double_literal -> value = literal -> value;
2759 double_literal -> symbol = control.double_type;
2760 }
2761 }
2762
2763
ProcessTrueLiteral(Ast * expr)2764 void Semantic::ProcessTrueLiteral(Ast* expr)
2765 {
2766 AstExpression* true_literal = (AstTrueLiteral*) expr;
2767
2768 true_literal -> value = control.int_pool.FindOrInsert((int) 1);
2769 true_literal -> symbol = control.boolean_type;
2770 }
2771
2772
ProcessFalseLiteral(Ast * expr)2773 void Semantic::ProcessFalseLiteral(Ast* expr)
2774 {
2775 AstExpression* false_literal = (AstFalseLiteral*) expr;
2776
2777 false_literal -> value = control.int_pool.FindOrInsert((int) 0);
2778 false_literal -> symbol = control.boolean_type;
2779 }
2780
2781
ProcessStringLiteral(Ast * expr)2782 void Semantic::ProcessStringLiteral(Ast* expr)
2783 {
2784 AstStringLiteral* string_literal = (AstStringLiteral*) expr;
2785
2786 LiteralSymbol* literal =
2787 lex_stream -> LiteralSymbol(string_literal -> string_literal_token);
2788
2789 if (! literal -> value)
2790 control.Utf8_pool.FindOrInsertString(literal);
2791 if (literal -> value == control.BadValue())
2792 string_literal -> symbol = control.no_type;
2793 else
2794 {
2795 string_literal -> value = literal -> value;
2796 string_literal -> symbol = control.String();
2797 }
2798 }
2799
2800
ProcessArrayAccess(Ast * expr)2801 void Semantic::ProcessArrayAccess(Ast* expr)
2802 {
2803 AstArrayAccess* array_access = (AstArrayAccess*) expr;
2804
2805 ProcessExpression(array_access -> base);
2806 ProcessExpression(array_access -> expression);
2807 array_access -> expression =
2808 PromoteUnaryNumericExpression(array_access -> expression);
2809 if (array_access -> expression -> Type() != control.int_type)
2810 {
2811 TypeSymbol* type = array_access -> expression -> Type();
2812 if (array_access -> expression -> symbol != control.no_type)
2813 ReportSemError(SemanticError::TYPE_NOT_INTEGER,
2814 array_access -> expression,
2815 type -> ContainingPackageName(),
2816 type -> ExternalName());
2817 array_access -> symbol = control.no_type;
2818 }
2819
2820 TypeSymbol* array_type = array_access -> base -> Type();
2821 if (array_type -> IsArray())
2822 {
2823 if (! array_access -> symbol)
2824 array_access -> symbol = array_type -> ArraySubtype();
2825 }
2826 else
2827 {
2828 if (array_type != control.no_type)
2829 ReportSemError(SemanticError::TYPE_NOT_ARRAY,
2830 array_access -> base,
2831 array_type -> ContainingPackageName(),
2832 array_type -> ExternalName());
2833 array_access -> symbol = control.no_type;
2834 }
2835 }
2836
2837
FindMethodMember(TypeSymbol * type,AstMethodInvocation * method_call)2838 MethodShadowSymbol* Semantic::FindMethodMember(TypeSymbol* type,
2839 AstMethodInvocation* method_call)
2840 {
2841 AstExpression* base = method_call -> base_opt;
2842 TokenIndex id_token = method_call -> identifier_token;
2843 assert(base);
2844 //
2845 // TypeCast() returns true for super, this, and instance creation as
2846 // well as true type names, hence the extra check
2847 //
2848 bool base_is_type = base -> symbol -> TypeCast() && base -> NameCast();
2849 MethodShadowSymbol* shadow = NULL;
2850
2851 if (type -> Bad())
2852 {
2853 //
2854 // If no error has been detected so far, report this as an error so
2855 // that we don't try to generate code later. On the other hand, if an
2856 // error had been detected prior to this, don't flood the user with
2857 // spurious messages.
2858 //
2859 if (NumErrors() == 0)
2860 ReportMethodNotFound(method_call, type);
2861 method_call -> symbol = control.no_type;
2862 }
2863 else if (type == control.null_type || type -> Primitive())
2864 {
2865 ReportSemError(SemanticError::TYPE_NOT_REFERENCE, base,
2866 type -> Name());
2867 method_call -> symbol = control.no_type;
2868 }
2869 else
2870 {
2871 TypeSymbol* this_type = ThisType();
2872 if (! TypeAccessCheck(type))
2873 {
2874 ReportTypeInaccessible(base, type);
2875 method_call -> symbol = control.no_type;
2876 return shadow;
2877 }
2878
2879 shadow = FindMethodInType(type, method_call);
2880 MethodSymbol* method = (shadow ? shadow -> method_symbol
2881 : (MethodSymbol*) NULL);
2882 if (method)
2883 {
2884 assert(method -> IsTyped());
2885
2886 if (base_is_type && ! method -> ACC_STATIC())
2887 {
2888 ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
2889 method_call -> LeftToken(), id_token,
2890 lex_stream -> NameString(id_token));
2891 method_call -> symbol = control.no_type;
2892 return NULL;
2893 }
2894 if (method -> ACC_STATIC() && ! base_is_type)
2895 {
2896 ReportSemError(SemanticError::CLASS_METHOD_INVOKED_VIA_INSTANCE,
2897 method_call -> LeftToken(), id_token,
2898 lex_stream -> NameString(id_token));
2899 }
2900
2901 //
2902 // Apply method invocation conversion to the parameters
2903 //
2904 MethodInvocationConversion(method_call -> arguments, method);
2905
2906 //
2907 // Access to a private or protected variable in or via an enclosing
2908 // type? If the base is a super expression, be sure to start from
2909 // the correct enclosing instance.
2910 //
2911 TypeSymbol* containing_type = method -> containing_type;
2912 TypeSymbol* target_type = containing_type;
2913 if (! method -> ACC_STATIC() && base -> SuperExpressionCast())
2914 {
2915 AstSuperExpression* super_expr = (AstSuperExpression*) base;
2916 if (super_expr -> base_opt)
2917 target_type = super_expr -> base_opt -> symbol;
2918 }
2919 if (this_type != target_type &&
2920 (method -> ACC_PRIVATE() ||
2921 (method -> ACC_PROTECTED() &&
2922 ! ProtectedAccessCheck(containing_type)) ||
2923 (target_type != containing_type &&
2924 target_type != this_type)))
2925 {
2926 //
2927 // Find the right enclosing class to place the accessor method
2928 // in. For private methods, the containing type; for protected
2929 // methods or superclass methods, an enclosing class which is
2930 // related to the containing type.
2931 //
2932 TypeSymbol* environment_type = containing_type;
2933 if (! method -> ACC_PRIVATE())
2934 {
2935 for (SemanticEnvironment* env = this_type -> semantic_environment;
2936 env; env = env -> previous)
2937 {
2938 if (env -> Type() -> IsSubclass(target_type))
2939 {
2940 environment_type = env -> Type();
2941 break;
2942 }
2943 }
2944 assert(environment_type != containing_type &&
2945 environment_type != this_type);
2946 }
2947
2948 AstArguments* args = compilation_unit -> ast_pool ->
2949 GenArguments(method_call -> arguments -> left_parenthesis_token,
2950 method_call -> arguments -> right_parenthesis_token);
2951 unsigned num_args = method_call -> arguments -> NumArguments();
2952 if (! method -> ACC_STATIC())
2953 {
2954 args -> AllocateArguments(num_args + 1);
2955 args -> AddArgument(base);
2956 }
2957 else args -> AllocateArguments(num_args);
2958 for (unsigned i = 0; i < num_args; i++)
2959 args -> AddArgument(method_call -> arguments -> Argument(i));
2960
2961 AstMethodInvocation* accessor = compilation_unit ->
2962 ast_pool -> GenMethodInvocation(id_token);
2963 // TODO: WARNING: sharing of subtrees...
2964 accessor -> base_opt = base;
2965 accessor -> arguments = args;
2966 accessor -> symbol = environment_type ->
2967 GetReadAccessMethod(method, base -> Type());
2968
2969 method_call -> symbol = method;
2970 method_call -> resolution_opt = accessor;
2971 }
2972 else method_call -> symbol = method;
2973 }
2974 else
2975 {
2976 method_call -> symbol = control.no_type;
2977 }
2978 }
2979 return shadow;
2980 }
2981
2982
ProcessMethodName(AstMethodInvocation * method_call)2983 void Semantic::ProcessMethodName(AstMethodInvocation* method_call)
2984 {
2985 TypeSymbol* this_type = ThisType();
2986 AstExpression* base = method_call -> base_opt;
2987 TokenIndex id_token = method_call -> identifier_token;
2988 TypeSymbol* base_type;
2989 MethodShadowSymbol* method_shadow;
2990 if (! base)
2991 {
2992 SemanticEnvironment* where_found;
2993 method_shadow = FindMethodInEnvironment(where_found, method_call);
2994 if (! method_shadow)
2995 {
2996 method_call -> symbol = control.no_type;
2997 base_type = NULL;
2998 }
2999 else
3000 {
3001 base_type = where_found -> Type();
3002 MethodSymbol* method = method_shadow -> method_symbol;
3003 assert(method -> IsTyped());
3004
3005 if (! method -> ACC_STATIC())
3006 {
3007 if (ExplicitConstructorInvocation())
3008 {
3009 if (where_found == state_stack.Top())
3010 {
3011 //
3012 // If the method belongs to this type, including
3013 // inherited from an enclosing type, it is not
3014 // accessible.
3015 //
3016 ReportSemError(SemanticError::INSTANCE_METHOD_IN_EXPLICIT_CONSTRUCTOR,
3017 method_call, method -> Header(),
3018 method -> containing_type -> Name());
3019 method_call -> symbol = control.no_type;
3020 method_shadow = NULL;
3021 }
3022 }
3023 else if (StaticRegion())
3024 {
3025 ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
3026 method_call,
3027 lex_stream -> NameString(id_token));
3028 method_call -> symbol = control.no_type;
3029 method_shadow = NULL;
3030 }
3031 }
3032
3033 //
3034 // Apply method invocation conversion to the parameters
3035 //
3036 MethodInvocationConversion(method_call -> arguments, method);
3037 method_call -> symbol = method;
3038
3039 //
3040 // If the method is a private method belonging to an outer type,
3041 // give the ast simple_name access to its read_method.
3042 //
3043 if (where_found != state_stack.Top())
3044 CreateAccessToScopedMethod(method_call, where_found -> Type());
3045 }
3046 }
3047 else
3048 {
3049 //
3050 // ...First, classify the name or expression to the left of the '.'...
3051 // If there are more names to the left, we short-circuit
3052 // ProcessFieldAccess, since we already know what context the name
3053 // is in.
3054 //
3055 if (base -> NameCast())
3056 ProcessAmbiguousName((AstName*) base);
3057 else // The qualifier might be a complex String constant
3058 ProcessExpressionOrStringConstant(base);
3059
3060 if (base -> symbol -> PackageCast())
3061 {
3062 ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME, base,
3063 base -> symbol -> PackageCast() -> PackageName());
3064 base -> symbol = control.no_type;
3065 }
3066
3067 base_type = base -> Type();
3068 assert(base_type);
3069
3070 if (base_type == control.no_type)
3071 {
3072 method_call -> symbol = control.no_type;
3073 method_shadow = NULL;
3074 }
3075 else
3076 method_shadow = FindMethodMember(base_type, method_call);
3077 if (base -> SuperExpressionCast())
3078 {
3079 //
3080 // JLS2 15.12.3 requires this test
3081 //
3082 MethodSymbol* method = method_call -> symbol -> MethodCast();
3083 if (method && method -> ACC_ABSTRACT())
3084 {
3085 ReportSemError(SemanticError::ABSTRACT_METHOD_INVOCATION,
3086 method_call,
3087 lex_stream -> NameString(id_token));
3088 }
3089 }
3090 else AddDependence(this_type, base_type);
3091 }
3092
3093 //
3094 // If we found a candidate, proceed to check the throws clauses. If
3095 // base_type inherited multiple abstract methods, then this calling
3096 // environment must merge the throws clauses (although it may invoke an
3097 // arbitrary method from the set). Be careful of default and protected
3098 // abstract methods which are not accessible when doing this merge.
3099 //
3100 if (method_shadow)
3101 {
3102 MethodSymbol* method = (MethodSymbol*) method_call -> symbol;
3103 if (! MemberAccessCheck(base_type, method, base))
3104 {
3105 assert(method_shadow -> NumConflicts() > 0);
3106 method = method_shadow -> Conflict(0);
3107 method_call -> symbol = method;
3108 }
3109
3110 SymbolSet exceptions(method -> NumThrows());
3111 int i, j;
3112 // First, the base set
3113 for (i = method -> NumThrows(); --i >= 0; )
3114 exceptions.AddElement(method -> Throws(i));
3115 // Next, add all subclasses thrown in method conflicts
3116 for (i = method_shadow -> NumConflicts(); --i >= 0; )
3117 {
3118 MethodSymbol* conflict = method_shadow -> Conflict(i);
3119 conflict -> ProcessMethodThrows(this,
3120 method_call -> identifier_token);
3121 for (j = conflict -> NumThrows(); --j >= 0; )
3122 {
3123 TypeSymbol* candidate = conflict -> Throws(j);
3124 for (TypeSymbol* ex = (TypeSymbol*) exceptions.FirstElement();
3125 ex; ex = (TypeSymbol*) exceptions.NextElement())
3126 {
3127 if (candidate -> IsSubclass(ex))
3128 {
3129 exceptions.AddElement(candidate);
3130 break;
3131 }
3132 }
3133 }
3134 }
3135 // Finally, prune all methods not thrown by all conflicts, and report
3136 // uncaught exceptions.
3137 TypeSymbol* ex = (TypeSymbol*) exceptions.FirstElement();
3138 while (ex)
3139 {
3140 bool remove = false;
3141 for (i = method_shadow -> NumConflicts(); --i >= 0; )
3142 {
3143 MethodSymbol* conflict = method_shadow -> Conflict(i);
3144 for (j = conflict -> NumThrows(); --j >= 0; )
3145 {
3146 TypeSymbol* candidate = conflict -> Throws(j);
3147 if (ex -> IsSubclass(candidate))
3148 break;
3149 }
3150 if (j < 0)
3151 {
3152 remove = true;
3153 break;
3154 }
3155 }
3156 TypeSymbol* temp = (TypeSymbol*) exceptions.NextElement();
3157 if (remove)
3158 exceptions.RemoveElement(ex);
3159 else if (UncaughtException(ex))
3160 ReportSemError(SemanticError::UNCAUGHT_METHOD_EXCEPTION,
3161 method_call, method -> Header(),
3162 ex -> ContainingPackageName(),
3163 ex -> ExternalName(),
3164 UncaughtExceptionContext());
3165 ex = temp;
3166 }
3167
3168 SymbolSet* exception_set = TryExceptionTableStack().Top();
3169 if (exception_set)
3170 exception_set -> Union(exceptions);
3171 }
3172 else
3173 {
3174 //
3175 // There was no candidate, so we have no idea what can be thrown in
3176 // a try block if it had been a valid method call.
3177 //
3178 SymbolSet* exception_set = TryExceptionTableStack().Top();
3179 if (exception_set)
3180 exception_set -> AddElement(control.no_type);
3181 }
3182 }
3183
3184
3185 //
3186 // Processes the argument list, returning true if the list contains an
3187 // invalid expression.
3188 //
ProcessArguments(AstArguments * args)3189 bool Semantic::ProcessArguments(AstArguments* args)
3190 {
3191 bool bad_argument = false;
3192 for (unsigned i = 0; i < args -> NumArguments(); i++)
3193 {
3194 AstExpression* expr = args -> Argument(i);
3195 ProcessExpressionOrStringConstant(expr);
3196 if (expr -> symbol == control.no_type)
3197 bad_argument = true;
3198 else if (expr -> Type() == control.void_type)
3199 {
3200 ReportSemError(SemanticError::TYPE_IS_VOID, expr,
3201 expr -> Type() -> Name());
3202 bad_argument = true;
3203 }
3204 }
3205 return bad_argument;
3206 }
3207
3208
ProcessMethodInvocation(Ast * expr)3209 void Semantic::ProcessMethodInvocation(Ast* expr)
3210 {
3211 AstMethodInvocation* method_call = (AstMethodInvocation*) expr;
3212
3213 if (method_call -> type_arguments_opt)
3214 {
3215 ReportSemError(SemanticError::EXPLICIT_TYPE_ARGUMENTS_UNSUPPORTED,
3216 method_call -> type_arguments_opt);
3217 }
3218 bool bad_argument = ProcessArguments(method_call -> arguments);
3219 if (bad_argument)
3220 method_call -> symbol = control.no_type;
3221 else ProcessMethodName(method_call);
3222 assert(method_call -> symbol == control.no_type ||
3223 ((MethodSymbol*) method_call -> symbol) -> IsTyped());
3224 }
3225
3226
ProcessNullLiteral(Ast * expr)3227 void Semantic::ProcessNullLiteral(Ast* expr)
3228 {
3229 //
3230 // Null is not a compile-time constant, so don't give it a value
3231 //
3232 AstNullLiteral* null_literal = (AstNullLiteral*) expr;
3233 null_literal -> symbol = control.null_type;
3234 }
3235
3236
ProcessClassLiteral(Ast * expr)3237 void Semantic::ProcessClassLiteral(Ast* expr)
3238 {
3239 TypeSymbol* this_type = ThisType();
3240 AstClassLiteral* class_lit = (AstClassLiteral*) expr;
3241 //
3242 // In a clone, simply return control.no_type. We are in a clone only
3243 // when doing something like evaluating a forward reference to a final
3244 // field for its constant value, but a class literal has no constant
3245 // value. In such cases, this method will again be invoked when we
3246 // finally reach the field, and then it is finally appropriate to
3247 // resolve the reference.
3248 //
3249 if (error && error -> InClone())
3250 {
3251 class_lit -> symbol = control.no_type;
3252 return;
3253 }
3254 ProcessType(class_lit -> type);
3255 TypeSymbol* type = class_lit -> type -> symbol;
3256 AddDependence(this_type, type -> BoxedType(control));
3257 if (type == control.no_type)
3258 class_lit -> symbol = control.no_type;
3259 else if (type -> Primitive())
3260 {
3261 if (type == control.int_type)
3262 class_lit -> symbol = control.Integer_TYPE_Field();
3263 else if (type == control.double_type)
3264 class_lit -> symbol = control.Double_TYPE_Field();
3265 else if (type == control.char_type)
3266 class_lit -> symbol = control.Character_TYPE_Field();
3267 else if (type == control.long_type)
3268 class_lit -> symbol = control.Long_TYPE_Field();
3269 else if (type == control.float_type)
3270 class_lit -> symbol = control.Float_TYPE_Field();
3271 else if (type == control.byte_type)
3272 class_lit -> symbol = control.Byte_TYPE_Field();
3273 else if (type == control.short_type)
3274 class_lit -> symbol = control.Short_TYPE_Field();
3275 else if (type == control.boolean_type)
3276 class_lit -> symbol = control.Boolean_TYPE_Field();
3277 else
3278 {
3279 assert(type == control.void_type);
3280 class_lit -> symbol = control.Void_TYPE_Field();
3281 }
3282 }
3283 else if (control.option.target < JikesOption::SDK1_5)
3284 {
3285 //
3286 // We have already checked that the type is accessible. Older VMs
3287 // require a helper method to resolve the reference.
3288 //
3289 VariableSymbol* var = this_type -> FindOrInsertClassLiteral(type);
3290 AstName* name = compilation_unit -> ast_pool ->
3291 GenName(class_lit -> class_token);
3292 name -> symbol = var;
3293 class_lit -> symbol = var;
3294 class_lit -> resolution_opt = name;
3295 }
3296 else class_lit -> symbol = control.Class();
3297 }
3298
3299
ProcessThisExpression(Ast * expr)3300 void Semantic::ProcessThisExpression(Ast* expr)
3301 {
3302 TypeSymbol* this_type = ThisType();
3303 AstThisExpression* this_expression = (AstThisExpression*) expr;
3304 AstTypeName* base = this_expression -> base_opt;
3305 if (base)
3306 {
3307 ProcessType(base);
3308 TypeSymbol* enclosing_type = base -> symbol;
3309 if (enclosing_type == control.no_type)
3310 this_expression -> symbol = control.no_type;
3311 else if (! enclosing_type)
3312 {
3313 ReportSemError(SemanticError::NOT_A_TYPE, base);
3314 this_expression -> symbol = control.no_type;
3315 }
3316 else if (enclosing_type -> ACC_INTERFACE())
3317 {
3318 ReportSemError(SemanticError::NOT_A_CLASS, base,
3319 enclosing_type -> ContainingPackageName(),
3320 enclosing_type -> ExternalName());
3321 this_expression -> symbol = control.no_type;
3322 }
3323 else if (ExplicitConstructorInvocation() &&
3324 enclosing_type == this_type)
3325 {
3326 ReportSemError(SemanticError::SELF_IN_EXPLICIT_CONSTRUCTOR,
3327 base -> LeftToken(),
3328 this_expression -> this_token,
3329 StringConstant::US_this);
3330 this_expression -> symbol = control.no_type;
3331 }
3332 else if (! this_type -> IsNestedIn(enclosing_type))
3333 {
3334 ReportSemError(SemanticError::ILLEGAL_THIS_FIELD_ACCESS,
3335 base -> LeftToken(),
3336 this_expression -> this_token,
3337 enclosing_type -> ContainingPackageName(),
3338 enclosing_type -> ExternalName(),
3339 this_package -> PackageName(),
3340 this_type -> ExternalName());
3341 this_expression -> symbol = control.no_type;
3342 }
3343 else if (this_type == enclosing_type)
3344 {
3345 if (StaticRegion())
3346 {
3347 ReportSemError(SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE,
3348 base -> LeftToken(),
3349 this_expression -> this_token,
3350 enclosing_type -> ContainingPackageName(),
3351 enclosing_type -> ExternalName());
3352 this_expression -> symbol = control.no_type;
3353 }
3354 else this_expression -> symbol = this_type;
3355 }
3356 else
3357 {
3358 this_expression -> resolution_opt =
3359 CreateAccessToType(this_expression, enclosing_type);
3360 this_expression -> symbol =
3361 this_expression -> resolution_opt -> symbol;
3362 }
3363 }
3364 else // unqualified
3365 {
3366 if (ExplicitConstructorInvocation())
3367 {
3368 ReportSemError(SemanticError::SELF_IN_EXPLICIT_CONSTRUCTOR,
3369 this_expression -> this_token,
3370 StringConstant::US_this);
3371 this_expression -> symbol = control.no_type;
3372 }
3373 else if (StaticRegion())
3374 {
3375 ReportSemError(SemanticError::MISPLACED_THIS_EXPRESSION,
3376 this_expression -> this_token);
3377 this_expression -> symbol = control.no_type;
3378 }
3379 else this_expression -> symbol = this_type;
3380 }
3381 }
3382
3383
ProcessSuperExpression(Ast * expr)3384 void Semantic::ProcessSuperExpression(Ast* expr)
3385 {
3386 TypeSymbol* this_type = ThisType();
3387 AstSuperExpression* super_expression = (AstSuperExpression*) expr;
3388 AstTypeName* base = super_expression -> base_opt;
3389 if (base)
3390 {
3391 ProcessType(base);
3392 TypeSymbol* enclosing_type = base -> symbol;
3393 if (enclosing_type == control.no_type)
3394 super_expression -> symbol = control.no_type;
3395 else if (! enclosing_type)
3396 {
3397 ReportSemError(SemanticError::NOT_A_TYPE, base);
3398 super_expression -> symbol = control.no_type;
3399 }
3400 else if (enclosing_type -> ACC_INTERFACE())
3401 {
3402 ReportSemError(SemanticError::NOT_A_CLASS, base,
3403 enclosing_type -> ContainingPackageName(),
3404 enclosing_type -> ExternalName());
3405 super_expression -> symbol = control.no_type;
3406 }
3407 else if (this_type == control.Object())
3408 {
3409 ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
3410 base -> LeftToken(),
3411 super_expression -> super_token);
3412 super_expression -> symbol = control.no_type;
3413 }
3414 else if (ExplicitConstructorInvocation() &&
3415 enclosing_type == this_type)
3416 {
3417 ReportSemError(SemanticError::SELF_IN_EXPLICIT_CONSTRUCTOR,
3418 base -> LeftToken(),
3419 super_expression -> super_token,
3420 StringConstant::US_super);
3421 super_expression -> symbol = control.no_type;
3422 }
3423 else if (! this_type -> IsNestedIn(enclosing_type))
3424 {
3425 ReportSemError(SemanticError::ILLEGAL_THIS_FIELD_ACCESS,
3426 base -> LeftToken(),
3427 super_expression -> super_token,
3428 enclosing_type -> ContainingPackageName(),
3429 enclosing_type -> ExternalName(),
3430 this_package -> PackageName(),
3431 this_type -> ExternalName());
3432 super_expression -> symbol = control.no_type;
3433 }
3434 else if (this_type == enclosing_type)
3435 {
3436 if (StaticRegion())
3437 {
3438 ReportSemError(SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE,
3439 base -> LeftToken(),
3440 super_expression -> super_token,
3441 enclosing_type -> ContainingPackageName(),
3442 enclosing_type -> ExternalName());
3443 super_expression -> symbol = control.no_type;
3444 }
3445 else super_expression -> symbol = this_type -> super;
3446 }
3447 else
3448 {
3449 super_expression -> resolution_opt =
3450 CreateAccessToType(super_expression, enclosing_type);
3451 super_expression -> symbol =
3452 super_expression -> resolution_opt -> symbol;
3453 }
3454 }
3455 else // unqualified
3456 {
3457 if (ThisType() == control.Object())
3458 {
3459 ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
3460 super_expression -> super_token);
3461 super_expression -> symbol = control.no_type;
3462 }
3463 else if (ExplicitConstructorInvocation())
3464 {
3465 ReportSemError(SemanticError::SELF_IN_EXPLICIT_CONSTRUCTOR,
3466 super_expression -> super_token,
3467 StringConstant::US_super);
3468 super_expression -> symbol = control.no_type;
3469 }
3470 else if (StaticRegion())
3471 {
3472 ReportSemError(SemanticError::MISPLACED_SUPER_EXPRESSION,
3473 super_expression -> super_token);
3474 super_expression -> symbol = control.no_type;
3475 }
3476 else super_expression -> symbol = ThisType() -> super;
3477 }
3478 }
3479
3480
ProcessParenthesizedExpression(Ast * expr)3481 void Semantic::ProcessParenthesizedExpression(Ast* expr)
3482 {
3483 AstParenthesizedExpression* parenthesized =
3484 (AstParenthesizedExpression*) expr;
3485
3486 //
3487 // Do not use ProcessExpressionOrStringConstant here, to avoid generating
3488 // intermediate Strings - see CheckConstantString in lookup.cpp
3489 //
3490 ProcessExpression(parenthesized -> expression);
3491 if (parenthesized -> expression -> Type() == control.void_type)
3492 {
3493 ReportSemError(SemanticError::TYPE_IS_VOID,
3494 parenthesized -> expression,
3495 control.void_type -> Name());
3496 parenthesized -> symbol = control.no_type;
3497 }
3498 else
3499 {
3500 parenthesized -> value = parenthesized -> expression -> value;
3501 parenthesized -> symbol = parenthesized -> expression -> symbol;
3502 }
3503 }
3504
3505
UpdateLocalConstructors(TypeSymbol * inner_type)3506 void Semantic::UpdateLocalConstructors(TypeSymbol* inner_type)
3507 {
3508 assert(inner_type -> IsLocal() &&
3509 (! inner_type -> Anonymous() || ! inner_type -> EnclosingType()));
3510
3511 //
3512 // Update the constructor signatures to account for local shadow
3513 // parameters.
3514 //
3515 inner_type -> MarkLocalClassProcessingCompleted();
3516 unsigned param_count = inner_type -> NumConstructorParameters();
3517 if (param_count)
3518 {
3519 MethodSymbol* ctor;
3520 for (ctor = inner_type -> FindMethodSymbol(control.init_name_symbol);
3521 ctor; ctor = ctor -> next_method)
3522 {
3523 ctor -> SetSignature(control);
3524 }
3525 for (unsigned j = 0;
3526 j < inner_type -> NumPrivateAccessConstructors(); j++)
3527 {
3528 inner_type -> PrivateAccessConstructor(j) ->
3529 SetSignature(control, (inner_type -> outermost_type ->
3530 GetPlaceholderType()));
3531 }
3532 }
3533
3534 //
3535 // Update all constructor call contexts that were pending on this class.
3536 // These calls are necessarily located within the body of inner_type, and
3537 // are calling a constructor in inner_type.
3538 //
3539 for (unsigned i = 0;
3540 i < inner_type -> NumLocalConstructorCallEnvironments(); i++)
3541 {
3542 SemanticEnvironment* env =
3543 inner_type -> LocalConstructorCallEnvironment(i);
3544 state_stack.Push(env);
3545 AstArguments* args = env -> args;
3546
3547 args -> AllocateLocalArguments(param_count);
3548 for (unsigned k = 0; k < param_count; k++)
3549 {
3550 AstName* name = compilation_unit ->
3551 ast_pool -> GenName(args -> right_parenthesis_token);
3552 VariableSymbol* accessor =
3553 FindLocalVariable(inner_type -> ConstructorParameter(k),
3554 ThisType());
3555 name -> symbol = accessor;
3556 TypeSymbol* owner = accessor -> ContainingType();
3557 if (owner != ThisType())
3558 CreateAccessToScopedVariable(name, owner);
3559 args -> AddLocalArgument(name);
3560 }
3561 if (ThisType() -> Anonymous() &&
3562 ! ThisType() -> LocalClassProcessingCompleted())
3563 {
3564 UpdateLocalConstructors(ThisType());
3565 }
3566 state_stack.Pop();
3567 }
3568 }
3569
3570
3571 //
3572 // This creates the default constructor for an anonymous class, and sets
3573 // the resolution_opt field of the original to a generated instance creation
3574 // expression that has been adjusted for compilation purposes.
3575 //
GetAnonymousConstructor(AstClassCreationExpression * class_creation,TypeSymbol * anonymous_type)3576 void Semantic::GetAnonymousConstructor(AstClassCreationExpression* class_creation,
3577 TypeSymbol* anonymous_type)
3578 {
3579 TokenIndex left_loc = class_creation -> class_type -> LeftToken();
3580 TokenIndex right_loc =
3581 class_creation -> arguments -> right_parenthesis_token;
3582
3583 state_stack.Push(anonymous_type -> semantic_environment);
3584 TypeSymbol* super_type = anonymous_type -> super;
3585 MethodSymbol* super_constructor = FindConstructor(super_type,
3586 class_creation,
3587 left_loc, right_loc);
3588 if (! super_constructor)
3589 {
3590 class_creation -> class_type -> symbol = control.no_type;
3591 state_stack.Pop();
3592 return;
3593 }
3594 assert(super_constructor -> IsTyped());
3595
3596 //
3597 // Make replacement class instance creation expression.
3598 //
3599 AstArguments* resolution_args = compilation_unit -> ast_pool ->
3600 GenArguments(class_creation -> arguments -> left_parenthesis_token,
3601 right_loc);
3602
3603 AstClassCreationExpression* resolution =
3604 compilation_unit -> ast_pool -> GenClassCreationExpression();
3605 resolution -> new_token = class_creation -> new_token;
3606 // TODO: WARNING: sharing of subtrees...
3607 resolution -> class_type = class_creation -> class_type;
3608 resolution -> arguments = resolution_args;
3609 resolution -> symbol = anonymous_type;
3610 class_creation -> resolution_opt = resolution;
3611
3612 //
3613 // Make constructor symbol. The associated symbol table will not contain
3614 // too many elements...
3615 //
3616 BlockSymbol* block_symbol =
3617 new BlockSymbol(super_constructor -> NumFormalParameters() + 3);
3618 block_symbol -> max_variable_index = 1; // A spot for "this".
3619
3620 MethodSymbol* constructor =
3621 anonymous_type -> InsertMethodSymbol(control.init_name_symbol);
3622 constructor -> SetType(anonymous_type);
3623 constructor -> SetContainingType(anonymous_type);
3624 constructor -> SetBlockSymbol(block_symbol);
3625
3626 //
3627 // Anonymous class constructors may throw any exception listed in the
3628 // superclass; but this list may be expanded later since the anonymous
3629 // constructor also throws anything possible in instance initializers.
3630 //
3631 for (unsigned i = 0; i < super_constructor -> NumThrows(); i++)
3632 constructor -> AddThrows(super_constructor -> Throws(i));
3633
3634 //
3635 // If we are in a static region, the anonymous constructor does not need
3636 // a this$0 argument. Otherwise, a this$0 argument that points to an
3637 // instance of the immediately enclosing class is required.
3638 //
3639 if (anonymous_type -> EnclosingType())
3640 {
3641 VariableSymbol* this0_variable =
3642 block_symbol -> InsertVariableSymbol(control.this_name_symbol);
3643 this0_variable -> SetType(anonymous_type -> EnclosingType());
3644 this0_variable -> SetOwner(constructor);
3645 this0_variable -> SetFlags(AccessFlags::ACCESS_FINAL |
3646 AccessFlags::ACCESS_SYNTHETIC);
3647 this0_variable -> SetLocalVariableIndex(block_symbol ->
3648 max_variable_index++);
3649 this0_variable -> MarkComplete();
3650 AstThisExpression* this0_expression =
3651 compilation_unit -> ast_pool -> GenThisExpression(left_loc);
3652 this0_expression -> symbol = anonymous_type -> EnclosingType();
3653 resolution -> base_opt = this0_expression;
3654 }
3655
3656 //
3657 // Create an explicit call to the superconstructor, passing any necessary
3658 // shadow variables or enclosing instances.
3659 //
3660 AstArguments* super_args = compilation_unit -> ast_pool ->
3661 GenArguments(class_creation -> arguments -> left_parenthesis_token,
3662 right_loc);
3663
3664 AstSuperCall* super_call = compilation_unit -> ast_pool -> GenSuperCall();
3665 if (super_constructor -> ACC_PRIVATE())
3666 {
3667 super_constructor =
3668 super_type -> GetReadAccessConstructor(super_constructor);
3669 super_args -> AddNullArgument();
3670 }
3671
3672 // Use initial base_opt.
3673 super_call -> base_opt = class_creation -> base_opt;
3674 super_call -> super_token = class_creation -> new_token;
3675 super_call -> arguments = super_args;
3676 super_call -> semicolon_token = right_loc;
3677 super_call -> symbol = super_constructor;
3678
3679 AstClassBody* class_body = class_creation -> class_body_opt;
3680
3681 //
3682 // Construct the default constructor of the anonymous type.
3683 //
3684 AstMethodBody* constructor_block =
3685 compilation_unit -> ast_pool -> GenMethodBody();
3686 // This symbol table will be empty.
3687 constructor_block -> block_symbol =
3688 constructor -> block_symbol -> InsertBlockSymbol(0);
3689 constructor_block -> left_brace_token = class_body -> left_brace_token;
3690 constructor_block -> right_brace_token = class_body -> left_brace_token;
3691 constructor_block -> explicit_constructor_opt = super_call;
3692 constructor_block -> AllocateStatements(1); // for the generated return
3693
3694 AstMethodDeclarator* method_declarator =
3695 compilation_unit -> ast_pool -> GenMethodDeclarator();
3696 method_declarator -> identifier_token = left_loc;
3697 method_declarator -> left_parenthesis_token =
3698 class_creation -> arguments -> left_parenthesis_token;
3699 method_declarator -> right_parenthesis_token = right_loc;
3700
3701 AstConstructorDeclaration* constructor_declaration =
3702 compilation_unit -> ast_pool -> GenConstructorDeclaration();
3703 constructor_declaration -> constructor_declarator = method_declarator;
3704 constructor_declaration -> constructor_body = constructor_block;
3705 constructor_declaration -> constructor_symbol = constructor;
3706
3707 constructor -> declaration = constructor_declaration;
3708 class_body -> default_constructor = constructor_declaration;
3709
3710
3711 //
3712 // Update the enclosing instance of the supertype.
3713 //
3714 unsigned num_args = class_creation -> arguments -> NumArguments();
3715 if (class_creation -> base_opt)
3716 {
3717 VariableSymbol* super_this0_variable =
3718 block_symbol -> InsertVariableSymbol(control.MakeParameter(0));
3719 super_this0_variable -> SetACC_SYNTHETIC();
3720 super_this0_variable -> SetType(super_call -> base_opt -> Type());
3721 super_this0_variable -> SetOwner(constructor);
3722 super_this0_variable -> SetLocalVariableIndex(block_symbol ->
3723 max_variable_index++);
3724 super_this0_variable -> MarkComplete();
3725
3726 resolution_args -> AllocateArguments(num_args + 1);
3727 resolution_args -> AddArgument(class_creation -> base_opt);
3728 constructor -> AddFormalParameter(super_this0_variable);
3729
3730 AstName* name = compilation_unit -> ast_pool ->
3731 GenName(class_creation -> new_token);
3732 name -> symbol = super_this0_variable;
3733 super_call -> base_opt = name;
3734 }
3735 else resolution_args -> AllocateArguments(num_args);
3736 super_args -> AllocateArguments(super_constructor ->
3737 NumFormalParameters());
3738
3739 //
3740 // Next, simply pass all parameters through to the superclass.
3741 //
3742 for (unsigned j = 0; j < super_constructor -> NumFormalParameters(); j++)
3743 {
3744 VariableSymbol* param = super_constructor -> FormalParameter(j);
3745 VariableSymbol* symbol =
3746 block_symbol -> InsertVariableSymbol(param -> Identity());
3747 symbol -> SetType(param -> Type());
3748 symbol -> SetOwner(constructor);
3749 symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
3750 symbol -> MarkComplete();
3751 if (control.IsDoubleWordType(symbol -> Type()))
3752 block_symbol -> max_variable_index++;
3753
3754 resolution_args -> AddArgument(class_creation -> arguments -> Argument(j));
3755 constructor -> AddFormalParameter(symbol);
3756 AstName* name = compilation_unit -> ast_pool ->
3757 GenName(class_creation -> new_token);
3758 name -> symbol = symbol;
3759 super_args -> AddArgument(name);
3760 }
3761
3762 //
3763 // Worry about shadow variables in the super type
3764 //
3765 if (super_type -> IsLocal())
3766 {
3767 unsigned param_count = super_type -> NumConstructorParameters();
3768 if (super_type -> LocalClassProcessingCompleted() && param_count)
3769 {
3770 super_args -> AllocateLocalArguments(param_count);
3771 for (unsigned k = 0; k < param_count; k++)
3772 {
3773 //
3774 // We may need to create a shadow in the outermost
3775 // local class enclosing the variable.
3776 //
3777 AstName* name = compilation_unit ->
3778 ast_pool -> GenName(super_call -> super_token);
3779 VariableSymbol* accessor =
3780 FindLocalVariable(super_type -> ConstructorParameter(k),
3781 anonymous_type);
3782 name -> symbol = accessor;
3783 TypeSymbol* owner = accessor -> ContainingType();
3784 if (owner != anonymous_type)
3785 CreateAccessToScopedVariable(name, owner);
3786 super_args -> AddLocalArgument(name);
3787 }
3788 }
3789 else
3790 {
3791 //
3792 // We are within body of super_type; save processing for
3793 // later, since not all shadows may be known yet. See
3794 // ProcessClassDeclaration.
3795 //
3796 super_type -> AddLocalConstructorCallEnvironment
3797 (GetEnvironment(super_call -> arguments));
3798 }
3799 }
3800 //
3801 // We set the signature of the constructor now, although it may be modified
3802 // later if this is in a local constructor call environment.
3803 //
3804 constructor -> SetSignature(control);
3805 state_stack.Pop();
3806 }
3807
3808 //
3809 // super_type is the type specified in the anonymous constructor,
3810 // which is the supertype of the created anonymous type.
3811 //
GetAnonymousType(AstClassCreationExpression * class_creation,TypeSymbol * super_type)3812 TypeSymbol* Semantic::GetAnonymousType(AstClassCreationExpression* class_creation,
3813 TypeSymbol* super_type)
3814 {
3815 //
3816 // In a clone, simply return control.no_type. We are in a clone only when
3817 // doing something like evaluating a forward reference to a final field for
3818 // its constant value, but an anonymous class has no constant value. In
3819 // such cases, this method will again be invoked when we finally reach the
3820 // field, and then it is finally appropriate to create the class.
3821 //
3822 if (error && error -> InClone())
3823 return control.no_type;
3824
3825 TypeSymbol* this_type = ThisType();
3826 AstClassBody* class_body = class_creation -> class_body_opt;
3827 assert(class_body);
3828 TypeSymbol* outermost_type = this_type -> outermost_type;
3829
3830 //
3831 // Anonymous and local classes can clash if we don't use both when
3832 // determining the id number of this class.
3833 //
3834 IntToWstring value(this_type -> NumLocalTypes() +
3835 this_type -> NumAnonymousTypes() + 1);
3836
3837 int length = this_type -> ExternalNameLength() + 1 +
3838 value.Length(); // +1 for $
3839 wchar_t* anonymous_name = new wchar_t[length + 1]; // +1 for '\0'
3840 wcscpy(anonymous_name, this_type -> ExternalName());
3841 wcscat(anonymous_name, (control.option.target < JikesOption::SDK1_5
3842 ? StringConstant::US_DS : StringConstant::US_MI));
3843 wcscat(anonymous_name, value.String());
3844
3845 NameSymbol* name_symbol = control.FindOrInsertName(anonymous_name, length);
3846 delete [] anonymous_name;
3847
3848 assert(! ThisMethod() || LocalSymbolTable().Top());
3849
3850 TypeSymbol* anon_type =
3851 this_type -> InsertAnonymousTypeSymbol(name_symbol);
3852 anon_type -> MarkAnonymous();
3853 anon_type -> outermost_type = outermost_type;
3854 anon_type -> supertypes_closure = new SymbolSet;
3855 anon_type -> subtypes_closure = new SymbolSet;
3856 anon_type -> semantic_environment =
3857 new SemanticEnvironment(this, anon_type, state_stack.Top());
3858 anon_type -> declaration = class_body;
3859 anon_type -> declaration -> semantic_environment =
3860 anon_type -> semantic_environment;
3861 anon_type -> file_symbol = source_file_symbol;
3862 if (ThisMethod())
3863 anon_type -> SetOwner(ThisMethod());
3864 else if (ThisVariable())
3865 {
3866 //
3867 // Creating an anonymous class in a field initializer necessarily
3868 // requires non-trivial code, so the initializer method should
3869 // exist as the owner of this type.
3870 //
3871 assert(ThisVariable() -> ACC_STATIC()
3872 ? this_type -> static_initializer_method
3873 : (this_type -> FindMethodSymbol(control.
3874 block_init_name_symbol)));
3875 anon_type ->
3876 SetOwner(ThisVariable() -> ACC_STATIC()
3877 ? this_type -> static_initializer_method
3878 : (this_type ->
3879 FindMethodSymbol(control.block_init_name_symbol)));
3880 }
3881 else
3882 {
3883 assert(class_creation -> generated);
3884 anon_type -> SetOwner(this_type);
3885 }
3886
3887 //
3888 // Add 3 extra elements for padding. Need a default constructor and
3889 // other support elements.
3890 //
3891 anon_type -> SetSymbolTable(class_body -> NumClassBodyDeclarations() + 3);
3892 anon_type -> SetLocation();
3893 anon_type -> SetSignature(control);
3894
3895 //
3896 // By JLS2 15.9.5, an anonymous class is implicitly final, but never
3897 // static. However, the anonymous class only needs access to its enclosing
3898 // instance if it is not in a static context.
3899 //
3900 anon_type -> SetACC_FINAL();
3901 if (! StaticRegion())
3902 anon_type -> InsertThis0();
3903
3904 if (super_type -> ACC_INTERFACE())
3905 {
3906 anon_type -> AddInterface(super_type);
3907 anon_type -> super = control.Object();
3908 control.Object() -> subtypes -> AddElement(anon_type);
3909 }
3910 else anon_type -> super = super_type;
3911 AddDependence(anon_type, super_type);
3912 super_type -> subtypes -> AddElement(anon_type);
3913 if (super_type -> ACC_FINAL())
3914 {
3915 ReportSemError(SemanticError::SUPER_IS_FINAL,
3916 class_creation -> class_type,
3917 super_type -> ContainingPackageName(),
3918 super_type -> ExternalName());
3919 anon_type -> MarkBad();
3920 }
3921 else if (super_type -> Bad())
3922 anon_type -> MarkBad();
3923
3924 this_type -> AddAnonymousType(anon_type);
3925
3926 //
3927 // Provide the default constructor. For now, we don't worry about accessors
3928 // to final local variables; those are inserted later when completing
3929 // the class instance creation processing. Also, the throws clause may
3930 // expand after processing instance initializer blocks. We keep on
3931 // processing, even if the constructor failed, to detect other semantic
3932 // errors in the anonymous class body.
3933 //
3934 GetAnonymousConstructor(class_creation, anon_type);
3935
3936 //
3937 // Now process the body of the anonymous class !!!
3938 //
3939 CheckNestedMembers(anon_type, class_body);
3940 ProcessTypeHeaders(class_body, anon_type);
3941
3942 //
3943 // If the class body has not yet been parsed, do so now.
3944 //
3945 if (class_body -> UnparsedClassBodyCast())
3946 {
3947 if (! control.parser -> InitializerParse(lex_stream, class_body))
3948 compilation_unit -> MarkBad();
3949 else
3950 {
3951 ProcessMembers(class_body);
3952 CompleteSymbolTable(class_body);
3953 }
3954
3955 if (! control.parser -> BodyParse(lex_stream, class_body))
3956 compilation_unit -> MarkBad();
3957 else ProcessExecutableBodies(class_body);
3958 }
3959 else // The relevant bodies have already been parsed
3960 {
3961 ProcessMembers(class_body);
3962 CompleteSymbolTable(class_body);
3963 ProcessExecutableBodies(class_body);
3964 }
3965
3966 //
3967 // If we failed to provide a default constructor, this is as far as
3968 // we can go.
3969 //
3970 if (class_creation -> class_type -> symbol == control.no_type)
3971 return control.no_type;
3972
3973 //
3974 // Finally, mark the class complete, in order to add any shadow variable
3975 // parameters to the constructor.
3976 //
3977 if (! super_type -> IsLocal() ||
3978 super_type -> LocalClassProcessingCompleted() ||
3979 anon_type -> EnclosingType())
3980 {
3981 if (anon_type -> NumConstructorParameters() && ! anon_type -> Bad())
3982 {
3983 class_body -> default_constructor -> constructor_symbol ->
3984 SetSignature(control);
3985 }
3986 anon_type -> MarkLocalClassProcessingCompleted();
3987 }
3988 return anon_type;
3989 }
3990
3991
ProcessClassCreationExpression(Ast * expr)3992 void Semantic::ProcessClassCreationExpression(Ast* expr)
3993 {
3994 AstClassCreationExpression* class_creation =
3995 (AstClassCreationExpression*) expr;
3996 unsigned i;
3997
3998 //
3999 // For an anonymous type, the qualifier determines the enclosing instance
4000 // of the supertype; as the enclosing instance of the anonymous class (if
4001 // present) is the current class. We update actual_type after this.
4002 //
4003 AstName* actual_type = class_creation -> class_type -> name;
4004 TypeSymbol* type;
4005 if (class_creation -> base_opt)
4006 {
4007 ProcessExpression(class_creation -> base_opt);
4008 TypeSymbol* enclosing_type = class_creation -> base_opt -> Type();
4009 if (! enclosing_type -> IsSubclass(control.Object()))
4010 {
4011 if (enclosing_type != control.no_type)
4012 ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
4013 class_creation -> base_opt,
4014 enclosing_type -> ExternalName());
4015 enclosing_type = control.no_type;
4016 }
4017
4018 //
4019 // The grammar guarantees that the actual type is a simple name.
4020 //
4021 type = MustFindNestedType(enclosing_type, actual_type);
4022 if (type -> ACC_INTERFACE())
4023 {
4024 ReportSemError(SemanticError::INTERFACE_NOT_INNER_CLASS,
4025 actual_type, type -> ContainingPackageName(),
4026 type -> ExternalName());
4027 type = control.no_type;
4028 }
4029 else if (type -> ACC_STATIC())
4030 {
4031 ReportSemError(SemanticError::STATIC_NOT_INNER_CLASS,
4032 actual_type, type -> ContainingPackageName(),
4033 type -> ExternalName());
4034 type = control.no_type;
4035 }
4036 }
4037 else
4038 {
4039 ProcessType(class_creation -> class_type);
4040 type = class_creation -> class_type -> symbol;
4041 if (type -> EnclosingType())
4042 {
4043 AstThisExpression* this_expr = compilation_unit -> ast_pool ->
4044 GenThisExpression(class_creation -> new_token);
4045 this_expr -> resolution_opt =
4046 CreateAccessToType(class_creation, type -> EnclosingType());
4047 this_expr -> symbol = this_expr -> resolution_opt -> symbol;
4048 class_creation -> base_opt = this_expr;
4049 }
4050 }
4051
4052 //
4053 // Check the arguments to the constructor.
4054 //
4055 if (class_creation -> type_arguments_opt)
4056 {
4057 ReportSemError(SemanticError::EXPLICIT_TYPE_ARGUMENTS_UNSUPPORTED,
4058 class_creation -> type_arguments_opt);
4059 }
4060 ProcessArguments(class_creation -> arguments);
4061
4062 //
4063 // Create the anonymous class now, if needed; then check that the type
4064 // can be constructed. A side effect of creating the anonymous class is
4065 // building a resolution constructor invocation that does not have a body;
4066 // this new constructor is necessary to call parameters in the correct
4067 // order, when the superclass of the anonymous class has an enclosing
4068 // instance.
4069 //
4070 if (type -> IsEnum())
4071 {
4072 ReportSemError(SemanticError::CANNOT_CONSTRUCT_ENUM, actual_type,
4073 type -> ContainingPackageName(),
4074 type -> ExternalName());
4075 type = control.no_type;
4076 }
4077 else if (class_creation -> class_body_opt)
4078 {
4079 type = GetAnonymousType(class_creation, type);
4080 class_creation -> symbol = type;
4081 if (type != control.no_type)
4082 class_creation = class_creation -> resolution_opt;
4083 }
4084 else if (type -> ACC_INTERFACE())
4085 {
4086 ReportSemError(SemanticError::NOT_A_CLASS, actual_type,
4087 type -> ContainingPackageName(),
4088 type -> ExternalName());
4089 type = control.no_type;
4090 }
4091 else if (type -> ACC_ABSTRACT())
4092 {
4093 ReportSemError(SemanticError::ABSTRACT_TYPE_CREATION, actual_type,
4094 type -> ExternalName());
4095 }
4096
4097 MethodSymbol* ctor =
4098 FindConstructor(type, class_creation, actual_type -> LeftToken(),
4099 class_creation -> arguments -> right_parenthesis_token);
4100 //
4101 // Convert the arguments to the correct types.
4102 //
4103 if (ctor)
4104 {
4105 assert(ctor -> IsTyped());
4106 class_creation -> symbol = ctor;
4107
4108 if (class_creation -> base_opt)
4109 {
4110 assert(CanAssignmentConvertReference(ctor -> containing_type -> EnclosingType(),
4111 class_creation -> base_opt -> Type()));
4112 class_creation -> base_opt =
4113 ConvertToType(class_creation -> base_opt,
4114 ctor -> containing_type -> EnclosingType());
4115 }
4116 MethodInvocationConversion(class_creation -> arguments, ctor);
4117
4118 //
4119 // Process the throws clause.
4120 //
4121 SymbolSet* exception_set = TryExceptionTableStack().Top();
4122 for (i = 0; i < ctor -> NumThrows(); i++)
4123 {
4124 TypeSymbol* exception = ctor -> Throws(i);
4125 if (exception_set)
4126 exception_set -> AddElement(exception);
4127
4128 if (UncaughtException(exception))
4129 ReportSemError((class_creation -> class_body_opt
4130 ? SemanticError::UNCAUGHT_ANONYMOUS_CONSTRUCTOR_EXCEPTION
4131 : SemanticError::UNCAUGHT_CONSTRUCTOR_EXCEPTION),
4132 actual_type, type -> ExternalName(),
4133 exception -> ContainingPackageName(),
4134 exception -> ExternalName(),
4135 UncaughtExceptionContext());
4136 }
4137
4138 if (ctor -> ACC_PRIVATE() && ThisType() != type)
4139 {
4140 //
4141 // Add extra argument for read access constructor.
4142 //
4143 assert(ThisType() -> outermost_type == type -> outermost_type);
4144 ctor = type -> GetReadAccessConstructor(ctor);
4145 class_creation -> symbol = ctor;
4146 class_creation -> arguments -> AddNullArgument();
4147 }
4148 }
4149 else
4150 {
4151 //
4152 // No constructor was found (possibly because the type was not found),
4153 // so we don't know what exceptions could be thrown if the user fixes
4154 // the prior errors.
4155 //
4156 SymbolSet* exception_set = TryExceptionTableStack().Top();
4157 if (exception_set)
4158 exception_set -> AddElement(control.no_type);
4159 class_creation -> symbol = control.no_type;
4160 }
4161
4162 //
4163 // A local type may use enclosed local variables. If so, we must add
4164 // the parameters which allow the local type to initialize its shadows.
4165 //
4166 if (type -> IsLocal())
4167 {
4168 if (type -> LocalClassProcessingCompleted())
4169 {
4170 unsigned param_count = type -> NumConstructorParameters();
4171 class_creation -> arguments -> AllocateLocalArguments(param_count);
4172 for (i = 0; i < param_count; i++)
4173 {
4174 //
4175 // Are we currently within the body of the method that
4176 // contains the local variable in question? If not, we may need
4177 // to create a shadow in the outermost local class enclosing
4178 // the variable.
4179 //
4180 AstName* name = compilation_unit ->
4181 ast_pool -> GenName(class_creation -> new_token);
4182 VariableSymbol* accessor =
4183 FindLocalVariable(type -> ConstructorParameter(i),
4184 ThisType());
4185 name -> symbol = accessor;
4186 TypeSymbol* owner = accessor -> ContainingType();
4187 if (owner != ThisType())
4188 CreateAccessToScopedVariable(name, owner);
4189 class_creation -> arguments -> AddLocalArgument(name);
4190 }
4191 }
4192 else
4193 {
4194 //
4195 // We are within body of type; save processing for later, since
4196 // not all shadows may be known yet. See ProcessClassDeclaration
4197 // in body.cpp.
4198 //
4199 type -> AddLocalConstructorCallEnvironment
4200 (GetEnvironment(class_creation -> arguments));
4201 }
4202 }
4203 }
4204
4205
ProcessArrayCreationExpression(Ast * expr)4206 void Semantic::ProcessArrayCreationExpression(Ast* expr)
4207 {
4208 AstArrayCreationExpression* array_creation =
4209 (AstArrayCreationExpression*) expr;
4210 //
4211 // Either we have an initializer, or we have dimension expressions and
4212 // optional brackets.
4213 //
4214 assert(array_creation -> array_initializer_opt ?
4215 (! array_creation -> NumDimExprs() &&
4216 ! array_creation -> NumBrackets())
4217 : array_creation -> NumDimExprs());
4218 ProcessType(array_creation -> array_type);
4219 TypeSymbol* type = array_creation -> array_type -> symbol;
4220 unsigned dims = type -> num_dimensions +
4221 array_creation -> NumDimExprs() + array_creation -> NumBrackets();
4222 type = type -> GetArrayType(this, dims);
4223 array_creation -> symbol = type;
4224
4225 for (unsigned i = 0; i < array_creation -> NumDimExprs(); i++)
4226 {
4227 AstDimExpr* dim_expr = array_creation -> DimExpr(i);
4228 ProcessExpression(dim_expr -> expression);
4229 AstExpression* expr =
4230 PromoteUnaryNumericExpression(dim_expr -> expression);
4231 if (expr -> Type() != control.int_type &&
4232 expr -> symbol != control.no_type)
4233 {
4234 ReportSemError(SemanticError::TYPE_NOT_INTEGER,
4235 dim_expr -> expression,
4236 expr -> Type() -> ContainingPackageName(),
4237 expr -> Type() -> ExternalName());
4238 array_creation -> symbol = control.no_type;
4239 }
4240 dim_expr -> expression = expr;
4241 if (expr -> IsConstant() &&
4242 expr -> Type() == control.int_type &&
4243 (DYNAMIC_CAST<IntLiteralValue*> (expr -> value)) -> value < 0)
4244 {
4245 ReportSemError(SemanticError::NEGATIVE_ARRAY_SIZE,
4246 dim_expr -> expression);
4247 }
4248 }
4249
4250 if (array_creation -> array_initializer_opt)
4251 ProcessArrayInitializer(array_creation -> array_initializer_opt, type);
4252 }
4253
4254
ProcessPostUnaryExpression(Ast * expr)4255 void Semantic::ProcessPostUnaryExpression(Ast* expr)
4256 {
4257 AstPostUnaryExpression* postfix_expression =
4258 (AstPostUnaryExpression*) expr;
4259 AstExpression* expression = postfix_expression -> expression;
4260
4261 ProcessExpression(expression);
4262 postfix_expression -> symbol = expression -> symbol;
4263
4264 //
4265 // JLS2 added ability for parenthesized variable to remain a variable.
4266 //
4267 if (expression -> ParenthesizedExpressionCast())
4268 {
4269 ReportSemError(SemanticError::UNNECESSARY_PARENTHESIS, expression);
4270 while (expression -> ParenthesizedExpressionCast())
4271 expression = ((AstParenthesizedExpression*) expression) ->
4272 expression;
4273 }
4274
4275 if (expression -> symbol != control.no_type)
4276 {
4277 if (! expression -> IsLeftHandSide())
4278 {
4279 ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
4280 postfix_expression -> expression,
4281 postfix_expression -> expression -> Type() -> Name());
4282 postfix_expression -> symbol = control.no_type;
4283 }
4284 else if (! control.IsNumeric(expression -> Type()))
4285 {
4286 ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
4287 postfix_expression -> expression,
4288 expression -> Type() -> ContainingPackageName(),
4289 expression -> Type() -> ExternalName());
4290 postfix_expression -> symbol = control.no_type;
4291 }
4292 else if (! expression -> ArrayAccessCast()) // some kind of name
4293 {
4294 MethodSymbol* read_method = NULL;
4295 AstName* name = expression -> NameCast();
4296 if (name)
4297 {
4298 if (name -> resolution_opt)
4299 read_method =
4300 name -> resolution_opt -> symbol -> MethodCast();
4301 }
4302 else
4303 {
4304 AstFieldAccess* field_access = (AstFieldAccess*) expression;
4305 if (field_access -> resolution_opt)
4306 read_method = field_access -> resolution_opt ->
4307 symbol -> MethodCast();
4308 }
4309
4310 if (read_method)
4311 {
4312 postfix_expression -> write_method =
4313 read_method -> containing_type ->
4314 GetWriteAccessFromReadAccess(read_method);
4315 }
4316 }
4317 }
4318 }
4319
4320
ProcessPLUS(AstPreUnaryExpression * expr)4321 void Semantic::ProcessPLUS(AstPreUnaryExpression* expr)
4322 {
4323 ProcessExpression(expr -> expression);
4324 expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
4325 expr -> value = expr -> expression -> value;
4326 expr -> symbol = expr -> expression -> symbol;
4327 }
4328
4329
ProcessMINUS(AstPreUnaryExpression * expr)4330 void Semantic::ProcessMINUS(AstPreUnaryExpression* expr)
4331 {
4332 AstIntegerLiteral* int_literal =
4333 expr -> expression -> IntegerLiteralCast();
4334 AstLongLiteral* long_literal = expr -> expression -> LongLiteralCast();
4335
4336 if (int_literal)
4337 {
4338 LiteralSymbol* literal = lex_stream ->
4339 LiteralSymbol(int_literal -> integer_literal_token);
4340
4341 expr -> value = control.int_pool.FindOrInsertNegativeInt(literal);
4342 if (expr -> value == control.BadValue())
4343 {
4344 ReportSemError(SemanticError::INVALID_INT_VALUE, expr);
4345 expr -> symbol = control.no_type;
4346 }
4347 else expr -> symbol = control.int_type;
4348 }
4349 else if (long_literal)
4350 {
4351 LiteralSymbol* literal = lex_stream ->
4352 LiteralSymbol(long_literal -> long_literal_token);
4353
4354 expr -> value = control.long_pool.FindOrInsertNegativeLong(literal);
4355 if (expr -> value == control.BadValue())
4356 {
4357 ReportSemError(SemanticError::INVALID_LONG_VALUE, expr);
4358 expr -> symbol = control.no_type;
4359 }
4360 else expr -> symbol = control.long_type;
4361 }
4362 else
4363 {
4364 ProcessExpression(expr -> expression);
4365
4366 expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
4367 expr -> symbol = expr -> expression -> symbol;
4368 if (expr -> expression -> IsConstant())
4369 {
4370 TypeSymbol* type = expr -> Type();
4371
4372 if (type == control.double_type)
4373 {
4374 DoubleLiteralValue* literal = DYNAMIC_CAST<DoubleLiteralValue*>
4375 (expr -> expression -> value);
4376 expr -> value =
4377 control.double_pool.FindOrInsert(-literal -> value);
4378 }
4379 else if (type == control.float_type)
4380 {
4381 FloatLiteralValue* literal = DYNAMIC_CAST<FloatLiteralValue*>
4382 (expr -> expression -> value);
4383 expr -> value =
4384 control.float_pool.FindOrInsert(-literal -> value);
4385 }
4386 else if (type == control.long_type)
4387 {
4388 LongLiteralValue* literal = DYNAMIC_CAST<LongLiteralValue*>
4389 (expr -> expression -> value);
4390 CheckIntegerNegation(this, expr, literal -> value);
4391 expr -> value =
4392 control.long_pool.FindOrInsert(-literal -> value);
4393 }
4394 else if (expr -> Type() == control.int_type)
4395 {
4396 IntLiteralValue* literal = DYNAMIC_CAST<IntLiteralValue*>
4397 (expr -> expression -> value);
4398 CheckIntegerNegation(this, expr, literal -> value);
4399 expr -> value =
4400 control.int_pool.FindOrInsert(-literal -> value);
4401 }
4402 }
4403 }
4404 }
4405
4406
ProcessTWIDDLE(AstPreUnaryExpression * expr)4407 void Semantic::ProcessTWIDDLE(AstPreUnaryExpression* expr)
4408 {
4409 ProcessExpression(expr -> expression);
4410 expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
4411 expr -> symbol = expr -> expression -> symbol;
4412
4413 if (! control.IsIntegral(expr -> expression -> Type()))
4414 {
4415 TypeSymbol* type = expr -> expression -> Type();
4416 if (expr -> expression -> symbol != control.no_type)
4417 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
4418 expr -> expression,
4419 type -> ContainingPackageName(),
4420 type -> ExternalName());
4421 expr -> symbol = control.no_type;
4422 }
4423 else if (expr -> expression -> IsConstant())
4424 {
4425 if (expr -> expression -> Type() == control.long_type)
4426 {
4427 LongLiteralValue* literal = DYNAMIC_CAST<LongLiteralValue*>
4428 (expr -> expression -> value);
4429 expr -> value = control.long_pool.FindOrInsert(~literal -> value);
4430 }
4431 else // assert(expr -> expression -> Type() == control.int_type)
4432 {
4433 IntLiteralValue* literal = DYNAMIC_CAST<IntLiteralValue*>
4434 (expr -> expression -> value);
4435 expr -> value = control.int_pool.FindOrInsert(~literal -> value);
4436 }
4437 }
4438 }
4439
4440
ProcessNOT(AstPreUnaryExpression * expr)4441 void Semantic::ProcessNOT(AstPreUnaryExpression* expr)
4442 {
4443 ProcessExpression(expr -> expression);
4444
4445 if (expr -> expression -> Type() != control.boolean_type)
4446 {
4447 TypeSymbol* type = expr -> expression -> Type();
4448 if (expr -> expression -> symbol != control.no_type)
4449 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
4450 expr -> expression,
4451 type -> ContainingPackageName(),
4452 type -> ExternalName());
4453 expr -> symbol = control.no_type;
4454 }
4455 else
4456 {
4457 if (expr -> expression -> IsConstant())
4458 expr -> value = control.int_pool
4459 .FindOrInsert(IsConstantTrue(expr -> expression) ? 0 : 1);
4460 expr -> symbol = control.boolean_type;
4461 }
4462 }
4463
4464
ProcessPLUSPLUSOrMINUSMINUS(AstPreUnaryExpression * prefix_expression)4465 void Semantic::ProcessPLUSPLUSOrMINUSMINUS(AstPreUnaryExpression* prefix_expression)
4466 {
4467 AstExpression* expression = prefix_expression -> expression;
4468
4469 ProcessExpression(expression);
4470 prefix_expression -> symbol = expression -> symbol;
4471
4472 //
4473 // JLS2 added ability for parenthesized variable to remain a variable.
4474 //
4475 if (expression -> ParenthesizedExpressionCast())
4476 {
4477 ReportSemError(SemanticError::UNNECESSARY_PARENTHESIS, expression);
4478 while (expression -> ParenthesizedExpressionCast())
4479 expression = ((AstParenthesizedExpression*) expression) ->
4480 expression;
4481 }
4482
4483 if (expression -> symbol != control.no_type)
4484 {
4485 if (! expression -> IsLeftHandSide())
4486 {
4487 ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
4488 prefix_expression -> expression,
4489 prefix_expression -> expression -> Type() -> Name());
4490 prefix_expression -> symbol = control.no_type;
4491 }
4492 else if (! control.IsNumeric(expression -> Type()))
4493 {
4494 ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
4495 prefix_expression -> expression,
4496 expression -> Type() -> ContainingPackageName(),
4497 expression -> Type() -> ExternalName());
4498 prefix_expression -> symbol = control.no_type;
4499 }
4500 else if (! expression -> ArrayAccessCast()) // some kind of name
4501 {
4502 MethodSymbol* read_method = NULL;
4503 AstName* name = expression -> NameCast();
4504 if (name)
4505 {
4506 if (name -> resolution_opt)
4507 read_method =
4508 name -> resolution_opt -> symbol -> MethodCast();
4509 }
4510 else
4511 {
4512 AstFieldAccess* field_access = (AstFieldAccess*) expression;
4513 if (field_access -> resolution_opt)
4514 read_method = field_access -> resolution_opt -> symbol ->
4515 MethodCast();
4516 }
4517
4518 if (read_method)
4519 {
4520 prefix_expression -> write_method =
4521 read_method -> containing_type ->
4522 GetWriteAccessFromReadAccess(read_method);
4523 }
4524 }
4525 }
4526 }
4527
4528
ProcessPreUnaryExpression(Ast * expr)4529 void Semantic::ProcessPreUnaryExpression(Ast* expr)
4530 {
4531 AstPreUnaryExpression* prefix_expression = (AstPreUnaryExpression*) expr;
4532 (this ->* ProcessPreUnaryExpr[prefix_expression -> Tag()])
4533 (prefix_expression);
4534 }
4535
4536
4537 //
4538 // Returns true if both types are primitive, and the source type can be
4539 // widened into the target type.
4540 //
CanWideningPrimitiveConvert(const TypeSymbol * target_type,const TypeSymbol * source_type)4541 inline bool Semantic::CanWideningPrimitiveConvert(const TypeSymbol* target_type,
4542 const TypeSymbol* source_type)
4543 {
4544 if (target_type == control.double_type)
4545 return source_type == control.float_type ||
4546 source_type == control.long_type ||
4547 source_type == control.int_type ||
4548 source_type == control.char_type ||
4549 source_type == control.short_type ||
4550 source_type == control.byte_type;
4551 if (target_type == control.float_type)
4552 return source_type == control.long_type ||
4553 source_type == control.int_type ||
4554 source_type == control.char_type ||
4555 source_type == control.short_type ||
4556 source_type == control.byte_type;
4557 if (target_type == control.long_type)
4558 return source_type == control.int_type ||
4559 source_type == control.char_type ||
4560 source_type == control.short_type ||
4561 source_type == control.byte_type;
4562 if (target_type == control.int_type)
4563 return source_type == control.char_type ||
4564 source_type == control.short_type ||
4565 source_type == control.byte_type;
4566 if (target_type == control.short_type)
4567 return source_type == control.byte_type;
4568 return false;
4569 }
4570
4571
4572 //
4573 // Returns true if both types are primitive, and the source type can be
4574 // narrowed to the target type.
4575 //
CanNarrowingPrimitiveConvert(const TypeSymbol * target_type,const TypeSymbol * source_type)4576 inline bool Semantic::CanNarrowingPrimitiveConvert(const TypeSymbol* target_type,
4577 const TypeSymbol* source_type)
4578 {
4579 if (target_type == control.byte_type)
4580 return source_type == control.double_type ||
4581 source_type == control.float_type ||
4582 source_type == control.long_type ||
4583 source_type == control.int_type ||
4584 source_type == control.char_type ||
4585 source_type == control.short_type;
4586 if (target_type == control.char_type)
4587 return source_type == control.double_type ||
4588 source_type == control.float_type ||
4589 source_type == control.long_type ||
4590 source_type == control.int_type ||
4591 source_type == control.short_type ||
4592 source_type == control.byte_type;
4593 if (target_type == control.short_type)
4594 return source_type == control.double_type ||
4595 source_type == control.float_type ||
4596 source_type == control.long_type ||
4597 source_type == control.int_type ||
4598 source_type == control.char_type;
4599 if (target_type == control.int_type)
4600 return source_type == control.double_type ||
4601 source_type == control.float_type ||
4602 source_type == control.long_type;
4603 if (target_type == control.long_type)
4604 return source_type == control.double_type ||
4605 source_type == control.float_type;
4606 if (target_type == control.float_type)
4607 return source_type == control.double_type;
4608 return false;
4609 }
4610
4611
4612 //
4613 // Returns true if the source type can be converted to the target type in a
4614 // method invocation - this includes identity and widening conversions.
4615 //
CanMethodInvocationConvert(const TypeSymbol * target_type,const TypeSymbol * source_type)4616 bool Semantic::CanMethodInvocationConvert(const TypeSymbol* target_type,
4617 const TypeSymbol* source_type)
4618 {
4619 if (target_type == control.no_type) // Don't convert any class to bad type.
4620 return false;
4621 if (source_type == control.no_type) // Allow bad type to match anything.
4622 return true;
4623
4624 if (source_type -> Primitive())
4625 {
4626 return target_type -> Primitive() &&
4627 (target_type == source_type ||
4628 CanWideningPrimitiveConvert(target_type, source_type));
4629 }
4630
4631 if (target_type -> Primitive())
4632 return false;
4633 return source_type == control.null_type ||
4634 source_type -> IsSubtype(target_type);
4635 }
4636
4637
4638 //
4639 // Returns true if the reference source type can be automatically converted to
4640 // the target type in assignments. This works only for references (including
4641 // null), but allows a bad target type while method invocation does not.
4642 //
CanAssignmentConvertReference(const TypeSymbol * target_type,const TypeSymbol * source_type)4643 bool Semantic::CanAssignmentConvertReference(const TypeSymbol* target_type,
4644 const TypeSymbol* source_type)
4645 {
4646 return target_type == control.no_type ||
4647 CanMethodInvocationConvert(target_type, source_type);
4648 }
4649
4650
4651 //
4652 // Returns true if the source expression can be automatically converted to the
4653 // target type. This includes all method invocation conversions, and
4654 // additionally allows narrowing conversions of primitive constants.
4655 //
CanAssignmentConvert(const TypeSymbol * target_type,AstExpression * expr)4656 bool Semantic::CanAssignmentConvert(const TypeSymbol* target_type,
4657 AstExpression* expr)
4658 {
4659 return target_type == control.no_type ||
4660 expr -> symbol == control.no_type ||
4661 CanMethodInvocationConvert(target_type, expr -> Type()) ||
4662 IsIntValueRepresentableInType(expr, target_type);
4663 }
4664
4665
4666 //
4667 // Returns true if the source type can be cast into the target type, via an
4668 // identity, narrowing, or widening conversion. The lexical token is needed
4669 // in case an error is encountered when resolving the target type.
4670 //
CanCastConvert(TypeSymbol * target_type,TypeSymbol * source_type,TokenIndex tok)4671 bool Semantic::CanCastConvert(TypeSymbol* target_type, TypeSymbol* source_type,
4672 TokenIndex tok)
4673 {
4674 if (target_type == control.null_type)
4675 return false;
4676 if (source_type == target_type || source_type == control.no_type ||
4677 target_type == control.no_type)
4678 {
4679 return true;
4680 }
4681
4682 if (source_type -> Primitive())
4683 {
4684 return target_type -> Primitive() &&
4685 (CanWideningPrimitiveConvert(target_type, source_type) ||
4686 CanNarrowingPrimitiveConvert(target_type, source_type));
4687 }
4688
4689 if (target_type -> Primitive())
4690 return false;
4691
4692 // Now that primitives are removed, check if one subtypes the other.
4693 if (source_type == control.null_type ||
4694 target_type -> IsSubtype(source_type) ||
4695 source_type -> IsSubtype(target_type))
4696 {
4697 return true;
4698 }
4699
4700 // If we are left with arrays, see if the base types are compatible.
4701 if (source_type -> IsArray() || target_type -> IsArray())
4702 {
4703 if (source_type -> num_dimensions != target_type -> num_dimensions)
4704 return false;
4705 source_type = source_type -> base_type;
4706 target_type = target_type -> base_type;
4707 if (source_type -> Primitive() || target_type -> Primitive())
4708 return false;
4709 }
4710
4711 //
4712 // Here, we are left with two reference types. Two classes are not
4713 // compatible at this point, and final classes do not implement
4714 // interfaces. Otherwise, a class can implement an interface (even with
4715 // conflicting signatures), but two interfaces must be compatible.
4716 //
4717 if (source_type -> ACC_FINAL() || target_type -> ACC_FINAL() ||
4718 (! source_type -> ACC_INTERFACE() && ! target_type -> ACC_INTERFACE()))
4719 {
4720 return false;
4721 }
4722 if (! source_type -> ACC_INTERFACE() || ! target_type -> ACC_INTERFACE())
4723 return true;
4724 if (! source_type -> expanded_method_table)
4725 ComputeMethodsClosure(source_type, tok);
4726 if (! target_type -> expanded_method_table)
4727 ComputeMethodsClosure(target_type, tok);
4728 ExpandedMethodTable* source_method_table =
4729 source_type -> expanded_method_table;
4730 unsigned i;
4731 for (i = 0; i < source_method_table -> symbol_pool.Length(); i++)
4732 {
4733 MethodSymbol* method1 =
4734 source_method_table -> symbol_pool[i] -> method_symbol;
4735 MethodShadowSymbol* method_shadow2 =
4736 target_type -> expanded_method_table ->
4737 FindOverloadMethodShadow(method1, this, tok);
4738 if (method_shadow2)
4739 {
4740 if (! method1 -> IsTyped())
4741 method1 -> ProcessMethodSignature(this, tok);
4742
4743 MethodSymbol* method2 = method_shadow2 -> method_symbol;
4744 if (! method2 -> IsTyped())
4745 method2 -> ProcessMethodSignature(this, tok);
4746 if (method1 -> Type() != method2 -> Type())
4747 return false;
4748 }
4749 }
4750 return true; // All the methods passed the test.
4751 }
4752
4753
4754 //
4755 // Transfer a constant value across a primitive or String cast statement,
4756 // whether explicit or generated.
4757 //
CastValue(const TypeSymbol * target_type,AstExpression * expr)4758 LiteralValue* Semantic::CastValue(const TypeSymbol* target_type,
4759 AstExpression* expr)
4760 {
4761 TypeSymbol* source_type = expr -> Type();
4762
4763 if (target_type == source_type || source_type == control.no_type ||
4764 ! expr -> IsConstant())
4765 {
4766 assert(target_type == source_type || ! expr -> value);
4767 return expr -> value;
4768 }
4769 if (source_type == control.String())
4770 return NULL; // A string cast to a supertype is not constant.
4771
4772 LiteralValue* literal_value = NULL;
4773 if (target_type == control.String())
4774 {
4775 if (source_type == control.double_type)
4776 {
4777 DoubleLiteralValue* literal =
4778 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4779 DoubleToString ieee_double(literal -> value);
4780 literal_value =
4781 control.Utf8_pool.FindOrInsert(ieee_double.String(),
4782 ieee_double.Length());
4783 }
4784 else if (source_type == control.float_type)
4785 {
4786 FloatLiteralValue* literal =
4787 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4788 FloatToString ieee_float(literal -> value);
4789 literal_value =
4790 control.Utf8_pool.FindOrInsert(ieee_float.String(),
4791 ieee_float.Length());
4792 }
4793 else if (source_type == control.long_type)
4794 {
4795 LongLiteralValue* literal =
4796 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4797 LongToString long_integer(literal -> value);
4798 literal_value =
4799 control.Utf8_pool.FindOrInsert(long_integer.String(),
4800 long_integer.Length());
4801 }
4802 else if (source_type == control.char_type)
4803 {
4804 IntLiteralValue* literal =
4805 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4806 literal_value = control.Utf8_pool.FindOrInsert(literal -> value);
4807 }
4808 else if (source_type == control.boolean_type)
4809 {
4810 if (IsConstantFalse(expr))
4811 literal_value = control.false_name_symbol -> Utf8_literal;
4812 else
4813 {
4814 assert(IsConstantTrue(expr));
4815 literal_value = control.true_name_symbol -> Utf8_literal;
4816 }
4817 }
4818 else if (control.IsSimpleIntegerValueType(source_type))
4819 {
4820 IntLiteralValue* literal =
4821 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4822 IntToString integer(literal -> value);
4823 literal_value =
4824 control.Utf8_pool.FindOrInsert(integer.String(),
4825 integer.Length());
4826 }
4827 }
4828 else if (target_type == control.double_type)
4829 {
4830 if (source_type == control.float_type)
4831 {
4832 FloatLiteralValue* literal =
4833 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4834 literal_value =
4835 control.double_pool.FindOrInsert(literal -> value.DoubleValue());
4836 }
4837 else if (source_type == control.long_type)
4838 {
4839 LongLiteralValue* literal =
4840 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4841 IEEEdouble value(literal -> value);
4842 literal_value = control.double_pool.FindOrInsert(value);
4843 }
4844 else
4845 {
4846 IntLiteralValue* literal =
4847 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4848 IEEEdouble value(literal -> value);
4849 literal_value = control.double_pool.FindOrInsert(value);
4850 }
4851 }
4852 else if (target_type == control.float_type)
4853 {
4854 if (source_type == control.double_type)
4855 {
4856 DoubleLiteralValue* literal =
4857 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4858 literal_value =
4859 control.float_pool.FindOrInsert(literal -> value.FloatValue());
4860 }
4861 else if (source_type == control.long_type)
4862 {
4863 LongLiteralValue* literal =
4864 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4865 IEEEfloat value(literal -> value);
4866 literal_value = control.float_pool.FindOrInsert(value);
4867 }
4868 else
4869 {
4870 IntLiteralValue* literal =
4871 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4872 IEEEfloat value(literal -> value);
4873 literal_value = control.float_pool.FindOrInsert(value);
4874 }
4875 }
4876 else if (target_type == control.long_type)
4877 {
4878 if (source_type == control.double_type)
4879 {
4880 DoubleLiteralValue* literal =
4881 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4882 literal_value =
4883 control.long_pool.FindOrInsert(literal -> value.LongValue());
4884 }
4885 else if (source_type == control.float_type)
4886 {
4887 FloatLiteralValue* literal =
4888 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4889 literal_value =
4890 control.long_pool.FindOrInsert(literal -> value.LongValue());
4891 }
4892 else
4893 {
4894 IntLiteralValue* literal =
4895 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4896 literal_value =
4897 control.long_pool.FindOrInsert((LongInt) literal -> value);
4898 }
4899 }
4900 else if (target_type == control.int_type)
4901 {
4902 if (source_type == control.double_type)
4903 {
4904 DoubleLiteralValue* literal =
4905 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4906 literal_value =
4907 control.int_pool.FindOrInsert((literal -> value).IntValue());
4908 }
4909 else if (source_type == control.float_type)
4910 {
4911 FloatLiteralValue* literal =
4912 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4913 literal_value =
4914 control.int_pool.FindOrInsert(literal -> value.IntValue());
4915 }
4916 else if (source_type == control.long_type)
4917 {
4918 LongLiteralValue* literal =
4919 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4920 literal_value =
4921 control.int_pool.FindOrInsert((i4) (literal -> value).LowWord());
4922 }
4923 else literal_value = expr -> value;
4924 }
4925 else if (target_type == control.char_type)
4926 {
4927 if (source_type == control.double_type)
4928 {
4929 DoubleLiteralValue* literal =
4930 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4931 literal_value =
4932 control.int_pool.FindOrInsert((i4) (u2) (literal -> value.IntValue()));
4933 }
4934 else if (source_type == control.float_type)
4935 {
4936 FloatLiteralValue* literal =
4937 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4938 literal_value =
4939 control.int_pool.FindOrInsert((i4) (u2) (literal -> value.IntValue()));
4940 }
4941 else if (source_type == control.long_type)
4942 {
4943 LongLiteralValue* literal =
4944 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4945 literal_value =
4946 control.int_pool.FindOrInsert((i4) (u2) (literal -> value).LowWord());
4947 }
4948 else
4949 {
4950 IntLiteralValue* literal =
4951 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4952 literal_value =
4953 control.int_pool.FindOrInsert((i4) (u2) literal -> value);
4954 }
4955 }
4956 else if (target_type == control.short_type)
4957 {
4958 if (source_type == control.double_type)
4959 {
4960 DoubleLiteralValue* literal =
4961 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4962 literal_value =
4963 control.int_pool.FindOrInsert((i4) (i2) (literal -> value.IntValue()));
4964 }
4965 else if (source_type == control.float_type)
4966 {
4967 FloatLiteralValue* literal =
4968 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
4969 literal_value =
4970 control.int_pool.FindOrInsert((i4) (i2) (literal -> value.IntValue()));
4971 }
4972 else if (source_type == control.long_type)
4973 {
4974 LongLiteralValue* literal =
4975 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
4976 literal_value =
4977 control.int_pool.FindOrInsert((i4) (i2) (literal -> value).LowWord());
4978 }
4979 else
4980 {
4981 IntLiteralValue* literal =
4982 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
4983 literal_value =
4984 control.int_pool.FindOrInsert((i4) (i2) literal -> value);
4985 }
4986 }
4987 else if (target_type == control.byte_type)
4988 {
4989 if (source_type == control.double_type)
4990 {
4991 DoubleLiteralValue* literal =
4992 DYNAMIC_CAST<DoubleLiteralValue*> (expr -> value);
4993 literal_value =
4994 control.int_pool.FindOrInsert((i4) (i1) (literal -> value.IntValue()));
4995 }
4996 else if (source_type == control.float_type)
4997 {
4998 FloatLiteralValue* literal =
4999 DYNAMIC_CAST<FloatLiteralValue*> (expr -> value);
5000 literal_value =
5001 control.int_pool.FindOrInsert((i4) (i1) (literal -> value.IntValue()));
5002 }
5003 else if (source_type == control.long_type)
5004 {
5005 LongLiteralValue* literal =
5006 DYNAMIC_CAST<LongLiteralValue*> (expr -> value);
5007 literal_value =
5008 control.int_pool.FindOrInsert((i4) (i1)
5009 (literal -> value).LowWord());
5010 }
5011 else
5012 {
5013 IntLiteralValue* literal =
5014 DYNAMIC_CAST<IntLiteralValue*> (expr -> value);
5015 literal_value =
5016 control.int_pool.FindOrInsert((i4) (i1) literal -> value);
5017 }
5018 }
5019
5020 assert(literal_value);
5021 return literal_value;
5022 }
5023
5024
ProcessCastExpression(Ast * expr)5025 void Semantic::ProcessCastExpression(Ast* expr)
5026 {
5027 AstCastExpression* cast_expression = (AstCastExpression*) expr;
5028
5029 //
5030 // Do not use ProcessExpressionOrStringConstant here, to avoid generating
5031 // intermediate Strings - see CheckConstantString in lookup.cpp
5032 //
5033 ProcessType(cast_expression -> type);
5034 ProcessExpression(cast_expression -> expression);
5035
5036 TypeSymbol* source_type = cast_expression -> expression -> Type();
5037 TypeSymbol* target_type = cast_expression -> type -> symbol;
5038
5039 if (CanCastConvert(target_type, source_type,
5040 cast_expression -> right_parenthesis_token))
5041 {
5042 cast_expression -> symbol = target_type;
5043 cast_expression -> value = CastValue(target_type,
5044 cast_expression -> expression);
5045 }
5046 else
5047 {
5048 ReportSemError(SemanticError::INVALID_CAST_CONVERSION,
5049 cast_expression -> expression,
5050 source_type -> ContainingPackageName(),
5051 source_type -> Name(),
5052 target_type -> ContainingPackageName(),
5053 target_type -> Name());
5054 cast_expression -> symbol = control.no_type;
5055 }
5056 }
5057
5058
5059 //
5060 // Inserts a widening conversion, if necessary.
5061 //
ConvertToType(AstExpression * expr,TypeSymbol * target_type)5062 AstExpression* Semantic::ConvertToType(AstExpression* expr,
5063 TypeSymbol* target_type)
5064 {
5065 TypeSymbol* source_type = expr -> Type();
5066 if (source_type == control.null_type || source_type == target_type ||
5067 source_type == control.no_type || target_type -> Bad())
5068 {
5069 return expr;
5070 }
5071
5072 TokenIndex loc = expr -> LeftToken();
5073
5074 AstCastExpression* result =
5075 compilation_unit -> ast_pool -> GenCastExpression();
5076 result -> left_parenthesis_token = loc;
5077 //
5078 // Rather than generate an AstType, we leave this NULL and rely
5079 // on the resolved symbol for the type.
5080 //
5081 result -> type = NULL;
5082 result -> right_parenthesis_token = loc;
5083 result -> expression = expr;
5084 result -> symbol = target_type;
5085 result -> value = CastValue(target_type, expr);
5086 return result;
5087 }
5088
5089
PromoteUnaryNumericExpression(AstExpression * unary_expression)5090 AstExpression* Semantic::PromoteUnaryNumericExpression(AstExpression* unary_expression)
5091 {
5092 TypeSymbol* type = unary_expression -> Type();
5093
5094 if (type == control.no_type)
5095 return unary_expression;
5096
5097 if (! control.IsNumeric(type))
5098 {
5099 ReportSemError(SemanticError::TYPE_NOT_NUMERIC, unary_expression,
5100 type -> ContainingPackageName(),
5101 type -> ExternalName());
5102 unary_expression -> symbol = control.no_type;
5103 return unary_expression;
5104 }
5105 return (type == control.byte_type || type == control.short_type ||
5106 type == control.char_type)
5107 ? ConvertToType(unary_expression, control.int_type) : unary_expression;
5108 }
5109
5110
BinaryNumericPromotion(AstBinaryExpression * binary_expression)5111 void Semantic::BinaryNumericPromotion(AstBinaryExpression* binary_expression)
5112 {
5113 binary_expression -> symbol =
5114 BinaryNumericPromotion(binary_expression -> left_expression,
5115 binary_expression -> right_expression);
5116 }
5117
5118
BinaryNumericPromotion(AstAssignmentExpression * assignment_expression)5119 void Semantic::BinaryNumericPromotion(AstAssignmentExpression* assignment_expression)
5120 {
5121 AstExpression* left_expr = assignment_expression -> left_hand_side;
5122 while (left_expr -> ParenthesizedExpressionCast())
5123 left_expr = ((AstParenthesizedExpression*) left_expr) -> expression;
5124 TypeSymbol* type =
5125 BinaryNumericPromotion(left_expr, assignment_expression -> expression);
5126 assignment_expression -> left_hand_side = left_expr;
5127 if (type == control.no_type)
5128 assignment_expression -> symbol = control.no_type;
5129 }
5130
5131
BinaryNumericPromotion(AstConditionalExpression * conditional_expression)5132 void Semantic::BinaryNumericPromotion(AstConditionalExpression* conditional_expression)
5133 {
5134 conditional_expression -> symbol =
5135 BinaryNumericPromotion(conditional_expression -> true_expression,
5136 conditional_expression -> false_expression);
5137 }
5138
5139
BinaryNumericPromotion(AstExpression * & left_expr,AstExpression * & right_expr)5140 TypeSymbol* Semantic::BinaryNumericPromotion(AstExpression*& left_expr,
5141 AstExpression*& right_expr)
5142 {
5143 TypeSymbol* left_type = left_expr -> Type();
5144 TypeSymbol* right_type = right_expr -> Type();
5145
5146 if (! control.IsNumeric(left_type) || ! control.IsNumeric(right_type))
5147 {
5148 if (left_type != control.no_type && ! control.IsNumeric(left_type))
5149 ReportSemError(SemanticError::TYPE_NOT_NUMERIC, left_expr,
5150 left_type -> ContainingPackageName(),
5151 left_type -> ExternalName());
5152 if (right_type != control.no_type && ! control.IsNumeric(right_type))
5153 ReportSemError(SemanticError::TYPE_NOT_NUMERIC, right_expr,
5154 right_type -> ContainingPackageName(),
5155 right_type -> ExternalName());
5156 return control.no_type;
5157 }
5158 if (left_type == control.double_type)
5159 {
5160 right_expr = ConvertToType(right_expr, control.double_type);
5161 return control.double_type;
5162 }
5163 if (right_type == control.double_type)
5164 {
5165 left_expr = ConvertToType(left_expr, control.double_type);
5166 return control.double_type;
5167 }
5168 if (left_type == control.float_type)
5169 {
5170 right_expr = ConvertToType(right_expr, control.float_type);
5171 return control.float_type;
5172 }
5173 if (right_type == control.float_type)
5174 {
5175 left_expr = ConvertToType(left_expr, control.float_type);
5176 return control.float_type;
5177 }
5178 if (left_type == control.long_type)
5179 {
5180 right_expr = ConvertToType(right_expr, control.long_type);
5181 return control.long_type;
5182 }
5183 if (right_type == control.long_type)
5184 {
5185 left_expr = ConvertToType(left_expr, control.long_type);
5186 return control.long_type;
5187 }
5188 left_expr = ConvertToType(left_expr, control.int_type);
5189 right_expr = ConvertToType(right_expr, control.int_type);
5190 return control.int_type;
5191 }
5192
5193
MethodInvocationConversion(AstArguments * args,MethodSymbol * method)5194 void Semantic::MethodInvocationConversion(AstArguments* args,
5195 MethodSymbol* method)
5196 {
5197 assert(args -> NumArguments() == method -> NumFormalParameters());
5198 for (unsigned i = 0; i < args -> NumArguments(); i++)
5199 {
5200 AstExpression* expr = args -> Argument(i);
5201 if (expr -> Type() != method -> FormalParameter(i) -> Type())
5202 {
5203 args -> Argument(i) =
5204 ConvertToType(expr, method -> FormalParameter(i) -> Type());
5205 }
5206 }
5207 }
5208
5209
ProcessPLUS(AstBinaryExpression * expr)5210 void Semantic::ProcessPLUS(AstBinaryExpression* expr)
5211 {
5212 //
5213 // Do not use ProcessExpressionOrStringConstant here, to avoid generating
5214 // intermediate Strings - see CheckConstantString in lookup.cpp
5215 //
5216 AstExpression* left = expr -> left_expression;
5217 AstExpression* right = expr -> right_expression;
5218 ProcessExpression(left);
5219 ProcessExpression(right);
5220
5221 TypeSymbol* left_type = left -> Type();
5222 TypeSymbol* right_type = right -> Type();
5223
5224 if (left_type == control.no_type || right_type == control.no_type)
5225 expr -> symbol = control.no_type;
5226 else if (left_type == control.String() || right_type == control.String())
5227 {
5228 //
5229 // Convert the left expression if necessary.
5230 //
5231 if (left_type != control.String())
5232 {
5233 AddDependence(ThisType(), left_type -> BoxedType(control));
5234 if (left_type == control.void_type)
5235 {
5236 ReportSemError(SemanticError::VOID_TO_STRING, left);
5237 expr -> symbol = control.no_type;
5238 }
5239 else if (left_type == control.null_type || left -> IsConstant())
5240 {
5241 left -> value = CastValue(control.String(), left);
5242 left -> symbol = control.String();
5243 }
5244 }
5245
5246 //
5247 // Convert the right expression if necessary.
5248 //
5249 if (right_type != control.String())
5250 {
5251 AddDependence(ThisType(), right_type -> BoxedType(control));
5252 if (right_type == control.void_type)
5253 {
5254 ReportSemError(SemanticError::VOID_TO_STRING, right);
5255 expr -> symbol = control.no_type;
5256 }
5257 else if (right_type == control.null_type || right -> IsConstant())
5258 {
5259 right -> value = CastValue(control.String(), right);
5260 right -> symbol = control.String();
5261 }
5262 }
5263
5264 AddDependence(ThisType(), control.option.target >= JikesOption::SDK1_5
5265 ? control.StringBuilder() : control.StringBuffer());
5266
5267 //
5268 // If both subexpressions are string constants, identify the result as
5269 // as a string constant, but do not perform the concatenation here. The
5270 // reason being that if we have a long expression of the form
5271 //
5272 // s1 + s2 + ... + sn
5273 //
5274 // where each subexpression s(i) is a string constant, we want to
5275 // perform one concatenation and enter a single result into the
5276 // constant pool instead of n-1 subresults. See CheckStringConstant
5277 // in lookup.cpp.
5278 //
5279 if (expr -> symbol != control.no_type)
5280 expr -> symbol = control.String();
5281 }
5282 else
5283 {
5284 BinaryNumericPromotion(expr);
5285 left = expr -> left_expression;
5286 right = expr -> right_expression;
5287
5288 if (left -> IsConstant() && right -> IsConstant())
5289 {
5290 if (expr -> Type() == control.double_type)
5291 {
5292 DoubleLiteralValue* left_value =
5293 DYNAMIC_CAST<DoubleLiteralValue*> (left -> value);
5294 DoubleLiteralValue* right_value =
5295 DYNAMIC_CAST<DoubleLiteralValue*> (right -> value);
5296
5297 expr -> value =
5298 control.double_pool.FindOrInsert(left_value -> value +
5299 right_value -> value);
5300 }
5301 else if (expr -> Type() == control.float_type)
5302 {
5303 FloatLiteralValue* left_value =
5304 DYNAMIC_CAST<FloatLiteralValue*> (left -> value);
5305 FloatLiteralValue* right_value =
5306 DYNAMIC_CAST<FloatLiteralValue*> (right -> value);
5307 expr -> value =
5308 control.float_pool.FindOrInsert(left_value -> value +
5309 right_value -> value);
5310 }
5311 else if (expr -> Type() == control.long_type)
5312 {
5313 LongLiteralValue* left_value =
5314 DYNAMIC_CAST<LongLiteralValue*> (left -> value);
5315 LongLiteralValue* right_value =
5316 DYNAMIC_CAST<LongLiteralValue*> (right -> value);
5317
5318 CheckIntegerAddition(this, expr, left_value -> value,
5319 right_value -> value);
5320 expr -> value =
5321 control.long_pool.FindOrInsert(left_value -> value +
5322 right_value -> value);
5323 }
5324 else if (expr -> Type() == control.int_type)
5325 {
5326 IntLiteralValue* left_value =
5327 DYNAMIC_CAST<IntLiteralValue*> (left -> value);
5328 IntLiteralValue* right_value =
5329 DYNAMIC_CAST<IntLiteralValue*> (right -> value);
5330 CheckIntegerAddition(this, expr, left_value -> value,
5331 right_value -> value);
5332 expr -> value =
5333 control.int_pool.FindOrInsert(left_value -> value +
5334 right_value -> value);
5335 }
5336 }
5337 }
5338 }
5339
5340
ProcessShift(AstBinaryExpression * expr)5341 void Semantic::ProcessShift(AstBinaryExpression* expr)
5342 {
5343 ProcessExpression(expr -> left_expression);
5344 ProcessExpression(expr -> right_expression);
5345
5346 TypeSymbol* left_type = expr -> left_expression -> Type();
5347 TypeSymbol* right_type = expr -> right_expression -> Type();
5348
5349 if (! control.IsIntegral(left_type))
5350 {
5351 if (left_type != control.no_type)
5352 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5353 expr -> left_expression,
5354 left_type -> ContainingPackageName(),
5355 left_type -> ExternalName());
5356 expr -> symbol = control.no_type;
5357 }
5358 else
5359 {
5360 expr -> left_expression =
5361 PromoteUnaryNumericExpression(expr -> left_expression);
5362 }
5363 //
5364 // This call captures both unary numeric conversion (widening) of
5365 // byte, char, or short, and narrowing of long, since the bytecode
5366 // requires an int shift amount.
5367 //
5368 if (! control.IsIntegral(right_type))
5369 {
5370 if (right_type != control.no_type)
5371 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5372 expr -> right_expression,
5373 right_type -> ContainingPackageName(),
5374 right_type -> ExternalName());
5375 expr -> symbol = control.no_type;
5376 }
5377 else
5378 {
5379 expr -> right_expression = ConvertToType(expr -> right_expression,
5380 control.int_type);
5381 if (expr -> symbol != control.no_type)
5382 expr -> symbol = expr -> left_expression -> symbol;
5383
5384 ProcessShiftCount(left_type, expr -> right_expression);
5385 }
5386 }
5387
5388
5389 //
5390 // Checks whether 'expr' is a suitable shift count for something of type
5391 // 'left_type'. JLS2 15.19 is quite clear about the meaning of code with
5392 // with a negative or out-of-range shift count, so it's still valid code,
5393 // but the behavior is probably not what the author was expecting.
5394 //
ProcessShiftCount(TypeSymbol * left_type,AstExpression * expr)5395 void Semantic::ProcessShiftCount(TypeSymbol* left_type, AstExpression* expr)
5396 {
5397 if (! expr -> IsConstant())
5398 return;
5399
5400 IntLiteralValue* literal = DYNAMIC_CAST<IntLiteralValue*>(expr -> value);
5401 i4 count = literal -> value;
5402 IntToWstring count_text(count);
5403
5404 if (count < 0)
5405 {
5406 ReportSemError(SemanticError::NEGATIVE_SHIFT_COUNT,
5407 expr,
5408 count_text.String());
5409 }
5410
5411 int width = (left_type == control.long_type) ? 64 : 32;
5412 if (count >= width)
5413 {
5414 IntToWstring width_text(width);
5415 ReportSemError(SemanticError::SHIFT_COUNT_TOO_LARGE,
5416 expr,
5417 count_text.String(),
5418 width_text.String());
5419 }
5420 }
5421
5422
ProcessLEFT_SHIFT(AstBinaryExpression * expr)5423 void Semantic::ProcessLEFT_SHIFT(AstBinaryExpression* expr)
5424 {
5425 ProcessShift(expr);
5426
5427 if (expr -> left_expression -> IsConstant() &&
5428 expr -> right_expression -> IsConstant())
5429 {
5430 if (expr -> Type() == control.long_type)
5431 {
5432 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5433 (expr -> left_expression -> value);
5434 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5435 (expr -> right_expression -> value);
5436
5437 expr -> value = control.long_pool.FindOrInsert(left -> value <<
5438 (right -> value &
5439 LONG_SHIFT_MASK));
5440 }
5441 else if (expr -> Type() == control.int_type)
5442 {
5443 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5444 (expr -> left_expression -> value);
5445 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5446 (expr -> right_expression -> value);
5447
5448 expr -> value = control.int_pool.FindOrInsert(left -> value <<
5449 (right -> value &
5450 INT_SHIFT_MASK));
5451 }
5452 }
5453 }
5454
5455
ProcessRIGHT_SHIFT(AstBinaryExpression * expr)5456 void Semantic::ProcessRIGHT_SHIFT(AstBinaryExpression* expr)
5457 {
5458 ProcessShift(expr);
5459
5460 if (expr -> left_expression -> IsConstant() &&
5461 expr -> right_expression -> IsConstant())
5462 {
5463 if (expr -> Type() == control.long_type)
5464 {
5465 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5466 (expr -> left_expression -> value);
5467 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5468 (expr -> right_expression -> value);
5469
5470 expr -> value = control.long_pool.FindOrInsert(left -> value >>
5471 (right -> value &
5472 LONG_SHIFT_MASK));
5473 }
5474 else if (expr -> Type() == control.int_type)
5475 {
5476 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5477 (expr -> left_expression -> value);
5478 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5479 (expr -> right_expression -> value);
5480
5481 expr -> value = control.int_pool.FindOrInsert(left -> value >>
5482 (right -> value &
5483 INT_SHIFT_MASK));
5484 }
5485 }
5486 }
5487
5488
ProcessUNSIGNED_RIGHT_SHIFT(AstBinaryExpression * expr)5489 void Semantic::ProcessUNSIGNED_RIGHT_SHIFT(AstBinaryExpression* expr)
5490 {
5491 ProcessShift(expr);
5492
5493 if (expr -> left_expression -> IsConstant() &&
5494 expr -> right_expression -> IsConstant())
5495 {
5496 if (expr -> Type() == control.long_type)
5497 {
5498 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5499 (expr -> left_expression -> value);
5500 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5501 (expr -> right_expression -> value);
5502
5503 expr -> value = control.long_pool.FindOrInsert((LongInt)
5504 ((ULongInt) left -> value >> (right -> value & LONG_SHIFT_MASK)));
5505 }
5506 else if (expr -> Type() == control.int_type)
5507 {
5508 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5509 (expr -> left_expression -> value);
5510 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5511 (expr -> right_expression -> value);
5512
5513 expr -> value = control.int_pool.FindOrInsert((i4)
5514 ((u4) left -> value >> (right -> value & INT_SHIFT_MASK)));
5515 }
5516 }
5517 }
5518
5519
ProcessLESS(AstBinaryExpression * expr)5520 void Semantic::ProcessLESS(AstBinaryExpression* expr)
5521 {
5522 ProcessExpression(expr -> left_expression);
5523 ProcessExpression(expr -> right_expression);
5524
5525 BinaryNumericPromotion(expr);
5526 TypeSymbol* left_type = expr -> left_expression -> Type();
5527 TypeSymbol* right_type = expr -> right_expression -> Type();
5528
5529 expr -> symbol = (left_type == control.no_type ||
5530 right_type == control.no_type)
5531 ? control.no_type : control.boolean_type;
5532
5533 if (expr -> left_expression -> IsConstant() &&
5534 expr -> right_expression -> IsConstant())
5535 {
5536 if (left_type == control.double_type)
5537 {
5538 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
5539 (expr -> left_expression -> value);
5540 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
5541 (expr -> right_expression -> value);
5542
5543 expr -> value =
5544 control.int_pool.FindOrInsert(left -> value <
5545 right -> value ? 1 : 0);
5546 }
5547 else if (left_type == control.float_type)
5548 {
5549 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
5550 (expr -> left_expression -> value);
5551 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
5552 (expr -> right_expression -> value);
5553
5554 expr -> value =
5555 control.int_pool.FindOrInsert(left -> value <
5556 right -> value ? 1 : 0);
5557 }
5558 else if (left_type == control.long_type)
5559 {
5560 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5561 (expr -> left_expression -> value);
5562 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5563 (expr -> right_expression -> value);
5564
5565 expr -> value =
5566 control.int_pool.FindOrInsert(left -> value <
5567 right -> value ? 1 : 0);
5568 }
5569 else if (left_type == control.int_type)
5570 {
5571 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5572 (expr -> left_expression -> value);
5573 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5574 (expr -> right_expression -> value);
5575
5576 expr -> value =
5577 control.int_pool.FindOrInsert(left -> value <
5578 right -> value ? 1 : 0);
5579 }
5580 }
5581 }
5582
5583
ProcessGREATER(AstBinaryExpression * expr)5584 void Semantic::ProcessGREATER(AstBinaryExpression* expr)
5585 {
5586 ProcessExpression(expr -> left_expression);
5587 ProcessExpression(expr -> right_expression);
5588
5589 BinaryNumericPromotion(expr);
5590 TypeSymbol* left_type = expr -> left_expression -> Type();
5591 TypeSymbol* right_type = expr -> right_expression -> Type();
5592
5593 expr -> symbol = (left_type == control.no_type ||
5594 right_type == control.no_type)
5595 ? control.no_type : control.boolean_type;
5596
5597 if (expr -> left_expression -> IsConstant() &&
5598 expr -> right_expression -> IsConstant())
5599 {
5600 if (left_type == control.double_type)
5601 {
5602 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
5603 (expr -> left_expression -> value);
5604 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
5605 (expr -> right_expression -> value);
5606
5607 expr -> value =
5608 control.int_pool.FindOrInsert(left -> value >
5609 right -> value ? 1 : 0);
5610 }
5611 else if (left_type == control.float_type)
5612 {
5613 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
5614 (expr -> left_expression -> value);
5615 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
5616 (expr -> right_expression -> value);
5617
5618 expr -> value =
5619 control.int_pool.FindOrInsert(left -> value >
5620 right -> value ? 1 : 0);
5621 }
5622 else if (left_type == control.long_type)
5623 {
5624 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5625 (expr -> left_expression -> value);
5626 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5627 (expr -> right_expression -> value);
5628
5629 expr -> value =
5630 control.int_pool.FindOrInsert(left -> value >
5631 right -> value ? 1 : 0);
5632 }
5633 else if (left_type == control.int_type)
5634 {
5635 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5636 (expr -> left_expression -> value);
5637 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5638 (expr -> right_expression -> value);
5639
5640 expr -> value =
5641 control.int_pool.FindOrInsert(left -> value >
5642 right -> value ? 1 : 0);
5643 }
5644 }
5645 }
5646
5647
ProcessLESS_EQUAL(AstBinaryExpression * expr)5648 void Semantic::ProcessLESS_EQUAL(AstBinaryExpression* expr)
5649 {
5650 ProcessExpression(expr -> left_expression);
5651 ProcessExpression(expr -> right_expression);
5652
5653 BinaryNumericPromotion(expr);
5654 TypeSymbol* left_type = expr -> left_expression -> Type();
5655 TypeSymbol* right_type = expr -> right_expression -> Type();
5656
5657 expr -> symbol = (left_type == control.no_type ||
5658 right_type == control.no_type)
5659 ? control.no_type : control.boolean_type;
5660
5661 if (expr -> left_expression -> IsConstant() &&
5662 expr -> right_expression -> IsConstant())
5663 {
5664 if (left_type == control.double_type)
5665 {
5666 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
5667 (expr -> left_expression -> value);
5668 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
5669 (expr -> right_expression -> value);
5670
5671 expr -> value =
5672 control.int_pool.FindOrInsert(left -> value <=
5673 right -> value ? 1 : 0);
5674 }
5675 else if (left_type == control.float_type)
5676 {
5677 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
5678 (expr -> left_expression -> value);
5679 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
5680 (expr -> right_expression -> value);
5681
5682 expr -> value =
5683 control.int_pool.FindOrInsert(left -> value <=
5684 right -> value ? 1 : 0);
5685 }
5686 else if (left_type == control.long_type)
5687 {
5688 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5689 (expr -> left_expression -> value);
5690 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5691 (expr -> right_expression -> value);
5692
5693 expr -> value =
5694 control.int_pool.FindOrInsert(left -> value <=
5695 right -> value ? 1 : 0);
5696 }
5697 else if (left_type == control.int_type)
5698 {
5699 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5700 (expr -> left_expression -> value);
5701 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5702 (expr -> right_expression -> value);
5703
5704 expr -> value =
5705 control.int_pool.FindOrInsert(left -> value <=
5706 right -> value ? 1 : 0);
5707 }
5708 }
5709 }
5710
5711
ProcessGREATER_EQUAL(AstBinaryExpression * expr)5712 void Semantic::ProcessGREATER_EQUAL(AstBinaryExpression* expr)
5713 {
5714 ProcessExpression(expr -> left_expression);
5715 ProcessExpression(expr -> right_expression);
5716
5717 BinaryNumericPromotion(expr);
5718 TypeSymbol* left_type = expr -> left_expression -> Type();
5719 TypeSymbol* right_type = expr -> right_expression -> Type();
5720
5721 expr -> symbol = (left_type == control.no_type ||
5722 right_type == control.no_type)
5723 ? control.no_type : control.boolean_type;
5724
5725 if (expr -> left_expression -> IsConstant() &&
5726 expr -> right_expression -> IsConstant())
5727 {
5728 if (left_type == control.double_type)
5729 {
5730 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
5731 (expr -> left_expression -> value);
5732 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
5733 (expr -> right_expression -> value);
5734
5735 expr -> value =
5736 control.int_pool.FindOrInsert(left -> value >=
5737 right -> value ? 1 : 0);
5738 }
5739 else if (left_type == control.float_type)
5740 {
5741 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
5742 (expr -> left_expression -> value);
5743 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
5744 (expr -> right_expression -> value);
5745
5746 expr -> value =
5747 control.int_pool.FindOrInsert(left -> value >=
5748 right -> value ? 1 : 0);
5749 }
5750 else if (left_type == control.long_type)
5751 {
5752 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5753 (expr -> left_expression -> value);
5754 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5755 (expr -> right_expression -> value);
5756
5757 expr -> value =
5758 control.int_pool.FindOrInsert(left -> value >=
5759 right -> value ? 1 : 0);
5760 }
5761 else if (left_type == control.int_type)
5762 {
5763 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5764 (expr -> left_expression -> value);
5765 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5766 (expr -> right_expression -> value);
5767
5768 expr -> value =
5769 control.int_pool.FindOrInsert(left -> value >=
5770 right -> value ? 1 : 0);
5771 }
5772 }
5773 }
5774
5775
ProcessAND(AstBinaryExpression * expr)5776 void Semantic::ProcessAND(AstBinaryExpression* expr)
5777 {
5778 ProcessExpression(expr -> left_expression);
5779 ProcessExpression(expr -> right_expression);
5780
5781 TypeSymbol* left_type = expr -> left_expression -> Type();
5782 TypeSymbol* right_type = expr -> right_expression -> Type();
5783
5784 if (left_type == control.boolean_type ||
5785 right_type == control.boolean_type)
5786 {
5787 if (left_type != control.boolean_type)
5788 {
5789 if (left_type != control.no_type)
5790 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5791 expr -> left_expression,
5792 left_type -> ContainingPackageName(),
5793 left_type -> ExternalName());
5794 expr -> symbol = control.no_type;
5795 }
5796 else if (right_type != control.boolean_type)
5797 {
5798 if (right_type != control.no_type)
5799 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5800 expr -> right_expression,
5801 right_type -> ContainingPackageName(),
5802 right_type -> ExternalName());
5803 expr -> symbol = control.no_type;
5804 }
5805 else expr -> symbol = control.boolean_type;
5806 if (expr -> left_expression -> IsConstant() &&
5807 expr -> right_expression -> IsConstant())
5808 {
5809 expr -> value = control.int_pool
5810 .FindOrInsert((IsConstantTrue(expr -> left_expression) &&
5811 IsConstantTrue(expr -> right_expression))
5812 ? 1 : 0);
5813 }
5814 }
5815 else
5816 {
5817 BinaryNumericPromotion(expr);
5818 TypeSymbol* expr_type = expr -> Type();
5819
5820 if (! control.IsIntegral(expr_type))
5821 {
5822 if (! control.IsIntegral(left_type) &&
5823 left_type != control.no_type)
5824 {
5825 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5826 expr -> left_expression,
5827 left_type -> ContainingPackageName(),
5828 left_type -> ExternalName());
5829 }
5830 if (! control.IsIntegral(right_type) &&
5831 right_type != control.no_type)
5832 {
5833 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5834 expr -> right_expression,
5835 right_type -> ContainingPackageName(),
5836 right_type -> ExternalName());
5837 }
5838 expr -> symbol = control.no_type;
5839 }
5840 if (expr -> left_expression -> IsConstant() &&
5841 expr -> right_expression -> IsConstant())
5842 {
5843 if (expr_type == control.long_type)
5844 {
5845 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5846 (expr -> left_expression -> value);
5847 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5848 (expr -> right_expression -> value);
5849
5850 expr -> value = control.long_pool.FindOrInsert(left -> value &
5851 right -> value);
5852 }
5853 else if (expr_type == control.int_type)
5854 {
5855 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5856 (expr -> left_expression -> value);
5857 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5858 (expr -> right_expression -> value);
5859
5860 expr -> value = control.int_pool.FindOrInsert(left -> value &
5861 right -> value);
5862 }
5863 }
5864 }
5865 }
5866
5867
ProcessXOR(AstBinaryExpression * expr)5868 void Semantic::ProcessXOR(AstBinaryExpression* expr)
5869 {
5870 ProcessExpression(expr -> left_expression);
5871 ProcessExpression(expr -> right_expression);
5872
5873 TypeSymbol* left_type = expr -> left_expression -> Type();
5874 TypeSymbol* right_type = expr -> right_expression -> Type();
5875
5876 if (left_type == control.boolean_type ||
5877 right_type == control.boolean_type)
5878 {
5879 if (left_type != control.boolean_type)
5880 {
5881 if (left_type != control.no_type)
5882 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5883 expr -> left_expression,
5884 left_type -> ContainingPackageName(),
5885 left_type -> ExternalName());
5886 expr -> symbol = control.no_type;
5887 }
5888 else if (right_type != control.boolean_type)
5889 {
5890 if (right_type != control.no_type)
5891 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5892 expr -> right_expression,
5893 right_type -> ContainingPackageName(),
5894 right_type -> ExternalName());
5895 expr -> symbol = control.no_type;
5896 }
5897 else expr -> symbol = control.boolean_type;
5898 if (expr -> left_expression -> IsConstant() &&
5899 expr -> right_expression -> IsConstant())
5900 {
5901 expr -> value = control.int_pool
5902 .FindOrInsert((IsConstantTrue(expr -> left_expression) !=
5903 IsConstantTrue(expr -> right_expression))
5904 ? 1 : 0);
5905 }
5906 }
5907 else
5908 {
5909 BinaryNumericPromotion(expr);
5910 TypeSymbol* expr_type = expr -> Type();
5911
5912 if (! control.IsIntegral(expr_type))
5913 {
5914 if (! control.IsIntegral(left_type) &&
5915 left_type != control.no_type)
5916 {
5917 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5918 expr -> left_expression,
5919 left_type -> ContainingPackageName(),
5920 left_type -> ExternalName());
5921 }
5922 if (! control.IsIntegral(right_type) &&
5923 right_type != control.no_type)
5924 {
5925 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
5926 expr -> right_expression,
5927 right_type -> ContainingPackageName(),
5928 right_type -> ExternalName());
5929 }
5930 expr -> symbol = control.no_type;
5931 }
5932 if (expr -> left_expression -> IsConstant() &&
5933 expr -> right_expression -> IsConstant())
5934 {
5935 if (expr_type == control.long_type)
5936 {
5937 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
5938 (expr -> left_expression -> value);
5939 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
5940 (expr -> right_expression -> value);
5941
5942 expr -> value = control.long_pool.FindOrInsert(left -> value ^
5943 right -> value);
5944 }
5945 else if (expr_type == control.int_type)
5946 {
5947 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
5948 (expr -> left_expression -> value);
5949 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
5950 (expr -> right_expression -> value);
5951
5952 expr -> value = control.int_pool.FindOrInsert(left -> value ^
5953 right -> value);
5954 }
5955 }
5956 }
5957 }
5958
5959
ProcessIOR(AstBinaryExpression * expr)5960 void Semantic::ProcessIOR(AstBinaryExpression* expr)
5961 {
5962 ProcessExpression(expr -> left_expression);
5963 ProcessExpression(expr -> right_expression);
5964
5965 TypeSymbol* left_type = expr -> left_expression -> Type();
5966 TypeSymbol* right_type = expr -> right_expression -> Type();
5967
5968 if (left_type == control.boolean_type ||
5969 right_type == control.boolean_type)
5970 {
5971 if (left_type != control.boolean_type)
5972 {
5973 if (left_type != control.no_type)
5974 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5975 expr -> left_expression,
5976 left_type -> ContainingPackageName(),
5977 left_type -> ExternalName());
5978 expr -> symbol = control.no_type;
5979 }
5980 else if (right_type != control.boolean_type)
5981 {
5982 if (right_type != control.no_type)
5983 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
5984 expr -> right_expression,
5985 right_type -> ContainingPackageName(),
5986 right_type -> ExternalName());
5987 expr -> symbol = control.no_type;
5988 }
5989 else expr -> symbol = control.boolean_type;
5990 if (expr -> left_expression -> IsConstant() &&
5991 expr -> right_expression -> IsConstant())
5992 {
5993 expr -> value = control.int_pool
5994 .FindOrInsert((IsConstantTrue(expr -> left_expression) ||
5995 IsConstantTrue(expr -> right_expression))
5996 ? 1 : 0);
5997 }
5998 }
5999 else
6000 {
6001 BinaryNumericPromotion(expr);
6002 TypeSymbol* expr_type = expr -> Type();
6003
6004 if (! control.IsIntegral(expr_type))
6005 {
6006 if (! control.IsIntegral(left_type) &&
6007 left_type != control.no_type)
6008 {
6009 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
6010 expr -> left_expression,
6011 left_type -> ContainingPackageName(),
6012 left_type -> ExternalName());
6013 }
6014 if (! control.IsIntegral(right_type) &&
6015 right_type != control.no_type)
6016 {
6017 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
6018 expr -> right_expression,
6019 right_type -> ContainingPackageName(),
6020 right_type -> ExternalName());
6021 }
6022 expr -> symbol = control.no_type;
6023 }
6024 if (expr -> left_expression -> IsConstant() &&
6025 expr -> right_expression -> IsConstant())
6026 {
6027 if (expr_type == control.long_type)
6028 {
6029 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
6030 (expr -> left_expression -> value);
6031 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
6032 (expr -> right_expression -> value);
6033
6034 expr -> value = control.long_pool.FindOrInsert(left -> value |
6035 right -> value);
6036 }
6037 else if (expr_type == control.int_type)
6038 {
6039 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
6040 (expr -> left_expression -> value);
6041 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
6042 (expr -> right_expression -> value);
6043
6044 expr -> value = control.int_pool.FindOrInsert(left -> value |
6045 right -> value);
6046 }
6047 }
6048 }
6049 }
6050
6051
ProcessAND_AND(AstBinaryExpression * expr)6052 void Semantic::ProcessAND_AND(AstBinaryExpression* expr)
6053 {
6054 ProcessExpression(expr -> left_expression);
6055 ProcessExpression(expr -> right_expression);
6056
6057 TypeSymbol* left_type = expr -> left_expression -> Type();
6058 TypeSymbol* right_type = expr -> right_expression -> Type();
6059
6060 if (left_type != control.boolean_type)
6061 {
6062 if (left_type != control.no_type)
6063 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
6064 expr -> left_expression,
6065 left_type -> ContainingPackageName(),
6066 left_type -> ExternalName());
6067 expr -> symbol = control.no_type;
6068 }
6069 if (right_type != control.boolean_type)
6070 {
6071 if (right_type != control.no_type)
6072 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
6073 expr -> right_expression,
6074 right_type -> ContainingPackageName(),
6075 right_type -> ExternalName());
6076 expr -> symbol = control.no_type;
6077 }
6078 if (expr -> left_expression -> IsConstant() &&
6079 expr -> right_expression -> IsConstant())
6080 {
6081 //
6082 // Even when evaluating false && x, x must be constant for && to
6083 // be a constant expression according to JLS2 15.28.
6084 //
6085 expr -> value = control.int_pool.
6086 FindOrInsert((IsConstantTrue(expr -> left_expression) &&
6087 IsConstantTrue(expr -> right_expression))
6088 ? 1 : 0);
6089 }
6090 if (expr -> symbol != control.no_type)
6091 expr -> symbol = control.boolean_type;
6092 }
6093
6094
ProcessOR_OR(AstBinaryExpression * expr)6095 void Semantic::ProcessOR_OR(AstBinaryExpression* expr)
6096 {
6097 ProcessExpression(expr -> left_expression);
6098 ProcessExpression(expr -> right_expression);
6099
6100 TypeSymbol* left_type = expr -> left_expression -> Type();
6101 TypeSymbol* right_type = expr -> right_expression -> Type();
6102
6103 if (left_type != control.boolean_type)
6104 {
6105 if (left_type != control.no_type)
6106 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
6107 expr -> left_expression,
6108 left_type -> ContainingPackageName(),
6109 left_type -> ExternalName());
6110 expr -> symbol = control.no_type;
6111 }
6112 if (right_type != control.boolean_type)
6113 {
6114 if (right_type != control.no_type)
6115 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
6116 expr -> right_expression,
6117 right_type -> ContainingPackageName(),
6118 right_type -> ExternalName());
6119 expr -> symbol = control.no_type;
6120 }
6121 if (expr -> left_expression -> IsConstant() &&
6122 expr -> right_expression -> IsConstant())
6123 {
6124 //
6125 // Even when evaluating true || x, x must be constant for || to
6126 // be a constant expression according to JLS2 15.28.
6127 //
6128 expr -> value = control.int_pool.
6129 FindOrInsert((IsConstantTrue(expr -> left_expression) ||
6130 IsConstantTrue(expr -> right_expression))
6131 ? 1 : 0);
6132 }
6133 if (expr -> symbol != control.no_type)
6134 expr -> symbol = control.boolean_type;
6135 }
6136
6137
ProcessEQUAL_EQUAL(AstBinaryExpression * expr)6138 void Semantic::ProcessEQUAL_EQUAL(AstBinaryExpression* expr)
6139 {
6140 ProcessExpressionOrStringConstant(expr -> left_expression);
6141 ProcessExpressionOrStringConstant(expr -> right_expression);
6142
6143 TypeSymbol* left_type = expr -> left_expression -> Type();
6144 TypeSymbol* right_type = expr -> right_expression -> Type();
6145
6146 if (left_type == control.void_type || right_type == control.void_type)
6147 {
6148 if (left_type == control.void_type)
6149 ReportSemError(SemanticError::TYPE_IS_VOID,
6150 expr -> left_expression,
6151 left_type -> Name());
6152 if (right_type == control.void_type)
6153 ReportSemError(SemanticError::TYPE_IS_VOID,
6154 expr -> right_expression,
6155 right_type -> Name());
6156 expr -> symbol = control.no_type;
6157 }
6158 else if (left_type -> Primitive() && right_type -> Primitive())
6159 {
6160 if (left_type == control.boolean_type ||
6161 right_type == control.boolean_type)
6162 {
6163 if (left_type != right_type)
6164 {
6165 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
6166 expr, left_type -> ContainingPackageName(),
6167 left_type -> ExternalName(),
6168 right_type -> ContainingPackageName(),
6169 right_type -> ExternalName());
6170 expr -> symbol = control.no_type;
6171 }
6172 }
6173 else BinaryNumericPromotion(expr);
6174 if (expr -> symbol != control.no_type)
6175 expr -> symbol = control.boolean_type;
6176 }
6177 else if (CanCastConvert(left_type, right_type,
6178 expr -> binary_operator_token) ||
6179 (left_type == control.null_type &&
6180 (right_type == control.null_type ||
6181 right_type -> IsSubclass(control.Object()))))
6182 {
6183 expr -> symbol = control.boolean_type;
6184 }
6185 else
6186 {
6187 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
6188 expr, left_type -> ContainingPackageName(),
6189 left_type -> ExternalName(),
6190 right_type -> ContainingPackageName(),
6191 right_type -> ExternalName());
6192 expr -> symbol = control.no_type;
6193 }
6194
6195 if (expr -> left_expression -> IsConstant() &&
6196 expr -> right_expression -> IsConstant())
6197 {
6198 LiteralValue* left = expr -> left_expression -> value;
6199 LiteralValue* right = expr -> right_expression -> value;
6200
6201 //
6202 // Check double and float separately from long, int, and String; since
6203 // 0.0 and NaNs cause weird behavior.
6204 //
6205 if (expr -> left_expression -> Type() == control.double_type)
6206 {
6207 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6208 (expr -> left_expression -> value);
6209 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6210 (expr -> right_expression -> value);
6211 expr -> value =
6212 control.int_pool.FindOrInsert(left -> value ==
6213 right -> value ? 1 : 0);
6214 }
6215 else if (expr -> left_expression -> Type() == control.float_type)
6216 {
6217 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6218 (expr -> left_expression -> value);
6219 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6220 (expr -> right_expression -> value);
6221 expr -> value =
6222 control.int_pool.FindOrInsert(left -> value ==
6223 right -> value ? 1 : 0);
6224 }
6225 else expr -> value =
6226 control.int_pool.FindOrInsert(left == right ? 1 : 0);
6227 }
6228 }
6229
6230
ProcessNOT_EQUAL(AstBinaryExpression * expr)6231 void Semantic::ProcessNOT_EQUAL(AstBinaryExpression* expr)
6232 {
6233 ProcessExpressionOrStringConstant(expr -> left_expression);
6234 ProcessExpressionOrStringConstant(expr -> right_expression);
6235
6236 TypeSymbol* left_type = expr -> left_expression -> Type();
6237 TypeSymbol* right_type = expr -> right_expression -> Type();
6238
6239 if (left_type == control.void_type || right_type == control.void_type)
6240 {
6241 if (left_type == control.void_type)
6242 ReportSemError(SemanticError::TYPE_IS_VOID,
6243 expr -> left_expression,
6244 left_type -> Name());
6245 if (right_type == control.void_type)
6246 ReportSemError(SemanticError::TYPE_IS_VOID,
6247 expr -> right_expression,
6248 right_type -> Name());
6249 expr -> symbol = control.no_type;
6250 }
6251 else if (left_type -> Primitive() && right_type -> Primitive())
6252 {
6253 if (left_type == control.boolean_type ||
6254 right_type == control.boolean_type)
6255 {
6256 if (left_type != right_type)
6257 {
6258 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
6259 expr, left_type -> ContainingPackageName(),
6260 left_type -> ExternalName(),
6261 right_type -> ContainingPackageName(),
6262 right_type -> ExternalName());
6263 expr -> symbol = control.no_type;
6264 }
6265 }
6266 else BinaryNumericPromotion(expr);
6267 if (expr -> symbol != control.no_type)
6268 expr -> symbol = control.boolean_type;
6269 }
6270 else if (CanCastConvert(left_type, right_type,
6271 expr -> binary_operator_token) ||
6272 (left_type == control.null_type &&
6273 (right_type == control.null_type ||
6274 right_type -> IsSubclass(control.Object()))))
6275 {
6276 expr -> symbol = control.boolean_type;
6277 }
6278 else
6279 {
6280 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
6281 expr, left_type -> ContainingPackageName(),
6282 left_type -> ExternalName(),
6283 right_type -> ContainingPackageName(),
6284 right_type -> ExternalName());
6285 expr -> symbol = control.no_type;
6286 }
6287
6288 if (expr -> left_expression -> IsConstant() &&
6289 expr -> right_expression -> IsConstant())
6290 {
6291 LiteralValue* left = expr -> left_expression -> value;
6292 LiteralValue* right = expr -> right_expression -> value;
6293
6294 //
6295 // Check double and float separately from long, int, and String; since
6296 // 0.0 and NaNs cause weird behavior.
6297 //
6298 if (expr -> left_expression -> Type() == control.double_type)
6299 {
6300 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6301 (expr -> left_expression -> value);
6302 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6303 (expr -> right_expression -> value);
6304
6305 expr -> value =
6306 control.int_pool.FindOrInsert(left -> value !=
6307 right -> value ? 1 : 0);
6308 }
6309 else if (expr -> left_expression -> Type() == control.float_type)
6310 {
6311 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6312 (expr -> left_expression -> value);
6313 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6314 (expr -> right_expression -> value);
6315
6316 expr -> value =
6317 control.int_pool.FindOrInsert(left -> value !=
6318 right -> value ? 1 : 0);
6319 }
6320 else expr -> value =
6321 control.int_pool.FindOrInsert(left != right ? 1 : 0);
6322 }
6323 }
6324
6325
ProcessSTAR(AstBinaryExpression * expr)6326 void Semantic::ProcessSTAR(AstBinaryExpression* expr)
6327 {
6328 ProcessExpression(expr -> left_expression);
6329 ProcessExpression(expr -> right_expression);
6330 BinaryNumericPromotion(expr);
6331
6332 if (expr -> left_expression -> IsConstant() &&
6333 expr -> right_expression -> IsConstant())
6334 {
6335 if (expr -> Type() == control.double_type)
6336 {
6337 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6338 (expr -> left_expression -> value);
6339 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6340 (expr -> right_expression -> value);
6341
6342 expr -> value =
6343 control.double_pool.FindOrInsert(left -> value *
6344 right -> value);
6345 }
6346 else if (expr -> Type() == control.float_type)
6347 {
6348 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6349 (expr -> left_expression -> value);
6350 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6351 (expr -> right_expression -> value);
6352
6353 expr -> value =
6354 control.float_pool.FindOrInsert(left -> value *
6355 right -> value);
6356 }
6357 else if (expr -> Type() == control.long_type)
6358 {
6359 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
6360 (expr -> left_expression -> value);
6361 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
6362 (expr -> right_expression -> value);
6363 CheckIntegerMultiplication(this, expr,
6364 left -> value, right -> value);
6365 expr -> value =
6366 control.long_pool.FindOrInsert(left -> value *
6367 right -> value);
6368 }
6369 else if (expr -> Type() == control.int_type)
6370 {
6371 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
6372 (expr -> left_expression -> value);
6373 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
6374 (expr -> right_expression -> value);
6375 CheckIntegerMultiplication(this, expr,
6376 left -> value, right -> value);
6377 expr -> value =
6378 control.int_pool.FindOrInsert(left -> value *
6379 right -> value);
6380 }
6381 }
6382 }
6383
6384
ProcessMINUS(AstBinaryExpression * expr)6385 void Semantic::ProcessMINUS(AstBinaryExpression* expr)
6386 {
6387 ProcessExpression(expr -> left_expression);
6388 ProcessExpression(expr -> right_expression);
6389 BinaryNumericPromotion(expr);
6390
6391 if (expr -> left_expression -> IsConstant() &&
6392 expr -> right_expression -> IsConstant())
6393 {
6394 if (expr -> Type() == control.double_type)
6395 {
6396 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6397 (expr -> left_expression -> value);
6398 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6399 (expr -> right_expression -> value);
6400
6401 expr -> value =
6402 control.double_pool.FindOrInsert(left -> value -
6403 right -> value);
6404 }
6405 else if (expr -> Type() == control.float_type)
6406 {
6407 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6408 (expr -> left_expression -> value);
6409 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6410 (expr -> right_expression -> value);
6411
6412 expr -> value =
6413 control.float_pool.FindOrInsert(left -> value -
6414 right -> value);
6415 }
6416 else if (expr -> Type() == control.long_type)
6417 {
6418 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
6419 (expr -> left_expression -> value);
6420 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
6421 (expr -> right_expression -> value);
6422 CheckIntegerSubtraction(this, expr, left -> value, right -> value);
6423 expr -> value =
6424 control.long_pool.FindOrInsert(left -> value -
6425 right -> value);
6426 }
6427 else if (expr -> Type() == control.int_type)
6428 {
6429 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
6430 (expr -> left_expression -> value);
6431 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
6432 (expr -> right_expression -> value);
6433 CheckIntegerSubtraction(this, expr, left -> value, right -> value);
6434 expr -> value =
6435 control.int_pool.FindOrInsert(left -> value - right -> value);
6436 }
6437 }
6438 }
6439
6440
ProcessSLASH(AstBinaryExpression * expr)6441 void Semantic::ProcessSLASH(AstBinaryExpression* expr)
6442 {
6443 ProcessExpression(expr -> left_expression);
6444 ProcessExpression(expr -> right_expression);
6445 BinaryNumericPromotion(expr);
6446
6447 AstExpression* left_expression = expr -> left_expression;
6448 AstExpression* right_expression = expr -> right_expression;
6449 if (right_expression -> IsConstant())
6450 {
6451 //
6452 // If the type of the expression is int or long and the right-hand
6453 // side is 0 then issue an error message. Otherwise, if both
6454 // subexpressions are constant, calculate result.
6455 //
6456 if ((expr -> Type() == control.int_type &&
6457 DYNAMIC_CAST<IntLiteralValue*> (right_expression -> value) -> value == 0) ||
6458 (expr -> Type() == control.long_type &&
6459 DYNAMIC_CAST<LongLiteralValue*> (right_expression -> value) -> value == 0))
6460 {
6461 //
6462 // This will guarantee a runtime exception, but the
6463 // clarifications to JLS2 insist it is legal code.
6464 //
6465 ReportSemError(SemanticError::ZERO_DIVIDE_CAUTION, expr);
6466 }
6467 else if (left_expression -> IsConstant())
6468 {
6469 if (expr -> Type() == control.double_type)
6470 {
6471 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6472 (left_expression -> value);
6473 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6474 (right_expression -> value);
6475
6476 expr -> value =
6477 control.double_pool.FindOrInsert(left -> value /
6478 right -> value);
6479 }
6480 else if (expr -> Type() == control.float_type)
6481 {
6482 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6483 (left_expression -> value);
6484 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6485 (right_expression -> value);
6486
6487 expr -> value =
6488 control.float_pool.FindOrInsert(left -> value /
6489 right -> value);
6490 }
6491 else if (expr -> Type() == control.long_type)
6492 {
6493 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
6494 (left_expression -> value);
6495 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
6496 (right_expression -> value);
6497 CheckIntegerDivision(this, expr, left -> value,
6498 right -> value);
6499 expr -> value =
6500 control.long_pool.FindOrInsert(left -> value /
6501 right -> value);
6502 }
6503 else if (expr -> Type() == control.int_type)
6504 {
6505 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
6506 (left_expression -> value);
6507 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
6508 (right_expression -> value);
6509 CheckIntegerDivision(this, expr, left -> value,
6510 right -> value);
6511 //
6512 // There is a bug in the intel hardware where if one tries
6513 // to compute ((2**32-1) / -1), he gets a ZeroDivide
6514 // exception. Thus, instead of using the straightforward
6515 // code below, we use the short-circuited one that follows:
6516 //
6517 // expr -> value = control.int_pool
6518 // .FindOrInsert(left -> value / right -> value);
6519 //
6520 expr -> value = control.int_pool
6521 .FindOrInsert(right -> value == -1
6522 ? -(left -> value)
6523 : left -> value / right -> value);
6524 }
6525 }
6526 }
6527 }
6528
6529
ProcessMOD(AstBinaryExpression * expr)6530 void Semantic::ProcessMOD(AstBinaryExpression* expr)
6531 {
6532 ProcessExpression(expr -> left_expression);
6533 ProcessExpression(expr -> right_expression);
6534 BinaryNumericPromotion(expr);
6535
6536 AstExpression* left_expression = expr -> left_expression;
6537 AstExpression* right_expression = expr -> right_expression;
6538 if (right_expression -> IsConstant())
6539 {
6540 //
6541 // If the type of the expression is int or long and the right-hand
6542 // side is 0 then issue an error message. Otherwise, if both
6543 // subexpressions are constant, calculate result.
6544 //
6545 if ((expr -> Type() == control.int_type &&
6546 DYNAMIC_CAST<IntLiteralValue*> (right_expression -> value) -> value == 0) ||
6547 (expr -> Type() == control.long_type &&
6548 DYNAMIC_CAST<LongLiteralValue*> (right_expression -> value) -> value == 0))
6549 {
6550 //
6551 // This will guarantee a runtime exception, but the
6552 // clarifications to JLS2 insist it is legal code.
6553 //
6554 ReportSemError(SemanticError::ZERO_DIVIDE_CAUTION, expr);
6555 }
6556 else if (left_expression -> IsConstant())
6557 {
6558 if (expr -> Type() == control.double_type)
6559 {
6560 DoubleLiteralValue* left = DYNAMIC_CAST<DoubleLiteralValue*>
6561 (left_expression -> value);
6562 DoubleLiteralValue* right = DYNAMIC_CAST<DoubleLiteralValue*>
6563 (right_expression -> value);
6564
6565 expr -> value =
6566 control.double_pool.FindOrInsert(left -> value %
6567 right -> value);
6568 }
6569 else if (expr -> Type() == control.float_type)
6570 {
6571 FloatLiteralValue* left = DYNAMIC_CAST<FloatLiteralValue*>
6572 (left_expression -> value);
6573 FloatLiteralValue* right = DYNAMIC_CAST<FloatLiteralValue*>
6574 (right_expression -> value);
6575
6576 expr -> value =
6577 control.float_pool.FindOrInsert(left -> value %
6578 right -> value);
6579 }
6580 else if (expr -> Type() == control.long_type)
6581 {
6582 LongLiteralValue* left = DYNAMIC_CAST<LongLiteralValue*>
6583 (left_expression -> value);
6584 LongLiteralValue* right = DYNAMIC_CAST<LongLiteralValue*>
6585 (right_expression -> value);
6586
6587 expr -> value =
6588 control.long_pool.FindOrInsert(left -> value %
6589 right -> value);
6590 }
6591 else if (expr -> Type() == control.int_type)
6592 {
6593 IntLiteralValue* left = DYNAMIC_CAST<IntLiteralValue*>
6594 (left_expression -> value);
6595 IntLiteralValue* right = DYNAMIC_CAST<IntLiteralValue*>
6596 (right_expression -> value);
6597
6598 //
6599 // There is a bug in the intel hardware where if one tries
6600 // to compute ((2**32-1) / -1), he gets a ZeroDivide
6601 // exception. Thus, instead of using the straightforward
6602 // code below, we use the short-circuited one that follows:
6603 //
6604 // expr -> value = control.int_pool
6605 // .FindOrInsert(left -> value % right -> value);
6606 //
6607 expr -> value = control.int_pool
6608 .FindOrInsert((left -> value == (signed) 0x80000000 &&
6609 right -> value == (signed) 0xffffffff)
6610 ? 0 : left -> value % right -> value);
6611 }
6612 }
6613 }
6614 }
6615
6616
ProcessBinaryExpression(Ast * expr)6617 void Semantic::ProcessBinaryExpression(Ast* expr)
6618 {
6619 AstBinaryExpression* binary_expression = (AstBinaryExpression*) expr;
6620 (this ->* ProcessBinaryExpr[binary_expression -> Tag()])
6621 (binary_expression);
6622 }
6623
6624
ProcessInstanceofExpression(Ast * expr)6625 void Semantic::ProcessInstanceofExpression(Ast* expr)
6626 {
6627 AstInstanceofExpression* instanceof = (AstInstanceofExpression*) expr;
6628 ProcessExpressionOrStringConstant(instanceof -> expression);
6629 ProcessType(instanceof -> type);
6630
6631 TypeSymbol* left_type = instanceof -> expression -> Type();
6632 TypeSymbol* right_type = instanceof -> type -> symbol;
6633
6634 if (left_type -> Primitive())
6635 {
6636 ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
6637 instanceof -> expression,
6638 left_type -> Name());
6639 instanceof -> symbol = control.no_type;
6640 }
6641 // can left_type (source) be cast into right_type
6642 else if (! CanCastConvert(right_type, left_type,
6643 instanceof -> instanceof_token))
6644 {
6645 ReportSemError(SemanticError::INVALID_INSTANCEOF_CONVERSION,
6646 expr, left_type -> ContainingPackageName(),
6647 left_type -> ExternalName(),
6648 right_type -> ContainingPackageName(),
6649 right_type -> ExternalName());
6650 instanceof -> symbol = control.no_type;
6651 }
6652 else instanceof -> symbol = control.boolean_type;
6653 }
6654
6655
ProcessConditionalExpression(Ast * expr)6656 void Semantic::ProcessConditionalExpression(Ast* expr)
6657 {
6658 AstConditionalExpression* conditional_expression =
6659 (AstConditionalExpression*) expr;
6660
6661 ProcessExpression(conditional_expression -> test_expression);
6662 //
6663 // TODO: Should we delay calculating results of true/false expressions
6664 // until CheckStringConstant in lookup.cpp to put fewer intermediate
6665 // strings in the storage pools?
6666 //
6667 ProcessExpressionOrStringConstant(conditional_expression ->
6668 true_expression);
6669 ProcessExpressionOrStringConstant(conditional_expression ->
6670 false_expression);
6671
6672 TypeSymbol* test_type =
6673 conditional_expression -> test_expression -> Type();
6674 TypeSymbol* true_type =
6675 conditional_expression -> true_expression -> Type();
6676 TypeSymbol* false_type =
6677 conditional_expression -> false_expression -> Type();
6678
6679 if (test_type != control.boolean_type)
6680 {
6681 if (test_type != control.no_type)
6682 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
6683 conditional_expression -> test_expression,
6684 test_type -> ContainingPackageName(),
6685 test_type -> ExternalName());
6686 conditional_expression -> symbol = control.no_type;
6687 }
6688 if (true_type == control.void_type)
6689 {
6690 ReportSemError(SemanticError::TYPE_IS_VOID,
6691 conditional_expression -> true_expression,
6692 true_type -> Name());
6693 true_type = control.no_type;
6694 }
6695 if (false_type == control.void_type)
6696 {
6697 ReportSemError(SemanticError::TYPE_IS_VOID,
6698 conditional_expression -> false_expression,
6699 false_type -> Name());
6700 false_type = control.no_type;
6701 }
6702 if (true_type == control.no_type || false_type == control.no_type)
6703 conditional_expression -> symbol = control.no_type;
6704 else if (true_type -> Primitive())
6705 {
6706 if (! false_type -> Primitive() ||
6707 (true_type != false_type &&
6708 (true_type == control.boolean_type ||
6709 false_type == control.boolean_type)))
6710 {
6711 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
6712 conditional_expression -> true_expression -> LeftToken(),
6713 conditional_expression -> false_expression -> RightToken(),
6714 true_type -> ContainingPackageName(),
6715 true_type -> ExternalName(),
6716 false_type -> ContainingPackageName(),
6717 false_type -> ExternalName());
6718 conditional_expression -> symbol = control.no_type;
6719 }
6720 else // must be a primitive type
6721 {
6722 if (true_type == false_type)
6723 {
6724 if (conditional_expression -> symbol != control.no_type)
6725 conditional_expression -> symbol = true_type;
6726 }
6727 else // must be mixed numeric types
6728 {
6729 if (true_type == control.byte_type &&
6730 false_type == control.short_type)
6731 {
6732 conditional_expression -> true_expression =
6733 ConvertToType(conditional_expression -> true_expression,
6734 control.short_type);
6735 conditional_expression -> symbol = control.short_type;
6736 }
6737 else if (true_type == control.short_type &&
6738 false_type == control.byte_type)
6739 {
6740 conditional_expression -> false_expression =
6741 ConvertToType(conditional_expression -> false_expression,
6742 control.short_type);
6743 conditional_expression -> symbol = control.short_type;
6744 }
6745 else if (true_type == control.int_type &&
6746 control.IsSimpleIntegerValueType(true_type) &&
6747 IsIntValueRepresentableInType(conditional_expression -> true_expression,
6748 false_type))
6749 {
6750 conditional_expression -> true_expression =
6751 ConvertToType(conditional_expression -> true_expression,
6752 false_type);
6753 conditional_expression -> symbol = false_type;
6754 }
6755 else if (false_type == control.int_type &&
6756 control.IsSimpleIntegerValueType(false_type) &&
6757 IsIntValueRepresentableInType(conditional_expression -> false_expression,
6758 true_type))
6759 {
6760 conditional_expression -> false_expression =
6761 ConvertToType(conditional_expression -> false_expression,
6762 true_type);
6763 conditional_expression -> symbol = true_type;
6764 }
6765 else BinaryNumericPromotion(conditional_expression);
6766 }
6767
6768 //
6769 // Even when evaluating 'true ? constant : x' or
6770 // 'false ? x : constant', x must be constant for ?: to be a
6771 // constant expression according to JLS2 15.28.
6772 //
6773 if (conditional_expression -> true_expression -> IsConstant() &&
6774 conditional_expression -> false_expression -> IsConstant())
6775 {
6776 if (IsConstantTrue(conditional_expression -> test_expression))
6777 conditional_expression -> value =
6778 conditional_expression -> true_expression -> value;
6779 else if (IsConstantFalse(conditional_expression -> test_expression))
6780 conditional_expression -> value =
6781 conditional_expression -> false_expression -> value;
6782 }
6783 }
6784 }
6785 else // true_type is reference
6786 {
6787 if (CanAssignmentConvert(false_type,
6788 conditional_expression -> true_expression))
6789 {
6790 conditional_expression -> true_expression =
6791 ConvertToType(conditional_expression -> true_expression,
6792 false_type);
6793 conditional_expression -> symbol = false_type;
6794 }
6795 else if (CanAssignmentConvert(true_type,
6796 conditional_expression -> false_expression))
6797 {
6798 conditional_expression -> false_expression =
6799 ConvertToType(conditional_expression -> false_expression,
6800 true_type);
6801 conditional_expression -> symbol = true_type;
6802 }
6803 else
6804 {
6805 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
6806 conditional_expression -> true_expression -> LeftToken(),
6807 conditional_expression -> false_expression -> RightToken(),
6808 true_type -> ContainingPackageName(),
6809 true_type -> ExternalName(),
6810 false_type -> ContainingPackageName(),
6811 false_type -> ExternalName());
6812 conditional_expression -> symbol = control.no_type;
6813 }
6814
6815 //
6816 // If all the subexpressions are constants, compute the results and
6817 // set the value of the expression accordingly.
6818 //
6819 // Since null should not be a compile-time constant, the assert
6820 // should not need to check for null type.
6821 //
6822 if (conditional_expression -> true_expression -> IsConstant() &&
6823 conditional_expression -> false_expression -> IsConstant())
6824 {
6825 assert(conditional_expression -> symbol == control.String() ||
6826 conditional_expression -> symbol == control.no_type);
6827
6828 if (IsConstantTrue(conditional_expression -> test_expression))
6829 conditional_expression -> value =
6830 conditional_expression -> true_expression -> value;
6831 else if (IsConstantFalse(conditional_expression -> test_expression))
6832 conditional_expression -> value =
6833 conditional_expression -> false_expression -> value;
6834 }
6835 }
6836 }
6837
6838
ProcessAssignmentExpression(Ast * expr)6839 void Semantic::ProcessAssignmentExpression(Ast* expr)
6840 {
6841 AstAssignmentExpression* assignment_expression =
6842 (AstAssignmentExpression*) expr;
6843 ProcessExpressionOrStringConstant(assignment_expression -> expression);
6844
6845 AstExpression* left_hand_side = assignment_expression -> left_hand_side;
6846 //
6847 // JLS2 added ability for parenthesized variable to remain a variable.
6848 // Therefore, the grammar was changed to accept all expressions, to avoid
6849 // ambiguities, and we must filter out invalid left-hand sides.
6850 //
6851 if (left_hand_side -> ParenthesizedExpressionCast())
6852 {
6853 ReportSemError(SemanticError::UNNECESSARY_PARENTHESIS, left_hand_side);
6854 while (left_hand_side -> ParenthesizedExpressionCast())
6855 left_hand_side = ((AstParenthesizedExpression*) left_hand_side) ->
6856 expression;
6857 }
6858
6859 //
6860 // JLS2 8.3.2.3 permits simple assignment to a variable that has not
6861 // yet been declared in an initializer. If the left_hand_side is a
6862 // variable, we use ProcessingSimpleAssignment() to inform
6863 // CheckSimpleName() to treat it specially.
6864 //
6865 if ((assignment_expression -> Tag() ==
6866 AstAssignmentExpression::SIMPLE_EQUAL) &&
6867 left_hand_side -> NameCast() &&
6868 ! left_hand_side -> NameCast() -> base_opt)
6869 {
6870 ProcessingSimpleAssignment() = true;
6871 }
6872
6873 ProcessExpression(left_hand_side);
6874 ProcessingSimpleAssignment() = false;
6875
6876 if (! left_hand_side -> IsLeftHandSide())
6877 {
6878 ReportSemError(SemanticError::NOT_A_VARIABLE, left_hand_side);
6879 left_hand_side -> symbol = control.no_type;
6880 assignment_expression -> symbol = control.no_type;
6881 }
6882
6883 TypeSymbol* left_type = left_hand_side -> Type();
6884 TypeSymbol* right_type = assignment_expression -> expression -> Type();
6885
6886 if (left_type == control.no_type ||
6887 right_type == control.no_type || right_type == control.void_type)
6888 {
6889 if (right_type == control.void_type)
6890 ReportSemError(SemanticError::TYPE_IS_VOID,
6891 assignment_expression -> expression,
6892 right_type -> Name());
6893 assignment_expression -> symbol = control.no_type;
6894 return;
6895 }
6896 assignment_expression -> symbol = left_type;
6897
6898 if (! left_hand_side -> ArrayAccessCast()) // the left-hand-side is a name
6899 {
6900 MethodSymbol* read_method = NULL;
6901 AstName* name = left_hand_side -> NameCast();
6902 AstFieldAccess* field_access = left_hand_side -> FieldAccessCast();
6903 if (name)
6904 {
6905 if (name -> resolution_opt)
6906 read_method =
6907 name -> resolution_opt -> symbol -> MethodCast();
6908 }
6909 else if (field_access)
6910 {
6911 if (field_access -> resolution_opt)
6912 read_method =
6913 field_access -> resolution_opt -> symbol -> MethodCast();
6914 }
6915
6916 if (read_method)
6917 assignment_expression -> write_method = read_method ->
6918 containing_type -> GetWriteAccessFromReadAccess(read_method);
6919 }
6920
6921 if (assignment_expression -> Tag() ==
6922 AstAssignmentExpression::SIMPLE_EQUAL)
6923 {
6924 if (left_type != right_type)
6925 {
6926 if (CanAssignmentConvert(left_type,
6927 assignment_expression -> expression))
6928 {
6929 assignment_expression -> expression =
6930 ConvertToType(assignment_expression -> expression,
6931 left_type);
6932 }
6933 else if (assignment_expression -> expression -> IsConstant() &&
6934 control.IsSimpleIntegerValueType(left_type) &&
6935 control.IsSimpleIntegerValueType(right_type))
6936 {
6937 if (left_type == control.byte_type)
6938 ReportSemError(SemanticError::INVALID_BYTE_VALUE,
6939 assignment_expression -> expression);
6940 else if (left_type == control.short_type)
6941 ReportSemError(SemanticError::INVALID_SHORT_VALUE,
6942 assignment_expression -> expression);
6943 else
6944 {
6945 assert(left_type == control.char_type);
6946 ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
6947 assignment_expression -> expression);
6948 }
6949 assignment_expression -> symbol = control.no_type;
6950 }
6951 else
6952 {
6953 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_ASSIGNMENT,
6954 assignment_expression,
6955 left_type -> ContainingPackageName(),
6956 left_type -> ExternalName(),
6957 right_type -> ContainingPackageName(),
6958 right_type -> ExternalName());
6959 assignment_expression -> symbol = control.no_type;
6960 }
6961 }
6962 return;
6963 }
6964
6965 //
6966 // In JLS 2, it states that the only reference type on the left can
6967 // be String, for +=. However, some compilers accept any type on the left
6968 // that can be assigned a String, provided the right side is a String.
6969 // In the process, that means an array access could then throw an
6970 // ArrayStoreException when the left type is not String.
6971 //
6972 // TODO: Get the definative answer from Sun which behavior is correct
6973 //
6974 if (left_type == control.String() &&
6975 (assignment_expression -> Tag() ==
6976 AstAssignmentExpression::PLUS_EQUAL))
6977 {
6978 if (right_type != control.String())
6979 {
6980 if (right_type == control.void_type)
6981 {
6982 ReportSemError(SemanticError::VOID_TO_STRING,
6983 assignment_expression -> expression);
6984 assignment_expression -> symbol = control.no_type;
6985 }
6986 else
6987 {
6988 assignment_expression -> expression -> value =
6989 CastValue(control.String(),
6990 assignment_expression -> expression);
6991 if (assignment_expression -> expression -> IsConstant())
6992 {
6993 assignment_expression -> expression -> symbol =
6994 control.String();
6995 }
6996 }
6997 }
6998 return;
6999 }
7000
7001 switch (assignment_expression -> Tag())
7002 {
7003 case AstAssignmentExpression::PLUS_EQUAL:
7004 case AstAssignmentExpression::STAR_EQUAL:
7005 case AstAssignmentExpression::MINUS_EQUAL:
7006 BinaryNumericPromotion(assignment_expression);
7007 break;
7008 case AstAssignmentExpression::SLASH_EQUAL:
7009 case AstAssignmentExpression::MOD_EQUAL:
7010 BinaryNumericPromotion(assignment_expression);
7011 {
7012 AstExpression* right_expression =
7013 assignment_expression -> expression;
7014 if (right_expression -> IsConstant())
7015 {
7016 //
7017 // If the type of the expression is integral and the right
7018 // hand side is constant 0 then issue an error message.
7019 //
7020 if ((right_expression -> Type() == control.int_type &&
7021 DYNAMIC_CAST<IntLiteralValue*>
7022 (right_expression -> value) -> value == 0) ||
7023 (right_expression -> Type() == control.long_type &&
7024 DYNAMIC_CAST<LongLiteralValue*>
7025 (right_expression -> value) -> value == 0))
7026 {
7027 //
7028 // This will guarantee a runtime exception, but the
7029 // clarifications to JLS2 insist it is legal code.
7030 //
7031 ReportSemError(SemanticError::ZERO_DIVIDE_CAUTION,
7032 assignment_expression);
7033 }
7034 }
7035 }
7036 break;
7037 case AstAssignmentExpression::LEFT_SHIFT_EQUAL:
7038 case AstAssignmentExpression::RIGHT_SHIFT_EQUAL:
7039 case AstAssignmentExpression::UNSIGNED_RIGHT_SHIFT_EQUAL:
7040 assignment_expression -> left_hand_side
7041 = PromoteUnaryNumericExpression(left_hand_side);
7042 if (! control.IsIntegral(left_type))
7043 {
7044 if (assignment_expression -> left_hand_side -> symbol !=
7045 control.no_type)
7046 {
7047 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
7048 assignment_expression -> left_hand_side,
7049 left_type -> ContainingPackageName(),
7050 left_type -> ExternalName());
7051 }
7052 assignment_expression -> symbol = control.no_type;
7053 }
7054 //
7055 // This call captures both unary numeric conversion (widening) of
7056 // byte, char, or short, and narrowing of long, since the bytecode
7057 // requires an int shift amount.
7058 //
7059 if (! control.IsIntegral(right_type))
7060 {
7061 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
7062 assignment_expression -> expression,
7063 right_type -> ContainingPackageName(),
7064 right_type -> ExternalName());
7065 assignment_expression -> symbol = control.no_type;
7066 }
7067 assignment_expression -> expression =
7068 ConvertToType(assignment_expression -> expression,
7069 control.int_type);
7070 ProcessShiftCount(left_type, assignment_expression -> expression);
7071 break;
7072 case AstAssignmentExpression::AND_EQUAL:
7073 case AstAssignmentExpression::XOR_EQUAL:
7074 case AstAssignmentExpression::IOR_EQUAL:
7075 if (left_type == control.boolean_type)
7076 {
7077 if (right_type != control.boolean_type)
7078 {
7079 ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
7080 assignment_expression -> expression,
7081 right_type -> ContainingPackageName(),
7082 right_type -> ExternalName());
7083 assignment_expression -> symbol = control.no_type;
7084 }
7085 }
7086 else
7087 {
7088 if (! control.IsIntegral(left_type))
7089 {
7090 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
7091 left_hand_side,
7092 left_type -> ContainingPackageName(),
7093 left_type -> ExternalName());
7094 assignment_expression -> symbol = control.no_type;
7095 }
7096 if (! control.IsIntegral(right_type))
7097 {
7098 ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
7099 assignment_expression -> expression,
7100 right_type -> ContainingPackageName(),
7101 right_type -> ExternalName());
7102 assignment_expression -> symbol = control.no_type;
7103 }
7104 BinaryNumericPromotion(assignment_expression);
7105 }
7106 break;
7107 default:
7108 assert(false);
7109 break;
7110 }
7111 }
7112
7113 #ifdef HAVE_JIKES_NAMESPACE
7114 } // Close namespace Jikes block
7115 #endif
7116
7117