stage3/narrow_candidate_datatypes.cc
changeset 417 d48f53715f77
child 421 840cb1e1e177
equal deleted inserted replaced
416:0c2ef191b22a 417:d48f53715f77
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2012       Manuele Conti (manuele.conti@sirius-es.it)
       
     6  *  Copyright (C) 2012       Matteo Facchinetti (matteo.facchinetti@sirius-es.it)
       
     7  *
       
     8  *  This program is free software: you can redistribute it and/or modify
       
     9  *  it under the terms of the GNU General Public License as published by
       
    10  *  the Free Software Foundation, either version 3 of the License, or
       
    11  *  (at your option) any later version.
       
    12  *
       
    13  *  This program is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16  *  GNU General Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License
       
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    20  *
       
    21  *
       
    22  * This code is made available on the understanding that it will not be
       
    23  * used in safety-critical situations without a full and competent review.
       
    24  */
       
    25 
       
    26 /*
       
    27  * An IEC 61131-3 compiler.
       
    28  *
       
    29  * Based on the
       
    30  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    31  *
       
    32  */
       
    33 
       
    34 
       
    35 /*
       
    36  *  Narrow class select and store a data type from candidate data types list for all symbols
       
    37  */
       
    38 
       
    39 #include "narrow_candidate_datatypes.hh"
       
    40 #include "datatype_functions.hh"
       
    41 #include <typeinfo>
       
    42 #include <list>
       
    43 #include <string>
       
    44 #include <string.h>
       
    45 #include <strings.h>
       
    46 
       
    47 
       
    48 /* set to 1 to see debug info during execution */
       
    49 static int debug = 0;
       
    50 
       
    51 narrow_candidate_datatypes_c::narrow_candidate_datatypes_c(symbol_c *ignore) {
       
    52 }
       
    53 
       
    54 narrow_candidate_datatypes_c::~narrow_candidate_datatypes_c(void) {
       
    55 }
       
    56 
       
    57 bool narrow_candidate_datatypes_c::is_widening_compatible(symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, const struct widen_entry widen_table[]) {
       
    58 	for (int k = 0; NULL != widen_table[k].left;  k++) {
       
    59 		if        ((typeid(*left_type)   == typeid(*widen_table[k].left))
       
    60 		        && (typeid(*right_type)  == typeid(*widen_table[k].right))
       
    61 				&& (typeid(*result_type) == typeid(*widen_table[k].result))) {
       
    62 			return true;
       
    63 		}
       
    64 	}
       
    65 	return false;
       
    66 }
       
    67 
       
    68 void narrow_candidate_datatypes_c::narrow_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
       
    69 	symbol_c *call_param_value,  *param_type;
       
    70 	identifier_c *param_name;
       
    71 	function_param_iterator_c       fp_iterator(f_decl);
       
    72 	function_call_param_iterator_c fcp_iterator(f_call);
       
    73 	int extensible_parameter_highest_index = -1;
       
    74 	identifier_c *extensible_parameter_name;
       
    75 	unsigned int i;
       
    76 
       
    77 
       
    78 	/* Iterating through the non-formal parameters of the function call */
       
    79 	while((call_param_value = fcp_iterator.next_nf()) != NULL) {
       
    80 		/* Obtaining the type of the value being passed in the function call */
       
    81 		/* Iterate to the next parameter of the function being called.
       
    82 		 * Get the name of that parameter, and ignore if EN or ENO.
       
    83 		 */
       
    84 		do {
       
    85 			param_name = fp_iterator.next();
       
    86 			/* If there is no other parameter declared, then we are passing too many parameters... */
       
    87 			if(param_name == NULL) {
       
    88 				return;
       
    89 			}
       
    90 		} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
       
    91 
       
    92 		/* Get the parameter type */
       
    93 		call_param_value->datatype = base_type(fp_iterator.param_type());
       
    94 		call_param_value->accept(*this);
       
    95 		if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
    96 			extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
    97 			extensible_parameter_name = param_name;
       
    98 		}
       
    99 	}
       
   100     int extensible_param_count = -1;
       
   101     if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   102       extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   103     function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   104     if (function_invocation  != NULL) function_invocation->extensible_param_count = extensible_param_count;
       
   105 
       
   106 }
       
   107 
       
   108 void narrow_candidate_datatypes_c::narrow_formal_call(symbol_c *f_call, symbol_c *f_decl) {
       
   109 	symbol_c *call_param_value, *call_param_name, *param_type;
       
   110 	symbol_c *verify_duplicate_param;
       
   111 	identifier_c *param_name;
       
   112 	function_param_iterator_c       fp_iterator(f_decl);
       
   113 	function_call_param_iterator_c fcp_iterator(f_call);
       
   114 	int extensible_parameter_highest_index = -1;
       
   115 	identifier_c *extensible_parameter_name;
       
   116 	unsigned int i;
       
   117 
       
   118 
       
   119 	/* Iterating through the formal parameters of the function call */
       
   120 	while((call_param_name = fcp_iterator.next_f()) != NULL) {
       
   121 
       
   122 		/* Obtaining the value being passed in the function call */
       
   123 		call_param_value = fcp_iterator.get_current_value();
       
   124 		/* the following should never occur. If it does, then we have a bug in our code... */
       
   125 		if (NULL == call_param_value) ERROR;
       
   126 
       
   127 		/* Find the corresponding parameter in function declaration */
       
   128 		param_name = fp_iterator.search(call_param_name);
       
   129 
       
   130 		/* Get the parameter type */
       
   131 		call_param_name->datatype = base_type(fp_iterator.param_type());
       
   132 		call_param_name->accept(*this);
       
   133 	    /* the first parameter (il_def_variable) is correct */
       
   134 	    if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   135 	      extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   136 	    }
       
   137 	}
       
   138 	/* The function call may not have any errors! */
       
   139 	/* In the case of a call to an extensible function, we store the highest index
       
   140 	 * of the extensible parameters this particular call uses, in the symbol_c object
       
   141 	 * of the function call itself!
       
   142 	 * In calls to non-extensible functions, this value will be set to -1.
       
   143 	 * This information is later used in stage4 to correctly generate the
       
   144 	 * output code.
       
   145 	 */
       
   146 	int extensible_param_count = -1;
       
   147 	if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   148 		extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   149 	function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   150 	if (function_invocation  != NULL) function_invocation->extensible_param_count = extensible_param_count;
       
   151 }
       
   152 
       
   153 /* a helper function... */
       
   154 symbol_c *narrow_candidate_datatypes_c::base_type(symbol_c *symbol) {
       
   155 	/* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used
       
   156 	 *       in the code.
       
   157 	 */
       
   158 	return NULL;
       
   159 }
       
   160 
       
   161 /*********************/
       
   162 /* B 1.2 - Constants */
       
   163 /*********************/
       
   164 
       
   165 /**********************/
       
   166 /* B 1.3 - Data types */
       
   167 /**********************/
       
   168 /********************************/
       
   169 /* B 1.3.3 - Derived data types */
       
   170 /********************************/
       
   171 /*  signed_integer DOTDOT signed_integer */
       
   172 // SYM_REF2(subrange_c, lower_limit, upper_limit)
       
   173 void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) {
       
   174 	symbol->lower_limit->datatype = symbol->datatype;
       
   175 	symbol->lower_limit->accept(*this);
       
   176 	symbol->upper_limit->datatype = symbol->datatype;
       
   177 	symbol->upper_limit->accept(*this);
       
   178 	return NULL;
       
   179 }
       
   180 
       
   181 
       
   182 /*********************/
       
   183 /* B 1.4 - Variables */
       
   184 /*********************/
       
   185 
       
   186 /********************************************/
       
   187 /* B 1.4.1 - Directly Represented Variables */
       
   188 /********************************************/
       
   189 
       
   190 /*************************************/
       
   191 /* B 1.4.2 - Multi-element variables */
       
   192 /*************************************/
       
   193 /*  subscripted_variable '[' subscript_list ']' */
       
   194 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
       
   195 void *narrow_candidate_datatypes_c::visit(array_variable_c *symbol) {
       
   196 	/* we need to check the data types of the expressions used for the subscripts... */
       
   197 	symbol->subscript_list->accept(*this);
       
   198 	return NULL;
       
   199 }
       
   200 
       
   201 
       
   202 /* subscript_list ',' subscript */
       
   203 // SYM_LIST(subscript_list_c)
       
   204 void *narrow_candidate_datatypes_c::visit(subscript_list_c *symbol) {
       
   205 	for (int i = 0; i < symbol->n; i++) {
       
   206 		for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) {
       
   207 			if (is_ANY_INT_type(symbol->elements[i]->candidate_datatypes[k]))
       
   208 				symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k];
       
   209 		}
       
   210 		symbol->elements[i]->accept(*this);
       
   211 	}
       
   212 	return NULL;  
       
   213 }
       
   214 
       
   215 
       
   216 
       
   217 /************************************/
       
   218 /* B 1.5 Program organization units */
       
   219 /************************************/
       
   220 /*********************/
       
   221 /* B 1.5.1 Functions */
       
   222 /*********************/
       
   223 void *narrow_candidate_datatypes_c::visit(function_declaration_c *symbol) {
       
   224 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   225 	if (debug) printf("Narrowing candidate data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
       
   226 	prev_il_instruction = NULL;
       
   227 	symbol->function_body->accept(*this);
       
   228 	prev_il_instruction = NULL;
       
   229 	delete search_varfb_instance_type;
       
   230 	search_varfb_instance_type = NULL;
       
   231 	return NULL;
       
   232 }
       
   233 
       
   234 /***************************/
       
   235 /* B 1.5.2 Function blocks */
       
   236 /***************************/
       
   237 void *narrow_candidate_datatypes_c::visit(function_block_declaration_c *symbol) {
       
   238 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   239 	if (debug) printf("Narrowing candidate data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
       
   240 	prev_il_instruction = NULL;
       
   241 	symbol->fblock_body->accept(*this);
       
   242 	prev_il_instruction = NULL;
       
   243 	delete search_varfb_instance_type;
       
   244 	search_varfb_instance_type = NULL;
       
   245 	return NULL;
       
   246 }
       
   247 
       
   248 /********************/
       
   249 /* B 1.5.3 Programs */
       
   250 /********************/
       
   251 void *narrow_candidate_datatypes_c::visit(program_declaration_c *symbol) {
       
   252 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   253 	if (debug) printf("Narrowing candidate data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value);
       
   254 	prev_il_instruction = NULL;
       
   255 	symbol->function_block_body->accept(*this);
       
   256 	prev_il_instruction = NULL;
       
   257 	delete search_varfb_instance_type;
       
   258 	search_varfb_instance_type = NULL;
       
   259 	return NULL;
       
   260 }
       
   261 
       
   262 
       
   263 /********************************/
       
   264 /* B 1.7 Configuration elements */
       
   265 /********************************/
       
   266 void *narrow_candidate_datatypes_c::visit(configuration_declaration_c *symbol) {
       
   267 #if 0
       
   268 	// TODO !!!
       
   269 	/* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
       
   270 #endif
       
   271 	return NULL;
       
   272 }
       
   273 
       
   274 
       
   275 /****************************************/
       
   276 /* B.2 - Language IL (Instruction List) */
       
   277 /****************************************/
       
   278 /***********************************/
       
   279 /* B 2.1 Instructions and Operands */
       
   280 /***********************************/
       
   281 // void *visit(instruction_list_c *symbol);
       
   282 void *narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol) {
       
   283 	il_operand = symbol->il_operand;
       
   284 	if (NULL != symbol->il_operand) {
       
   285 		symbol->il_operand->accept(*this);
       
   286 	}
       
   287 	/* recursive call to see whether data types are compatible */
       
   288 	symbol->il_simple_operator->accept(*this);
       
   289 	il_operand = NULL;
       
   290 	return NULL;
       
   291 }
       
   292 
       
   293 void *narrow_candidate_datatypes_c::visit(il_function_call_c *symbol) {
       
   294 	return NULL;
       
   295 }
       
   296 
       
   297 /* MJS: Manuele, could you please not delete the following 2 lines of comments. They help me understand where this class is used
       
   298  *     and when it is created by bison - syntax parse, and how it can show up in the abstract syntax tree.
       
   299  *
       
   300  *       Actually, it could be helpful if we could have all the similar comments already present in visit_expression_type_c
       
   301  *       in the 3 new classes fill/narrow/print candidate datatype 
       
   302  */
       
   303 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
   304 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
       
   305 void *narrow_candidate_datatypes_c::visit(il_expression_c *symbol) {
       
   306 /* MJS: TODO... */
       
   307 return NULL;
       
   308 }
       
   309 
       
   310 void *narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol) {
       
   311 	return NULL;
       
   312 }
       
   313 
       
   314 void *narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) {
       
   315 	return NULL;
       
   316 }
       
   317 
       
   318 
       
   319 /*
       
   320     void *visit(il_operand_list_c *symbol);
       
   321     void *visit(simple_instr_list_c *symbol);
       
   322     void *visit(il_param_list_c *symbol);
       
   323     void *visit(il_param_assignment_c *symbol);
       
   324     void *visit(il_param_out_assignment_c *symbol);
       
   325  */
       
   326 
       
   327 /*******************/
       
   328 /* B 2.2 Operators */
       
   329 /*******************/
       
   330 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol) {
       
   331 	prev_il_instruction = symbol;
       
   332 	return NULL;
       
   333 }
       
   334 
       
   335 void *narrow_candidate_datatypes_c::visit(LDN_operator_c *symbol) {
       
   336 	if (symbol->candidate_datatypes.size() != 1)
       
   337 		return NULL;
       
   338 	symbol->datatype = symbol->candidate_datatypes[0];
       
   339 	il_operand->datatype = symbol->datatype;
       
   340 	il_operand->accept(*this);
       
   341 	prev_il_instruction = symbol;
       
   342 	return NULL;
       
   343 }
       
   344 
       
   345 void *narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) {
       
   346 	if (symbol->candidate_datatypes.size() != 1)
       
   347 		return NULL;
       
   348 	symbol->datatype = symbol->candidate_datatypes[0];
       
   349 	il_operand->datatype = symbol->datatype;
       
   350 	il_operand->accept(*this);
       
   351 	prev_il_instruction = symbol;
       
   352 	return NULL;
       
   353 }
       
   354 
       
   355 void *narrow_candidate_datatypes_c::visit(STN_operator_c *symbol) {
       
   356 	if (symbol->candidate_datatypes.size() != 1)
       
   357 		return NULL;
       
   358 	symbol->datatype = symbol->candidate_datatypes[0];
       
   359 	il_operand->datatype = symbol->datatype;
       
   360 	il_operand->accept(*this);
       
   361 	prev_il_instruction = symbol;
       
   362 	return NULL;
       
   363 }
       
   364 
       
   365 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
       
   366 	prev_il_instruction = symbol;
       
   367 	return NULL;
       
   368 }
       
   369 
       
   370 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol) {
       
   371 	if (symbol->candidate_datatypes.size() != 1)
       
   372 		return NULL;
       
   373 	symbol->datatype = symbol->candidate_datatypes[0];
       
   374 	il_operand->datatype = symbol->datatype;
       
   375 	il_operand->accept(*this);
       
   376 	prev_il_instruction = symbol;
       
   377 	return NULL;
       
   378 }
       
   379 
       
   380 void *narrow_candidate_datatypes_c::visit(R_operator_c *symbol) {
       
   381 	if (symbol->candidate_datatypes.size() != 1)
       
   382 		return NULL;
       
   383 	symbol->datatype = symbol->candidate_datatypes[0];
       
   384 	il_operand->datatype = symbol->datatype;
       
   385 	il_operand->accept(*this);
       
   386 	prev_il_instruction = symbol;
       
   387 	return NULL;
       
   388 }
       
   389 
       
   390 void *narrow_candidate_datatypes_c::visit(S1_operator_c *symbol) {
       
   391 	if (symbol->candidate_datatypes.size() != 1)
       
   392 		return NULL;
       
   393 	symbol->datatype = symbol->candidate_datatypes[0];
       
   394 	il_operand->datatype = symbol->datatype;
       
   395 	il_operand->accept(*this);
       
   396 	prev_il_instruction = symbol;
       
   397 	return NULL;
       
   398 }
       
   399 
       
   400 void *narrow_candidate_datatypes_c::visit(R1_operator_c *symbol) {
       
   401 	if (symbol->candidate_datatypes.size() != 1)
       
   402 		return NULL;
       
   403 	symbol->datatype = symbol->candidate_datatypes[0];
       
   404 	il_operand->datatype = symbol->datatype;
       
   405 	il_operand->accept(*this);
       
   406 	prev_il_instruction = symbol;
       
   407 	return NULL;
       
   408 }
       
   409 
       
   410 void *narrow_candidate_datatypes_c::visit(CLK_operator_c *symbol) {
       
   411 	prev_il_instruction = symbol;
       
   412 	return NULL;
       
   413 }
       
   414 
       
   415 void *narrow_candidate_datatypes_c::visit(CU_operator_c *symbol) {
       
   416 	prev_il_instruction = symbol;
       
   417 	return NULL;
       
   418 }
       
   419 
       
   420 void *narrow_candidate_datatypes_c::visit(CD_operator_c *symbol) {
       
   421 	prev_il_instruction = symbol;
       
   422 	return NULL;
       
   423 }
       
   424 
       
   425 void *narrow_candidate_datatypes_c::visit(PV_operator_c *symbol) {
       
   426 	prev_il_instruction = symbol;
       
   427 	return NULL;
       
   428 }
       
   429 
       
   430 void *narrow_candidate_datatypes_c::visit(IN_operator_c *symbol) {
       
   431 	prev_il_instruction = symbol;
       
   432 	return NULL;
       
   433 }
       
   434 
       
   435 void *narrow_candidate_datatypes_c::visit(PT_operator_c *symbol) {
       
   436 	prev_il_instruction = symbol;
       
   437 	return NULL;
       
   438 }
       
   439 
       
   440 void *narrow_candidate_datatypes_c::visit(AND_operator_c *symbol) {
       
   441 	if (symbol->candidate_datatypes.size() != 1)
       
   442 		return NULL;
       
   443 	symbol->datatype = symbol->candidate_datatypes[0];
       
   444 	il_operand->datatype = symbol->datatype;
       
   445 	il_operand->accept(*this);
       
   446 	prev_il_instruction = symbol;
       
   447 	return NULL;
       
   448 }
       
   449 
       
   450 void *narrow_candidate_datatypes_c::visit(OR_operator_c *symbol) {
       
   451 	if (symbol->candidate_datatypes.size() != 1)
       
   452 		return NULL;
       
   453 	symbol->datatype = symbol->candidate_datatypes[0];
       
   454 	il_operand->datatype = symbol->datatype;
       
   455 	il_operand->accept(*this);
       
   456 	prev_il_instruction = symbol;
       
   457 	return NULL;
       
   458 }
       
   459 
       
   460 void *narrow_candidate_datatypes_c::visit(XOR_operator_c *symbol) {
       
   461 	if (symbol->candidate_datatypes.size() != 1)
       
   462 		return NULL;
       
   463 	symbol->datatype = symbol->candidate_datatypes[0];
       
   464 	il_operand->datatype = symbol->datatype;
       
   465 	il_operand->accept(*this);
       
   466 	prev_il_instruction = symbol;
       
   467 	return NULL;
       
   468 }
       
   469 
       
   470 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {
       
   471 	if (symbol->candidate_datatypes.size() != 1)
       
   472 		return NULL;
       
   473 	symbol->datatype = symbol->candidate_datatypes[0];
       
   474 	il_operand->datatype = symbol->datatype;
       
   475 	il_operand->accept(*this);
       
   476 	prev_il_instruction = symbol;
       
   477 	return NULL;
       
   478 }
       
   479 
       
   480 void *narrow_candidate_datatypes_c::visit(ORN_operator_c *symbol) {
       
   481 	if (symbol->candidate_datatypes.size() != 1)
       
   482 		return NULL;
       
   483 	symbol->datatype = symbol->candidate_datatypes[0];
       
   484 	il_operand->datatype = symbol->datatype;
       
   485 	il_operand->accept(*this);
       
   486 	prev_il_instruction = symbol;
       
   487 	return NULL;
       
   488 }
       
   489 
       
   490 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol) {
       
   491 	if (symbol->candidate_datatypes.size() != 1)
       
   492 		return NULL;
       
   493 	symbol->datatype = symbol->candidate_datatypes[0];
       
   494 	il_operand->datatype = symbol->datatype;
       
   495 	il_operand->accept(*this);
       
   496 	prev_il_instruction = symbol;
       
   497 	return NULL;
       
   498 }
       
   499 
       
   500 void *narrow_candidate_datatypes_c::visit(ADD_operator_c *symbol) {
       
   501 	prev_il_instruction = symbol;
       
   502 	return NULL;
       
   503 }
       
   504 
       
   505 void *narrow_candidate_datatypes_c::visit(SUB_operator_c *symbol) {
       
   506 	prev_il_instruction = symbol;
       
   507 	return NULL;
       
   508 }
       
   509 
       
   510 void *narrow_candidate_datatypes_c::visit(MUL_operator_c *symbol) {
       
   511 	prev_il_instruction = symbol;
       
   512 	return NULL;
       
   513 }
       
   514 
       
   515 void *narrow_candidate_datatypes_c::visit(DIV_operator_c *symbol) {
       
   516 	prev_il_instruction = symbol;
       
   517 	return NULL;
       
   518 }
       
   519 
       
   520 void *narrow_candidate_datatypes_c::visit(MOD_operator_c *symbol) {
       
   521 	prev_il_instruction = symbol;
       
   522 	return NULL;
       
   523 }
       
   524 
       
   525 void *narrow_candidate_datatypes_c::visit(GT_operator_c *symbol) {
       
   526 	prev_il_instruction = symbol;
       
   527 	return NULL;
       
   528 }
       
   529 
       
   530 void *narrow_candidate_datatypes_c::visit(GE_operator_c *symbol) {
       
   531 	prev_il_instruction = symbol;
       
   532 	return NULL;
       
   533 }
       
   534 
       
   535 void *narrow_candidate_datatypes_c::visit(EQ_operator_c *symbol) {
       
   536 	prev_il_instruction = symbol;
       
   537 	return NULL;
       
   538 }
       
   539 
       
   540 void *narrow_candidate_datatypes_c::visit(LT_operator_c *symbol) {
       
   541 	prev_il_instruction = symbol;
       
   542 	return NULL;
       
   543 }
       
   544 
       
   545 void *narrow_candidate_datatypes_c::visit(LE_operator_c *symbol) {
       
   546 	prev_il_instruction = symbol;
       
   547 	return NULL;
       
   548 }
       
   549 
       
   550 void *narrow_candidate_datatypes_c::visit(NE_operator_c *symbol) {
       
   551 	prev_il_instruction = symbol;
       
   552 	return NULL;
       
   553 }
       
   554 
       
   555 void *narrow_candidate_datatypes_c::visit(CAL_operator_c *symbol) {
       
   556 	prev_il_instruction = symbol;
       
   557 	return NULL;
       
   558 }
       
   559 
       
   560 void *narrow_candidate_datatypes_c::visit(CALC_operator_c *symbol) {
       
   561 	prev_il_instruction = symbol;
       
   562 	return NULL;
       
   563 }
       
   564 
       
   565 void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {
       
   566 	prev_il_instruction = symbol;
       
   567 	return NULL;
       
   568 }
       
   569 
       
   570 void *narrow_candidate_datatypes_c::visit(RET_operator_c *symbol) {
       
   571 	prev_il_instruction = symbol;
       
   572 	return NULL;
       
   573 }
       
   574 
       
   575 void *narrow_candidate_datatypes_c::visit(RETC_operator_c *symbol) {
       
   576 	prev_il_instruction = symbol;
       
   577 	return NULL;
       
   578 }
       
   579 
       
   580 void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {
       
   581 	prev_il_instruction = symbol;
       
   582 	return NULL;
       
   583 }
       
   584 
       
   585 void *narrow_candidate_datatypes_c::visit(JMP_operator_c *symbol) {
       
   586 	prev_il_instruction = symbol;
       
   587 	return NULL;
       
   588 }
       
   589 
       
   590 void *narrow_candidate_datatypes_c::visit(JMPC_operator_c *symbol) {
       
   591 	prev_il_instruction = symbol;
       
   592 	return NULL;
       
   593 }
       
   594 
       
   595 void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {
       
   596 	prev_il_instruction = symbol;
       
   597 	return NULL;
       
   598 }
       
   599 
       
   600 /* Symbol class handled together with function call checks */
       
   601 // void *visit(il_assign_operator_c *symbol, variable_name);
       
   602 /* Symbol class handled together with function call checks */
       
   603 // void *visit(il_assign_operator_c *symbol, option, variable_name);
       
   604 
       
   605 
       
   606 /***************************************/
       
   607 /* B.3 - Language ST (Structured Text) */
       
   608 /***************************************/
       
   609 /***********************/
       
   610 /* B 3.1 - Expressions */
       
   611 /***********************/
       
   612 
       
   613 void *narrow_candidate_datatypes_c::visit(or_expression_c *symbol) {
       
   614 	symbol_c * selected_type = NULL;
       
   615 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   616 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   617 			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) {
       
   618 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   619 				break;
       
   620 			}
       
   621 		}
       
   622 	}
       
   623 
       
   624 	if (NULL != selected_type) {
       
   625 		symbol->l_exp->datatype = selected_type;
       
   626 		symbol->l_exp->accept(*this);
       
   627 		symbol->r_exp->datatype = selected_type;
       
   628 		symbol->r_exp->accept(*this);
       
   629 	}
       
   630 	else
       
   631 		ERROR;
       
   632 	return NULL;
       
   633 }
       
   634 
       
   635 
       
   636 void *narrow_candidate_datatypes_c::visit(xor_expression_c *symbol) {
       
   637 	symbol_c * selected_type = NULL;
       
   638 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   639 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   640 			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) {
       
   641 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   642 				break;
       
   643 			}
       
   644 		}
       
   645 	}
       
   646 
       
   647 	if (NULL != selected_type) {
       
   648 		symbol->l_exp->datatype = selected_type;
       
   649 		symbol->l_exp->accept(*this);
       
   650 		symbol->r_exp->datatype = selected_type;
       
   651 		symbol->r_exp->accept(*this);
       
   652 	}
       
   653 	else
       
   654 		ERROR;
       
   655 	return NULL;
       
   656 }
       
   657 
       
   658 
       
   659 void *narrow_candidate_datatypes_c::visit(and_expression_c *symbol) {
       
   660 	symbol_c * selected_type = NULL;
       
   661 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   662 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   663 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) {
       
   664 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   665 				break;
       
   666 			}
       
   667 		}
       
   668 	}
       
   669 
       
   670 	if (NULL != selected_type) {
       
   671 		symbol->l_exp->datatype = selected_type;
       
   672 		symbol->l_exp->accept(*this);
       
   673 		symbol->r_exp->datatype = selected_type;
       
   674 		symbol->r_exp->accept(*this);
       
   675 	}
       
   676 	else
       
   677 		ERROR;
       
   678 	return NULL;
       
   679 }
       
   680 
       
   681 
       
   682 void *narrow_candidate_datatypes_c::visit(equ_expression_c *symbol) {
       
   683 	/* Here symbol->datatype has already assigned to BOOL
       
   684 	 * In conditional symbols like =, <>, =<, <, >, >= we have to set
       
   685 	 * l_exp and r_exp expression matched with compatible type.
       
   686 	 * Example:
       
   687 	 * 		INT#14 = INT#81
       
   688 	 * 		equ_expression_c symbol->datatype = BOOL from top visit
       
   689 	 * 		symbol->l_exp->datatype => INT
       
   690 	 * 		symbol->r_exp->datatype => INT
       
   691 	 */
       
   692 	symbol_c * selected_type = NULL;
       
   693 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   694 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   695 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) {
       
   696 				/*
       
   697 				 * We do not need to check whether the type is an ANY_ELEMENTARY here.
       
   698 				 * That was already done in fill_candidate_datatypes_c.
       
   699 				 */
       
   700 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   701 				break;
       
   702 			}
       
   703 		}
       
   704 	}
       
   705 
       
   706 	if (NULL != selected_type) {
       
   707 		symbol->l_exp->datatype = selected_type;
       
   708 		symbol->l_exp->accept(*this);
       
   709 		symbol->r_exp->datatype = selected_type;
       
   710 		symbol->r_exp->accept(*this);
       
   711 	}
       
   712 	else
       
   713 		ERROR;
       
   714 	return NULL;
       
   715 }
       
   716 
       
   717 void *narrow_candidate_datatypes_c::visit(notequ_expression_c *symbol)  {
       
   718 	symbol_c * selected_type = NULL;
       
   719 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   720 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   721 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) {
       
   722 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   723 				break;
       
   724 			}
       
   725 		}
       
   726 	}
       
   727 
       
   728 	if (NULL != selected_type) {
       
   729 		symbol->l_exp->datatype = selected_type;
       
   730 		symbol->l_exp->accept(*this);
       
   731 		symbol->r_exp->datatype = selected_type;
       
   732 		symbol->r_exp->accept(*this);
       
   733 	}
       
   734 	else
       
   735 		ERROR;
       
   736 	return NULL;
       
   737 }
       
   738 
       
   739 
       
   740 void *narrow_candidate_datatypes_c::visit(lt_expression_c *symbol) {
       
   741 	symbol_c * selected_type = NULL;
       
   742 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   743 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   744 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])
       
   745 					&& is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) {
       
   746 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   747 				break;
       
   748 			}
       
   749 		}
       
   750 	}
       
   751 
       
   752 	if (NULL != selected_type) {
       
   753 		symbol->l_exp->datatype = selected_type;
       
   754 		symbol->l_exp->accept(*this);
       
   755 		symbol->r_exp->datatype = selected_type;
       
   756 		symbol->r_exp->accept(*this);
       
   757 	}
       
   758 	else
       
   759 		ERROR;
       
   760 	return NULL;
       
   761 }
       
   762 
       
   763 
       
   764 void *narrow_candidate_datatypes_c::visit(gt_expression_c *symbol) {
       
   765 	symbol_c * selected_type = NULL;
       
   766 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   767 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   768 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])
       
   769 					&& is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) {
       
   770 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   771 				break;
       
   772 			}
       
   773 		}
       
   774 	}
       
   775 
       
   776 	if (NULL != selected_type) {
       
   777 		symbol->l_exp->datatype = selected_type;
       
   778 		symbol->l_exp->accept(*this);
       
   779 		symbol->r_exp->datatype = selected_type;
       
   780 		symbol->r_exp->accept(*this);
       
   781 	}
       
   782 	else
       
   783 		ERROR;
       
   784 	return NULL;
       
   785 }
       
   786 
       
   787 
       
   788 void *narrow_candidate_datatypes_c::visit(le_expression_c *symbol) {
       
   789 	symbol_c * selected_type = NULL;
       
   790 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   791 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   792 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])
       
   793 					&& is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) {
       
   794 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   795 				break;
       
   796 			}
       
   797 		}
       
   798 	}
       
   799 
       
   800 	if (NULL != selected_type) {
       
   801 		symbol->l_exp->datatype = selected_type;
       
   802 		symbol->l_exp->accept(*this);
       
   803 		symbol->r_exp->datatype = selected_type;
       
   804 		symbol->r_exp->accept(*this);
       
   805 	}
       
   806 	else
       
   807 		ERROR;
       
   808 	return NULL;
       
   809 }
       
   810 
       
   811 
       
   812 void *narrow_candidate_datatypes_c::visit(ge_expression_c *symbol) {
       
   813 	symbol_c * selected_type = NULL;
       
   814 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   815 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   816 			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])
       
   817 					&& is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) {
       
   818 				selected_type = symbol->l_exp->candidate_datatypes[i];
       
   819 				break;
       
   820 			}
       
   821 		}
       
   822 	}
       
   823 
       
   824 	if (NULL != selected_type) {
       
   825 		symbol->l_exp->datatype = selected_type;
       
   826 		symbol->l_exp->accept(*this);
       
   827 		symbol->r_exp->datatype = selected_type;
       
   828 		symbol->r_exp->accept(*this);
       
   829 	}
       
   830 	else
       
   831 		ERROR;
       
   832 	return NULL;
       
   833 }
       
   834 
       
   835 void *narrow_candidate_datatypes_c::visit(add_expression_c *symbol) {
       
   836 	int count = 0;
       
   837 
       
   838 	if (is_ANY_NUM_compatible(symbol->datatype)) {
       
   839 		symbol->l_exp->datatype = symbol->datatype;
       
   840 		symbol->r_exp->datatype = symbol->datatype;
       
   841 		count++;
       
   842 	} else {
       
   843 		/* TIME data type */
       
   844 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   845 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   846 				/* test widening compatibility */
       
   847 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
       
   848 						symbol->r_exp->candidate_datatypes[j],
       
   849 						symbol->datatype, widen_ADD_table)) {
       
   850 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
   851 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
   852 					count ++;
       
   853 				}
       
   854 			}
       
   855 		}
       
   856 	}
       
   857 	if (count > 1)
       
   858 		ERROR;
       
   859 	symbol->l_exp->accept(*this);
       
   860 	symbol->r_exp->accept(*this);
       
   861 	return NULL;
       
   862 }
       
   863 
       
   864 
       
   865 
       
   866 void *narrow_candidate_datatypes_c::visit(sub_expression_c *symbol) {
       
   867 	int count = 0;
       
   868 
       
   869 	if (is_ANY_NUM_compatible(symbol->datatype)) {
       
   870 		symbol->l_exp->datatype = symbol->datatype;
       
   871 		symbol->r_exp->datatype = symbol->datatype;
       
   872 		count++;
       
   873 	} else {
       
   874 		/* TIME data type */
       
   875 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   876 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   877 				/* test widening compatibility */
       
   878 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
       
   879 						symbol->r_exp->candidate_datatypes[j],
       
   880 						symbol->datatype, widen_SUB_table)) {
       
   881 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
   882 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
   883 					count ++;
       
   884 				}
       
   885 			}
       
   886 		}
       
   887 	}
       
   888 	if (count > 1)
       
   889 		ERROR;
       
   890 	symbol->l_exp->accept(*this);
       
   891 	symbol->r_exp->accept(*this);
       
   892 	return NULL;
       
   893 }
       
   894 
       
   895 void *narrow_candidate_datatypes_c::visit(mul_expression_c *symbol) {
       
   896 	int count = 0;
       
   897 
       
   898 	if (is_ANY_NUM_compatible(symbol->datatype)) {
       
   899 		symbol->l_exp->datatype = symbol->datatype;
       
   900 		symbol->r_exp->datatype = symbol->datatype;
       
   901 		count++;
       
   902 	} else {
       
   903 		/* TIME data type */
       
   904 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   905 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   906 				/* test widening compatibility */
       
   907 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
       
   908 						symbol->r_exp->candidate_datatypes[j],
       
   909 						symbol->datatype, widen_MUL_table)) {
       
   910 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
   911 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
   912 					count ++;
       
   913 				}
       
   914 			}
       
   915 		}
       
   916 	}
       
   917 	if (count > 1)
       
   918 		ERROR;
       
   919 	symbol->l_exp->accept(*this);
       
   920 	symbol->r_exp->accept(*this);
       
   921 	return NULL;
       
   922 }
       
   923 
       
   924 void *narrow_candidate_datatypes_c::visit(div_expression_c *symbol) {
       
   925 	int count = 0;
       
   926 
       
   927 	if (is_ANY_NUM_compatible(symbol->datatype)) {
       
   928 		symbol->l_exp->datatype = symbol->datatype;
       
   929 		symbol->r_exp->datatype = symbol->datatype;
       
   930 		count++;
       
   931 	} else {
       
   932 		/* TIME data type */
       
   933 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
       
   934 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
       
   935 				/* test widening compatibility */
       
   936 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
       
   937 						symbol->r_exp->candidate_datatypes[j],
       
   938 						symbol->datatype, widen_DIV_table)) {
       
   939 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
   940 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
   941 					count ++;
       
   942 				}
       
   943 			}
       
   944 		}
       
   945 	}
       
   946 	if (count > 1)
       
   947 		ERROR;
       
   948 	symbol->l_exp->accept(*this);
       
   949 	symbol->r_exp->accept(*this);
       
   950 	return NULL;
       
   951 }
       
   952 
       
   953 
       
   954 void *narrow_candidate_datatypes_c::visit(mod_expression_c *symbol) {
       
   955 	symbol->l_exp->datatype = symbol->datatype;
       
   956 	symbol->l_exp->accept(*this);
       
   957 	symbol->r_exp->datatype = symbol->datatype;
       
   958 	symbol->r_exp->accept(*this);
       
   959 	return NULL;
       
   960 }
       
   961 
       
   962 
       
   963 void *narrow_candidate_datatypes_c::visit(power_expression_c *symbol) {
       
   964 	symbol->l_exp->datatype = symbol->datatype;
       
   965 	symbol->l_exp->accept(*this);
       
   966 	if (! symbol->r_exp->candidate_datatypes.size()){
       
   967 		symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[0];
       
   968 		symbol->r_exp->accept(*this);
       
   969 	}
       
   970 	return NULL;
       
   971 }
       
   972 
       
   973 
       
   974 void *narrow_candidate_datatypes_c::visit(neg_expression_c *symbol) {
       
   975 	symbol->exp->datatype = symbol->datatype;
       
   976 	symbol->exp->accept(*this);
       
   977 	return NULL;
       
   978 }
       
   979 
       
   980 
       
   981 void *narrow_candidate_datatypes_c::visit(not_expression_c *symbol) {
       
   982 	symbol->exp->datatype = symbol->datatype;
       
   983 	symbol->exp->accept(*this);
       
   984 	return NULL;
       
   985 }
       
   986 
       
   987 
       
   988 void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) {
       
   989 	function_declaration_c *f_decl;
       
   990 	list_c *parameter_list;
       
   991 	list_c *parameter_candidate_datatypes;
       
   992 	function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
   993 	function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
   994 
       
   995 	if (NULL != symbol->formal_param_list)
       
   996 		parameter_list = (list_c *)symbol->formal_param_list;
       
   997 	else if (NULL != symbol->nonformal_param_list)
       
   998 		parameter_list = (list_c *)symbol->nonformal_param_list;
       
   999 	else ERROR;
       
  1000 	for(; lower != upper; lower++) {
       
  1001 		f_decl = function_symtable.get_value(lower);
       
  1002 		symbol_c * return_type = base_type(f_decl->type_name);
       
  1003 		if (return_type && typeid(*symbol->datatype) != typeid(*return_type))
       
  1004 			continue;
       
  1005 		/* We set which function declaration it'll use in STAGE4 */
       
  1006 		symbol->called_function_declaration = f_decl;
       
  1007 		/* Check if function declaration in symbol_table is compatible with parameters */
       
  1008 		if (NULL != symbol->nonformal_param_list)
       
  1009 			/* nonformal parameter function call */
       
  1010 			narrow_nonformal_call(symbol, f_decl);
       
  1011 		else
       
  1012 			/* formal parameter function call */
       
  1013 			narrow_formal_call (symbol, f_decl);
       
  1014 		break;
       
  1015 	}
       
  1016 
       
  1017 	return NULL;
       
  1018 }
       
  1019 
       
  1020 /********************/
       
  1021 /* B 3.2 Statements */
       
  1022 /********************/
       
  1023 
       
  1024 
       
  1025 /*********************************/
       
  1026 /* B 3.2.1 Assignment Statements */
       
  1027 /*********************************/
       
  1028 
       
  1029 void *narrow_candidate_datatypes_c::visit(assignment_statement_c *symbol) {
       
  1030 	if (symbol->candidate_datatypes.size() != 1)
       
  1031 		return NULL;
       
  1032 	symbol->datatype = symbol->candidate_datatypes[0];
       
  1033 	symbol->l_exp->datatype = symbol->datatype;
       
  1034 	symbol->l_exp->accept(*this);
       
  1035 	symbol->r_exp->datatype = symbol->datatype;
       
  1036 	symbol->r_exp->accept(*this);
       
  1037 	return NULL;
       
  1038 }
       
  1039 
       
  1040 
       
  1041 /*****************************************/
       
  1042 /* B 3.2.2 Subprogram Control Statements */
       
  1043 /*****************************************/
       
  1044 
       
  1045 /********************************/
       
  1046 /* B 3.2.3 Selection Statements */
       
  1047 /********************************/
       
  1048 
       
  1049 void *narrow_candidate_datatypes_c::visit(if_statement_c *symbol) {
       
  1050 	for(unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
       
  1051 		if (is_type(symbol->expression->candidate_datatypes[i], bool_type_name_c))
       
  1052 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
       
  1053 	}
       
  1054 	symbol->expression->accept(*this);
       
  1055 	if (NULL != symbol->statement_list)
       
  1056 		symbol->statement_list->accept(*this);
       
  1057 	if (NULL != symbol->elseif_statement_list)
       
  1058 		symbol->elseif_statement_list->accept(*this);
       
  1059 	if (NULL != symbol->else_statement_list)
       
  1060 		symbol->else_statement_list->accept(*this);
       
  1061 	return NULL;
       
  1062 }
       
  1063 
       
  1064 
       
  1065 void *narrow_candidate_datatypes_c::visit(elseif_statement_c *symbol) {
       
  1066 	for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
       
  1067 		if (is_type(symbol->expression->candidate_datatypes[i], bool_type_name_c))
       
  1068 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
       
  1069 	}
       
  1070 	symbol->expression->accept(*this);
       
  1071 	if (NULL != symbol->statement_list)
       
  1072 		symbol->statement_list->accept(*this);
       
  1073 	return NULL;
       
  1074 }
       
  1075 
       
  1076 /* CASE expression OF case_element_list ELSE statement_list END_CASE */
       
  1077 // SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
       
  1078 void *narrow_candidate_datatypes_c::visit(case_statement_c *symbol) {
       
  1079 	for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
       
  1080 		if ((is_ANY_INT_type(symbol->expression->candidate_datatypes[i]))
       
  1081 				 || (search_base_type.type_is_enumerated(symbol->expression->candidate_datatypes[i])))
       
  1082 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
       
  1083 	}
       
  1084 	symbol->expression->accept(*this);
       
  1085 	if (NULL != symbol->statement_list)
       
  1086 		symbol->statement_list->accept(*this);
       
  1087 	if (NULL != symbol->case_element_list) {
       
  1088 		symbol->case_element_list->datatype = symbol->expression->datatype;
       
  1089 		symbol->case_element_list->accept(*this);
       
  1090 	}
       
  1091 	return NULL;
       
  1092 }
       
  1093 
       
  1094 /* helper symbol for case_statement */
       
  1095 // SYM_LIST(case_element_list_c)
       
  1096 void *narrow_candidate_datatypes_c::visit(case_element_list_c *symbol) {
       
  1097 	for (int i = 0; i < symbol->n; i++) {
       
  1098 		symbol->elements[i]->datatype = symbol->datatype;
       
  1099 		symbol->elements[i]->accept(*this);
       
  1100 	}
       
  1101 	return NULL;
       
  1102 }
       
  1103 
       
  1104 /*  case_list ':' statement_list */
       
  1105 // SYM_REF2(case_element_c, case_list, statement_list)
       
  1106 void *narrow_candidate_datatypes_c::visit(case_element_c *symbol) {
       
  1107 	symbol->case_list->datatype = symbol->datatype;
       
  1108 	symbol->case_list->accept(*this);
       
  1109 	symbol->statement_list->accept(*this);
       
  1110 	return NULL;
       
  1111 }
       
  1112 
       
  1113 // SYM_LIST(case_list_c)
       
  1114 void *narrow_candidate_datatypes_c::visit(case_list_c *symbol) {
       
  1115 	for (int i = 0; i < symbol->n; i++) {
       
  1116 		for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) {
       
  1117 			if (is_type_equal(symbol->datatype, symbol->elements[i]->candidate_datatypes[k]))
       
  1118 				symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k];
       
  1119 		}
       
  1120 		/* NOTE: this may be an integer, a subrange_c, or a enumerated value! */
       
  1121 		symbol->elements[i]->accept(*this);
       
  1122 	}
       
  1123 	return NULL;
       
  1124 }
       
  1125 
       
  1126 
       
  1127 /********************************/
       
  1128 /* B 3.2.4 Iteration Statements */
       
  1129 /********************************/
       
  1130 void *narrow_candidate_datatypes_c::visit(for_statement_c *symbol) {
       
  1131 	/* Control variable */
       
  1132 	for(unsigned int i = 0; i < symbol->control_variable->candidate_datatypes.size(); i++) {
       
  1133 		if (is_ANY_INT_type(symbol->control_variable->candidate_datatypes[i])) {
       
  1134 			symbol->control_variable->datatype = symbol->control_variable->candidate_datatypes[i];
       
  1135 		}
       
  1136 	}
       
  1137 	symbol->control_variable->accept(*this);
       
  1138 	/* BEG expression */
       
  1139 	for(unsigned int i = 0; i < symbol->beg_expression->candidate_datatypes.size(); i++) {
       
  1140 		if (is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) &&
       
  1141 				is_ANY_INT_type(symbol->beg_expression->candidate_datatypes[i])) {
       
  1142 			symbol->beg_expression->datatype = symbol->beg_expression->candidate_datatypes[i];
       
  1143 		}
       
  1144 	}
       
  1145 	symbol->beg_expression->accept(*this);
       
  1146 	/* END expression */
       
  1147 	for(unsigned int i = 0; i < symbol->end_expression->candidate_datatypes.size(); i++) {
       
  1148 		if (is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) &&
       
  1149 				is_ANY_INT_type(symbol->end_expression->candidate_datatypes[i])) {
       
  1150 			symbol->end_expression->datatype = symbol->end_expression->candidate_datatypes[i];
       
  1151 		}
       
  1152 	}
       
  1153 	symbol->end_expression->accept(*this);
       
  1154 	/* BY expression */
       
  1155 	if (NULL != symbol->by_expression) {
       
  1156 		for(unsigned int i = 0; i < symbol->by_expression->candidate_datatypes.size(); i++) {
       
  1157 			if (is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) &&
       
  1158 					is_ANY_INT_type(symbol->by_expression->candidate_datatypes[i])) {
       
  1159 				symbol->by_expression->datatype = symbol->by_expression->candidate_datatypes[i];
       
  1160 			}
       
  1161 		}
       
  1162 		symbol->by_expression->accept(*this);
       
  1163 	}
       
  1164 	if (NULL != symbol->statement_list)
       
  1165 		symbol->statement_list->accept(*this);
       
  1166 	return NULL;
       
  1167 }
       
  1168 
       
  1169 void *narrow_candidate_datatypes_c::visit(while_statement_c *symbol) {
       
  1170 	for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
       
  1171 		if(is_BOOL_type(symbol->expression->candidate_datatypes[i]))
       
  1172 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
       
  1173 	}
       
  1174 	symbol->expression->accept(*this);
       
  1175 	if (NULL != symbol->statement_list)
       
  1176 		symbol->statement_list->accept(*this);
       
  1177 	return NULL;
       
  1178 }
       
  1179 
       
  1180 void *narrow_candidate_datatypes_c::visit(repeat_statement_c *symbol) {
       
  1181 	for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
       
  1182 		if(is_BOOL_type(symbol->expression->candidate_datatypes[i]))
       
  1183 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
       
  1184 	}
       
  1185 	symbol->expression->accept(*this);
       
  1186 	if (NULL != symbol->statement_list)
       
  1187 		symbol->statement_list->accept(*this);
       
  1188 	return NULL;
       
  1189 }
       
  1190 
       
  1191 
       
  1192 
       
  1193 
       
  1194