stage3/fill_candidate_datatypes.cc
changeset 939 5074236fb3c4
parent 938 31e3b3f2eff1
child 940 61e2bdae5899
equal deleted inserted replaced
938:31e3b3f2eff1 939:5074236fb3c4
   269 
   269 
   270 
   270 
   271 fill_candidate_datatypes_c::fill_candidate_datatypes_c(symbol_c *ignore) {
   271 fill_candidate_datatypes_c::fill_candidate_datatypes_c(symbol_c *ignore) {
   272 	il_operand = NULL;
   272 	il_operand = NULL;
   273 	prev_il_instruction = NULL;
   273 	prev_il_instruction = NULL;
   274 	search_varfb_instance_type = NULL;
   274 	search_var_instance_decl = NULL;
   275 	current_enumerated_spec_type = NULL;
   275 	current_enumerated_spec_type = NULL;
   276 }
   276 }
   277 
   277 
   278 fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) {
   278 fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) {
   279 }
   279 }
   553 /* handle implicit FB call in IL.
   553 /* handle implicit FB call in IL.
   554  * e.g.  CLK ton_var
   554  * e.g.  CLK ton_var
   555  *        CU counter_var
   555  *        CU counter_var
   556  */
   556  */
   557 void *fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) {
   557 void *fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) {
   558 	symbol_c *fb_decl = (NULL == il_operand)? NULL : search_varfb_instance_type->get_basetype_decl(il_operand);
   558 	symbol_c *fb_decl = (NULL == il_operand)? NULL : search_var_instance_decl->get_basetype_decl(il_operand);
   559 	if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
   559 	if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
   560 
   560 
   561 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
   561 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
   562 	/* However, when calling using the 'S' and 'R' operators, this error is not caught by stage 2, as these operators have two possible semantics */
   562 	/* However, when calling using the 'S' and 'R' operators, this error is not caught by stage 2, as these operators have two possible semantics */
   563 	// if (NULL == fb_type_id) ERROR;
   563 	// if (NULL == fb_type_id) ERROR;
  1171 /*********************/
  1171 /*********************/
  1172 void *fill_candidate_datatypes_c::visit(symbolic_variable_c *symbol) {
  1172 void *fill_candidate_datatypes_c::visit(symbolic_variable_c *symbol) {
  1173 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1173 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1174 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1174 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1175 	 *        in this fill_candidate_datatypes_c itself!
  1175 	 *        in this fill_candidate_datatypes_c itself!
  1176 	 *        This is needed because we need to know in which scope (i.e. the datatype of the recor_variable in a structtured_variable_c)
  1176 	 *        This is needed because we need to know in which scope (i.e. the datatype of the record_variable in a structtured_variable_c)
  1177 	 *        we will search for the field_variable of the structured_variable_c
  1177 	 *        we will search for the field_variable of the structured_variable_c. Similarly, it is also used to determine the datatype 
       
  1178 	 *        to which a REF_TO variable points to. 
  1178 	 */
  1179 	 */
  1179 	symbol->datatype = search_varfb_instance_type->get_basetype_decl(symbol); // Do the narrow algorithm in this fill_candidate_datatypes_c!!
  1180 	symbol->datatype = search_var_instance_decl->get_basetype_decl(symbol); // Do the narrow algorithm in this fill_candidate_datatypes_c!!
  1180 	add_datatype_to_candidate_list(symbol, symbol->datatype); /* will only add if non NULL */
  1181 	add_datatype_to_candidate_list(symbol, symbol->datatype); /* will only add if non NULL */
  1181 	if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n";
  1182 	if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n";
  1182 	return NULL;
  1183 	return NULL;
  1183 }
  1184 }
  1184 
  1185 
  1211 /* B 1.4.2 - Multi-element variables */
  1212 /* B 1.4.2 - Multi-element variables */
  1212 /*************************************/
  1213 /*************************************/
  1213 /*  subscripted_variable '[' subscript_list ']' */
  1214 /*  subscripted_variable '[' subscript_list ']' */
  1214 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
  1215 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
  1215 void *fill_candidate_datatypes_c::visit(array_variable_c *symbol) {
  1216 void *fill_candidate_datatypes_c::visit(array_variable_c *symbol) {
  1216 	/* get the declaration of the data type __stored__ in the array... */
       
  1217 	/* if we were to want the data type of the array itself, then we should call_param_name
       
  1218 	 * search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable)
       
  1219 	 */
       
  1220 	add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol));   /* will only add if non NULL */
       
  1221 
       
  1222 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
       
  1223 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
       
  1224 	 *        in this fill_candidate_datatypes_c itself!
       
  1225 	 *        This is needed because we need to know in which scope (i.e. the datatype of the recor_variable in a structtured_variable_c)
       
  1226 	 *        we will search for the field_variable of the structured_variable_c
       
  1227 	 */
       
  1228 	if (symbol->candidate_datatypes.size() == 1)
       
  1229 	  // narrow the symbol->datatype for this strcutured_variable as explained above!
       
  1230 	  symbol->datatype = symbol->candidate_datatypes[0];
       
  1231 
       
  1232 	/* recursively call the subscript list, so we can check the data types of the expressions used for the subscripts */
       
  1233 	symbol->subscript_list->accept(*this);
       
  1234 
       
  1235 	/* recursively call the subscripted_variable. We need to do this since the array variable may be stored inside a structured
  1217 	/* recursively call the subscripted_variable. We need to do this since the array variable may be stored inside a structured
  1236 	 * variable (i.e. if it is an element inside a struct), in which case we want to recursively visit every element of the struct,
  1218 	 * variable (i.e. if it is an element inside a struct), in which case we want to recursively visit every element of the struct,
  1237 	 * as it may contain more arrays whose subscripts must also be visited!
  1219 	 * as it may contain more arrays whose subscripts must also be visited!
  1238 	 * e.g.   structvar.a1[v1+2].b1.c1[v2+3].d1
  1220 	 * e.g.   structvar.a1[v1+2].b1.c1[v2+3].d1
  1239 	 *        TYPE
  1221 	 *        TYPE
  1247 	 *        VAR
  1229 	 *        VAR
  1248 	 *          structvar: a_s;
  1230 	 *          structvar: a_s;
  1249 	 *        END_VAR
  1231 	 *        END_VAR
  1250 	 */
  1232 	 */
  1251 	symbol->subscripted_variable->accept(*this);
  1233 	symbol->subscripted_variable->accept(*this);
       
  1234 
       
  1235 	add_datatype_to_candidate_list(symbol, search_base_type_c::get_basetype_decl(get_datatype_info_c::get_array_storedtype_id(symbol->subscripted_variable->datatype)));   /* will only add if non NULL */
       
  1236 
       
  1237 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
       
  1238 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
       
  1239 	 *        in this fill_candidate_datatypes_c itself!
       
  1240 	 *        This is needed because we need to know in which scope (i.e. the datatype of the record_variable in a structtured_variable_c)
       
  1241 	 *        we will search for the field_variable of the structured_variable_c. Similarly, it is also used to determine the datatype 
       
  1242 	 *        to which a REF_TO variable points to. 
       
  1243 	 */
       
  1244 	if (symbol->candidate_datatypes.size() == 1)
       
  1245 	  // narrow the symbol->datatype for this array_variable as explained above!
       
  1246 	  symbol->datatype = symbol->candidate_datatypes[0];
       
  1247 
       
  1248 	/* recursively call the subscript list, so we can check the data types of the expressions used for the subscripts */
       
  1249 	symbol->subscript_list->accept(*this);
  1252 
  1250 
  1253 	if (debug) std::cout << "ARRAY_VAR [" << symbol->candidate_datatypes.size() << "]\n";	
  1251 	if (debug) std::cout << "ARRAY_VAR [" << symbol->candidate_datatypes.size() << "]\n";	
  1254 	return NULL;
  1252 	return NULL;
  1255 }
  1253 }
  1256 
  1254 
  1277 	symbol->record_variable->accept(*this);
  1275 	symbol->record_variable->accept(*this);
  1278 	
  1276 	
  1279 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1277 	/*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1280 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1278 	 *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1281 	 *        in this fill_candidate_datatypes_c itself!
  1279 	 *        in this fill_candidate_datatypes_c itself!
  1282 	 *        This is needed because we need to know in which scope (i.e. the datatype of the recor_variable in a structtured_variable_c)
  1280 	 *        This is needed because we need to know in which scope (i.e. the datatype of the record_variable in a structtured_variable_c)
  1283 	 *        we will search for the field_variable of the structured_variable_c
  1281 	 *        we will search for the field_variable of the structured_variable_c. Similarly, it is also used to determine the datatype 
       
  1282 	 *        to which a REF_TO variable points to. 
  1284 	 */
  1283 	 */
  1285 	if (NULL != symbol->record_variable->datatype) 
  1284 	if (NULL != symbol->record_variable->datatype) 
  1286 	  // We relly on the fact that we have already narrowed the symbol->datatype for the record variable, and use it as the scope in which the filed_variable is declared!
  1285 	  // We relly on the fact that we have already narrowed the symbol->datatype for the record variable, and use it as the scope in which the filed_variable is declared!
  1287 	  add_datatype_to_candidate_list(symbol, search_base_type_c::get_basetype_decl(get_datatype_info_c::get_struct_field_type_id(symbol->record_variable->datatype, symbol->field_selector)));  /* will only add if non NULL */
  1286 	  add_datatype_to_candidate_list(symbol, search_base_type_c::get_basetype_decl(get_datatype_info_c::get_struct_field_type_id(symbol->record_variable->datatype, symbol->field_selector)));  /* will only add if non NULL */
  1288 	if (symbol->candidate_datatypes.size() == 1)
  1287 	if (symbol->candidate_datatypes.size() == 1)
  1297 /* B 1.4.3 - Declaration & Initialisation */
  1296 /* B 1.4.3 - Declaration & Initialisation */
  1298 /******************************************/
  1297 /******************************************/
  1299 
  1298 
  1300 void *fill_candidate_datatypes_c::visit(var1_list_c *symbol) {
  1299 void *fill_candidate_datatypes_c::visit(var1_list_c *symbol) {
  1301   for(int i = 0; i < symbol->n; i++) {
  1300   for(int i = 0; i < symbol->n; i++) {
  1302     /* We don't really need to set the datatype of each variable. We just check the declaration itself! 
       
  1303     add_datatype_to_candidate_list(symbol->elements[i], search_varfb_instance_type->get_basetype_decl(symbol->elements[i])); // will only add if non NULL 
       
  1304     */
       
  1305     symbol->elements[i]->accept(*this); // handle the extensible_input_parameter_c, etc...
  1301     symbol->elements[i]->accept(*this); // handle the extensible_input_parameter_c, etc...
  1306   }
  1302   }
  1307   return NULL;
  1303   return NULL;
  1308 }  
  1304 }  
  1309 
  1305 
  1416 void *fill_candidate_datatypes_c::visit(function_declaration_c *symbol) {
  1412 void *fill_candidate_datatypes_c::visit(function_declaration_c *symbol) {
  1417 	if (debug) printf("Filling candidate data types list of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
  1413 	if (debug) printf("Filling candidate data types list of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
  1418 	local_enumerated_value_symtable.reset();
  1414 	local_enumerated_value_symtable.reset();
  1419 	symbol->var_declarations_list->accept(populate_enumvalue_symtable);
  1415 	symbol->var_declarations_list->accept(populate_enumvalue_symtable);
  1420 
  1416 
  1421 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
  1417 	search_var_instance_decl = new search_var_instance_decl_c(symbol);
  1422 	symbol->var_declarations_list->accept(*this);
  1418 	symbol->var_declarations_list->accept(*this);
  1423 	symbol->function_body->accept(*this);
  1419 	symbol->function_body->accept(*this);
  1424 	delete search_varfb_instance_type;
  1420 	delete search_var_instance_decl;
  1425 	search_varfb_instance_type = NULL;
  1421 	search_var_instance_decl = NULL;
  1426 
  1422 
  1427 	local_enumerated_value_symtable.reset();
  1423 	local_enumerated_value_symtable.reset();
  1428 	return NULL;
  1424 	return NULL;
  1429 }
  1425 }
  1430 
  1426 
  1434 void *fill_candidate_datatypes_c::visit(function_block_declaration_c *symbol) {
  1430 void *fill_candidate_datatypes_c::visit(function_block_declaration_c *symbol) {
  1435 	if (debug) printf("Filling candidate data types list of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
  1431 	if (debug) printf("Filling candidate data types list of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
  1436 	local_enumerated_value_symtable.reset();
  1432 	local_enumerated_value_symtable.reset();
  1437 	symbol->var_declarations->accept(populate_enumvalue_symtable);
  1433 	symbol->var_declarations->accept(populate_enumvalue_symtable);
  1438 
  1434 
  1439 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
  1435 	search_var_instance_decl = new search_var_instance_decl_c(symbol);
  1440 	symbol->var_declarations->accept(*this);
  1436 	symbol->var_declarations->accept(*this);
  1441 	symbol->fblock_body->accept(*this);
  1437 	symbol->fblock_body->accept(*this);
  1442 	delete search_varfb_instance_type;
  1438 	delete search_var_instance_decl;
  1443 	search_varfb_instance_type = NULL;
  1439 	search_var_instance_decl = NULL;
  1444 
  1440 
  1445 	local_enumerated_value_symtable.reset();
  1441 	local_enumerated_value_symtable.reset();
  1446 	
  1442 	
  1447 	/* The FB declaration itself may be used as a dataype! We now do the fill algorithm considering 
  1443 	/* The FB declaration itself may be used as a dataype! We now do the fill algorithm considering 
  1448 	 * function_block_declaration_c a data type declaration...
  1444 	 * function_block_declaration_c a data type declaration...
  1458 void *fill_candidate_datatypes_c::visit(program_declaration_c *symbol) {
  1454 void *fill_candidate_datatypes_c::visit(program_declaration_c *symbol) {
  1459 	if (debug) printf("Filling candidate data types list in program %s\n", ((token_c *)(symbol->program_type_name))->value);
  1455 	if (debug) printf("Filling candidate data types list in program %s\n", ((token_c *)(symbol->program_type_name))->value);
  1460 	local_enumerated_value_symtable.reset();
  1456 	local_enumerated_value_symtable.reset();
  1461 	symbol->var_declarations->accept(populate_enumvalue_symtable);
  1457 	symbol->var_declarations->accept(populate_enumvalue_symtable);
  1462 	
  1458 	
  1463 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
  1459 	search_var_instance_decl = new search_var_instance_decl_c(symbol);
  1464 	symbol->var_declarations->accept(*this);
  1460 	symbol->var_declarations->accept(*this);
  1465 	symbol->function_block_body->accept(*this);
  1461 	symbol->function_block_body->accept(*this);
  1466 	delete search_varfb_instance_type;
  1462 	delete search_var_instance_decl;
  1467 	search_varfb_instance_type = NULL;
  1463 	search_var_instance_decl = NULL;
  1468 
  1464 
  1469 	local_enumerated_value_symtable.reset();
  1465 	local_enumerated_value_symtable.reset();
  1470 	return NULL;
  1466 	return NULL;
  1471 }
  1467 }
  1472 
  1468 
  1677  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
  1673  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
  1678  */
  1674  */
  1679 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 */
  1675 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 */
  1680 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
  1676 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
  1681 void *fill_candidate_datatypes_c::visit(il_fb_call_c *symbol) {
  1677 void *fill_candidate_datatypes_c::visit(il_fb_call_c *symbol) {
  1682 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
  1678 	symbol_c *fb_decl = search_var_instance_decl->get_basetype_decl(symbol->fb_name);
  1683 	if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
  1679 	if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
  1684 
  1680 
  1685 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  1681 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  1686 	if (NULL == fb_decl) ERROR;
  1682 	if (NULL == fb_decl) ERROR;
  1687 
  1683 
  1946   }
  1942   }
  1947 
  1943 
  1948   /*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1944   /*  NOTE: We need to fully determine the datatype of each element in the structured_variable inside this fill_candidate_datatypes class!
  1949    *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1945    *        Basically, for variables (be they symbolic_variable, structured_variable, array_variable), we do the narrow algorithm
  1950    *        in this fill_candidate_datatypes_c itself!
  1946    *        in this fill_candidate_datatypes_c itself!
  1951    *        This is needed because we need to know in which scope (i.e. the datatype of the recor_variable in a structtured_variable_c)
  1947    *        This is needed because we need to know in which scope (i.e. the datatype of the record_variable in a structtured_variable_c)
  1952    *        we will search for the field_variable of the structured_variable_c
  1948    *        we will search for the field_variable of the structured_variable_c. Similarly, it is also used to determine the datatype 
       
  1949    *        to which a REF_TO variable points to. 
  1953    * 
  1950    * 
  1954    *        Since the deref_operator_c may be used inside structures, we must narrow it here, if possible!
  1951    *        Since the deref_operator_c may be used inside structures, we must narrow it here, if possible!
  1955    */
  1952    */
  1956   if (symbol->candidate_datatypes.size() == 1)
  1953   if (symbol->candidate_datatypes.size() == 1)
  1957     // narrow the symbol->datatype for this symbol as explained above!
  1954     // narrow the symbol->datatype for this symbol as explained above!
  2118 
  2115 
  2119 /*****************************************/
  2116 /*****************************************/
  2120 /* B 3.2.2 Subprogram Control Statements */
  2117 /* B 3.2.2 Subprogram Control Statements */
  2121 /*****************************************/
  2118 /*****************************************/
  2122 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
  2119 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
  2123 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
  2120 	symbol_c *fb_decl = search_var_instance_decl->get_basetype_decl(symbol->fb_name);
  2124 	if (! get_datatype_info_c::is_function_block(fb_decl )) fb_decl = NULL;
  2121 	if (! get_datatype_info_c::is_function_block(fb_decl )) fb_decl = NULL;
  2125 	if (NULL == fb_decl) ERROR; /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  2122 	if (NULL == fb_decl) ERROR; /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  2126 	
  2123 	
  2127 	if (symbol->   formal_param_list != NULL) symbol->formal_param_list->accept(*this);
  2124 	if (symbol->   formal_param_list != NULL) symbol->formal_param_list->accept(*this);
  2128 	if (symbol->nonformal_param_list != NULL) symbol->nonformal_param_list->accept(*this);
  2125 	if (symbol->nonformal_param_list != NULL) symbol->nonformal_param_list->accept(*this);