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

  1. // $Id: init.cpp,v 1.12 2001/01/10 16:49:45 mdejong Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "platform.h"
  11. #include "semantic.h"
  12. #include "control.h"
  13.  
  14. #ifdef    HAVE_JIKES_NAMESPACE
  15. namespace Jikes {    // Open namespace Jikes block
  16. #endif
  17.  
  18. void Semantic::ProcessVariableInitializer(AstVariableDeclarator *variable_declarator)
  19. {
  20.     if (! variable_declarator -> variable_initializer_opt)
  21.         return;
  22.  
  23.     VariableSymbol *symbol = variable_declarator -> symbol;
  24.  
  25.     AstArrayInitializer *array_initializer = variable_declarator -> variable_initializer_opt -> ArrayInitializerCast();
  26.     if (array_initializer)
  27.     {
  28.         //
  29.         // TODO: This code is not needed
  30.         //
  31.         // REMOVE:
  32.         //
  33.         // This operation may throw OutOfMemoryError
  34.         //
  35.         // SymbolSet *exception_set = TryExceptionTableStack().Top();
  36.         // if (exception_set)
  37.         // {
  38.         //     exception_set -> AddElement(control.RuntimeException());
  39.         //     exception_set -> AddElement(control.Error());
  40.         // }
  41.  
  42.         ProcessArrayInitializer(array_initializer, symbol -> Type());
  43.     }
  44.     else
  45.     {
  46.         AstExpression *init = (AstExpression *) variable_declarator -> variable_initializer_opt;
  47.         ProcessExpressionOrStringConstant(init);
  48.  
  49.         if (symbol -> Type() != init -> Type() && init -> Type() != control.no_type)
  50.         {
  51.             if (CanAssignmentConvert(symbol -> Type(), init))
  52.             {
  53.                 init = ConvertToType(init, symbol -> Type());
  54.                 variable_declarator -> variable_initializer_opt = init;
  55.             }
  56.             else if (init -> IsConstant() && control.IsSimpleIntegerValueType(init -> Type())
  57.                                           && control.IsSimpleIntegerValueType(symbol -> Type()))
  58.             {
  59.                 if (symbol -> Type() == control.byte_type)
  60.                      ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  61.                                     init -> LeftToken(),
  62.                                     init -> RightToken());
  63.                 else if (symbol -> Type() == control.char_type)
  64.                      ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  65.                                     init -> LeftToken(),
  66.                                     init -> RightToken());
  67.                 else ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  68.                                     init -> LeftToken(),
  69.                                     init -> RightToken());
  70.             }
  71.             else
  72.             {
  73.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_ASSIGNMENT,
  74.                                variable_declarator -> LeftToken(),
  75.                                init -> RightToken(),
  76.                                symbol -> Type() -> ContainingPackage() -> PackageName(),
  77.                                symbol -> Type() -> ExternalName(),
  78.                                init -> Type() -> ContainingPackage() -> PackageName(),
  79.                                init -> Type() -> ExternalName());
  80.             }
  81.         }
  82.  
  83.         if (symbol -> ACC_FINAL() && init -> IsConstant())
  84.             symbol -> initial_value = init -> value;
  85.     }
  86.  
  87.     return;
  88. }
  89.  
  90.  
  91. void Semantic::ProcessArrayInitializer(AstArrayInitializer *array_initializer, TypeSymbol *type)
  92. {
  93.     if (! type -> IsArray())
  94.     {
  95.         ReportSemError(SemanticError::INIT_SCALAR_WITH_ARRAY,
  96.                        array_initializer -> LeftToken(),
  97.                        array_initializer -> RightToken(),
  98.                        type -> Name());
  99.     }
  100.     else
  101.     {
  102.         for (int i = 0; i < array_initializer -> NumVariableInitializers(); i++)
  103.         {
  104.             AstArrayInitializer *sub_array_initializer = array_initializer -> VariableInitializer(i) -> ArrayInitializerCast();
  105.             TypeSymbol *array_subtype = type -> ArraySubtype();
  106.             if (sub_array_initializer)
  107.                  ProcessArrayInitializer(sub_array_initializer, array_subtype);
  108.             else
  109.             {
  110.                 AstExpression *init = (AstExpression *) array_initializer -> VariableInitializer(i);
  111.                 ProcessExpressionOrStringConstant(init);
  112.  
  113.                 if (array_subtype != init -> Type())
  114.                 {
  115.                     if (CanAssignmentConvert(array_subtype, init))
  116.                         array_initializer -> VariableInitializer(i) = ConvertToType(init, array_subtype);
  117.                     else if (array_subtype -> IsArray() && init -> Type() -> Primitive())
  118.                     {
  119.                         ReportSemError(SemanticError::INIT_ARRAY_WITH_SCALAR,
  120.                                        init -> LeftToken(),
  121.                                        init -> RightToken(),
  122.                                        array_subtype -> Name());
  123.                     }
  124.                     else if (init -> IsConstant() && control.IsSimpleIntegerValueType(init -> Type())
  125.                                                   && control.IsSimpleIntegerValueType(array_subtype))
  126.                     {
  127.                         if (array_subtype == control.byte_type)
  128.                              ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  129.                                             init -> LeftToken(),
  130.                                             init -> RightToken());
  131.                         else if (array_subtype == control.char_type)
  132.                              ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  133.                                             init -> LeftToken(),
  134.                                             init -> RightToken());
  135.                         else ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  136.                                             init -> LeftToken(),
  137.                                             init -> RightToken());
  138.                     }
  139.                     else
  140.                     {
  141.                         ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_INITIALIZATION,
  142.                                        init -> LeftToken(),
  143.                                        init -> RightToken(),
  144.                                        array_subtype -> ContainingPackage() -> PackageName(),
  145.                                        array_subtype -> ExternalName(),
  146.                                        init -> Type() -> ContainingPackage() -> PackageName(),
  147.                                        init -> Type() -> ExternalName());
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154.     return;
  155. }
  156.  
  157.  
  158. LiteralValue *Semantic::ComputeFinalValue(AstVariableDeclarator *variable_declarator)
  159. {
  160.     LiteralValue *value = NULL;
  161.  
  162.     VariableSymbol *variable = variable_declarator -> symbol;
  163.     TypeSymbol *type = (TypeSymbol *) variable -> owner;
  164.  
  165.     state_stack.Push(type -> semantic_environment);
  166.     if (! error)
  167.         error = new SemanticError(control, source_file_symbol);
  168.     error -> EnteringClone();
  169.     variable_declarator -> pending = true;
  170.  
  171.     AstExpression *init_expr = (AstExpression *) variable_declarator -> variable_initializer_opt;
  172.     AstExpression *init_clone = (AstExpression *) init_expr -> Clone(compilation_unit -> ast_pool);
  173.     ProcessExpressionOrStringConstant(init_clone);
  174.     if (variable -> Type() != init_clone -> Type() && init_clone -> Type() != control.no_type)
  175.     {
  176.         if (CanAssignmentConvert(variable -> Type(), init_clone))
  177.              init_clone = ConvertToType(init_clone, variable -> Type());
  178.         else init_clone -> value = NULL;
  179.     }
  180.     value = init_clone -> value;
  181.  
  182. //
  183. // STG:
  184. //        delete init_clone; // destroy the clone
  185. //
  186.  
  187.     variable_declarator -> pending = false;
  188.     error -> ExitingClone();
  189.     state_stack.Pop();
  190.  
  191.     return value;
  192. }
  193.  
  194. #ifdef    HAVE_JIKES_NAMESPACE
  195. }            // Close namespace Jikes block
  196. #endif
  197.  
  198.