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

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