home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / decl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  184.8 KB  |  4,608 lines

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