1 // $Id: decl.cpp,v 1.144 2004/09/26 22:40:41 elliott-oss Exp $
2 //
3 // This software is subject to the terms of the IBM Jikes Compiler
4 // License Agreement available at the following URL:
5 // http://ibm.com/developerworks/opensource/jikes.
6 // Copyright (C) 1996, 2004 IBM Corporation and others. All Rights Reserved.
7 // You must accept the terms of that agreement to use this software.
8 //
9 #include "platform.h"
10 #include "semantic.h"
11 #include "control.h"
12 #include "depend.h"
13 #include "table.h"
14 #include "tuple.h"
15 #include "spell.h"
16 #include "option.h"
17 #include "stream.h"
18
19 #ifdef HAVE_JIKES_NAMESPACE
20 namespace Jikes { // Open namespace Jikes block
21 #endif
22
23 //
24 // If this compilation unit contains a package declaration, make sure
25 // the package is not also associated with a type. We used to also
26 // require that the package exist at compile time, but this was
27 // changed so that we are compatible with other Java compilers.
28 //
CheckPackage()29 inline void Semantic::CheckPackage()
30 {
31 if (! compilation_unit -> package_declaration_opt)
32 return;
33 AstPackageDeclaration* package_decl =
34 compilation_unit -> package_declaration_opt;
35 //
36 // Make sure that the package or any of its parents does not match the
37 // name of a type.
38 //
39 for (PackageSymbol* subpackage = this_package,
40 *package = subpackage -> owner;
41 package; subpackage = package, package = package -> owner)
42 {
43 FileSymbol* file_symbol =
44 Control::GetFile(control, package, subpackage -> Identity());
45 if (file_symbol)
46 {
47 char* file_name = file_symbol -> FileName();
48 int length = file_symbol -> FileNameLength();
49 wchar_t* error_name = new wchar_t[length + 1];
50 for (int i = 0; i < length; i++)
51 error_name[i] = file_name[i];
52 error_name[length] = U_NULL;
53
54 ReportSemError(SemanticError::PACKAGE_TYPE_CONFLICT,
55 compilation_unit -> package_declaration_opt -> name,
56 package -> PackageName(), subpackage -> Name(),
57 error_name);
58 delete [] error_name;
59 }
60 }
61 // TODO: Warn about package annotations outside of package-info.java.
62 ProcessPackageModifiers(package_decl);
63 }
64
65
66 //
67 // Pass 1: Introduce the main package, the current package and all types
68 // specified into their proper scope
69 //
ProcessTypeNames()70 void Semantic::ProcessTypeNames()
71 {
72 import_on_demand_packages.Next() = control.LangPackage();
73 compilation_unit = source_file_symbol -> compilation_unit;
74
75 //
76 // If we are supposed to be verbose, report empty declarations...
77 //
78 if (control.option.pedantic)
79 {
80 if (compilation_unit -> EmptyCompilationUnitCast())
81 ReportSemError(SemanticError::NO_TYPES, compilation_unit);
82 for (unsigned i = 0;
83 i < compilation_unit -> NumTypeDeclarations(); i++)
84 {
85 AstDeclaredType* type_declaration =
86 compilation_unit -> TypeDeclaration(i);
87 if (type_declaration -> EmptyDeclarationCast())
88 {
89 ReportSemError(SemanticError::EMPTY_DECLARATION,
90 type_declaration);
91 }
92 }
93 }
94
95 //
96 // If we have a bad compilation unit insert its types as "bad types"
97 //
98 if (compilation_unit -> BadCompilationUnitCast())
99 {
100 for (unsigned i = 0; i < lex_stream -> NumTypes(); i++)
101 {
102 TokenIndex identifier_token =
103 lex_stream -> Next(lex_stream -> Type(i));
104 NameSymbol* name_symbol =
105 lex_stream -> NameSymbol(identifier_token);
106 if (name_symbol)
107 {
108 TypeSymbol* type = this_package -> FindTypeSymbol(name_symbol);
109 assert(type);
110
111 type -> MarkBad();
112 type -> MarkSourceNoLongerPending();
113 type -> supertypes_closure = new SymbolSet;
114 type -> subtypes = new SymbolSet;
115 type -> semantic_environment =
116 new SemanticEnvironment(this, type, NULL);
117 if (type != control.Object())
118 type -> super = control.no_type;
119 if (! type -> FindMethodSymbol(control.init_name_symbol))
120 AddDefaultConstructor(type);
121 source_file_symbol -> types.Next() = type;
122 }
123 }
124 return;
125 }
126
127 //
128 // Use this tuple to compute the list of valid types encountered in this
129 // compilation unit.
130 //
131 Tuple<TypeSymbol*> type_list;
132
133 //
134 // Process each type in this compilation unit, in turn
135 //
136 for (unsigned k = 0; k < compilation_unit -> NumTypeDeclarations(); k++)
137 {
138 TokenIndex identifier_token = BAD_TOKEN;
139 TypeSymbol* type = NULL;
140 AstDeclaredType* declaration = compilation_unit -> TypeDeclaration(k);
141 if (! declaration -> EmptyDeclarationCast())
142 {
143 identifier_token = declaration -> class_body -> identifier_token;
144 NameSymbol* name_symbol =
145 lex_stream -> NameSymbol(identifier_token);
146 //
147 // Warn against unconventional names.
148 //
149 if (name_symbol -> IsBadStyleForClass())
150 {
151 ReportSemError(SemanticError::UNCONVENTIONAL_CLASS_NAME,
152 identifier_token, name_symbol -> Name());
153 }
154 type = this_package -> FindTypeSymbol(name_symbol);
155 // type was already created in Control::ProcessPackageDeclaration
156 assert(type);
157 if (! type -> SourcePending())
158 {
159 ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
160 identifier_token, name_symbol -> Name(),
161 type -> FileLoc());
162 type = NULL;
163 }
164 else
165 {
166 if (type -> ContainingPackage() == control.UnnamedPackage())
167 {
168 TypeSymbol* old_type = (TypeSymbol*) control.
169 unnamed_package_types.Image(name_symbol);
170 if (old_type != type)
171 {
172 ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
173 identifier_token, name_symbol -> Name(),
174 old_type -> FileLoc());
175 }
176 }
177 // Save valid type for later processing. See below.
178 type_list.Next() = type;
179 type -> MarkSourceNoLongerPending();
180 type -> semantic_environment =
181 new SemanticEnvironment(this, type, NULL);
182 type -> declaration = declaration -> class_body;
183 type -> SetFlags(ProcessTopLevelTypeModifiers(declaration));
184 //
185 // Add 3 extra elements for padding. May need a default
186 // constructor and other support elements.
187 //
188 type -> SetSymbolTable(declaration -> class_body ->
189 NumClassBodyDeclarations() + 3);
190 type -> SetLocation();
191
192 if (lex_stream -> IsDeprecated(declaration -> LeftToken()))
193 type -> MarkDeprecated();
194 source_file_symbol -> types.Next() = type;
195 declaration -> class_body -> semantic_environment =
196 type -> semantic_environment;
197 CheckNestedMembers(type, declaration -> class_body);
198 }
199 }
200
201 //
202 // If we successfully processed this type, check that
203 // . its name does not conflict with a subpackage
204 // . if it is contained in a file with a different name
205 // than its own name that there does not also exist a
206 // (java or class) file with its name.
207 //
208 if (type)
209 {
210 NameSymbol* name_symbol =
211 lex_stream -> NameSymbol(identifier_token);
212 for (unsigned i = 0; i < this_package -> directory.Length(); i++)
213 {
214 //
215 // The unnamed package cannot contain subpackages, as
216 // subpackages require a named top-level package. In other
217 // words, java.lang is not a subpackage of the unnamed one.
218 //
219 if ((this_package -> directory[i] ->
220 FindDirectorySymbol(name_symbol)) &&
221 this_package != control.UnnamedPackage())
222 {
223 char* file_name = type -> file_symbol -> FileName();
224 int length = type -> file_symbol -> FileNameLength();
225 wchar_t* error_name = new wchar_t[length + 1];
226 for (int j = 0; j < length; j++)
227 error_name[j] = file_name[j];
228 error_name[length] = U_NULL;
229
230 ReportSemError(SemanticError::PACKAGE_TYPE_CONFLICT,
231 identifier_token,
232 this_package -> PackageName(),
233 name_symbol -> Name(), error_name);
234 delete [] error_name;
235 }
236 }
237
238 if (type -> Identity() != source_file_symbol -> Identity())
239 {
240 PackageSymbol* package = this_package;
241 FileSymbol* file_symbol =
242 Control::GetJavaFile(package, type -> Identity());
243
244 if (file_symbol)
245 {
246 ReportSemError(SemanticError::TYPE_IN_MULTIPLE_FILES,
247 identifier_token,
248 this_package -> PackageName(),
249 source_file_symbol -> Name(),
250 package -> PackageName(), type -> Name());
251 }
252 }
253 }
254 }
255
256 CheckPackage();
257 ProcessImports();
258
259 //
260 // Make sure that compilation unit contains exactly one public type, and
261 // that it matches the file name.
262 //
263 TypeSymbol* public_type = NULL;
264 for (unsigned i = 0; i < type_list.Length(); i++)
265 {
266 TypeSymbol* type = type_list[i];
267 if (type && type -> ACC_PUBLIC())
268 {
269 if (! public_type)
270 {
271 public_type = type;
272 if (source_file_symbol -> Identity() !=
273 public_type -> Identity())
274 {
275 ReportSemError(SemanticError::MISMATCHED_TYPE_AND_FILE_NAMES,
276 type -> declaration -> identifier_token,
277 public_type -> Name());
278 }
279 }
280 else
281 {
282 ReportSemError(SemanticError::MULTIPLE_PUBLIC_TYPES,
283 type -> declaration -> identifier_token,
284 type -> Name(),
285 public_type -> Name());
286 }
287 }
288 }
289 }
290
291
CheckNestedMembers(TypeSymbol * containing_type,AstClassBody * class_body)292 void Semantic::CheckNestedMembers(TypeSymbol* containing_type,
293 AstClassBody* class_body)
294 {
295 unsigned i;
296 for (i = 0; i < class_body -> NumNestedClasses(); i++)
297 {
298 AstClassDeclaration* decl = class_body -> NestedClass(i);
299 ProcessNestedTypeName(containing_type, decl);
300 }
301 for (i = 0; i < class_body -> NumNestedEnums(); i++)
302 {
303 AstEnumDeclaration* decl = class_body -> NestedEnum(i);
304 ProcessNestedTypeName(containing_type, decl);
305 }
306 for (i = 0; i < class_body -> NumNestedInterfaces(); i++)
307 {
308 AstInterfaceDeclaration* decl = class_body -> NestedInterface(i);
309 ProcessNestedTypeName(containing_type, decl);
310 }
311 for (i = 0; i < class_body -> NumNestedAnnotations(); i++)
312 {
313 AstAnnotationDeclaration* decl = class_body -> NestedAnnotation(i);
314 ProcessNestedTypeName(containing_type, decl);
315 }
316 for (i = 0; i < class_body -> NumEmptyDeclarations(); i++)
317 {
318 if (control.option.pedantic)
319 ReportSemError(SemanticError::EMPTY_DECLARATION,
320 class_body -> EmptyDeclaration(i));
321 }
322 }
323
324
325 //
326 // Given a type shadow symbol, returns the first accessible type, and reports
327 // an error for any other accessible types.
328 //
FindTypeInShadow(TypeShadowSymbol * type_shadow_symbol,TokenIndex identifier_token)329 inline TypeSymbol* Semantic::FindTypeInShadow(TypeShadowSymbol* type_shadow_symbol,
330 TokenIndex identifier_token)
331 {
332 TypeSymbol* type = type_shadow_symbol -> type_symbol;
333 unsigned i = 0;
334 if (! TypeAccessCheck(type))
335 {
336 if (type_shadow_symbol -> NumConflicts())
337 {
338 //
339 // The conflicts are necessarily accessible, because they are
340 // public types inherited from interfaces.
341 //
342 type = type_shadow_symbol -> Conflict(0);
343 i = 1;
344 }
345 else type = NULL;
346 }
347 for ( ; i < type_shadow_symbol -> NumConflicts(); i++)
348 {
349 ReportSemError(SemanticError::AMBIGUOUS_TYPE,
350 identifier_token, type -> Name(),
351 type -> owner -> TypeCast() -> ContainingPackageName(),
352 type -> owner -> TypeCast() -> ExternalName(),
353 type_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ContainingPackageName(),
354 type_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ExternalName());
355 }
356 return type;
357 }
358
359
CheckNestedTypeDuplication(SemanticEnvironment * env,TokenIndex identifier_token)360 void Semantic::CheckNestedTypeDuplication(SemanticEnvironment* env,
361 TokenIndex identifier_token)
362 {
363 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
364
365 //
366 // First check to see if we have a duplication at the same level...
367 //
368 TypeSymbol* old_type = NULL;
369 if (env -> symbol_table.Size())
370 {
371 for (int i = env -> symbol_table.Size(); --i >= 0; )
372 {
373 old_type = env -> symbol_table[i] -> FindTypeSymbol(name_symbol);
374 if (old_type)
375 break;
376 }
377 }
378 else old_type = env -> Type() -> FindTypeSymbol(name_symbol);
379 if (old_type)
380 {
381 ReportSemError(SemanticError::DUPLICATE_TYPE_DECLARATION,
382 identifier_token, name_symbol -> Name(),
383 old_type -> FileLoc());
384 }
385 else
386 {
387 //
388 // ... Then check the enclosing environments...
389 //
390 for (; env; env = env -> previous)
391 {
392 if (env -> Type() -> Identity() == name_symbol)
393 {
394 ReportSemError(SemanticError::DUPLICATE_INNER_TYPE_NAME,
395 identifier_token, name_symbol -> Name(),
396 env -> Type() -> FileLoc());
397 break;
398 }
399 }
400 }
401 }
402
403
ProcessNestedTypeName(TypeSymbol * containing_type,AstDeclaredType * declaration)404 TypeSymbol* Semantic::ProcessNestedTypeName(TypeSymbol* containing_type,
405 AstDeclaredType* declaration)
406 {
407 AstClassBody* class_body = declaration -> class_body;
408 NameSymbol* name_symbol =
409 lex_stream -> NameSymbol(class_body -> identifier_token);
410 TypeSymbol* outermost_type = containing_type -> outermost_type;
411
412 CheckNestedTypeDuplication(containing_type -> semantic_environment,
413 class_body -> identifier_token);
414 int length = containing_type -> ExternalNameLength() + 1 +
415 name_symbol -> NameLength(); // +1 for $,... +1 for $
416 wchar_t* external_name = new wchar_t[length + 1]; // +1 for '\0';
417 wcscpy(external_name, containing_type -> ExternalName());
418 wcscat(external_name, StringConstant::US_DS);
419 wcscat(external_name, name_symbol -> Name());
420
421 TypeSymbol* inner_type =
422 containing_type -> InsertNestedTypeSymbol(name_symbol);
423 inner_type -> outermost_type = outermost_type;
424 inner_type -> supertypes_closure = new SymbolSet;
425 inner_type -> subtypes = new SymbolSet;
426 inner_type -> SetExternalIdentity(control.FindOrInsertName(external_name,
427 length));
428 inner_type -> semantic_environment =
429 new SemanticEnvironment(this, inner_type,
430 containing_type -> semantic_environment);
431 inner_type -> declaration = declaration -> class_body;
432 inner_type -> file_symbol = source_file_symbol;
433 inner_type -> SetFlags(ProcessNestedTypeModifiers(containing_type,
434 declaration));
435 inner_type -> SetOwner(containing_type);
436 //
437 // Add 3 extra elements for padding. May need a default constructor and
438 // other support elements.
439 //
440 inner_type -> SetSymbolTable(class_body -> NumClassBodyDeclarations() + 3);
441 inner_type -> SetLocation();
442 inner_type -> SetSignature(control);
443
444 delete [] external_name;
445
446 if (lex_stream -> IsDeprecated(declaration -> LeftToken()))
447 inner_type -> MarkDeprecated();
448
449 //
450 // If not a top-level type, then add pointer to enclosing type.
451 //
452 if (! inner_type -> ACC_STATIC())
453 inner_type -> InsertThis0();
454 else if (containing_type -> IsInner())
455 {
456 ReportSemError(SemanticError::STATIC_TYPE_IN_INNER_CLASS,
457 class_body -> identifier_token, name_symbol -> Name(),
458 containing_type -> Name(),
459 containing_type -> FileLoc());
460 // Change its status so we can continue compiling...
461 inner_type -> ResetACC_STATIC();
462 }
463
464 if (inner_type -> IsLocal())
465 {
466 if (! outermost_type -> local)
467 outermost_type -> local = new SymbolSet;
468 outermost_type -> local -> AddElement(inner_type);
469 }
470 else
471 {
472 if (! outermost_type -> non_local)
473 outermost_type -> non_local = new SymbolSet;
474 outermost_type -> non_local -> AddElement(inner_type);
475 }
476
477 //
478 // Warn against unconventional names.
479 //
480 if (name_symbol -> IsBadStyleForClass())
481 {
482 ReportSemError(SemanticError::UNCONVENTIONAL_CLASS_NAME,
483 class_body -> identifier_token, name_symbol -> Name());
484 }
485
486 declaration -> class_body -> semantic_environment =
487 inner_type -> semantic_environment;
488 CheckNestedMembers(inner_type, class_body);
489 return inner_type;
490 }
491
492
493 //
494 // Pass 1.2: Process all import statements
495 //
ProcessImports()496 void Semantic::ProcessImports()
497 {
498 for (unsigned i = 0; i < compilation_unit -> NumImportDeclarations(); i++)
499 {
500 AstImportDeclaration* import_declaration =
501 compilation_unit -> ImportDeclaration(i);
502 if (import_declaration -> static_token_opt)
503 {
504 // TODO: Add static import support for 1.5.
505 // if (control.option.source < JikesOption::SDK1_5)
506 {
507 ReportSemError(SemanticError::STATIC_IMPORT_UNSUPPORTED,
508 import_declaration -> static_token_opt);
509 }
510 }
511 if (import_declaration -> star_token_opt)
512 ProcessTypeImportOnDemandDeclaration(import_declaration);
513 else ProcessSingleTypeImportDeclaration(import_declaration);
514 }
515 }
516
517
518 //
519 // Pass 2: Process "extends" and "implements" clauses associated with the
520 // types.
521 //
ProcessTypeHeader(AstClassDeclaration * declaration)522 void Semantic::ProcessTypeHeader(AstClassDeclaration* declaration)
523 {
524 TypeSymbol* type =
525 declaration -> class_body -> semantic_environment -> Type();
526 assert(! type -> HeaderProcessed() || type -> Bad());
527 type -> MarkHeaderProcessed();
528 if (type -> Bad())
529 return;
530
531 //
532 // Special case certain classes in java.lang. We can't use the
533 // control.Classname() accessor method here, because that causes problems
534 // with recursion or non-existant classes.
535 //
536 if (this_package == control.LangPackage() && ! type -> IsNested())
537 {
538 // java.lang.Object is the only class with no supertype.
539 if (type -> Identity() == control.Object_name_symbol)
540 {
541 if (declaration -> super_opt || declaration -> NumInterfaces())
542 {
543 ReportSemError(SemanticError::OBJECT_WITH_SUPER_TYPE,
544 declaration -> LeftToken(),
545 declaration -> class_body -> left_brace_token - 1);
546 }
547 if (declaration -> type_parameters_opt)
548 {
549 ReportSemError(SemanticError::TYPE_MAY_NOT_HAVE_PARAMETERS,
550 declaration -> LeftToken(),
551 declaration -> class_body -> left_brace_token - 1,
552 type -> ContainingPackageName(),
553 type -> ExternalName());
554 }
555 type -> MarkHeaderProcessed();
556 return;
557 }
558 //
559 // java.lang.Enum didn't exist before 1.5.
560 //
561 else if (type -> Identity() == control.Enum_name_symbol)
562 type -> MarkEnum();
563 }
564
565 if (declaration -> type_parameters_opt)
566 ProcessTypeParameters(type, declaration -> type_parameters_opt);
567
568 //
569 // Process the supertypes.
570 //
571 if (declaration -> super_opt)
572 {
573 ProcessType(declaration -> super_opt);
574 TypeSymbol* super_type = declaration -> super_opt -> symbol;
575 assert(! super_type -> SourcePending());
576 if (! super_type -> HeaderProcessed())
577 super_type -> ProcessTypeHeaders();
578 if (control.option.deprecation && state_stack.Size() == 0 &&
579 super_type -> IsDeprecated() && ! type -> IsDeprecated())
580 {
581 ReportSemError(SemanticError::DEPRECATED_TYPE,
582 declaration -> super_opt,
583 super_type -> ContainingPackageName(),
584 super_type -> ExternalName());
585 }
586 if (super_type -> IsEnum())
587 {
588 ReportSemError(SemanticError::SUPER_IS_ENUM,
589 declaration -> super_opt,
590 super_type -> ContainingPackageName(),
591 super_type -> ExternalName());
592 }
593 else if (super_type -> ACC_INTERFACE())
594 {
595 ReportSemError(SemanticError::NOT_A_CLASS,
596 declaration -> super_opt,
597 super_type -> ContainingPackageName(),
598 super_type -> ExternalName());
599 }
600 else if (super_type -> ACC_FINAL())
601 {
602 ReportSemError(SemanticError::SUPER_IS_FINAL,
603 declaration -> super_opt,
604 super_type -> ContainingPackageName(),
605 super_type -> ExternalName());
606 }
607 else if (super_type -> Bad())
608 ; // ignore
609 else
610 {
611 super_type -> subtypes -> AddElement(type);
612 type -> super = super_type;
613 while (super_type)
614 {
615 type -> supertypes_closure -> AddElement(super_type);
616 type -> supertypes_closure -> Union(*super_type ->
617 supertypes_closure);
618 if (super_type -> owner -> TypeCast())
619 super_type = super_type -> ContainingType();
620 else super_type = NULL;
621 }
622 }
623 }
624 if (! type -> super)
625 {
626 type -> super = control.Object();
627 type -> supertypes_closure -> AddElement(control.Object());
628 control.Object() -> subtypes -> AddElement(type);
629 }
630 AddDependence(type, type -> super);
631
632 for (unsigned i = 0; i < declaration -> NumInterfaces(); i++)
633 ProcessSuperinterface(type, declaration -> Interface(i));
634
635 // if there is a cycle, break it and issue an error message
636 if (type -> supertypes_closure -> IsElement(type))
637 {
638 type -> super = control.Object();
639 type -> ResetInterfaces();
640 type -> MarkCircular();
641 ReportSemError(SemanticError::CIRCULAR_CLASS,
642 declaration -> class_body -> identifier_token,
643 declaration -> class_body -> left_brace_token - 1,
644 type -> ContainingPackageName(),
645 type -> ExternalName());
646 }
647 else if (declaration -> type_parameters_opt &&
648 type -> IsSubclass(control.Throwable()))
649 {
650 ReportSemError(SemanticError::TYPE_MAY_NOT_HAVE_PARAMETERS,
651 declaration -> LeftToken(),
652 declaration -> class_body -> left_brace_token - 1,
653 type -> ContainingPackageName(),
654 type -> ExternalName());
655 }
656 }
657
658
ProcessTypeHeader(AstEnumDeclaration * declaration)659 void Semantic::ProcessTypeHeader(AstEnumDeclaration* declaration)
660 {
661 TypeSymbol* type =
662 declaration -> class_body -> semantic_environment -> Type();
663 assert(! type -> HeaderProcessed() || type -> Bad());
664 type -> MarkHeaderProcessed();
665 // TODO: Add enum support for 1.5.
666 // if (control.option.source < JikesOption::SDK1_5)
667 {
668 ReportSemError(SemanticError::ENUM_TYPE_UNSUPPORTED,
669 declaration -> enum_token);
670 type -> super = control.Object();
671 type -> MarkBad();
672 }
673 if (type -> Bad())
674 return;
675
676 //
677 // Process the supertypes.
678 //
679 type -> super = control.Enum();
680 type -> supertypes_closure -> AddElement(control.Enum());
681 type -> MarkEnum(); // Since ACC_ENUM is only for enum constants.
682 control.Enum() -> subtypes -> AddElement(type);
683 AddDependence(type, type -> super);
684 for (unsigned i = 0; i < declaration -> NumInterfaces(); i++)
685 ProcessSuperinterface(type, declaration -> Interface(i));
686 // there will not be a cycle
687 assert(! type -> supertypes_closure -> IsElement(type));
688 }
689
690
ProcessTypeHeader(AstInterfaceDeclaration * declaration)691 void Semantic::ProcessTypeHeader(AstInterfaceDeclaration* declaration)
692 {
693 TypeSymbol* type =
694 declaration -> class_body -> semantic_environment -> Type();
695 assert(! type -> HeaderProcessed() || type -> Bad());
696 type -> MarkHeaderProcessed();
697 if (declaration -> type_parameters_opt)
698 ProcessTypeParameters(type, declaration -> type_parameters_opt);
699
700 //
701 // Although interfaces do not have a superclass in source code, in
702 // bytecode they are treated as subclasses of Object.
703 //
704 type -> super = control.Object();
705 AddDependence(type, control.Object());
706 for (unsigned k = 0; k < declaration -> NumInterfaces(); k++)
707 ProcessSuperinterface(type, declaration -> Interface(k));
708
709 if (type -> supertypes_closure -> IsElement(type))
710 {
711 //
712 // Remove all the interfaces if a loop is detected.
713 //
714 type -> ResetInterfaces();
715 type -> MarkCircular();
716 ReportSemError(SemanticError::CIRCULAR_INTERFACE,
717 declaration -> class_body -> identifier_token,
718 declaration -> class_body -> left_brace_token - 1,
719 type -> ContainingPackageName(),
720 type -> ExternalName());
721 }
722 }
723
724
ProcessTypeHeader(AstAnnotationDeclaration * declaration)725 void Semantic::ProcessTypeHeader(AstAnnotationDeclaration* declaration)
726 {
727 TypeSymbol* type =
728 declaration -> class_body -> semantic_environment -> Type();
729 assert(! type -> HeaderProcessed() || type -> Bad());
730 type -> MarkHeaderProcessed();
731 // TODO: Add annotation support for 1.5.
732 // if (control.option.source < JikesOption::SDK1_5)
733 {
734 ReportSemError(SemanticError::ANNOTATION_TYPE_UNSUPPORTED,
735 declaration -> interface_token - 1,
736 declaration -> interface_token);
737 type -> MarkBad();
738 }
739 //
740 // All annotations are treated as subclasses of Object and Annotation.
741 //
742 type -> super = control.Object();
743 AddDependence(type, control.Object());
744 type -> AddInterface(control.Annotation());
745 AddDependence(type, control.Annotation());
746 }
747
748
ProcessSuperinterface(TypeSymbol * base_type,AstTypeName * name)749 void Semantic::ProcessSuperinterface(TypeSymbol* base_type, AstTypeName* name)
750 {
751 ProcessType(name);
752 TypeSymbol* interf = name -> symbol;
753
754 assert(! interf -> SourcePending());
755 if (! interf -> HeaderProcessed())
756 interf -> ProcessTypeHeaders();
757 if (control.option.deprecation && state_stack.Size() == 0 &&
758 interf -> IsDeprecated() && ! base_type -> IsDeprecated())
759 {
760 ReportSemError(SemanticError::DEPRECATED_TYPE, name,
761 interf -> ContainingPackageName(),
762 interf -> ExternalName());
763 }
764 if (! interf -> ACC_INTERFACE())
765 {
766 if (! interf -> Bad())
767 {
768 ReportSemError(SemanticError::NOT_AN_INTERFACE, name,
769 interf -> ContainingPackageName(),
770 interf -> ExternalName());
771 }
772 name -> symbol = NULL;
773 }
774 else
775 {
776 for (unsigned k = 0; k < base_type -> NumInterfaces(); k++)
777 {
778 if (base_type -> Interface(k) == interf)
779 {
780 ReportSemError(SemanticError::DUPLICATE_INTERFACE, name,
781 interf -> ContainingPackageName(),
782 interf -> ExternalName(),
783 base_type -> ExternalName());
784 name -> symbol = NULL;
785 return;
786 }
787 }
788 name -> symbol = interf; // save type name in ast.
789 base_type -> AddInterface(interf);
790 interf -> subtypes -> AddElement(base_type);
791 AddDependence(base_type, interf);
792 while (interf)
793 {
794 base_type -> supertypes_closure -> AddElement(interf);
795 base_type -> supertypes_closure -> Union(*interf ->
796 supertypes_closure);
797 if (interf -> owner -> TypeCast())
798 interf = interf -> ContainingType();
799 else interf = NULL;
800 }
801 }
802 }
803
804
805 //
806 // Processes the type parameters of a class or interface.
807 //
ProcessTypeParameters(TypeSymbol *,AstTypeParameters * parameters)808 void Semantic::ProcessTypeParameters(TypeSymbol* /*type*/,
809 AstTypeParameters* parameters)
810 {
811 // TODO: Add generics support for 1.5.
812 ReportSemError(SemanticError::TYPE_PARAMETERS_UNSUPPORTED, parameters);
813 }
814
815
816 //
817 // Process the type headers of the owner of body. Anonymous types have no
818 // owner, so anon_type must be non-null only in that case.
819 //
ProcessTypeHeaders(AstClassBody * body,TypeSymbol * anon_type)820 TypeSymbol* Semantic::ProcessTypeHeaders(AstClassBody* body,
821 TypeSymbol* anon_type)
822 {
823 assert(! body -> owner ^ ! anon_type);
824 SemanticEnvironment* sem = anon_type ? anon_type -> semantic_environment
825 : body -> semantic_environment;
826 TypeSymbol* type = anon_type ? anon_type : sem -> Type();
827 if (type -> HeaderProcessed())
828 return type; // Possible if a subclass was declared in the same file.
829 if (anon_type)
830 anon_type -> MarkHeaderProcessed();
831 else if (body -> owner -> ClassDeclarationCast())
832 ProcessTypeHeader((AstClassDeclaration*) body -> owner);
833 else if (body -> owner -> EnumDeclarationCast())
834 ProcessTypeHeader((AstEnumDeclaration*) body -> owner);
835 else if (body -> owner -> InterfaceDeclarationCast())
836 ProcessTypeHeader((AstInterfaceDeclaration*) body -> owner);
837 else
838 {
839 assert(body -> owner -> AnnotationDeclarationCast());
840 ProcessTypeHeader((AstAnnotationDeclaration*) body -> owner);
841 }
842 state_stack.Push(sem);
843 unsigned i;
844 for (i = 0; i < body -> NumNestedClasses(); i++)
845 {
846 AstClassDeclaration* nested_class = body -> NestedClass(i);
847 type -> AddNestedType(ProcessTypeHeaders(nested_class -> class_body));
848 }
849 for (i = 0; i < body -> NumNestedEnums(); i++)
850 {
851 AstEnumDeclaration* nested_enum = body -> NestedEnum(i);
852 type -> AddNestedType(ProcessTypeHeaders(nested_enum -> class_body));
853 }
854 for (i = 0; i < body -> NumNestedInterfaces(); i++)
855 {
856 AstInterfaceDeclaration* nested = body -> NestedInterface(i);
857 type -> AddNestedType(ProcessTypeHeaders(nested -> class_body));
858 }
859 for (i = 0; i < body -> NumNestedAnnotations(); i++)
860 {
861 AstAnnotationDeclaration* nested = body -> NestedAnnotation(i);
862 type -> AddNestedType(ProcessTypeHeaders(nested -> class_body));
863 }
864 state_stack.Pop();
865 return type;
866 }
867
868
ReportTypeInaccessible(TokenIndex left_tok,TokenIndex right_tok,TypeSymbol * type)869 void Semantic::ReportTypeInaccessible(TokenIndex left_tok,
870 TokenIndex right_tok,
871 TypeSymbol* type)
872 {
873 ReportSemError(SemanticError::TYPE_NOT_ACCESSIBLE, left_tok, right_tok,
874 type -> ContainingPackageName(),
875 type -> ExternalName(), type -> AccessString());
876 }
877
878
879 //
880 // Finds an accessible member type named identifier_token within type, or
881 // returns NULL. Issues an error if there are multiple ambiguous types. The
882 // caller is responsible for searching for inaccessible member types.
883 //
FindNestedType(TypeSymbol * type,TokenIndex identifier_token)884 TypeSymbol* Semantic::FindNestedType(TypeSymbol* type,
885 TokenIndex identifier_token)
886 {
887 if (type == control.null_type || type -> Bad() || type -> Primitive())
888 {
889 return NULL;
890 }
891
892 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
893
894 if (! type -> expanded_type_table)
895 ComputeTypesClosure(type, identifier_token);
896 TypeShadowSymbol* type_shadow_symbol =
897 type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
898
899 return (type_shadow_symbol
900 ? FindTypeInShadow(type_shadow_symbol, identifier_token)
901 : (TypeSymbol*) NULL);
902 }
903
904
905 //
906 // Finds a nested type named name within the enclosing type, and establishes
907 // a dependence relation. This also searches for inaccessible types, and
908 // reports an error before returning the inaccessible type. For any other
909 // error, the return is control.no_type.
910 //
MustFindNestedType(TypeSymbol * type,AstName * name)911 TypeSymbol* Semantic::MustFindNestedType(TypeSymbol* type, AstName* name)
912 {
913 if (type -> Bad())
914 return control.no_type;
915 if (name -> base_opt && ! name -> base_opt -> symbol)
916 type = MustFindNestedType(type, name -> base_opt);
917 TypeSymbol* inner_type = FindNestedType(type, name -> identifier_token);
918 if (! inner_type)
919 {
920 //
921 // Before failing completely, check whether or not the user is trying
922 // to access an inaccessible nested type.
923 //
924 NameSymbol* name_symbol =
925 lex_stream -> NameSymbol(name -> identifier_token);
926 for (TypeSymbol* super_type = type -> super;
927 super_type && ! super_type -> Bad();
928 super_type = super_type -> super)
929 {
930 assert(super_type -> expanded_type_table);
931
932 TypeShadowSymbol* type_shadow_symbol = super_type ->
933 expanded_type_table -> FindTypeShadowSymbol(name_symbol);
934 if (type_shadow_symbol)
935 {
936 inner_type = FindTypeInShadow(type_shadow_symbol,
937 name -> identifier_token);
938 break;
939 }
940 }
941
942 if (inner_type)
943 ReportTypeInaccessible(name, inner_type);
944 else inner_type = GetBadNestedType(type, name -> identifier_token);
945 }
946 return inner_type -> Bad() ? control.no_type : inner_type;
947 }
948
949
950 //
951 // Pass 3: Process all method and constructor declarations within the
952 // compilation unit so that any field initialization enclosed in the
953 // compilation unit can invoke any constructor or method within the unit.
954 //
ProcessConstructorMembers(AstClassBody * class_body)955 inline void Semantic::ProcessConstructorMembers(AstClassBody* class_body)
956 {
957 TypeSymbol* this_type = ThisType();
958 assert(this_type -> HeaderProcessed());
959 if (class_body -> NumConstructors())
960 {
961 for (unsigned i = 0; i < class_body -> NumConstructors(); i++)
962 ProcessConstructorDeclaration(class_body -> Constructor(i));
963 }
964 else if (! this_type -> Anonymous() && ! this_type -> ACC_INTERFACE())
965 AddDefaultConstructor(this_type);
966 this_type -> MarkConstructorMembersProcessed();
967 }
968
969
ProcessMethodMembers(AstClassBody * class_body)970 inline void Semantic::ProcessMethodMembers(AstClassBody* class_body)
971 {
972 assert(ThisType() -> HeaderProcessed());
973 for (unsigned i = 0; i < class_body -> NumMethods(); i++)
974 ProcessMethodDeclaration(class_body -> Method(i));
975 ThisType() -> MarkMethodMembersProcessed();
976 }
977
978
ProcessFieldMembers(AstClassBody * class_body)979 inline void Semantic::ProcessFieldMembers(AstClassBody* class_body)
980 {
981 assert(ThisType() -> HeaderProcessed());
982 unsigned i;
983 for (i = 0; i < class_body -> NumInstanceVariables(); i++)
984 ProcessFieldDeclaration(class_body -> InstanceVariable(i));
985 for (i = 0; i < class_body -> NumClassVariables(); i++)
986 ProcessFieldDeclaration(class_body -> ClassVariable(i));
987 ThisType() -> MarkFieldMembersProcessed();
988 }
989
990
ProcessClassBodyForEffectiveJavaChecks(AstClassBody * class_body)991 void Semantic::ProcessClassBodyForEffectiveJavaChecks(AstClassBody* class_body)
992 {
993 TypeSymbol* this_type = ThisType();
994 assert(this_type -> HeaderProcessed());
995
996 //
997 // Find out about this class' constructors:
998 // Does it have any non-default constructors?
999 // Does it have a private constructor?
1000 //
1001 bool has_private_constructor = false;
1002 bool has_non_default_constructor = false;
1003 for (unsigned i = 0; i < class_body -> NumConstructors(); ++i)
1004 {
1005 AstConstructorDeclaration* constructor_declaration =
1006 class_body -> Constructor(i);
1007 if (! constructor_declaration -> IsValid())
1008 continue;
1009 MethodSymbol* constructor = constructor_declaration ->
1010 constructor_symbol;
1011 if (constructor -> ACC_PRIVATE())
1012 {
1013 has_private_constructor = true;
1014 }
1015
1016 if (class_body -> default_constructor == NULL ||
1017 constructor != class_body -> default_constructor -> constructor_symbol)
1018 {
1019 has_non_default_constructor = true;
1020 }
1021 }
1022
1023 //
1024 // Find out about equals and hashCode, and count how many instance methods
1025 // we have (the NumMethods member function returns the sum of both the
1026 // class and instance methods).
1027 //
1028 bool has_correct_equals_method = false;
1029 AstMethodDeclaration* equals_method = NULL;
1030 AstMethodDeclaration* hashCode_method = NULL;
1031 int instance_method_count = 0;
1032
1033 for (unsigned i = 0; i < class_body -> NumMethods(); ++i)
1034 {
1035 AstMethodDeclaration* method_declaration = class_body -> Method(i);
1036 if (! method_declaration -> IsValid())
1037 continue;
1038 MethodSymbol* method = method_declaration -> method_symbol;
1039 if (! method -> ACC_STATIC())
1040 {
1041 ++instance_method_count;
1042 }
1043 if (! this_type -> ACC_INTERFACE() && ! method -> ACC_ABSTRACT())
1044 {
1045 //
1046 // Is it "boolean equals(T other)" for some type T?
1047 //
1048 if (method -> name_symbol == control.equals_name_symbol &&
1049 method -> Type() == control.boolean_type &&
1050 method -> NumFormalParameters() == 1 &&
1051 ! has_correct_equals_method)
1052 {
1053 equals_method = method_declaration;
1054 has_correct_equals_method = (method -> FormalParameter(0) ->
1055 Type() == control.Object());
1056 }
1057
1058 //
1059 // Is it "int hashCode()"?
1060 //
1061 if (method -> name_symbol == control.hashCode_name_symbol &&
1062 method -> Type() == control.int_type &&
1063 method -> NumFormalParameters() == 0)
1064 {
1065 hashCode_method = method_declaration;
1066 }
1067 }
1068 }
1069
1070 //
1071 // Warn about problems with equals and hashCode.
1072 //
1073 if (equals_method != NULL && ! has_correct_equals_method)
1074 {
1075 ReportSemError(SemanticError::EJ_AVOID_OVERLOADING_EQUALS,
1076 equals_method -> method_declarator -> identifier_token,
1077 this_type -> Name());
1078 }
1079 if (equals_method != NULL && hashCode_method == NULL)
1080 {
1081 ReportSemError(SemanticError::EJ_EQUALS_WITHOUT_HASH_CODE,
1082 equals_method -> method_declarator -> identifier_token,
1083 this_type -> Name());
1084 }
1085 if (equals_method == NULL && hashCode_method != NULL)
1086 {
1087 ReportSemError(SemanticError::EJ_HASH_CODE_WITHOUT_EQUALS,
1088 hashCode_method -> method_declarator -> identifier_token,
1089 this_type -> Name());
1090 }
1091
1092 //
1093 // Warn against utility classes that don't have a private constructor.
1094 // Empty classes don't count. They're not very useful, but they do
1095 // exist. The jacks test suite uses one to check the compiler's
1096 // working, and Sun use them in the Swing Windows plaf, for example.
1097 //
1098 bool is_non_empty_class = (class_body -> NumClassVariables() > 0 ||
1099 class_body -> NumInstanceVariables() > 0 ||
1100 class_body -> NumMethods() > 0);
1101 bool has_instance_members = (class_body -> NumInstanceVariables() > 0 ||
1102 instance_method_count > 0);
1103 if (is_non_empty_class &&
1104 ! has_instance_members &&
1105 ! this_type -> ACC_INTERFACE() &&
1106 ! this_type -> ACC_ABSTRACT() &&
1107 ! this_type -> Anonymous() &&
1108 ! has_non_default_constructor &&
1109 ! has_private_constructor &&
1110 this_type -> super == control.Object())
1111 {
1112 ReportSemError(SemanticError::EJ_MISSING_PRIVATE_CONSTRUCTOR,
1113 class_body -> identifier_token,
1114 this_type -> Name());
1115 }
1116
1117 //
1118 // Warn against interfaces that don't define any behavior.
1119 //
1120 if (this_type -> ACC_INTERFACE() &&
1121 this_type -> super == control.Object() &&
1122 class_body -> NumMethods() == 0)
1123 {
1124 //
1125 // Tag interfaces such as java.io.Serializable are okay, so we need
1126 // to check that there is actually something in this interface
1127 // before we complain about it.
1128 //
1129 int field_count = class_body -> NumClassVariables() +
1130 class_body -> NumInstanceVariables();
1131 if (field_count != 0)
1132 {
1133 ReportSemError(SemanticError::EJ_INTERFACE_DOES_NOT_DEFINE_TYPE,
1134 class_body -> identifier_token,
1135 this_type -> Name());
1136 }
1137 }
1138
1139 CheckForSerializationMistakes(class_body);
1140 }
1141
1142
CheckForSerializationMistakes(AstClassBody * class_body)1143 void Semantic::CheckForSerializationMistakes(AstClassBody* class_body)
1144 {
1145 TypeSymbol* this_type = ThisType();
1146
1147 if (! this_type -> Implements(control.Serializable()))
1148 return;
1149
1150 if (this_type -> IsInner())
1151 {
1152 // FIXME: If the class implements the readObject and writeObject
1153 // methods, it should be okay. But would anyone really do that?
1154 ReportSemError(SemanticError::EJ_SERIALIZABLE_INNER_CLASS,
1155 class_body -> identifier_token);
1156 }
1157
1158 //
1159 // Warn against Serializable classes without an explicit serialVersionUID.
1160 //
1161 bool found_serialVersionUID = false;
1162 for (unsigned i = 0; i < class_body -> NumClassVariables(); ++i)
1163 {
1164 AstFieldDeclaration* fd = class_body -> ClassVariable(i);
1165 for (unsigned j = 0; j < fd -> NumVariableDeclarators(); ++j)
1166 {
1167 AstVariableDeclarator* vd = fd -> VariableDeclarator(j);
1168 NameSymbol* name_symbol = lex_stream -> NameSymbol(vd ->
1169 variable_declarator_name -> identifier_token);
1170 if (name_symbol == control.serialVersionUID_name_symbol)
1171 {
1172 found_serialVersionUID = true;
1173 }
1174 }
1175 }
1176 if (! found_serialVersionUID)
1177 {
1178 ReportSemError(SemanticError::MISSING_SERIAL_VERSION_UID,
1179 class_body -> identifier_token);
1180 }
1181 }
1182
1183
ProcessMembers(AstClassBody * class_body)1184 void Semantic::ProcessMembers(AstClassBody* class_body)
1185 {
1186 state_stack.Push(class_body -> semantic_environment);
1187 TypeSymbol* this_type = ThisType();
1188 unsigned i;
1189
1190 assert(! this_type -> ConstructorMembersProcessed() || this_type -> Bad());
1191 assert(! this_type -> MethodMembersProcessed() || this_type -> Bad());
1192 assert(! this_type -> FieldMembersProcessed() || this_type -> Bad());
1193
1194 ProcessConstructorMembers(class_body);
1195 ProcessMethodMembers(class_body);
1196 ProcessFieldMembers(class_body);
1197 ProcessClassBodyForEffectiveJavaChecks(class_body);
1198
1199 delete this_type -> innertypes_closure; // save some space !!!
1200 this_type -> innertypes_closure = NULL;
1201
1202 if (this_type -> IsInner())
1203 {
1204 for (i = 0; i < class_body -> NumStaticInitializers(); i++)
1205 {
1206 ReportSemError(SemanticError::STATIC_INITIALIZER_IN_INNER_CLASS,
1207 class_body -> StaticInitializer(i),
1208 this_type -> Name(), this_type -> FileLoc());
1209 }
1210 }
1211
1212 for (i = 0; i < this_type -> NumNestedTypes(); i++)
1213 {
1214 TypeSymbol* inner_type = this_type -> NestedType(i);
1215 ProcessMembers(inner_type -> declaration);
1216 }
1217 state_stack.Pop();
1218 }
1219
1220
1221 //
1222 // Pass 4: Process the field declarations at the top level of the types
1223 //
CompleteSymbolTable(AstClassBody * class_body)1224 void Semantic::CompleteSymbolTable(AstClassBody* class_body)
1225 {
1226 if (compilation_unit -> BadCompilationUnitCast())
1227 return;
1228
1229 state_stack.Push(class_body -> semantic_environment);
1230 TypeSymbol* this_type = ThisType();
1231 TokenIndex identifier = class_body -> identifier_token;
1232
1233 assert(this_type -> ConstructorMembersProcessed());
1234 assert(this_type -> MethodMembersProcessed());
1235 assert(this_type -> FieldMembersProcessed());
1236
1237 if (! this_type -> expanded_method_table)
1238 ComputeMethodsClosure(this_type, identifier);
1239
1240 if (this_type -> super && ! this_type -> Bad())
1241 {
1242 if (! this_type -> ACC_ABSTRACT())
1243 {
1244 //
1245 // We already checked that this class does not declare abstract
1246 // methods. Now see that there are no unimplemented abstract
1247 // methods in any of the superclasses or superinterfaces. Exploit
1248 // the fact that the method table is built with the first element
1249 // being from a superclass; all conflicts are inherited from
1250 // interfaces and are necessarily abstract.
1251 //
1252 ExpandedMethodTable* expanded_table =
1253 this_type -> expanded_method_table;
1254 for (unsigned i = 0;
1255 i < expanded_table -> symbol_pool.Length(); i++)
1256 {
1257 MethodSymbol* method =
1258 expanded_table -> symbol_pool[i] -> method_symbol;
1259
1260 if (method -> ACC_ABSTRACT())
1261 {
1262 TypeSymbol* containing_type = method -> containing_type;
1263 if (containing_type != this_type)
1264 {
1265 if (! method -> IsTyped())
1266 method -> ProcessMethodSignature(this, identifier);
1267
1268 ReportSemError(SemanticError::NON_ABSTRACT_TYPE_INHERITS_ABSTRACT_METHOD,
1269 identifier, method -> Header(),
1270 containing_type -> ContainingPackageName(),
1271 containing_type -> ExternalName(),
1272 this_type -> ContainingPackageName(),
1273 this_type -> ExternalName());
1274 }
1275 }
1276 }
1277 }
1278
1279 //
1280 // If any superclass of this_type is abstract and is contained in a
1281 // different package, check to see if its members include abstract
1282 // methods with default access. If so, this class must be abstract,
1283 // as it cannot override them. And if this class has a protected or
1284 // public method with a conflicting signature, then it is cannot be
1285 // implemented. However, this can be tricky: suppose abstract p1.A
1286 // declares abstract foo(), abstract p2.B extends p1.A, abstract p1.C
1287 // extends p2.B and implements foo(). Then, p2.B does not inherit foo()
1288 // and thus neither does p1.C, but p1.C DOES override foo() with a
1289 // valid implementation. And thus, p2.D extends p1.C may be concrete.
1290 //
1291 PackageSymbol* package = this_type -> ContainingPackage();
1292 for (TypeSymbol* super_type = this_type -> super;
1293 super_type && super_type -> ACC_ABSTRACT();
1294 super_type = super_type -> super)
1295 {
1296 if (super_type -> ContainingPackage() == package)
1297 continue;
1298
1299 package = super_type -> ContainingPackage();
1300 ExpandedMethodTable* super_expanded_table =
1301 super_type -> expanded_method_table;
1302 for (unsigned i = 0;
1303 i < super_expanded_table -> symbol_pool.Length(); i++)
1304 {
1305 MethodSymbol* method =
1306 super_expanded_table -> symbol_pool[i] -> method_symbol;
1307
1308 //
1309 // Remember that abstract methods cannot be private, and that
1310 // non-default methods were inherited.
1311 //
1312 if (! method -> ACC_ABSTRACT() || method -> ACC_PUBLIC() ||
1313 method -> ACC_PROTECTED())
1314 {
1315 continue;
1316 }
1317 TypeSymbol* containing_type = method -> containing_type;
1318 if (! method -> IsTyped())
1319 method -> ProcessMethodSignature(this, identifier);
1320
1321 //
1322 // Search all intermediate superclasses in the same package
1323 // as the current super_class for an override of the abstract
1324 // method in question. Also report any protected or public
1325 // methods outside super's package that cause this class
1326 // to be uninstantiable.
1327 //
1328 TypeSymbol* intermediate;
1329 MethodSymbol* method_clash = NULL;
1330 for (intermediate = this_type;
1331 intermediate != super_type;
1332 intermediate = intermediate -> super)
1333 {
1334 MethodShadowSymbol* shadow = intermediate ->
1335 expanded_method_table ->
1336 FindOverloadMethodShadow(method, this, identifier);
1337 if (! shadow)
1338 continue;
1339 if (intermediate -> ContainingPackage() != package)
1340 {
1341 if ((shadow -> method_symbol -> ACC_PUBLIC() ||
1342 shadow -> method_symbol -> ACC_PROTECTED()) &&
1343 (shadow -> method_symbol -> Type() !=
1344 method -> Type()))
1345 {
1346 //
1347 // No need to repeat the warning for subclasses of
1348 // where the problem originally occurred.
1349 //
1350 if (method_clash == shadow -> method_symbol)
1351 method_clash = NULL;
1352 else method_clash = shadow -> method_symbol;
1353 }
1354 }
1355 else if (shadow -> method_symbol -> containing_type ==
1356 intermediate)
1357 {
1358 break;
1359 }
1360 }
1361
1362 if (intermediate == super_type &&
1363 ! this_type -> ACC_ABSTRACT())
1364 {
1365 ReportSemError((this_type -> Anonymous()
1366 ? SemanticError::ANONYMOUS_TYPE_CANNOT_OVERRIDE_DEFAULT_ABSTRACT_METHOD
1367 : SemanticError::NON_ABSTRACT_TYPE_CANNOT_OVERRIDE_DEFAULT_ABSTRACT_METHOD),
1368 identifier, method -> Header(),
1369 containing_type -> ContainingPackageName(),
1370 containing_type -> ExternalName(),
1371 this_type -> ContainingPackageName(),
1372 this_type -> ExternalName());
1373 }
1374 if (method_clash)
1375 {
1376 TypeSymbol* base_type = method_clash -> containing_type;
1377 ReportSemError(SemanticError::UNIMPLEMENTABLE_CLASS,
1378 identifier,
1379 this_type -> ContainingPackageName(),
1380 this_type -> ExternalName(),
1381 method_clash -> Header(),
1382 base_type -> ContainingPackageName(),
1383 base_type -> ExternalName(),
1384 method -> Header(),
1385 containing_type -> ContainingPackageName(),
1386 containing_type -> ExternalName());
1387 }
1388 }
1389 }
1390 }
1391
1392 //
1393 // Compute the set of final variables declared by the user in this type.
1394 // Then process all variable initializers and initializer blocks.
1395 //
1396 DefiniteSetup();
1397 ProcessStaticInitializers(class_body);
1398 ProcessInstanceInitializers(class_body);
1399
1400 //
1401 // Reset the this_variable and this_method may have been set in
1402 // ProcessStaticInitializers and/or ProcessInstanceInitializers.
1403 // Indicate that there is no method being currently compiled
1404 // in this environment.
1405 //
1406 ThisVariable() = NULL;
1407 ThisMethod() = NULL;
1408
1409 //
1410 // Recursively process all inner types
1411 //
1412 for (unsigned l = 0; l < this_type -> NumNestedTypes(); l++)
1413 {
1414 TypeSymbol* inner_type = this_type -> NestedType(l);
1415 CompleteSymbolTable(inner_type -> declaration);
1416 }
1417 state_stack.Pop();
1418 }
1419
1420
1421 //
1422 // Pass 5: Free up unneeded space.
1423 //
CleanUp()1424 void Semantic::CleanUp()
1425 {
1426 if (control.option.nocleanup)
1427 return;
1428
1429 for (unsigned i = 0; i < compilation_unit -> NumTypeDeclarations(); i++)
1430 {
1431 AstDeclaredType* type_declaration =
1432 compilation_unit -> TypeDeclaration(i);
1433 if (type_declaration -> class_body &&
1434 type_declaration -> class_body -> semantic_environment)
1435 {
1436 CleanUpType(type_declaration -> class_body ->
1437 semantic_environment -> Type());
1438 }
1439 }
1440 }
1441
1442
CleanUpType(TypeSymbol * type)1443 void Semantic::CleanUpType(TypeSymbol* type)
1444 {
1445 type -> DeleteAnonymousTypes();
1446 for (unsigned i = 0; i < type -> NumNestedTypes(); i++)
1447 CleanUpType(type -> NestedType(i));
1448
1449 type -> CompressSpace(); // space optimization
1450
1451 for (unsigned j = 0; j < type -> NumMethodSymbols(); j++)
1452 type -> MethodSym(j) -> CleanUp();
1453
1454 delete type -> local;
1455
1456 type -> local = NULL;
1457
1458 delete type -> non_local;
1459 type -> non_local = NULL;
1460
1461 delete type -> semantic_environment;
1462 type -> semantic_environment = NULL;
1463
1464 type -> declaration = NULL;
1465 }
1466
1467
ReadType(FileSymbol * file_symbol,PackageSymbol * package,NameSymbol * name_symbol,TokenIndex tok)1468 TypeSymbol* Semantic::ReadType(FileSymbol* file_symbol, PackageSymbol* package,
1469 NameSymbol* name_symbol, TokenIndex tok)
1470 {
1471 TypeSymbol* type;
1472
1473 if (file_symbol && file_symbol -> IsJava())
1474 {
1475 if (! file_symbol -> semantic)
1476 control.ProcessHeaders(file_symbol);
1477 type = package -> FindTypeSymbol(name_symbol);
1478 if (! type)
1479 {
1480 type = package -> InsertOuterTypeSymbol(name_symbol);
1481 type -> MarkBad();
1482 type -> outermost_type = type;
1483 type -> supertypes_closure = new SymbolSet;
1484 type -> subtypes = new SymbolSet;
1485 type -> semantic_environment =
1486 new SemanticEnvironment(this, type, NULL);
1487 if (type != control.Object())
1488 type -> super = (type == control.Throwable()
1489 ? control.Object() : control.Throwable());
1490 type -> SetOwner(package);
1491 type -> SetSignature(control);
1492 AddDefaultConstructor(type);
1493 type -> file_symbol = file_symbol;
1494 file_symbol -> types.Next() = type;
1495
1496 ReportSemError(SemanticError::TYPE_NOT_FOUND, tok,
1497 type -> ContainingPackageName(),
1498 type -> ExternalName());
1499 }
1500 else if (file_symbol -> semantic -> NumErrors())
1501 {
1502 ReportSemError(SemanticError::INVALID_TYPE_FOUND, tok,
1503 name_symbol -> Name());
1504 }
1505 }
1506 else if (file_symbol)
1507 {
1508 // Read class file.
1509 type = package -> InsertOuterTypeSymbol(name_symbol);
1510 type -> outermost_type = type;
1511 type -> supertypes_closure = new SymbolSet;
1512 type -> subtypes = new SymbolSet;
1513 type -> SetOwner(package);
1514 type -> SetSignature(control);
1515 type -> file_symbol = file_symbol;
1516 type -> SetLocation();
1517
1518 file_symbol -> package = package;
1519 file_symbol -> types.Next() = type;
1520
1521 ReadClassFile(type, tok);
1522 assert (! type -> IsNested());
1523 control.input_class_file_set.AddElement(file_symbol);
1524 }
1525 else
1526 {
1527 //
1528 // No file found. See if a package by the same name exists, otherwise
1529 // create a placeholder type to avoid errors when the type name is
1530 // subsequently used.
1531 //
1532 PackageSymbol* subpackage = package -> FindPackageSymbol(name_symbol);
1533 if (! subpackage)
1534 subpackage = package -> InsertPackageSymbol(name_symbol);
1535 control.FindPathsToDirectory(subpackage);
1536 if (subpackage -> directory.Length())
1537 {
1538 if (package -> directory.Length())
1539 ReportSemError(SemanticError::PACKAGE_NOT_TYPE, tok,
1540 subpackage -> PackageName());
1541 type = control.no_type;
1542 }
1543 else
1544 {
1545 type = package -> InsertOuterTypeSymbol(name_symbol);
1546 type -> outermost_type = type;
1547 type -> SetOwner(package);
1548 type -> SetSignature(control);
1549 control.ProcessBadType(type);
1550 type -> MarkBad();
1551 ReportSemError(SemanticError::TYPE_NOT_FOUND, tok,
1552 type -> ContainingPackageName(),
1553 type -> ExternalName());
1554 }
1555 }
1556 return type;
1557 }
1558
1559
GetBadNestedType(TypeSymbol * type,TokenIndex identifier_token)1560 TypeSymbol* Semantic::GetBadNestedType(TypeSymbol* type,
1561 TokenIndex identifier_token)
1562 {
1563 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
1564
1565 TypeSymbol* outermost_type = type -> outermost_type;
1566 if (! outermost_type -> non_local)
1567 outermost_type -> non_local = new SymbolSet;
1568 if (! outermost_type -> local)
1569 outermost_type -> local = new SymbolSet;
1570
1571 int length = type -> ExternalNameLength() + 1 +
1572 name_symbol -> NameLength(); // +1 for $,... +1 for $
1573 wchar_t* external_name = new wchar_t[length + 1]; // +1 for '\0';
1574 wcscpy(external_name, type -> ExternalName());
1575 wcscat(external_name, StringConstant::US_DS);
1576 wcscat(external_name, name_symbol -> Name());
1577
1578 TypeSymbol* inner_type = type -> InsertNestedTypeSymbol(name_symbol);
1579 inner_type -> MarkBad();
1580 inner_type -> outermost_type = type -> outermost_type;
1581 inner_type -> supertypes_closure = new SymbolSet;
1582 inner_type -> subtypes = new SymbolSet;
1583 inner_type -> SetExternalIdentity(control.FindOrInsertName(external_name,
1584 length));
1585 inner_type -> super = control.Object();
1586 inner_type -> SetOwner(type);
1587 if (! type -> Bad())
1588 ReportSemError(SemanticError::TYPE_NOT_FOUND, identifier_token,
1589 inner_type -> ContainingPackageName(),
1590 inner_type -> ExternalName());
1591
1592 delete [] external_name;
1593
1594 return inner_type;
1595 }
1596
1597
ProcessImportQualifiedName(AstName * name)1598 void Semantic::ProcessImportQualifiedName(AstName* name)
1599 {
1600 if (name -> base_opt)
1601 {
1602 ProcessImportQualifiedName(name -> base_opt);
1603 Symbol* symbol = name -> base_opt -> symbol;
1604
1605 TypeSymbol* type = symbol -> TypeCast();
1606 NameSymbol* name_symbol =
1607 lex_stream -> NameSymbol(name -> identifier_token);
1608 if (type) // The base name is a type
1609 {
1610 if (type -> Bad()) // Avoid chain-reaction errors.
1611 {
1612 name -> symbol = control.no_type;
1613 return;
1614 }
1615 if (! type -> expanded_type_table)
1616 ComputeTypesClosure(type, name -> identifier_token);
1617 TypeSymbol* inner_type = NULL;
1618 TypeShadowSymbol* type_shadow_symbol = type ->
1619 expanded_type_table -> FindTypeShadowSymbol(name_symbol);
1620 //
1621 // Only canonical names may be used in import statements, so we
1622 // don't worry about ambiguous names (which are necessarily
1623 // inherited and hence non-canonical). But we do need an extra
1624 // filter on the containing type being correct.
1625 //
1626 if (type_shadow_symbol)
1627 inner_type = type_shadow_symbol -> type_symbol;
1628 if (! inner_type)
1629 inner_type = control.no_type;
1630 else if (type != inner_type -> owner)
1631 {
1632 ReportSemError(SemanticError::IMPORT_NOT_CANONICAL,
1633 name, name_symbol -> Name(),
1634 inner_type -> ContainingPackageName(),
1635 inner_type -> ExternalName());
1636 }
1637 else if (inner_type -> ACC_PRIVATE() ||
1638 (! inner_type -> ACC_PUBLIC() &&
1639 inner_type -> ContainingPackage() != this_package))
1640 {
1641 ReportTypeInaccessible(name, inner_type);
1642 }
1643 name -> symbol = inner_type;
1644 }
1645 else
1646 {
1647 PackageSymbol* package = symbol -> PackageCast();
1648 type = package -> FindTypeSymbol(name_symbol);
1649 if (! type)
1650 {
1651 FileSymbol* file_symbol =
1652 Control::GetFile(control, package, name_symbol);
1653 if (file_symbol)
1654 type = ReadType(file_symbol, package, name_symbol,
1655 name -> identifier_token);
1656 }
1657 else if (type -> SourcePending())
1658 control.ProcessHeaders(type -> file_symbol);
1659
1660 //
1661 // If the field_access was resolved to a type, save it later use.
1662 // Otherwise, assume the field_access is a package name.
1663 //
1664 if (type)
1665 {
1666 if (! type -> ACC_PUBLIC() &&
1667 type -> ContainingPackage() != this_package)
1668 {
1669 ReportTypeInaccessible(name, type);
1670 }
1671 name -> symbol = type;
1672 }
1673 else
1674 {
1675 PackageSymbol* subpackage =
1676 package -> FindPackageSymbol(name_symbol);
1677 if (! subpackage)
1678 subpackage = package -> InsertPackageSymbol(name_symbol);
1679 control.FindPathsToDirectory(subpackage);
1680 name -> symbol = subpackage;
1681 }
1682 }
1683 }
1684 else // unqualified name
1685 {
1686 //
1687 // JLS 6.3 The leading simple name of a type import must be a package
1688 // name, as class names are not in scope. JLS 7.5: Nested classes of
1689 // all sorts (top-level or inner) can be imported by either kind of
1690 // import statement. Class names in import statements must be the
1691 // canonical version.
1692 //
1693 TypeSymbol* type = FindSimpleNameType(control.UnnamedPackage(),
1694 name -> identifier_token);
1695
1696 //
1697 // If the name is a type, detect the error. Otherwise, assume
1698 // it is a package, and legal.
1699 //
1700 if (type)
1701 {
1702 ReportSemError(SemanticError::IMPORT_FROM_UNNAMED_PACKAGE,
1703 name -> identifier_token,
1704 lex_stream -> NameString(name -> identifier_token));
1705 name -> symbol = control.no_type;
1706 }
1707 else
1708 {
1709 NameSymbol* name_symbol =
1710 lex_stream -> NameSymbol(name -> identifier_token);
1711 PackageSymbol* package =
1712 control.external_table.FindPackageSymbol(name_symbol);
1713 if (! package)
1714 package = control.external_table.
1715 InsertPackageSymbol(name_symbol, NULL);
1716 control.FindPathsToDirectory(package);
1717 name -> symbol = package;
1718 }
1719 }
1720 }
1721
1722
1723 //
1724 // Processes a package-or-type name. If an accessible type exists, it is
1725 // chosen. Next, if a package exists, it is chosen. Then, an error is issued,
1726 // but a check for an inaccessible type is made before inventing a package.
1727 // The result is stored in name->symbol.
1728 //
ProcessPackageOrType(AstName * name)1729 void Semantic::ProcessPackageOrType(AstName* name)
1730 {
1731 if (name -> base_opt)
1732 {
1733 ProcessPackageOrType(name -> base_opt);
1734 Symbol* symbol = name -> base_opt -> symbol;
1735
1736 TypeSymbol* type = symbol -> TypeCast();
1737 if (type) // The base name is a type
1738 name -> symbol = MustFindNestedType(type, name);
1739 else
1740 {
1741 // Base name is package. Search for type, then subpackage.
1742 PackageSymbol* package = symbol -> PackageCast();
1743 NameSymbol* name_symbol =
1744 lex_stream -> NameSymbol(name -> identifier_token);
1745 type = package -> FindTypeSymbol(name_symbol);
1746 if (! type)
1747 {
1748 FileSymbol* file_symbol =
1749 Control::GetFile(control, package, name_symbol);
1750 if (file_symbol)
1751 type = ReadType(file_symbol, package, name_symbol,
1752 name -> identifier_token);
1753 }
1754 else if (type -> SourcePending())
1755 control.ProcessHeaders(type -> file_symbol);
1756
1757 //
1758 // If the field access was resolved into a type, then save it.
1759 // Otherwise, assume it is a package
1760 //
1761 if (type)
1762 // save the resolved type of this expression for later use...
1763 name -> symbol = type;
1764 else
1765 {
1766 NameSymbol* name_symbol =
1767 lex_stream -> NameSymbol(name -> identifier_token);
1768 PackageSymbol* subpackage =
1769 package -> FindPackageSymbol(name_symbol);
1770 if (! subpackage)
1771 subpackage = package -> InsertPackageSymbol(name_symbol);
1772 control.FindPathsToDirectory(subpackage);
1773 name -> symbol = subpackage;
1774 if (subpackage -> directory.Length() == 0)
1775 {
1776 ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
1777 name -> identifier_token,
1778 subpackage -> PackageName());
1779 }
1780 }
1781 }
1782 }
1783 else // unqualified name
1784 {
1785 TypeSymbol* type = FindType(name -> identifier_token);
1786 if (type)
1787 name -> symbol = type;
1788 else
1789 {
1790 NameSymbol* name_symbol =
1791 lex_stream -> NameSymbol(name -> identifier_token);
1792 PackageSymbol* package =
1793 control.external_table.FindPackageSymbol(name_symbol);
1794 if (! package)
1795 package = control.external_table.
1796 InsertPackageSymbol(name_symbol, NULL);
1797 control.FindPathsToDirectory(package);
1798 if (package -> directory.Length() == 0)
1799 {
1800 //
1801 // If there is no package, see if the user is trying to access
1802 // an inaccessible nested type before giving up.
1803 //
1804 if (state_stack.Size())
1805 {
1806 NameSymbol* name_symbol = lex_stream ->
1807 NameSymbol(name -> identifier_token);
1808 for (TypeSymbol* super_type = ThisType() -> super;
1809 super_type; super_type = super_type -> super)
1810 {
1811 assert(super_type -> expanded_type_table);
1812
1813 TypeShadowSymbol* type_shadow_symbol =
1814 super_type -> expanded_type_table ->
1815 FindTypeShadowSymbol(name_symbol);
1816 if (type_shadow_symbol)
1817 {
1818 type = type_shadow_symbol -> type_symbol;
1819 break;
1820 }
1821 }
1822 }
1823 if (type)
1824 {
1825 ReportTypeInaccessible(name, type);
1826 name -> symbol = type;
1827 }
1828 else
1829 {
1830 ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
1831 name -> identifier_token,
1832 package -> PackageName());
1833 name -> symbol = package;
1834 }
1835 }
1836 else name -> symbol = package;
1837 }
1838 }
1839 }
1840
1841
ProcessTypeImportOnDemandDeclaration(AstImportDeclaration * import_declaration)1842 void Semantic::ProcessTypeImportOnDemandDeclaration(AstImportDeclaration* import_declaration)
1843 {
1844 ProcessImportQualifiedName(import_declaration -> name);
1845 Symbol* symbol = import_declaration -> name -> symbol;
1846
1847 PackageSymbol* package = symbol -> PackageCast();
1848 if (package && package -> directory.Length() == 0)
1849 {
1850 ReportSemError(SemanticError::PACKAGE_NOT_FOUND,
1851 import_declaration -> name,
1852 package -> PackageName());
1853 }
1854
1855 //
1856 // Two or more type-import-on-demand may name the same package; the effect
1857 // is as if there were only one such declaration. Likewise, importing the
1858 // current package or java.lang.* is ok, although useless.
1859 // TODO: In pedantic mode, warn about duplicate imports of the same
1860 // package, of the current package, or of java.lang.*.
1861 //
1862 if (symbol == this_package)
1863 return;
1864 for (unsigned i = 0; i < import_on_demand_packages.Length(); i++)
1865 {
1866 if (symbol == import_on_demand_packages[i])
1867 return;
1868 }
1869
1870 import_on_demand_packages.Next() = symbol;
1871
1872 TypeSymbol* type = symbol -> TypeCast();
1873 if (control.option.deprecation && type &&
1874 type -> IsDeprecated() && type -> file_symbol != source_file_symbol)
1875 {
1876 ReportSemError(SemanticError::DEPRECATED_TYPE,
1877 import_declaration -> name,
1878 type -> ContainingPackageName(),
1879 type -> ExternalName());
1880 }
1881 }
1882
1883
FindSimpleNameType(PackageSymbol * package,TokenIndex identifier_token)1884 TypeSymbol* Semantic::FindSimpleNameType(PackageSymbol* package,
1885 TokenIndex identifier_token)
1886 {
1887 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
1888 TypeSymbol* type = package -> FindTypeSymbol(name_symbol);
1889 if (type)
1890 {
1891 if (type -> SourcePending())
1892 control.ProcessHeaders(type -> file_symbol);
1893 }
1894 else
1895 {
1896 //
1897 // Check whether or not the type was declared in another compilation
1898 // unit in the main package.
1899 //
1900 FileSymbol* file_symbol = Control::GetFile(control, package,
1901 name_symbol);
1902 if (file_symbol)
1903 type = ReadType(file_symbol, package, name_symbol,
1904 identifier_token);
1905 }
1906
1907 return type;
1908 }
1909
ProcessSingleTypeImportDeclaration(AstImportDeclaration * import_declaration)1910 void Semantic::ProcessSingleTypeImportDeclaration(AstImportDeclaration* import_declaration)
1911 {
1912 ProcessImportQualifiedName(import_declaration -> name);
1913 Symbol* symbol = import_declaration -> name -> symbol;
1914 PackageSymbol* package = symbol -> PackageCast();
1915 TypeSymbol* type = symbol -> TypeCast();
1916
1917 //
1918 // Technically, the JLS grammar forbids "import foo;". However, our
1919 // grammar parses it, and will either find or create the package foo, so
1920 // we can give a better message than "expected '.'". If a non-type is
1921 // imported, we create a place-holder type so that the use of the
1922 // unqualified type name won't cause cascading errors elsewhere.
1923 //
1924 if (package)
1925 {
1926 ReportSemError(SemanticError::UNKNOWN_ON_DEMAND_IMPORT,
1927 import_declaration -> name,
1928 package -> PackageName());
1929 NameSymbol* name_symbol = lex_stream ->
1930 NameSymbol(import_declaration -> name -> RightToken());
1931 type = package -> InsertOuterTypeSymbol(name_symbol);
1932 type -> MarkBad();
1933 type -> super = control.no_type;
1934 type -> outermost_type = control.no_type;
1935 }
1936
1937 //
1938 // If two single-type-import declarations in the same compilation unit
1939 // attempt to import types with the same simple name, then a compile-time
1940 // error occurs, unless the two types are the same type, in which case the
1941 // duplicate declaration is ignored.
1942 // TODO: Give pedantic warnings about duplicate type declarations.
1943 //
1944 for (unsigned i = 0; i < single_type_imports.Length(); i++)
1945 {
1946 if (type == single_type_imports[i])
1947 return;
1948 }
1949
1950 TypeSymbol* old_type = NULL;
1951 unsigned k;
1952 for (k = 0; k < compilation_unit -> NumTypeDeclarations(); k++)
1953 {
1954 AstDeclaredType* declaration = compilation_unit -> TypeDeclaration(k);
1955 if (declaration -> class_body &&
1956 declaration -> class_body -> semantic_environment)
1957 {
1958 old_type =
1959 declaration -> class_body -> semantic_environment -> Type();
1960 if (old_type -> Identity() == type -> Identity())
1961 break;
1962 }
1963 }
1964
1965 if (k < compilation_unit -> NumTypeDeclarations())
1966 {
1967 AstName* name = import_declaration -> name;
1968 package = name -> base_opt
1969 ? name -> base_opt -> symbol -> PackageCast()
1970 : control.UnnamedPackage();
1971
1972 //
1973 // It's ok to import a type that is being compiled...
1974 //
1975 if (type == old_type && package == this_package)
1976 {
1977 ReportSemError(SemanticError::UNNECESSARY_TYPE_IMPORT, name,
1978 lex_stream -> NameString(name -> identifier_token),
1979 old_type -> FileLoc());
1980 }
1981 else
1982 {
1983 ReportSemError(SemanticError::DUPLICATE_IMPORT_NAME, name,
1984 lex_stream -> NameString(name -> identifier_token),
1985 old_type -> FileLoc());
1986 }
1987 }
1988 else
1989 {
1990 unsigned i = 0;
1991 for (i = 0; i < compilation_unit -> NumImportDeclarations(); i++)
1992 {
1993 TypeSymbol* other_type =
1994 compilation_unit -> ImportDeclaration(i) -> name -> Type();
1995 if (compilation_unit -> ImportDeclaration(i) == import_declaration ||
1996 (other_type && other_type -> Identity() == type -> Identity()))
1997 {
1998 break;
1999 }
2000 }
2001
2002 assert(i < compilation_unit -> NumImportDeclarations());
2003
2004 if (compilation_unit -> ImportDeclaration(i) == import_declaration)
2005 {
2006 // No duplicate found
2007 import_declaration -> name -> symbol = type;
2008 single_type_imports.Next() = type;
2009 }
2010 else
2011 {
2012 AstName* name = compilation_unit -> ImportDeclaration(i) -> name;
2013 FileLocation file_location(lex_stream, name -> identifier_token);
2014 ReportSemError(SemanticError::DUPLICATE_IMPORT_NAME, name,
2015 lex_stream -> NameString(name -> identifier_token),
2016 file_location.location);
2017 }
2018 }
2019
2020 if (control.option.deprecation && type -> IsDeprecated() &&
2021 type -> file_symbol != source_file_symbol)
2022 {
2023 ReportSemError(SemanticError::DEPRECATED_TYPE,
2024 import_declaration -> name,
2025 type -> ContainingPackageName(),
2026 type -> ExternalName());
2027 }
2028 }
2029
2030
ProcessFieldDeclaration(AstFieldDeclaration * field_declaration)2031 void Semantic::ProcessFieldDeclaration(AstFieldDeclaration* field_declaration)
2032 {
2033 TypeSymbol* this_type = ThisType();
2034 AccessFlags access_flags = this_type -> ACC_INTERFACE()
2035 ? ProcessInterfaceFieldModifiers(field_declaration)
2036 : ProcessFieldModifiers(field_declaration);
2037
2038 //
2039 // JLS2 8.1.2 - Inner classes may not have static fields unless they are
2040 // final and initialized by a constant. Hence, the type of the static
2041 // field may only be a primitive or String. Here, we check that the
2042 // entire declaration is final, then that each variableDeclarator is
2043 // of the right type and is initialized. Later, when processing the
2044 // initializer, we check that it is indeed a compile-time constant
2045 // (see init.cpp, Semantic::ProcessVariableInitializer)
2046 //
2047 bool must_be_constant = false;
2048 if (this_type -> IsInner() && access_flags.ACC_STATIC())
2049 {
2050 if (access_flags.ACC_FINAL())
2051 must_be_constant = true;
2052 else
2053 {
2054 assert(field_declaration -> modifiers_opt &&
2055 field_declaration -> modifiers_opt -> static_token_opt);
2056 ReportSemError(SemanticError::STATIC_FIELD_IN_INNER_CLASS_NOT_FINAL,
2057 field_declaration -> modifiers_opt -> static_token_opt,
2058 this_type -> Name(),
2059 this_type -> FileLoc());
2060 }
2061 }
2062
2063 //
2064 // To avoid deprecated type warnings when processing a deprecated field
2065 // declaration, we must temporarily mark this type as deprecated, because
2066 // the field variable symbol(s) do not yet exist.
2067 //
2068 bool deprecated_declarations =
2069 lex_stream -> IsDeprecated(field_declaration -> LeftToken());
2070 bool deprecated_type = this_type -> IsDeprecated();
2071 if (deprecated_declarations)
2072 this_type -> MarkDeprecated();
2073 ProcessType(field_declaration -> type);
2074 TypeSymbol* field_type = field_declaration -> type -> symbol;
2075 if (! deprecated_type && deprecated_declarations)
2076 this_type -> ResetDeprecated();
2077
2078 for (unsigned i = 0;
2079 i < field_declaration -> NumVariableDeclarators(); i++)
2080 {
2081 AstVariableDeclarator* variable_declarator =
2082 field_declaration -> VariableDeclarator(i);
2083 AstVariableDeclaratorId* name =
2084 variable_declarator -> variable_declarator_name;
2085 NameSymbol* name_symbol =
2086 lex_stream -> NameSymbol(name -> identifier_token);
2087
2088 if (this_type -> FindVariableSymbol(name_symbol))
2089 {
2090 ReportSemError(SemanticError::DUPLICATE_FIELD,
2091 name -> identifier_token, name_symbol -> Name(),
2092 this_type -> Name(),
2093 this_type -> FindVariableSymbol(name_symbol) -> FileLoc());
2094 }
2095 else
2096 {
2097 if (name_symbol != control.serialVersionUID_name_symbol)
2098 {
2099 WarnOfAccessibleFieldWithName(SemanticError::HIDDEN_FIELD,
2100 name, name_symbol, access_flags.ACC_STATIC());
2101 }
2102
2103 VariableSymbol* variable =
2104 this_type -> InsertVariableSymbol(name_symbol);
2105 unsigned dims =
2106 field_type -> num_dimensions + name -> NumBrackets();
2107 variable -> SetType(field_type -> GetArrayType(this, dims));
2108 variable -> SetFlags(access_flags);
2109 variable -> SetOwner(this_type);
2110 variable -> declarator = variable_declarator;
2111 if (must_be_constant &&
2112 (dims || ! variable_declarator -> variable_initializer_opt ||
2113 (! field_type -> Primitive() &&
2114 field_type != control.String())))
2115 {
2116 ReportSemError(SemanticError::STATIC_FIELD_IN_INNER_CLASS_NOT_CONSTANT,
2117 name -> identifier_token, name_symbol -> Name(),
2118 this_type -> Name(), this_type -> FileLoc());
2119 }
2120 variable_declarator -> symbol = variable;
2121 variable -> SetLocation();
2122 if (deprecated_declarations)
2123 variable -> MarkDeprecated();
2124 }
2125
2126 CheckFieldDeclaration(field_declaration, name, access_flags);
2127 }
2128 }
2129
2130
CheckFieldDeclaration(AstFieldDeclaration * field_declaration,AstVariableDeclaratorId * name,const AccessFlags & access_flags)2131 void Semantic::CheckFieldDeclaration(AstFieldDeclaration* field_declaration,
2132 AstVariableDeclaratorId* name,
2133 const AccessFlags& access_flags)
2134 {
2135 TypeSymbol* field_type = field_declaration -> type -> symbol;
2136 NameSymbol* name_symbol = lex_stream ->
2137 NameSymbol(name -> identifier_token);
2138
2139 //
2140 // Warn against public static final array fields.
2141 //
2142 bool is_constant_field = (access_flags.ACC_FINAL() &&
2143 access_flags.ACC_STATIC());
2144 if (access_flags.ACC_PUBLIC() &&
2145 is_constant_field &&
2146 field_type -> IsArray())
2147 {
2148 // FIXME: shouldn't warn if it's a zero-length array.
2149 ReportSemError(SemanticError::EJ_PUBLIC_STATIC_FINAL_ARRAY_FIELD,
2150 name, name_symbol -> Name());
2151 }
2152
2153 if (name_symbol == control.serialVersionUID_name_symbol)
2154 {
2155 //
2156 // Warn about serialVersionUID mistakes.
2157 //
2158 TypeSymbol* this_type = ThisType();
2159 bool is_serializable = this_type -> Implements(control.Serializable());
2160 if (! is_serializable)
2161 {
2162 ReportSemError(SemanticError::UNNEEDED_SERIAL_VERSION_UID,
2163 name);
2164 }
2165 else if (field_type != control.long_type ||
2166 is_constant_field == false ||
2167 access_flags.ACC_PUBLIC() ||
2168 access_flags.ACC_PROTECTED())
2169 {
2170 ReportSemError(SemanticError::BAD_SERIAL_VERSION_UID,
2171 field_declaration);
2172 }
2173 }
2174 else if (name_symbol == control.serialPersistentFields_name_symbol)
2175 {
2176 //
2177 // FIXME: Warn about serialPersistentFields mistakes; has anyone
2178 // ever seen this used in the wild?
2179 //
2180 }
2181 else
2182 {
2183 //
2184 // Warn against unconventional field names. This doesn't apply to
2185 // the serialization fields above, because their names are mandated
2186 // names that break conventions, and aren't likely to be fixed.
2187 //
2188 CheckFieldName(name, name_symbol, is_constant_field);
2189 }
2190 }
2191
2192
CheckFieldName(AstVariableDeclaratorId * name,NameSymbol * name_symbol,bool is_constant_field)2193 void Semantic::CheckFieldName(AstVariableDeclaratorId* name,
2194 NameSymbol* name_symbol,
2195 bool is_constant_field)
2196 {
2197 if (is_constant_field &&
2198 name_symbol -> IsBadStyleForConstantField())
2199 {
2200 ReportSemError(SemanticError::UNCONVENTIONAL_CONSTANT_FIELD_NAME,
2201 name -> identifier_token, name_symbol -> Name());
2202 }
2203 else if (! is_constant_field &&
2204 name_symbol -> IsBadStyleForField())
2205 {
2206 ReportSemError(SemanticError::UNCONVENTIONAL_FIELD_NAME,
2207 name -> identifier_token, name_symbol -> Name());
2208 }
2209 }
2210
2211
ProcessConstructorDeclaration(AstConstructorDeclaration * constructor_declaration)2212 void Semantic::ProcessConstructorDeclaration(AstConstructorDeclaration* constructor_declaration)
2213 {
2214 TypeSymbol* this_type = ThisType();
2215
2216 AccessFlags access_flags =
2217 ProcessConstructorModifiers(constructor_declaration);
2218 if (this_type -> ACC_STRICTFP())
2219 access_flags.SetACC_STRICTFP();
2220
2221 if (constructor_declaration -> type_parameters_opt)
2222 {
2223 // TODO: Add generics support for 1.5.
2224 ReportSemError(SemanticError::TYPE_PARAMETERS_UNSUPPORTED,
2225 constructor_declaration -> type_parameters_opt);
2226 }
2227
2228 AstMethodDeclarator* constructor_declarator =
2229 constructor_declaration -> constructor_declarator;
2230 const wchar_t* constructor_name =
2231 lex_stream -> NameString(constructor_declarator -> identifier_token);
2232
2233 //
2234 // A bad name indicates either a misspelling, or a method missing
2235 // a return type. In an anonymous class, assume a missing return
2236 // type. In all other classes, if the probability of misspelling
2237 // >= 50%, correct the name, otherwise treat it as a method with
2238 // bad return type.
2239 //
2240 bool treat_as_method = false;
2241 if (this_type -> Anonymous())
2242 {
2243 ReportSemError(SemanticError::CONSTRUCTOR_FOUND_IN_ANONYMOUS_CLASS,
2244 constructor_declarator, constructor_name);
2245 treat_as_method = true;
2246 }
2247 else if (lex_stream -> NameSymbol(constructor_declarator -> identifier_token) != this_type -> Identity())
2248 {
2249 if (Spell::Index(constructor_name, this_type -> Name()) >= 5)
2250 {
2251 ReportSemError(SemanticError::MISSPELLED_CONSTRUCTOR_NAME,
2252 constructor_declarator -> identifier_token,
2253 constructor_name, this_type -> Name());
2254 constructor_name = this_type -> Name(); // correct the name
2255 }
2256 else
2257 {
2258 ReportSemError(SemanticError::MISMATCHED_CONSTRUCTOR_NAME,
2259 constructor_declarator -> identifier_token,
2260 constructor_name, this_type -> Name());
2261 treat_as_method = true;
2262 }
2263 }
2264
2265 //
2266 // As the body of the constructor may not have been parsed yet, we estimate
2267 // a size for its symbol table based on the number of lines in the body + a
2268 // margin for one-liners.
2269 //
2270 BlockSymbol* block_symbol =
2271 new BlockSymbol(constructor_declarator -> NumFormalParameters() + 3);
2272 // All types need a spot for "this".
2273 block_symbol -> max_variable_index = 1;
2274
2275 ProcessFormalParameters(block_symbol, constructor_declarator);
2276
2277 //
2278 // Note that constructors are always named "<init>", but if this is a
2279 // method with missing return type, we use the method name.
2280 //
2281 NameSymbol* name_symbol = treat_as_method
2282 ? lex_stream -> NameSymbol(constructor_declarator -> identifier_token)
2283 : control.init_name_symbol;
2284 MethodSymbol* constructor = this_type -> FindMethodSymbol(name_symbol);
2285 if (constructor && this_type -> FindOverloadMethod(constructor,
2286 constructor_declarator))
2287 {
2288 ReportSemError(SemanticError::DUPLICATE_CONSTRUCTOR,
2289 constructor_declarator, this_type -> Name(),
2290 constructor -> FileLoc());
2291 delete block_symbol;
2292 return;
2293 }
2294
2295 constructor = this_type -> InsertMethodSymbol(name_symbol);
2296 TypeSymbol* ctor_type = this_type;
2297 if (treat_as_method)
2298 ctor_type = control.no_type;
2299 constructor -> SetType(ctor_type);
2300 constructor -> SetFlags(access_flags);
2301 constructor -> SetContainingType(this_type);
2302 constructor -> SetBlockSymbol(block_symbol);
2303 constructor -> declaration = constructor_declaration;
2304 constructor -> SetLocation();
2305 if (this_type -> EnclosingType())
2306 {
2307 VariableSymbol* this0_variable =
2308 block_symbol -> InsertVariableSymbol(control.this_name_symbol);
2309 this0_variable -> SetType(this_type -> ContainingType());
2310 this0_variable -> SetOwner(constructor);
2311 this0_variable -> SetLocalVariableIndex(block_symbol ->
2312 max_variable_index++);
2313 this0_variable -> MarkComplete();
2314 this0_variable -> SetACC_SYNTHETIC();
2315 }
2316
2317 for (unsigned i = 0;
2318 i < constructor_declarator -> NumFormalParameters(); i++)
2319 {
2320 AstVariableDeclarator* formal_declarator =
2321 constructor_declarator -> FormalParameter(i) -> formal_declarator;
2322 VariableSymbol* symbol = formal_declarator -> symbol;
2323
2324 symbol -> SetOwner(constructor);
2325 symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
2326 symbol -> MarkComplete();
2327 if (control.IsDoubleWordType(symbol -> Type()))
2328 block_symbol -> max_variable_index++;
2329 symbol -> declarator = formal_declarator;
2330 symbol -> SetLocation();
2331 constructor -> AddFormalParameter(symbol);
2332 }
2333 constructor -> SetSignature(control);
2334
2335 for (unsigned k = 0; k < constructor_declaration -> NumThrows(); k++)
2336 {
2337 AstTypeName* throw_expr = constructor_declaration -> Throw(k);
2338 ProcessType(throw_expr);
2339 constructor -> AddThrows(throw_expr -> symbol);
2340 }
2341
2342 // save for processing bodies later.
2343 constructor_declaration -> constructor_symbol = constructor;
2344
2345 if (lex_stream -> IsDeprecated(constructor_declaration -> LeftToken()))
2346 constructor -> MarkDeprecated();
2347 }
2348
2349
AddDefaultConstructor(TypeSymbol * type)2350 void Semantic::AddDefaultConstructor(TypeSymbol* type)
2351 {
2352 assert(! type -> ACC_INTERFACE());
2353 MethodSymbol* constructor =
2354 type -> InsertMethodSymbol(control.init_name_symbol);
2355
2356 BlockSymbol* block_symbol = new BlockSymbol(1);
2357 block_symbol -> max_variable_index = 1; // All types need a spot for "this"
2358
2359 constructor -> SetType(type);
2360 constructor -> SetContainingType(type);
2361 constructor -> SetBlockSymbol(block_symbol);
2362 if (type -> ACC_PUBLIC())
2363 constructor -> SetACC_PUBLIC();
2364 else if (type -> ACC_PROTECTED())
2365 constructor -> SetACC_PROTECTED();
2366 else if (type -> ACC_PRIVATE())
2367 constructor -> SetACC_PRIVATE();
2368
2369 if (type -> ACC_STRICTFP())
2370 constructor -> SetACC_STRICTFP();
2371
2372 if (type -> EnclosingType())
2373 {
2374 VariableSymbol* this0_variable =
2375 block_symbol -> InsertVariableSymbol(control.this_name_symbol);
2376 this0_variable -> SetType(type -> ContainingType());
2377 this0_variable -> SetOwner(constructor);
2378 this0_variable -> SetLocalVariableIndex(block_symbol ->
2379 max_variable_index++);
2380 this0_variable -> MarkComplete();
2381 this0_variable -> SetACC_SYNTHETIC();
2382 }
2383 constructor -> SetSignature(control);
2384
2385 AstClassBody* class_body = type -> declaration;
2386 if (class_body)
2387 {
2388 TokenIndex left_loc = class_body -> identifier_token;
2389 TokenIndex right_loc = class_body -> left_brace_token - 1;
2390
2391 AstMethodDeclarator* method_declarator =
2392 compilation_unit -> ast_pool -> GenMethodDeclarator();
2393 method_declarator -> identifier_token = left_loc;
2394 method_declarator -> left_parenthesis_token = left_loc;
2395 method_declarator -> right_parenthesis_token = right_loc;
2396
2397 AstSuperCall* super_call = NULL;
2398 if (type != control.Object())
2399 {
2400 super_call = compilation_unit -> ast_pool -> GenSuperCall();
2401 super_call -> super_token = left_loc;
2402 super_call -> arguments = compilation_unit -> ast_pool ->
2403 GenArguments(left_loc, right_loc);
2404 super_call -> semicolon_token = right_loc;
2405 }
2406
2407 AstReturnStatement* return_statement =
2408 compilation_unit -> ast_pool -> GenReturnStatement();
2409 return_statement -> return_token = left_loc;
2410 return_statement -> semicolon_token = left_loc;
2411 return_statement -> is_reachable = true;
2412
2413 AstMethodBody* constructor_block =
2414 compilation_unit -> ast_pool -> GenMethodBody();
2415 // This symbol table will be empty.
2416 constructor_block -> block_symbol = new BlockSymbol(0);
2417 constructor_block -> left_brace_token = left_loc;
2418 constructor_block -> right_brace_token = right_loc;
2419 constructor_block -> AllocateStatements(1);
2420 constructor_block -> AddStatement(return_statement);
2421 constructor_block -> explicit_constructor_opt = super_call;
2422
2423 AstConstructorDeclaration* constructor_declaration =
2424 compilation_unit -> ast_pool -> GenConstructorDeclaration();
2425 constructor_declaration -> constructor_declarator = method_declarator;
2426 constructor_declaration -> constructor_body = constructor_block;
2427
2428 constructor_declaration -> constructor_symbol = constructor;
2429 constructor -> declaration = constructor_declaration;
2430 class_body -> default_constructor = constructor_declaration;
2431 }
2432 }
2433
2434
2435 //
2436 // This is called by AddInheritedMethods in two conditions: First, method
2437 // belongs to base_type, so it must successfully override hidden_method,
2438 // including a compatible throws clause. Second, method belongs to a supertype
2439 // of base_type, in which case base_type is inheriting two declarations, one
2440 // of which is necessarily abstract. If one is non-abstract, it must
2441 // successfully override the abstract version, including the throws clause;
2442 // but if both are abstract the throws clauses are inconsequential. It is
2443 // possible that both method and hidden_method were declared in the same
2444 // class, and inherited through two paths, in which case we do nothing.
2445 // See JLS 8.4.6.4, and 9.4.1.
2446 //
CheckMethodOverride(MethodSymbol * method,MethodSymbol * hidden_method,TypeSymbol * base_type)2447 void Semantic::CheckMethodOverride(MethodSymbol* method,
2448 MethodSymbol* hidden_method,
2449 TypeSymbol* base_type)
2450 {
2451 assert(! hidden_method -> ACC_PRIVATE());
2452
2453 //
2454 // If we inherit the same method from multiple paths (including methods
2455 // of Object via interfaces), we already know the result.
2456 //
2457 if (hidden_method == method ||
2458 (method -> containing_type -> ACC_INTERFACE() &&
2459 method -> containing_type != base_type &&
2460 hidden_method -> containing_type == control.Object()))
2461 {
2462 return;
2463 }
2464
2465 TokenIndex left_tok;
2466 TokenIndex right_tok;
2467
2468 if (method -> containing_type == base_type && ThisType() == base_type)
2469 {
2470 AstMethodDeclaration* method_declaration =
2471 (AstMethodDeclaration*) method -> declaration;
2472 assert(method_declaration);
2473 AstMethodDeclarator* method_declarator =
2474 method_declaration -> method_declarator;
2475
2476 left_tok = method_declarator -> LeftToken();
2477 right_tok = method_declarator -> RightToken();
2478 }
2479 else
2480 {
2481 left_tok = ThisType() -> declaration -> identifier_token;
2482 right_tok = ThisType() -> declaration -> left_brace_token - 1;
2483 }
2484
2485 //
2486 // Return types must match.
2487 //
2488 if (hidden_method -> Type() != method -> Type())
2489 {
2490 //
2491 // We support covariant return types when loading from .class files,
2492 // even though this is not strictly legal in JLS2 (according to binary
2493 // compatibility, changing the return type need not be supported).
2494 // This is done in anticipation of JDK 1.5, when covariance is likely
2495 // to be introduced. The resultant effect is that the subclass must
2496 // conform to the narrower return type. Note that we currently only
2497 // support Object->Object covariance (and not primitive->primitive,
2498 // void->primitive, or void->Object). When loading from .java files,
2499 // however, we enforce exact return type matching.
2500 //
2501 if (hidden_method -> Type() -> IsSubtype(control.Object()) &&
2502 method -> Type() -> IsSubtype(hidden_method -> Type()))
2503 {
2504 // Silent acceptance for .class files only.
2505 // They must work, because the 1.5 library is covariant,
2506 // even for -source 1.4!
2507 if (control.option.source < JikesOption::SDK1_5 &&
2508 ! hidden_method -> containing_type ->
2509 file_symbol -> IsClassOnly())
2510 {
2511 ReportSemError(SemanticError::COVARIANCE_UNSUPPORTED,
2512 left_tok, right_tok, method -> Header(),
2513 hidden_method -> Header());
2514 }
2515 }
2516 else if (method -> containing_type == base_type)
2517 {
2518 if (base_type -> ACC_INTERFACE() &&
2519 hidden_method -> containing_type == control.Object())
2520 {
2521 //
2522 // TODO: Review this when JLS3 is published. See Sun bug
2523 // 4479715, which explains our current stance of allowing
2524 // int clone() throws MyException; or Object finalize();.
2525 //
2526 if (hidden_method -> ACC_PUBLIC())
2527 {
2528 ReportSemError(SemanticError::MISMATCHED_IMPLICIT_METHOD,
2529 left_tok, right_tok, method -> Header(),
2530 hidden_method -> Header());
2531 base_type -> MarkBad();
2532 }
2533 else
2534 {
2535 ReportSemError(SemanticError::UNIMPLEMENTABLE_INTERFACE,
2536 left_tok, right_tok,
2537 base_type -> ContainingPackageName(),
2538 base_type -> ExternalName(),
2539 method -> Header(),
2540 hidden_method -> Header());
2541 }
2542 }
2543 else
2544 {
2545 ReportSemError(SemanticError::MISMATCHED_INHERITED_METHOD,
2546 left_tok, right_tok, method -> Header(),
2547 hidden_method -> Header(),
2548 hidden_method -> containing_type -> ContainingPackageName(),
2549 hidden_method -> containing_type -> ExternalName());
2550 base_type -> MarkBad();
2551 }
2552 }
2553 else
2554 {
2555 ReportSemError(SemanticError::MISMATCHED_INHERITED_METHOD_EXTERNALLY,
2556 left_tok, right_tok, base_type -> ExternalName(),
2557 method -> Header(),
2558 method -> containing_type -> ContainingPackageName(),
2559 method -> containing_type -> ExternalName(),
2560 hidden_method -> Header(),
2561 hidden_method -> containing_type -> ContainingPackageName(),
2562 hidden_method -> containing_type -> ExternalName());
2563 base_type -> MarkBad();
2564 }
2565 }
2566
2567
2568 //
2569 // If base_type declared method, hidden_method must not be final. On the
2570 // other hand, if a type inherits a final method from a superclass and
2571 // an abstract method from an interface, it is legal.
2572 //
2573 if (method -> containing_type == base_type &&
2574 (hidden_method -> ACC_FINAL() ||
2575 hidden_method -> containing_type -> ACC_FINAL()))
2576 {
2577 if (base_type -> ACC_INTERFACE())
2578 {
2579 ReportSemError(SemanticError::FINAL_IMPLICIT_METHOD_OVERRIDE,
2580 left_tok, right_tok, method -> Header(),
2581 hidden_method -> Header());
2582 }
2583 else
2584 {
2585 ReportSemError(SemanticError::FINAL_METHOD_OVERRIDE,
2586 left_tok, right_tok, method -> Header(),
2587 hidden_method -> Header(),
2588 hidden_method -> containing_type -> ContainingPackageName(),
2589 hidden_method -> containing_type -> ExternalName());
2590 }
2591 base_type -> MarkBad();
2592 }
2593
2594 //
2595 // Warn if a method we have source for is overriding a deprecated method.
2596 //
2597 if (control.option.deprecation &&
2598 hidden_method -> IsDeprecated() &&
2599 ! method -> containing_type -> file_symbol -> IsClassOnly())
2600 {
2601 ReportSemError(SemanticError::DEPRECATED_METHOD_OVERRIDE,
2602 left_tok, right_tok, method -> Header(),
2603 hidden_method -> containing_type -> ContainingPackageName(),
2604 hidden_method -> containing_type -> ExternalName());
2605 }
2606
2607 //
2608 // Both or neither versions must be static.
2609 //
2610 if (method -> ACC_STATIC() != hidden_method -> ACC_STATIC())
2611 {
2612 if (method -> containing_type == base_type)
2613 {
2614 ReportSemError((method -> ACC_STATIC()
2615 ? SemanticError::INSTANCE_METHOD_OVERRIDE
2616 : SemanticError::CLASS_METHOD_OVERRIDE),
2617 left_tok, right_tok, method -> Header(),
2618 hidden_method -> Header(),
2619 hidden_method -> containing_type -> ContainingPackageName(),
2620 hidden_method -> containing_type -> ExternalName());
2621 }
2622 else
2623 {
2624 assert(method -> ACC_STATIC());
2625 ReportSemError(SemanticError::INSTANCE_METHOD_OVERRIDE_EXTERNALLY,
2626 left_tok, right_tok, base_type -> ExternalName(),
2627 method -> Header(),
2628 method -> containing_type -> ContainingPackageName(),
2629 method -> containing_type -> ExternalName(),
2630 hidden_method -> Header(),
2631 hidden_method -> containing_type -> ContainingPackageName(),
2632 hidden_method -> containing_type -> ExternalName());
2633 }
2634 base_type -> MarkBad();
2635 }
2636
2637 //
2638 // An overriding method cannot be less accessible. On the other hand, it
2639 // is legal to inherit two abstract methods when one is not public.
2640 //
2641 if (hidden_method -> ACC_PUBLIC())
2642 {
2643 if (! method -> ACC_PUBLIC())
2644 {
2645 if (method -> containing_type == base_type)
2646 {
2647 ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
2648 left_tok, right_tok, method -> Header(),
2649 method -> AccessString(),
2650 hidden_method -> Header(),
2651 StringConstant::US_public,
2652 hidden_method -> containing_type -> ContainingPackageName(),
2653 hidden_method -> containing_type -> ExternalName());
2654 base_type -> MarkBad();
2655 }
2656 else if (! method -> ACC_ABSTRACT())
2657 {
2658 ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE_EXTERNALLY,
2659 left_tok, right_tok,
2660 base_type -> ExternalName(),
2661 method -> Header(),
2662 method -> AccessString(),
2663 method -> containing_type -> ContainingPackageName(),
2664 method -> containing_type -> ExternalName(),
2665 hidden_method -> Header(),
2666 StringConstant::US_public,
2667 hidden_method -> containing_type -> ContainingPackageName(),
2668 hidden_method -> containing_type -> ExternalName());
2669 base_type -> MarkBad();
2670 }
2671 }
2672 }
2673 else if (hidden_method -> ACC_PROTECTED())
2674 {
2675 if (! method -> ACC_PROTECTED() &&
2676 ! method -> ACC_PUBLIC())
2677 {
2678 ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
2679 left_tok, right_tok, method -> Header(),
2680 method -> AccessString(),
2681 hidden_method -> Header(),
2682 StringConstant::US_protected,
2683 hidden_method -> containing_type -> ContainingPackageName(),
2684 hidden_method -> containing_type -> ExternalName());
2685 base_type -> MarkBad();
2686 }
2687 }
2688 else if (method -> ACC_PRIVATE())
2689 {
2690 ReportSemError(SemanticError::BAD_ACCESS_METHOD_OVERRIDE,
2691 left_tok, right_tok, method -> Header(),
2692 StringConstant::US_private,
2693 hidden_method -> Header(),
2694 StringConstant::US_default,
2695 hidden_method -> containing_type -> ContainingPackageName(),
2696 hidden_method -> containing_type -> ExternalName());
2697 base_type -> MarkBad();
2698 }
2699
2700 //
2701 // Check the throws clause, unless base_type is inheriting two abstract
2702 // methods.
2703 //
2704 if (method -> containing_type != base_type && method -> ACC_ABSTRACT())
2705 return;
2706
2707 method -> ProcessMethodThrows(this, left_tok);
2708 hidden_method -> ProcessMethodThrows(this, left_tok);
2709
2710 for (int i = method -> NumThrows() - 1; i >= 0; i--)
2711 {
2712 TypeSymbol* exception = method -> Throws(i);
2713
2714 if (! CheckedException(exception))
2715 continue;
2716
2717 int k;
2718 for (k = hidden_method -> NumThrows() - 1; k >= 0; k--)
2719 {
2720 if (exception -> IsSubclass(hidden_method -> Throws(k)))
2721 break;
2722 }
2723
2724 if (k < 0)
2725 {
2726 if (method -> containing_type == base_type)
2727 {
2728 if (base_type -> ACC_INTERFACE() &&
2729 hidden_method -> containing_type == control.Object())
2730 {
2731 //
2732 // TODO: Review this when JLS3 is published. See Sun bug
2733 // 4479715, which explains our current stance of allowing
2734 // int clone() throws MyException; or Object finalize();.
2735 //
2736 if (hidden_method -> ACC_PUBLIC())
2737 {
2738 ReportSemError(SemanticError::MISMATCHED_IMPLICIT_OVERRIDDEN_EXCEPTION,
2739 left_tok, right_tok,
2740 exception -> Name(),
2741 method -> Header());
2742 base_type -> MarkBad();
2743 }
2744 }
2745 else
2746 {
2747 ReportSemError(SemanticError::MISMATCHED_OVERRIDDEN_EXCEPTION,
2748 left_tok, right_tok,
2749 exception -> Name(),
2750 hidden_method -> Header(),
2751 hidden_method -> containing_type -> ContainingPackageName(),
2752 hidden_method -> containing_type -> ExternalName());
2753 base_type -> MarkBad();
2754 }
2755 }
2756 else
2757 {
2758 ReportSemError(SemanticError::MISMATCHED_OVERRIDDEN_EXCEPTION_EXTERNALLY,
2759 left_tok, right_tok,
2760 base_type -> ExternalName(),
2761 exception -> Name(), method -> Header(),
2762 method -> containing_type -> ContainingPackageName(),
2763 method -> containing_type -> ExternalName(),
2764 hidden_method -> Header(),
2765 hidden_method -> containing_type -> ContainingPackageName(),
2766 hidden_method -> containing_type -> ExternalName());
2767 base_type -> MarkBad();
2768 }
2769 }
2770 }
2771 }
2772
2773
AddInheritedTypes(TypeSymbol * base_type,TypeSymbol * super_type)2774 void Semantic::AddInheritedTypes(TypeSymbol* base_type, TypeSymbol* super_type)
2775 {
2776 if (super_type -> Bad())
2777 {
2778 base_type -> MarkBad();
2779 return;
2780 }
2781
2782 ExpandedTypeTable& base_expanded_table =
2783 *(base_type -> expanded_type_table);
2784 ExpandedTypeTable& super_expanded_table =
2785 *(super_type -> expanded_type_table);
2786
2787 for (unsigned i = 0; i < super_expanded_table.symbol_pool.Length(); i++)
2788 {
2789 TypeShadowSymbol* type_shadow_symbol =
2790 super_expanded_table.symbol_pool[i];
2791 TypeSymbol* type = type_shadow_symbol -> type_symbol;
2792
2793 //
2794 // Note that since all types in an interface are implicitly public,
2795 // all other types encountered here are enclosed in a type that is a
2796 // super class of base_type.
2797 //
2798 if (type -> ACC_PUBLIC() ||
2799 type -> ACC_PROTECTED() ||
2800 (! type -> ACC_PRIVATE() &&
2801 super_type -> ContainingPackage() == base_type -> ContainingPackage()))
2802 {
2803 TypeShadowSymbol* shadow =
2804 base_expanded_table.FindTypeShadowSymbol(type -> Identity());
2805
2806 if (! shadow || shadow -> type_symbol -> owner != base_type)
2807 {
2808 if (! shadow)
2809 shadow = base_expanded_table.InsertTypeShadowSymbol(type);
2810 else shadow -> AddConflict(type);
2811
2812 assert(type -> owner != super_type ||
2813 type_shadow_symbol -> NumConflicts() == 0);
2814 for (unsigned j = 0;
2815 j < type_shadow_symbol -> NumConflicts(); j++)
2816 {
2817 shadow -> AddConflict(type_shadow_symbol -> Conflict(j));
2818 }
2819 }
2820 }
2821 //
2822 // The main type was not accessible. But it may have been inherited
2823 // from yet another class, in which case any conflicts (which are
2824 // necessarily public types from interfaces) are still inherited in
2825 // the base_type.
2826 //
2827 else if (! type -> ACC_PRIVATE() &&
2828 type_shadow_symbol -> NumConflicts())
2829 {
2830 assert(type -> owner != super_type);
2831 TypeShadowSymbol* shadow =
2832 base_expanded_table.FindTypeShadowSymbol(type -> Identity());
2833
2834 if (shadow)
2835 assert(shadow -> type_symbol -> owner == base_type);
2836 else
2837 {
2838 shadow = base_expanded_table.
2839 InsertTypeShadowSymbol(type_shadow_symbol -> Conflict(0));
2840 for (unsigned k = 1;
2841 k < type_shadow_symbol -> NumConflicts(); k++)
2842 {
2843 shadow -> AddConflict(type_shadow_symbol -> Conflict(k));
2844 }
2845 }
2846 }
2847 }
2848 }
2849
2850
AddInheritedFields(TypeSymbol * base_type,TypeSymbol * super_type)2851 void Semantic::AddInheritedFields(TypeSymbol* base_type,
2852 TypeSymbol* super_type)
2853 {
2854 if (super_type -> Bad())
2855 {
2856 base_type -> MarkBad();
2857 return;
2858 }
2859
2860 ExpandedFieldTable& base_expanded_table =
2861 *(base_type -> expanded_field_table);
2862 ExpandedFieldTable& super_expanded_table =
2863 *(super_type -> expanded_field_table);
2864
2865 for (unsigned i = 0; i < super_expanded_table.symbol_pool.Length(); i++)
2866 {
2867 VariableShadowSymbol* variable_shadow_symbol =
2868 super_expanded_table.symbol_pool[i];
2869 VariableSymbol* variable = variable_shadow_symbol -> variable_symbol;
2870 //
2871 // Note that since all fields in an interface are implicitly public,
2872 // all other fields encountered here are enclosed in a type that is a
2873 // super class of base_type.
2874 //
2875 if (variable -> ACC_PUBLIC() ||
2876 variable -> ACC_PROTECTED() ||
2877 (! variable -> ACC_PRIVATE() &&
2878 super_type -> ContainingPackage() == base_type -> ContainingPackage()))
2879 {
2880 VariableShadowSymbol* shadow = base_expanded_table.
2881 FindVariableShadowSymbol(variable -> Identity());
2882
2883 if (! shadow || shadow -> variable_symbol -> owner != base_type)
2884 {
2885 if (! shadow)
2886 shadow = base_expanded_table.
2887 InsertVariableShadowSymbol(variable);
2888 else shadow -> AddConflict(variable);
2889
2890 assert(variable -> owner != super_type ||
2891 variable_shadow_symbol -> NumConflicts() == 0);
2892 for (unsigned j = 0;
2893 j < variable_shadow_symbol -> NumConflicts(); j++)
2894 {
2895 shadow -> AddConflict(variable_shadow_symbol ->
2896 Conflict(j));
2897 }
2898 }
2899 }
2900 //
2901 // The main field was not accessible. But it may have been inherited
2902 // from yet another class, in which case any conflicts (which are
2903 // necessarily public fields from interfaces) are still inherited in
2904 // the base_type.
2905 //
2906 else if (! variable -> ACC_PRIVATE() &&
2907 ! variable -> ACC_SYNTHETIC() &&
2908 variable_shadow_symbol -> NumConflicts())
2909 {
2910 assert(variable -> owner != super_type);
2911 VariableShadowSymbol* shadow = base_expanded_table.
2912 FindVariableShadowSymbol(variable -> Identity());
2913
2914 if (shadow)
2915 assert(shadow -> variable_symbol -> owner == base_type);
2916 else
2917 {
2918 shadow = base_expanded_table.
2919 InsertVariableShadowSymbol(variable_shadow_symbol ->
2920 Conflict(0));
2921
2922 for (unsigned k = 1;
2923 k < variable_shadow_symbol -> NumConflicts(); k++)
2924 {
2925 shadow -> AddConflict(variable_shadow_symbol ->
2926 Conflict(k));
2927 }
2928 }
2929 }
2930 }
2931 }
2932
2933
AddInheritedMethods(TypeSymbol * base_type,TypeSymbol * super_type,TokenIndex tok)2934 void Semantic::AddInheritedMethods(TypeSymbol* base_type,
2935 TypeSymbol* super_type, TokenIndex tok)
2936 {
2937 if (super_type -> Bad())
2938 {
2939 base_type -> MarkBad();
2940 return;
2941 }
2942
2943 ExpandedMethodTable* base_expanded_table =
2944 base_type -> expanded_method_table;
2945 ExpandedMethodTable* super_expanded_table =
2946 super_type -> expanded_method_table;
2947 PackageSymbol* base_package = base_type -> ContainingPackage();
2948 unsigned i;
2949
2950 for (i = 0; i < super_expanded_table -> symbol_pool.Length(); i++)
2951 {
2952 MethodShadowSymbol* method_shadow_symbol =
2953 super_expanded_table -> symbol_pool[i];
2954 MethodSymbol* method = method_shadow_symbol -> method_symbol;
2955
2956 //
2957 // We have to special case interfaces, since they implicitly declare
2958 // the public methods of Object. In ComputeMethodsClosure, we add all
2959 // methods from Object after adding those from interfaces. Also, since
2960 // user code cannot invoke synthetic methods, we ignore those.
2961 //
2962 if ((base_type -> ACC_INTERFACE() &&
2963 super_type -> ACC_INTERFACE() &&
2964 method -> containing_type == control.Object()) ||
2965 method -> ACC_SYNTHETIC())
2966 {
2967 continue;
2968 }
2969
2970 //
2971 // Note that since all methods in an interface are implicitly
2972 // public, all other methods encountered here are enclosed in a
2973 // type that is a super class of base_type.
2974 //
2975 if (method -> ACC_PUBLIC() || method -> ACC_PROTECTED() ||
2976 (! method -> ACC_PRIVATE() &&
2977 super_type -> ContainingPackage() == base_package))
2978 {
2979 //
2980 // Check that method is compatible with every method it
2981 // overrides.
2982 //
2983 MethodShadowSymbol* shadow = base_expanded_table ->
2984 FindOverloadMethodShadow(method, this, tok);
2985 if (shadow)
2986 {
2987 CheckMethodOverride(shadow -> method_symbol, method,
2988 base_type);
2989 for (unsigned m = 0;
2990 m < method_shadow_symbol -> NumConflicts(); m++)
2991 {
2992 CheckMethodOverride(shadow -> method_symbol,
2993 method_shadow_symbol -> Conflict(m),
2994 base_type);
2995 }
2996 }
2997
2998 if (! shadow ||
2999 shadow -> method_symbol -> containing_type != base_type)
3000 {
3001 if (! shadow)
3002 shadow = base_expanded_table -> Overload(method);
3003 else shadow -> AddConflict(method);
3004
3005 assert(method -> containing_type != super_type ||
3006 method_shadow_symbol -> NumConflicts() == 0);
3007 for (unsigned j = 0;
3008 j < method_shadow_symbol -> NumConflicts(); j++)
3009 {
3010 shadow -> AddConflict(method_shadow_symbol -> Conflict(j));
3011 }
3012 }
3013 }
3014 //
3015 // The main method was not accessible. But it may have been inherited
3016 // from yet another class, in which case any conflicts (which are
3017 // necessarily public methods from interfaces) are still inherited in
3018 // the base_type.
3019 //
3020 else if (! method -> ACC_PRIVATE())
3021 {
3022 MethodShadowSymbol* shadow = base_expanded_table ->
3023 FindOverloadMethodShadow(method, this, tok);
3024 if (method_shadow_symbol -> NumConflicts())
3025 {
3026 assert(method -> containing_type != super_type);
3027
3028 if (shadow)
3029 {
3030 assert(shadow -> method_symbol -> containing_type == base_type);
3031 for (unsigned k = 0;
3032 k < method_shadow_symbol -> NumConflicts(); k++)
3033 {
3034 CheckMethodOverride(shadow -> method_symbol,
3035 method_shadow_symbol -> Conflict(k),
3036 base_type);
3037 }
3038 }
3039 else
3040 {
3041 shadow = base_expanded_table ->
3042 Overload(method_shadow_symbol -> Conflict(0));
3043
3044 for (unsigned l = 1;
3045 l < method_shadow_symbol -> NumConflicts(); l++)
3046 {
3047 shadow -> AddConflict(method_shadow_symbol ->
3048 Conflict(l));
3049 }
3050 }
3051 }
3052 else if (shadow && control.option.pedantic)
3053 {
3054 //
3055 // The base_type declares a method by the same name as a
3056 // method in the superclass, but the new method does not
3057 // override or hide the old. Warn the user about this fact,
3058 // although it is usually not an error.
3059 //
3060 assert(shadow -> method_symbol -> containing_type ==
3061 base_type);
3062 TokenIndex left_tok;
3063 TokenIndex right_tok;
3064
3065 if (ThisType() == base_type)
3066 {
3067 AstMethodDeclaration* method_declaration =
3068 (AstMethodDeclaration*) shadow -> method_symbol -> declaration;
3069 AstMethodDeclarator* method_declarator =
3070 method_declaration -> method_declarator;
3071
3072 left_tok = method_declarator -> LeftToken();
3073 right_tok = method_declarator -> RightToken();
3074 }
3075 else
3076 {
3077 left_tok = ThisType() -> declaration -> identifier_token;
3078 right_tok =
3079 ThisType() -> declaration -> right_brace_token - 1;
3080 }
3081
3082 if (! method -> IsTyped())
3083 method -> ProcessMethodSignature(this, tok);
3084
3085 //
3086 // We filter here, because CompleteSymbolTable gives a
3087 // different warning for unimplementable abstract classes.
3088 //
3089 if (! method -> ACC_ABSTRACT() ||
3090 method -> Type() == shadow -> method_symbol -> Type() ||
3091 (! shadow -> method_symbol -> ACC_PUBLIC() &&
3092 ! shadow -> method_symbol -> ACC_PROTECTED()))
3093 {
3094 ReportSemError(SemanticError::DEFAULT_METHOD_NOT_OVERRIDDEN,
3095 left_tok, right_tok, method -> Header(),
3096 base_type -> ContainingPackageName(),
3097 base_type -> ExternalName(),
3098 super_type -> ContainingPackageName(),
3099 super_type -> ExternalName());
3100 }
3101 }
3102 }
3103 }
3104 //
3105 // Now, we must ensure that any time the inheritance tree left and
3106 // reentered the package, the non-inherited default methods were
3107 // correctly overridden or hidden if redeclared in this class. A method
3108 // is non-inherited only if a class C is in the package, it's subclass
3109 // is not, and there is no interface method also inherited into C.
3110 //
3111 while (super_type -> super)
3112 {
3113 TypeSymbol* prev = super_type;
3114 super_type = super_type -> super;
3115 if (prev -> ContainingPackage() == base_package ||
3116 super_type -> ContainingPackage() != base_package)
3117 {
3118 continue;
3119 }
3120 super_expanded_table = super_type -> expanded_method_table;
3121 for (i = 0; i < super_expanded_table -> symbol_pool.Length(); i++)
3122 {
3123 MethodShadowSymbol* method_shadow_symbol =
3124 super_expanded_table -> symbol_pool[i];
3125 MethodSymbol* method = method_shadow_symbol -> method_symbol;
3126 if (! method -> ACC_PUBLIC() && ! method -> ACC_PROTECTED() &&
3127 ! method -> ACC_PRIVATE() && ! method -> ACC_SYNTHETIC() &&
3128 method_shadow_symbol -> NumConflicts() == 0)
3129 {
3130 // found a non-inherited package scope method
3131 MethodShadowSymbol* shadow = base_expanded_table ->
3132 FindOverloadMethodShadow(method, this, tok);
3133 if (shadow &&
3134 shadow -> method_symbol -> containing_type == base_type)
3135 {
3136 CheckMethodOverride(shadow -> method_symbol, method,
3137 base_type);
3138 }
3139 }
3140 }
3141 }
3142 }
3143
3144
ComputeTypesClosure(TypeSymbol * type,TokenIndex tok)3145 void Semantic::ComputeTypesClosure(TypeSymbol* type, TokenIndex tok)
3146 {
3147 if (! type -> HeaderProcessed())
3148 type -> ProcessTypeHeaders();
3149 type -> expanded_type_table = new ExpandedTypeTable();
3150
3151 TypeSymbol* super_class = type -> super;
3152 if (super_class)
3153 {
3154 if (! super_class -> expanded_type_table)
3155 ComputeTypesClosure(super_class, tok);
3156 }
3157
3158 for (unsigned j = 0; j < type -> NumInterfaces(); j++)
3159 {
3160 TypeSymbol* interf = type -> Interface(j);
3161 if (! interf -> expanded_type_table)
3162 ComputeTypesClosure(interf, tok);
3163 }
3164
3165 if (! type -> NestedTypesProcessed())
3166 type -> ProcessNestedTypeSignatures(this, tok);
3167 for (unsigned i = 0; i < type -> NumTypeSymbols(); i++)
3168 {
3169 if (! type -> TypeSym(i) -> Bad())
3170 {
3171 type -> expanded_type_table ->
3172 InsertTypeShadowSymbol(type -> TypeSym(i));
3173 }
3174 }
3175 if (super_class)
3176 AddInheritedTypes(type, super_class);
3177 for (unsigned k = 0; k < type -> NumInterfaces(); k++)
3178 AddInheritedTypes(type, type -> Interface(k));
3179 type -> expanded_type_table -> CompressSpace();
3180 }
3181
3182
ComputeFieldsClosure(TypeSymbol * type,TokenIndex tok)3183 void Semantic::ComputeFieldsClosure(TypeSymbol* type, TokenIndex tok)
3184 {
3185 type -> expanded_field_table = new ExpandedFieldTable();
3186
3187 TypeSymbol* super_class = type -> super;
3188 if (super_class)
3189 {
3190 if (! super_class -> expanded_field_table)
3191 ComputeFieldsClosure(super_class, tok);
3192 }
3193
3194 for (unsigned j = 0; j < type -> NumInterfaces(); j++)
3195 {
3196 TypeSymbol* interf = type -> Interface(j);
3197 if (! interf -> expanded_field_table)
3198 ComputeFieldsClosure(interf, tok);
3199 }
3200
3201 assert(type -> FieldMembersProcessed());
3202
3203 for (unsigned i = 0; i < type -> NumVariableSymbols(); i++)
3204 {
3205 VariableSymbol* variable = type -> VariableSym(i);
3206 type -> expanded_field_table -> InsertVariableShadowSymbol(variable);
3207 }
3208
3209 //
3210 // As the type Object which is the super type of all interfaces does
3211 // not contain any field declarations, we don't have to do any special
3212 // check here as we have to when computing method closures.
3213 //
3214 if (super_class)
3215 AddInheritedFields(type, super_class);
3216 for (unsigned k = 0; k < type -> NumInterfaces(); k++)
3217 AddInheritedFields(type, type -> Interface(k));
3218 type -> expanded_field_table -> CompressSpace();
3219 }
3220
3221
ComputeMethodsClosure(TypeSymbol * type,TokenIndex tok)3222 void Semantic::ComputeMethodsClosure(TypeSymbol* type, TokenIndex tok)
3223 {
3224 type -> expanded_method_table = new ExpandedMethodTable();
3225
3226 TypeSymbol* super_class = type -> super;
3227 if (super_class)
3228 {
3229 if (! super_class -> expanded_method_table)
3230 ComputeMethodsClosure(super_class, tok);
3231 }
3232
3233 for (unsigned j = 0; j < type -> NumInterfaces(); j++)
3234 {
3235 TypeSymbol* interf = type -> Interface(j);
3236
3237 if (! interf -> expanded_method_table)
3238 ComputeMethodsClosure(interf, tok);
3239 }
3240
3241 assert(type -> MethodMembersProcessed());
3242
3243 for (unsigned i = 0; i < type -> NumMethodSymbols(); i++)
3244 {
3245 MethodSymbol* method = type -> MethodSym(i);
3246 //
3247 // If the method in question is neither a constructor nor an
3248 // initializer, then ...
3249 //
3250 if (*(method -> Name()) != U_LESS)
3251 {
3252 type -> expanded_method_table -> Overload(method);
3253 }
3254 }
3255
3256 //
3257 // We build in this order to guarantee that the first method listed in
3258 // the table will be declared in a class. Conflicts, if any, are from
3259 // interfaces and are necessarily abstract; but if the first method
3260 // is not abstract, it implements all the conflicts.
3261 //
3262 if (super_class && ! type -> ACC_INTERFACE())
3263 AddInheritedMethods(type, super_class, tok);
3264 for (unsigned k = 0; k < type -> NumInterfaces(); k++)
3265 AddInheritedMethods(type, type -> Interface(k), tok);
3266 if (type -> ACC_INTERFACE()) // the super class is Object
3267 AddInheritedMethods(type, control.Object(), tok);
3268 type -> expanded_method_table -> CompressSpace();
3269 }
3270
3271
ProcessFormalParameters(BlockSymbol * block,AstMethodDeclarator * method_declarator)3272 void Semantic::ProcessFormalParameters(BlockSymbol* block,
3273 AstMethodDeclarator* method_declarator)
3274 {
3275 for (unsigned i = 0; i < method_declarator -> NumFormalParameters(); i++)
3276 {
3277 AstFormalParameter* parameter =
3278 method_declarator -> FormalParameter(i);
3279 AccessFlags access_flags = ProcessFormalModifiers(parameter);
3280 ProcessType(parameter -> type);
3281 TypeSymbol* parm_type = parameter -> type -> symbol;
3282
3283 AstVariableDeclaratorId* name =
3284 parameter -> formal_declarator -> variable_declarator_name;
3285 NameSymbol* name_symbol =
3286 lex_stream -> NameSymbol(name -> identifier_token);
3287 VariableSymbol* symbol = block -> FindVariableSymbol(name_symbol);
3288 if (symbol)
3289 {
3290 ReportSemError(SemanticError::DUPLICATE_FORMAL_PARAMETER,
3291 name -> identifier_token, name_symbol -> Name());
3292 }
3293 else symbol = block -> InsertVariableSymbol(name_symbol);
3294
3295 unsigned dims = parm_type -> num_dimensions + name -> NumBrackets();
3296 if (parameter -> ellipsis_token_opt)
3297 {
3298 assert(i == method_declarator -> NumFormalParameters() - 1);
3299 dims++;
3300 access_flags.SetACC_VARARGS();
3301 // TODO: Add varargs support for 1.5.
3302 // if (control.option.source < JikesOption::SDK1_5)
3303 {
3304 ReportSemError(SemanticError::VARARGS_UNSUPPORTED,
3305 parameter -> ellipsis_token_opt);
3306 }
3307 }
3308 symbol -> SetType(parm_type -> GetArrayType(this, dims));
3309 symbol -> SetFlags(access_flags);
3310 symbol -> MarkComplete();
3311 symbol -> MarkInitialized();
3312
3313 parameter -> formal_declarator -> symbol = symbol;
3314 }
3315 }
3316
3317
ProcessMethodDeclaration(AstMethodDeclaration * method_declaration)3318 void Semantic::ProcessMethodDeclaration(AstMethodDeclaration* method_declaration)
3319 {
3320 TypeSymbol* this_type = ThisType();
3321 AccessFlags access_flags = this_type -> ACC_INTERFACE()
3322 ? ProcessInterfaceMethodModifiers(method_declaration)
3323 : ProcessMethodModifiers(method_declaration);
3324 //
3325 // By JLS2 8.4.3.3, a private method and all methods declared in a
3326 // final class are implicitly final. Also, all methods in a strictfp
3327 // class are strictfp.
3328 //
3329 if (access_flags.ACC_PRIVATE() || this_type -> ACC_FINAL())
3330 access_flags.SetACC_FINAL();
3331 if (this_type -> ACC_STRICTFP())
3332 access_flags.SetACC_STRICTFP();
3333
3334 if (method_declaration -> type_parameters_opt)
3335 {
3336 // TODO: Add generics support for 1.5.
3337 ReportSemError(SemanticError::TYPE_PARAMETERS_UNSUPPORTED,
3338 method_declaration -> type_parameters_opt);
3339 }
3340 //
3341 // A method enclosed in an inner type may not be declared static.
3342 //
3343 if (access_flags.ACC_STATIC() && this_type -> IsInner())
3344 {
3345 assert(method_declaration -> modifiers_opt &&
3346 method_declaration -> modifiers_opt -> static_token_opt);
3347 ReportSemError(SemanticError::STATIC_METHOD_IN_INNER_CLASS,
3348 method_declaration -> modifiers_opt -> static_token_opt,
3349 lex_stream -> NameString(method_declaration -> method_declarator -> identifier_token),
3350 this_type -> Name(),
3351 this_type -> FileLoc());
3352 }
3353
3354 //
3355 // To avoid deprecated type warnings when processing a deprecated method
3356 // declaration, we must temporarily mark this type as deprecated, because
3357 // the method symbol does not yet exist. We fix it after formal parameter
3358 // processing.
3359 //
3360 bool deprecated_method =
3361 lex_stream -> IsDeprecated(method_declaration -> LeftToken());
3362 bool deprecated_type = this_type -> IsDeprecated();
3363 if (deprecated_method)
3364 this_type -> MarkDeprecated();
3365 ProcessType(method_declaration -> type);
3366 TypeSymbol* method_type = method_declaration -> type -> symbol;
3367
3368 AstMethodDeclarator* method_declarator =
3369 method_declaration -> method_declarator;
3370 if (method_declarator -> NumBrackets())
3371 {
3372 if (method_type == control.void_type)
3373 {
3374 ReportSemError(SemanticError::VOID_ARRAY,
3375 method_declaration -> type -> LeftToken(),
3376 method_declarator -> RightToken());
3377 method_type = control.no_type;
3378 }
3379 else ReportSemError(SemanticError::OBSOLESCENT_BRACKETS,
3380 method_declarator);
3381 }
3382
3383 NameSymbol* name_symbol =
3384 lex_stream -> NameSymbol(method_declarator -> identifier_token);
3385
3386 if (name_symbol == this_type -> Identity())
3387 {
3388 ReportSemError(SemanticError::METHOD_WITH_CONSTRUCTOR_NAME,
3389 method_declaration -> type -> LeftToken(),
3390 method_declarator -> identifier_token,
3391 name_symbol -> Name());
3392 }
3393
3394 //
3395 // Warn against unconventional names.
3396 //
3397 if (name_symbol -> IsBadStyleForMethod())
3398 {
3399 ReportSemError(SemanticError::UNCONVENTIONAL_METHOD_NAME,
3400 method_declarator -> identifier_token,
3401 name_symbol -> Name());
3402 }
3403
3404 //
3405 // As the body of the method may not have been parsed yet, we estimate a
3406 // size for its symbol table based on the number of lines in the body + a
3407 // margin for one-liners.
3408 //
3409 BlockSymbol* block_symbol =
3410 new BlockSymbol(method_declarator -> NumFormalParameters());
3411 block_symbol -> max_variable_index = (access_flags.ACC_STATIC() ? 0 : 1);
3412 ProcessFormalParameters(block_symbol, method_declarator);
3413 if (! deprecated_type && deprecated_method)
3414 this_type -> ResetDeprecated();
3415
3416 MethodSymbol* method = this_type -> FindMethodSymbol(name_symbol);
3417 if (method && this_type -> FindOverloadMethod(method, method_declarator))
3418 {
3419 ReportSemError(SemanticError::DUPLICATE_METHOD,
3420 method_declarator, name_symbol -> Name(),
3421 this_type -> Name(), method -> FileLoc());
3422 delete block_symbol;
3423 return;
3424 }
3425
3426 method = this_type -> InsertMethodSymbol(name_symbol);
3427 unsigned dims =
3428 method_type -> num_dimensions + method_declarator -> NumBrackets();
3429 method -> SetType(method_type -> GetArrayType(this, dims));
3430
3431 //
3432 // if the method is not static, leave a slot for the "this" pointer.
3433 //
3434 method -> SetFlags(access_flags);
3435 method -> SetContainingType(this_type);
3436 method -> SetBlockSymbol(block_symbol);
3437 method -> declaration = method_declaration;
3438 method -> SetLocation();
3439 for (unsigned i = 0; i < method_declarator -> NumFormalParameters(); i++)
3440 {
3441 AstVariableDeclarator* formal_declarator =
3442 method_declarator -> FormalParameter(i) -> formal_declarator;
3443 VariableSymbol* symbol = formal_declarator -> symbol;
3444
3445 symbol -> SetOwner(method);
3446 symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
3447 symbol -> MarkComplete();
3448 if (control.IsDoubleWordType(symbol -> Type()))
3449 block_symbol -> max_variable_index++;
3450 symbol -> declarator = formal_declarator;
3451 symbol -> SetLocation();
3452 method -> AddFormalParameter(symbol);
3453 }
3454 method -> SetSignature(control);
3455
3456 for (unsigned k = 0; k < method_declaration -> NumThrows(); k++)
3457 {
3458 AstTypeName* throw_expr = method_declaration -> Throw(k);
3459 ProcessType(throw_expr);
3460 method -> AddThrows(throw_expr -> symbol);
3461 }
3462
3463 // save for processing bodies later.
3464 method_declaration -> method_symbol = method;
3465
3466 if (method -> ACC_ABSTRACT() && ! this_type -> ACC_ABSTRACT())
3467 {
3468 ReportSemError(SemanticError::NON_ABSTRACT_TYPE_CONTAINS_ABSTRACT_METHOD,
3469 method_declaration -> LeftToken(),
3470 method_declarator -> identifier_token,
3471 name_symbol -> Name(),
3472 this_type -> Name());
3473 }
3474
3475 if (deprecated_method)
3476 method -> MarkDeprecated();
3477 }
3478
3479
3480 //
3481 // Return the type corresponding to a primitive type keyword.
3482 //
FindPrimitiveType(AstPrimitiveType * primitive_type)3483 TypeSymbol* Semantic::FindPrimitiveType(AstPrimitiveType* primitive_type)
3484 {
3485 switch (primitive_type -> kind)
3486 {
3487 case Ast::INT:
3488 return control.int_type;
3489 case Ast::DOUBLE:
3490 return control.double_type;
3491 case Ast::CHAR:
3492 return control.char_type;
3493 case Ast::LONG:
3494 return control.long_type;
3495 case Ast::FLOAT:
3496 return control.float_type;
3497 case Ast::BYTE:
3498 return control.byte_type;
3499 case Ast::SHORT:
3500 return control.short_type;
3501 case Ast::BOOLEAN:
3502 return control.boolean_type;
3503 default:
3504 break;
3505 }
3506
3507 return control.void_type;
3508 }
3509
3510
3511 //
3512 // Search the import-on-demand locations for a type with the given name. This
3513 // returns inaccessible types if found, with no error message, but favors
3514 // accessible ones. It will issue an error if the only way an accessible type
3515 // was found is non-canonical. If no type is found, NULL is returned.
3516 //
ImportType(TokenIndex identifier_token,NameSymbol * name_symbol)3517 TypeSymbol* Semantic::ImportType(TokenIndex identifier_token,
3518 NameSymbol* name_symbol)
3519 {
3520 //
3521 // To keep track of inaccessible types, we note the first one we find,
3522 // while leaving the location as NULL. Once we find an accessible type, we
3523 // set location, so that we know that future types are duplicates. We
3524 // pre-filtered duplicate import-on-demands, as well as adding java.lang.*.
3525 //
3526 TypeSymbol* type = NULL;
3527 PackageSymbol* location = NULL;
3528
3529 for (unsigned i = 0; i < import_on_demand_packages.Length(); i++)
3530 {
3531 PackageSymbol* import_package =
3532 import_on_demand_packages[i] -> PackageCast();
3533 TypeSymbol* possible_type = NULL;
3534
3535 if (import_package)
3536 {
3537 possible_type = import_package -> FindTypeSymbol(name_symbol);
3538 if (! possible_type)
3539 {
3540 FileSymbol* file_symbol =
3541 Control::GetFile(control, import_package, name_symbol);
3542 if (file_symbol)
3543 possible_type = ReadType(file_symbol, import_package,
3544 name_symbol, identifier_token);
3545 }
3546 else if (possible_type -> SourcePending())
3547 control.ProcessHeaders(possible_type -> file_symbol);
3548 }
3549 else
3550 {
3551 TypeSymbol* import_type =
3552 (TypeSymbol*) import_on_demand_packages[i];
3553 if (! import_type -> expanded_type_table)
3554 ComputeTypesClosure(import_type, identifier_token);
3555 TypeShadowSymbol* type_shadow_symbol = import_type ->
3556 expanded_type_table -> FindTypeShadowSymbol(name_symbol);
3557 if (type_shadow_symbol)
3558 {
3559 //
3560 // Only canonical names may be used in import statements, hence
3561 // the extra filter on the containing type being correct. If
3562 // we encounter conflicts, they are necessarily accessible
3563 // inherited types from interfaces (and hence non-canonical).
3564 //
3565 possible_type = (type_shadow_symbol -> NumConflicts()
3566 ? type_shadow_symbol -> Conflict(0)
3567 : type_shadow_symbol -> type_symbol);
3568 if (! possible_type -> ACC_PRIVATE() &&
3569 import_type == possible_type -> owner)
3570 {
3571 import_package = import_type -> ContainingPackage();
3572 }
3573 }
3574 }
3575 if (possible_type)
3576 {
3577 if (location && import_package &&
3578 (possible_type -> ACC_PUBLIC() ||
3579 import_package == this_package))
3580 {
3581 ReportSemError(SemanticError::DUPLICATE_ON_DEMAND_IMPORT,
3582 identifier_token, name_symbol -> Name(),
3583 location -> PackageName(),
3584 import_package -> PackageName());
3585 }
3586 else
3587 {
3588 type = possible_type;
3589 if (type -> ACC_PUBLIC() || import_package == this_package)
3590 location = import_package; // may be NULL
3591 }
3592 }
3593 }
3594
3595 if (type && ! location && ! type -> ACC_PRIVATE() &&
3596 (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package))
3597 {
3598 ReportSemError(SemanticError::IMPORT_NOT_CANONICAL,
3599 identifier_token, type -> Name(),
3600 type -> ContainingPackageName(),
3601 type -> ExternalName());
3602 }
3603
3604 // Keep track of referenced types.
3605 if (type && location)
3606 referenced_package_imports.AddElement(location);
3607
3608 return type;
3609 }
3610
3611
3612 //
3613 // Finds an accessible type by the name located at identifier_token, or returns
3614 // NULL. If there are ambiguous accessible types, this issues an error in the
3615 // process. Note that inaccessible types are skipped - if the caller wishes
3616 // to use an inaccessible type, they must search for it.
3617 //
FindType(TokenIndex identifier_token)3618 TypeSymbol* Semantic::FindType(TokenIndex identifier_token)
3619 {
3620 TypeSymbol* type;
3621 NameSymbol* name_symbol = lex_stream -> NameSymbol(identifier_token);
3622
3623 SemanticEnvironment* env = NULL;
3624 if (state_stack.Size())
3625 env = state_stack.Top();
3626 for ( ; env; env = env -> previous)
3627 {
3628 // Search for local types, which are always accessible.
3629 type = env -> symbol_table.FindTypeSymbol(name_symbol);
3630 if (type)
3631 break;
3632 // Search for declared or inherited member types.
3633 type = env -> Type();
3634 if (! type -> expanded_type_table)
3635 ComputeTypesClosure(type, identifier_token);
3636 TypeShadowSymbol* type_shadow_symbol =
3637 type -> expanded_type_table -> FindTypeShadowSymbol(name_symbol);
3638 if (type_shadow_symbol)
3639 {
3640 type = FindTypeInShadow(type_shadow_symbol, identifier_token);
3641 if (type)
3642 break;
3643 }
3644 }
3645
3646 if (env) // The type was found in some enclosing environment?
3647 {
3648 //
3649 // A static type cannot access a non-static member type of an enclosing
3650 // class by simple name.
3651 //
3652 TypeSymbol* this_type = ThisType();
3653 assert(this_type);
3654 if (this_type -> ACC_STATIC() && ! type -> ACC_STATIC() &&
3655 ! this_type -> IsSubclass(type -> ContainingType()))
3656 {
3657 ReportSemError(SemanticError::STATIC_TYPE_ACCESSING_MEMBER_TYPE,
3658 identifier_token,
3659 this_type -> ContainingPackageName(),
3660 this_type -> ExternalName(),
3661 type -> ContainingPackageName(),
3662 type -> ExternalName(),
3663 env -> Type() -> ContainingPackageName(),
3664 env -> Type() -> ExternalName());
3665 }
3666 //
3667 // If the type was inherited, give a warning if it shadowed another
3668 // type of the same name within an enclosing lexical scope.
3669 //
3670 if (type -> owner -> TypeCast() && type -> owner != env -> Type())
3671 {
3672 TypeSymbol* supertype = (TypeSymbol*) type -> owner;
3673 for ( ; env; env = env -> previous)
3674 {
3675 //
3676 // First, check the enclosing type name - this is a caution,
3677 // because this behavior is opposite C++ when a type inherits
3678 // a membertype with the same name.
3679 //
3680 if (name_symbol == env -> Type() -> Identity() &&
3681 env -> Type() != type)
3682 {
3683 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_TYPE,
3684 identifier_token,
3685 lex_stream -> NameString(identifier_token),
3686 type -> ContainingPackageName(),
3687 type -> ExternalName(),
3688 env -> Type() -> ContainingPackageName(),
3689 env -> Type() -> ExternalName());
3690 break;
3691 }
3692 if (env -> previous && control.option.pedantic)
3693 {
3694 // Next, in pedantic mode, check local type
3695 SemanticEnvironment* env2 = env -> previous;
3696 TypeSymbol* outer_type =
3697 env2 -> symbol_table.FindTypeSymbol(name_symbol);
3698 if (outer_type)
3699 {
3700 assert(outer_type -> owner -> MethodCast());
3701 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_LOCAL,
3702 identifier_token,
3703 lex_stream -> NameString(identifier_token),
3704 supertype -> ContainingPackageName(),
3705 supertype -> ExternalName(),
3706 ((MethodSymbol*) outer_type -> owner) -> Name());
3707 break;
3708 }
3709 // If local type not found, check inner type.
3710 if (! env2 -> Type() -> expanded_type_table)
3711 ComputeTypesClosure(env2 -> Type(), identifier_token);
3712 TypeShadowSymbol* type_shadow_symbol =
3713 env2 -> Type() -> expanded_type_table ->
3714 FindTypeShadowSymbol(name_symbol);
3715 if (type_shadow_symbol)
3716 outer_type = FindTypeInShadow(type_shadow_symbol,
3717 identifier_token);
3718 if (outer_type && outer_type != type &&
3719 outer_type -> owner == env2 -> Type())
3720 {
3721 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
3722 identifier_token,
3723 lex_stream -> NameString(identifier_token),
3724 supertype -> ContainingPackageName(),
3725 supertype -> ExternalName(),
3726 env2 -> Type() -> ContainingPackageName(),
3727 env2 -> Type() -> ExternalName());
3728 break;
3729 }
3730 }
3731 }
3732 }
3733
3734 return type;
3735 }
3736
3737 //
3738 // Search for the type in the current compilation unit if it was declared
3739 // as a class or interface or imported by a single-type-import declaration.
3740 //
3741 for (unsigned i = 0; i < single_type_imports.Length(); i++)
3742 {
3743 type = single_type_imports[i];
3744 if (name_symbol == type -> Identity())
3745 {
3746 // Keep track of referenced types.
3747 referenced_type_imports.AddElement(type);
3748 return type;
3749 }
3750 }
3751
3752 //
3753 // Search for another file in the current package, and if that fails, check
3754 // for an accessible import-on-demand.
3755 //
3756 type = FindSimpleNameType(this_package, identifier_token);
3757 TypeSymbol* imported_type = (! type || type -> Bad()
3758 ? ImportType(identifier_token, name_symbol)
3759 : (TypeSymbol*) NULL);
3760
3761 //
3762 // If a valid type can be imported on demand, choose that type.
3763 // Otherwise, if a type was found at all, do some final checks on it.
3764 //
3765 // Note that a type T contained in a package P is always accessible to all
3766 // other types contained in P. I.e., we do not need to perform access check
3767 // for type...
3768 //
3769 if (imported_type && TypeAccessCheck(imported_type))
3770 type = imported_type;
3771 else if (type)
3772 {
3773 //
3774 // If a type T was specified in a source file that is not called T.java
3775 // but X.java (where X != T) and we are not currently compiling file X,
3776 // issue a warning to alert the user that in some circumstances, this
3777 // may not be visible. (i.e., if the file X has not yet been compiled,
3778 // then T is invisile as the compiler will only look for T in T.java.)
3779 //
3780 FileSymbol* file_symbol = type -> file_symbol;
3781 if (file_symbol && type -> Identity() != file_symbol -> Identity() &&
3782 file_symbol != this -> source_file_symbol)
3783 {
3784 ReportSemError(SemanticError::REFERENCE_TO_TYPE_IN_MISMATCHED_FILE,
3785 identifier_token, type -> Name(),
3786 file_symbol -> Name());
3787 }
3788 }
3789
3790 return type;
3791 }
3792
3793
3794 //
3795 // Returns an inaccessible type of the given name, or 0 if there is none.
3796 // No errors are reported by this method.
3797 //
FindInaccessibleType(AstName * name)3798 TypeSymbol* Semantic::FindInaccessibleType(AstName* name)
3799 {
3800 assert(! name -> base_opt);
3801 NameSymbol* name_symbol =
3802 lex_stream -> NameSymbol(name -> identifier_token);
3803
3804 // Check for inaccessible member type.
3805 if (state_stack.Size())
3806 {
3807 for (TypeSymbol* super_type = ThisType() -> super;
3808 super_type; super_type = super_type -> super)
3809 {
3810 assert(super_type -> expanded_type_table);
3811 TypeShadowSymbol* type_shadow_symbol = super_type ->
3812 expanded_type_table -> FindTypeShadowSymbol(name_symbol);
3813 if (type_shadow_symbol)
3814 {
3815 return type_shadow_symbol -> type_symbol;
3816 }
3817 }
3818 }
3819 // Check for an inaccessible import.
3820 return ImportType(name -> identifier_token, name_symbol);
3821 }
3822
3823
3824 //
3825 // Finds a type by the given name, and add the dependence information. If one
3826 // exists, but is not accessible, it is returned after an error. After other
3827 // errors, control.no_type is returned.
3828 //
MustFindType(AstName * name)3829 TypeSymbol* Semantic::MustFindType(AstName* name)
3830 {
3831 TypeSymbol* type;
3832 NameSymbol* name_symbol =
3833 lex_stream -> NameSymbol(name -> identifier_token);
3834 if (! name -> base_opt)
3835 {
3836 type = FindType(name -> identifier_token);
3837 //
3838 // If the type was not found, generate an appropriate error message.
3839 //
3840 if (! type)
3841 {
3842 type = FindInaccessibleType(name);
3843 if (type)
3844 ReportTypeInaccessible(name, type);
3845 else
3846 {
3847 // Try reading the file again, to force an error.
3848 NameSymbol* name_symbol =
3849 lex_stream -> NameSymbol(name -> identifier_token);
3850 FileSymbol* file_symbol =
3851 Control::GetFile(control, this_package, name_symbol);
3852 type = ReadType(file_symbol, this_package, name_symbol,
3853 name -> identifier_token);
3854 }
3855 }
3856 }
3857 else // qualified name
3858 {
3859 ProcessPackageOrType(name -> base_opt);
3860 Symbol* symbol = name -> base_opt -> symbol;
3861 type = symbol -> TypeCast();
3862 if (type)
3863 type = MustFindNestedType(type, name);
3864 else
3865 {
3866 PackageSymbol* package = symbol -> PackageCast();
3867 type = package -> FindTypeSymbol(name_symbol);
3868 if (! type)
3869 {
3870 FileSymbol* file_symbol =
3871 Control::GetFile(control, package, name_symbol);
3872 type = ReadType(file_symbol, package, name_symbol,
3873 name -> identifier_token);
3874 }
3875 else if (type -> SourcePending())
3876 control.ProcessHeaders(type -> file_symbol);
3877 if (! TypeAccessCheck(type))
3878 ReportTypeInaccessible(name, type);
3879 }
3880 }
3881
3882 //
3883 // Establish a dependence from the base_type to a type that it "must find".
3884 // Note that the only time an environment is not available is when were are
3885 // processing the type header of an outermost type.
3886 //
3887 if (state_stack.Size())
3888 {
3889 AddDependence(ThisType(), type);
3890 if (control.option.deprecation && type -> IsDeprecated() &&
3891 ! InDeprecatedContext())
3892 {
3893 ReportSemError(SemanticError::DEPRECATED_TYPE, name,
3894 type -> ContainingPackageName(),
3895 type -> ExternalName());
3896 }
3897 }
3898 if (type -> Anonymous() && ! type -> Bad())
3899 {
3900 ReportSemError(SemanticError::UNNAMED_TYPE_ACCESS, name,
3901 type -> ContainingPackageName(),
3902 type -> ExternalName());
3903 }
3904 if (type -> ACC_SYNTHETIC() && ! type -> Bad())
3905 {
3906 ReportSemError(SemanticError::SYNTHETIC_TYPE_ACCESS, name,
3907 type -> ContainingPackageName(),
3908 type -> ExternalName());
3909 }
3910 return type -> Bad() ? control.no_type : type;
3911 }
3912
3913
ProcessType(AstType * type_expr)3914 void Semantic::ProcessType(AstType* type_expr)
3915 {
3916 if (type_expr -> symbol)
3917 return; // already processed
3918 AstArrayType* array_type = type_expr -> ArrayTypeCast();
3919 AstType* actual_type = array_type ? array_type -> type : type_expr;
3920 AstTypeName* name = actual_type -> TypeNameCast();
3921 AstPrimitiveType* primitive_type = actual_type -> PrimitiveTypeCast();
3922 AstWildcard* wildcard_type = actual_type -> WildcardCast();
3923 if (wildcard_type)
3924 {
3925 ReportSemError(SemanticError::WILDCARD_UNSUPPORTED, type_expr);
3926 type_expr -> symbol = control.no_type;
3927 return;
3928 }
3929 assert(name || primitive_type);
3930 //
3931 // Occaisionally, MustFindType finds a bad type (for example, if we
3932 // reference Other.java which has syntax errors), but does not know to
3933 // report the error.
3934 //
3935 unsigned error_count = NumErrors();
3936 TypeSymbol* type;
3937 if (primitive_type)
3938 type = FindPrimitiveType(primitive_type);
3939 else
3940 {
3941 if (name -> base_opt)
3942 {
3943 ProcessType(name -> base_opt);
3944 type = MustFindNestedType(name -> base_opt -> symbol,
3945 name -> name);
3946 }
3947 else type = MustFindType(name -> name);
3948 if (name -> type_arguments_opt)
3949 {
3950 // TODO: Add generics support for 1.5.
3951 ReportSemError(SemanticError::TYPE_ARGUMENTS_UNSUPPORTED,
3952 name -> type_arguments_opt,
3953 type -> ContainingPackageName(),
3954 type -> ExternalName());
3955 }
3956 }
3957 if (type -> Bad() && NumErrors() == error_count)
3958 {
3959 if (type == control.no_type)
3960 {
3961 ReportSemError(SemanticError::TYPE_NOT_FOUND, actual_type,
3962 NULL,
3963 lex_stream -> NameString(type_expr ->
3964 IdentifierToken()));
3965 }
3966 else
3967 {
3968 ReportSemError(SemanticError::INVALID_TYPE_FOUND, actual_type,
3969 lex_stream -> NameString(type_expr ->
3970 IdentifierToken()));
3971 }
3972 }
3973 if (array_type)
3974 type = type -> GetArrayType(this, array_type -> NumBrackets());
3975 type_expr -> symbol = type;
3976 }
3977
3978
3979 //
3980 // Initializes a static or instance field. In addition to checking the
3981 // semantics, the initialization is added as a statement in the init_method,
3982 // for easy bytecode emission, if it has an initializer and is not a constant.
3983 //
InitializeVariable(AstFieldDeclaration * field_declaration,MethodSymbol * init_method)3984 void Semantic::InitializeVariable(AstFieldDeclaration* field_declaration,
3985 MethodSymbol* init_method)
3986 {
3987 ThisMethod() = NULL;
3988 AstMethodDeclaration* declaration =
3989 (AstMethodDeclaration*) init_method -> declaration;
3990 assert(declaration -> method_body_opt);
3991
3992 for (unsigned i = 0;
3993 i < field_declaration -> NumVariableDeclarators(); i++)
3994 {
3995 AstVariableDeclarator* variable_declarator =
3996 field_declaration -> VariableDeclarator(i);
3997 VariableSymbol* variable = variable_declarator -> symbol;
3998
3999 if (variable)
4000 {
4001 ThisVariable() = variable;
4002 if (variable_declarator -> variable_initializer_opt)
4003 {
4004 variable_declarator -> pending = true;
4005
4006 unsigned start_num_errors = NumErrors();
4007 ProcessVariableInitializer(variable_declarator);
4008 if (NumErrors() == start_num_errors)
4009 {
4010 DefiniteFieldInitializer(variable_declarator);
4011 if (! variable -> initial_value)
4012 {
4013 declaration -> method_body_opt ->
4014 AddStatement(variable_declarator);
4015 }
4016 }
4017 else if (variable -> ACC_FINAL())
4018 {
4019 // Suppress further error messages.
4020 DefinitelyAssignedVariables() ->
4021 AssignElement(variable -> LocalVariableIndex());
4022 }
4023
4024 variable_declarator -> pending = false;
4025 }
4026 variable -> MarkComplete();
4027 }
4028 }
4029 }
4030
4031
4032 //
4033 // Adds an initializer block to the init_method, after checking its semantics,
4034 // for easier bytecode emission.
4035 //
ProcessInitializer(AstInitializerDeclaration * initializer,MethodSymbol * init_method)4036 inline void Semantic::ProcessInitializer(AstInitializerDeclaration* initializer,
4037 MethodSymbol* init_method)
4038 {
4039 ThisVariable() = NULL;
4040 ThisMethod() = init_method;
4041 AstMethodDeclaration* declaration =
4042 (AstMethodDeclaration*) init_method -> declaration;
4043
4044 LocalBlockStack().Push(declaration -> method_body_opt);
4045 LocalSymbolTable().Push(init_method -> block_symbol -> Table());
4046
4047 //
4048 // Initializer blocks are always reachable, as prior blocks must be able
4049 // to complete normally.
4050 //
4051 initializer -> block -> is_reachable = true;
4052
4053 if (initializer -> block -> explicit_constructor_opt)
4054 {
4055 AstStatement* explicit_ctor =
4056 initializer -> block -> explicit_constructor_opt;
4057 ReportSemError(SemanticError::MISPLACED_EXPLICIT_CONSTRUCTOR,
4058 explicit_ctor);
4059 }
4060 ProcessInitializerModifiers(initializer);
4061 ProcessBlock(initializer -> block);
4062 DefiniteBlockInitializer(initializer -> block, LocalBlockStack().max_size);
4063
4064 declaration -> method_body_opt -> AddStatement(initializer -> block);
4065
4066 //
4067 // If the initializer has a higher max_variable_index than the overall
4068 // block, update max_variable_index in the method_body, accordingly.
4069 //
4070 if (init_method -> block_symbol -> max_variable_index <
4071 LocalBlockStack().TopMaxEnclosedVariableIndex())
4072 {
4073 init_method -> block_symbol -> max_variable_index =
4074 LocalBlockStack().TopMaxEnclosedVariableIndex();
4075 }
4076
4077 if (! initializer -> block -> can_complete_normally)
4078 ReportSemError(SemanticError::ABRUPT_INITIALIZER, initializer);
4079
4080 LocalBlockStack().Pop();
4081 LocalSymbolTable().Pop();
4082 }
4083
4084
4085 //
4086 // Lazily create and return the static initializer for this type. The estimate
4087 // is for the number of initializers that will be grouped into this method.
4088 // This is called both when processing static initializers, and any time an
4089 // assert statement is encountered (since assertions require an initialized
4090 // static variable to operate).
4091 //
GetStaticInitializerMethod(unsigned estimate)4092 MethodSymbol* Semantic::GetStaticInitializerMethod(unsigned estimate)
4093 {
4094 TypeSymbol* this_type = ThisType();
4095 if (this_type -> static_initializer_method)
4096 return this_type -> static_initializer_method;
4097
4098 StoragePool* ast_pool = compilation_unit -> ast_pool;
4099 TokenIndex loc = this_type -> declaration -> identifier_token;
4100
4101 // The symbol table associated with this block has no elements.
4102 BlockSymbol* block_symbol = new BlockSymbol(0);
4103 block_symbol -> max_variable_index = 0;
4104
4105 // The body of the static initializer. This will contain each initializer
4106 // block in sequence.
4107 AstMethodBody* block = ast_pool -> GenMethodBody();
4108 block -> left_brace_token = loc;
4109 block -> right_brace_token = loc;
4110 block -> block_symbol = block_symbol;
4111 block -> AllocateStatements(estimate);
4112
4113 // The return type (void).
4114 AstType* return_type = ast_pool -> GenPrimitiveType(Ast::VOID_TYPE, loc);
4115 return_type -> symbol = control.void_type;
4116
4117 // The method declaration. We leave some fields uninitialized, because
4118 // they are not needed in bytecode.cpp.
4119 AstMethodDeclaration* declaration = ast_pool -> GenMethodDeclaration();
4120 MethodSymbol* init_method =
4121 this_type -> InsertMethodSymbol(control.clinit_name_symbol);
4122 declaration -> type = return_type;
4123 declaration -> method_symbol = init_method;
4124 declaration -> method_body_opt = block;
4125
4126 // The method symbol.
4127 init_method -> SetType(control.void_type);
4128 init_method -> SetFlags(AccessFlags::ACCESS_PRIVATE |
4129 AccessFlags::ACCESS_FINAL |
4130 AccessFlags::ACCESS_STATIC);
4131 if (this_type -> ACC_STRICTFP())
4132 init_method -> SetACC_STRICTFP();
4133 init_method -> SetContainingType(this_type);
4134 init_method -> SetBlockSymbol(block_symbol);
4135 init_method -> SetSignature(control);
4136 init_method -> declaration = declaration;
4137
4138 this_type -> static_initializer_method = init_method;
4139 return init_method;
4140 }
4141
4142
ProcessStaticInitializers(AstClassBody * class_body)4143 void Semantic::ProcessStaticInitializers(AstClassBody* class_body)
4144 {
4145 //
4146 // Notice that the bodies of methods have not been processed yet when this
4147 // is called. If any method contains an assert, it will generate a static
4148 // initializer for the $noassert variable as needed. On the other hand, if
4149 // we already encountered an assert statement in an instance initializer,
4150 // the static initializer already exists. The assert variable initializer
4151 // is magically implemented by bytecode.cpp, rather than adding all the AST
4152 // structure to the block of the static initializer.
4153 //
4154 if (class_body -> NumStaticInitializers() == 0 &&
4155 class_body -> NumClassVariables() == 0)
4156 {
4157 return;
4158 }
4159
4160 TypeSymbol* this_type = ThisType();
4161 LocalBlockStack().max_size = 1;
4162 assert(FinalFields());
4163
4164 //
4165 // Work out how many statements we'll need.
4166 //
4167 unsigned estimate = class_body -> NumStaticInitializers();
4168 for (unsigned i = 0; i < class_body -> NumClassVariables(); ++i)
4169 {
4170 estimate += class_body -> ClassVariable(i) -> NumVariableDeclarators();
4171 }
4172 MethodSymbol* init_method = GetStaticInitializerMethod(estimate);
4173
4174 //
4175 // The static initializers and class variable initializers are executed
4176 // in textual order, with the exception that assignments may occur before
4177 // declaration. See JLS 8.5.
4178 //
4179 unsigned j = 0;
4180 unsigned k = 0;
4181 while (j < class_body -> NumClassVariables() &&
4182 k < class_body -> NumStaticInitializers())
4183 {
4184 if (class_body -> ClassVariable(j) -> semicolon_token <
4185 class_body -> StaticInitializer(k) -> block -> right_brace_token)
4186 {
4187 InitializeVariable(class_body -> ClassVariable(j++), init_method);
4188 }
4189 else
4190 {
4191 ProcessInitializer(class_body -> StaticInitializer(k++),
4192 init_method);
4193 }
4194 }
4195 while (j < class_body -> NumClassVariables())
4196 {
4197 InitializeVariable(class_body -> ClassVariable(j++), init_method);
4198 }
4199 while (k < class_body -> NumStaticInitializers())
4200 {
4201 ProcessInitializer(class_body -> StaticInitializer(k++),
4202 init_method);
4203 }
4204
4205 //
4206 // Check that each static final variable has been initialized by now.
4207 // If not, issue an error and assume it is. Notice that for inner
4208 // classes, we have already reported that a non-constant static
4209 // field is illegal, so we only need an error here for top-level
4210 // and static classes.
4211 //
4212 for (unsigned l = 0; l < FinalFields() -> Length(); l++)
4213 {
4214 VariableSymbol* final_var = (*FinalFields())[l];
4215 if (final_var -> ACC_STATIC() &&
4216 ! DefinitelyAssignedVariables() -> da_set[l])
4217 {
4218 if (! this_type -> IsInner())
4219 {
4220 ReportSemError(SemanticError::UNINITIALIZED_STATIC_FINAL_VARIABLE,
4221 final_var -> declarator, final_var -> Name());
4222 }
4223 DefinitelyAssignedVariables() -> AssignElement(l);
4224 }
4225 }
4226
4227 //
4228 // If an initialization method has been defined, update its
4229 // max_block_depth.
4230 //
4231 if (this_type -> static_initializer_method)
4232 {
4233 MethodSymbol* init_method = this_type -> static_initializer_method;
4234 init_method -> max_block_depth = LocalBlockStack().max_size;
4235 init_method -> block_symbol -> CompressSpace(); // space optimization
4236 }
4237 }
4238
4239
ProcessInstanceInitializers(AstClassBody * class_body)4240 void Semantic::ProcessInstanceInitializers(AstClassBody* class_body)
4241 {
4242 //
4243 // For instance initializers, we create a method to do all the
4244 // initialization. We name the method 'this', which is legal in VM's
4245 // but an illegal user name, to avoid name clashes. Constructors which
4246 // do not invoke another constructor via the this() statement will call
4247 // the instance initializer method after calling super(). We rely on the
4248 // fact that VM's allow assignment of final instance variables in an
4249 // instance method, rather than requiring it to be in a constructor.
4250 //
4251 if (class_body -> NumInstanceInitializers() == 0 &&
4252 class_body -> NumInstanceVariables() == 0)
4253 {
4254 return;
4255 }
4256
4257 TypeSymbol* this_type = ThisType();
4258 LocalBlockStack().max_size = 1;
4259 StoragePool* ast_pool = compilation_unit -> ast_pool;
4260 TokenIndex loc = this_type -> declaration -> identifier_token;
4261
4262 // The symbol table associated with this block has one element, the
4263 // current instance 'this'.
4264 BlockSymbol* block_symbol = new BlockSymbol(1);
4265 block_symbol -> max_variable_index = 1;
4266
4267 // The combined block of the instance initializations. This will contain
4268 // each initializer block in sequence, and be inlined into constructors.
4269 AstMethodBody* block = ast_pool -> GenMethodBody();
4270 block -> left_brace_token = loc;
4271 block -> right_brace_token = loc;
4272 block -> block_symbol = block_symbol;
4273
4274 // The return type (void).
4275 AstType* return_type = ast_pool -> GenPrimitiveType(Ast::VOID_TYPE, loc);
4276 return_type -> symbol = control.void_type;
4277
4278 // The method declaration. We leave some fields uninitialized, because
4279 // they are not needed in bytecode.cpp.
4280 AstMethodDeclaration* declaration = ast_pool -> GenMethodDeclaration();
4281 MethodSymbol* init_method =
4282 this_type -> InsertMethodSymbol(control.block_init_name_symbol);
4283 declaration -> type = return_type;
4284 declaration -> method_symbol = init_method;
4285 declaration -> method_body_opt = block;
4286
4287 // The method symbol.
4288 init_method -> SetType(control.void_type);
4289 init_method -> SetFlags(AccessFlags::ACCESS_PRIVATE |
4290 AccessFlags::ACCESS_FINAL |
4291 AccessFlags::ACCESS_SYNTHETIC);
4292 if (this_type -> ACC_STRICTFP())
4293 init_method -> SetACC_STRICTFP();
4294 init_method -> SetContainingType(this_type);
4295 init_method -> SetBlockSymbol(block_symbol);
4296 init_method -> SetSignature(control);
4297 init_method -> declaration = declaration;
4298
4299 assert(this_type -> instance_initializer_method == NULL);
4300 this_type -> instance_initializer_method = init_method;
4301
4302 //
4303 // Make sure the instance final fields are properly set.
4304 //
4305 assert(FinalFields());
4306 for (unsigned i = 0; i < FinalFields() -> Length(); i++)
4307 {
4308 VariableSymbol* variable_symbol = (*FinalFields())[i];
4309 if (variable_symbol -> ACC_STATIC())
4310 {
4311 DefinitelyAssignedVariables() -> AssignElement(i);
4312 BlankFinals() -> RemoveElement(i);
4313 }
4314 else
4315 DefinitelyAssignedVariables() -> ReclaimElement(i);
4316 }
4317
4318 //
4319 // Work out how many statements we'll need.
4320 //
4321 unsigned estimate = class_body -> NumInstanceInitializers();
4322 for (unsigned i = 0; i < class_body -> NumInstanceVariables(); ++i)
4323 {
4324 estimate += class_body -> InstanceVariable(i) ->
4325 NumVariableDeclarators();
4326 }
4327 block -> AllocateStatements(estimate);
4328
4329 //
4330 // Initialization code is executed by every constructor that does not call
4331 // this(), just after the superclass constructor is called, in textual
4332 // order along with any instance variable initializations.
4333 //
4334 unsigned j = 0;
4335 unsigned k = 0;
4336 while (j < class_body -> NumInstanceVariables() &&
4337 k < class_body -> NumInstanceInitializers())
4338 {
4339 if (class_body -> InstanceVariable(j) -> semicolon_token <
4340 class_body -> InstanceInitializer(k) -> block -> right_brace_token)
4341 {
4342 InitializeVariable(class_body -> InstanceVariable(j++),
4343 init_method);
4344 }
4345 else
4346 {
4347 ProcessInitializer(class_body -> InstanceInitializer(k++),
4348 init_method);
4349 }
4350 }
4351 while (j < class_body -> NumInstanceVariables())
4352 {
4353 InitializeVariable(class_body -> InstanceVariable(j++), init_method);
4354 }
4355 while (k < class_body -> NumInstanceInitializers())
4356 {
4357 ProcessInitializer(class_body -> InstanceInitializer(k++),
4358 init_method);
4359 }
4360
4361 //
4362 // Note that unlike the case of static fields, we do not ensure here that
4363 // each final instance variable has been initialized at this point. This
4364 // is because the user may choose instead to initialize such a final
4365 // variable in every constructor instead. See body.cpp
4366 //
4367 init_method -> max_block_depth = LocalBlockStack().max_size;
4368 init_method -> block_symbol -> CompressSpace(); // space optimization
4369 }
4370
4371 #ifdef HAVE_JIKES_NAMESPACE
4372 } // Close namespace Jikes block
4373 #endif
4374
4375