home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / jikes-1.02 / src / decl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-01  |  186.3 KB  |  4,632 lines

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