stage3/array_range_check.cc
changeset 583 e1df3781be84
parent 581 1e158dc9f9c1
child 585 be7330d9b65c
equal deleted inserted replaced
582:8495119e7271 583:e1df3781be84
    92 
    92 
    93 	var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
    93 	var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
    94 	array_dimension_iterator_c array_dimension_iterator(var_decl);
    94 	array_dimension_iterator_c array_dimension_iterator(var_decl);
    95 	for (dimension_count = 0; NULL != array_dimension_iterator.next(); dimension_count++);
    95 	for (dimension_count = 0; NULL != array_dimension_iterator.next(); dimension_count++);
    96 	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
    96 	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
    97 		STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count);
    97 		STAGE3_ERROR(0, symbol, symbol, "Number of subscripts/indexes does not match the number of subscripts/indexes in the array's declaration (array has %d indexes)", dimension_count);
    98 }
    98 }
    99 
    99 
   100 void array_range_check_c::check_bounds(array_variable_c *symbol) {
   100 void array_range_check_c::check_bounds(array_variable_c *symbol) {
   101   list_c *l;
   101   list_c *l; /* the subscript_list */
   102   symbol_c *var_decl;
   102   symbol_c *var_decl;
   103 
   103 
   104   l = (list_c *)symbol->subscript_list;
   104   l = (list_c *)symbol->subscript_list;
   105   var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
   105   var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
   106   array_dimension_iterator_c array_dimension_iterator(var_decl);
   106   array_dimension_iterator_c array_dimension_iterator(var_decl);
   107   for (int i =  0; i < l->n; i++) {
   107   for (int i =  0; i < l->n; i++) {
   108     subrange_c *dimension = array_dimension_iterator.next();
   108     subrange_c *dimension = array_dimension_iterator.next();
   109     /* Index is not constant*/
   109     
   110     if (!VALID_CVALUE(int64, l->elements[i]))
   110     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
   111     	continue;
   111       if ( GET_CVALUE( int64, l->elements[i])   <  GET_CVALUE( int64, dimension->lower_limit))
   112     /* Limits must be constant */
   112       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   113     if (!VALID_CVALUE(int64,  dimension->lower_limit) ||!VALID_CVALUE(int64,  dimension->upper_limit))
       
   114         ERROR;
       
   115 
   113 
   116     if ( GET_CVALUE(int64, l->elements[i]) < GET_CVALUE(int64, dimension->lower_limit) ||
   114     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
   117     	 GET_CVALUE(int64, l->elements[i]) > GET_CVALUE(int64, dimension->upper_limit))
   115       if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
   118       STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds.");
   116       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
       
   117 
       
   118     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
       
   119       if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
       
   120       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
       
   121 
       
   122     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
       
   123       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
       
   124       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   119   }
   125   }
   120 }
   126 }
   121 
   127 
   122 /*********************/
   128 /*********************/
   123 /* B 1.4 - Variables */
   129 /* B 1.4 - Variables */