stage3/datatype_functions.cc
changeset 425 c8e6cf57324a
parent 417 d48f53715f77
child 434 c1278e52bcbc
equal deleted inserted replaced
424:43d73e28eca8 425:c8e6cf57324a
   146     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeusint_type_name,       &search_constant_type_c::safetime_type_name },
   146     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeusint_type_name,       &search_constant_type_c::safetime_type_name },
   147 
   147 
   148     { NULL, NULL, NULL },
   148     { NULL, NULL, NULL },
   149 };
   149 };
   150 
   150 
   151 /* NOTE on data type handling and literals...
   151 /* Search for a datatype inside a candidate_datatypes list.
   152  * ==========================================
   152  * Returns: position of datatype in the list, or -1 if not found.
   153  *
       
   154  * Literals that are explicitly type cast
       
   155  *   e.g.:   BYTE#42
       
   156  *           INT#65
       
   157  *           TIME#45h23m
       
   158  *               etc...
       
   159  *  are NOT considered literals in the following code.
       
   160  *  Since they are type cast, and their data type is fixed and well known,
       
   161  *  they are treated as a variable of that data type (except when determining lvalues)
       
   162  *  In other words, when calling search_constant_type_c on these constants, it returns
       
   163  *  a xxxxx_type_name_c, and not one of the xxxx_literal_c !
       
   164  *
       
   165  *  When the following code handles a literal, it is really a literal of unknown data type.
       
   166  *    e.g.   42, may be considered an int, a byte, a word, etc...
       
   167  *
       
   168  * NOTE: type_symbol == NULL is valid!
       
   169  *       This will occur, for example, when and undefined/undeclared symbolic_variable is used in the program.
       
   170  *       This will not be of any type, so we always return false.
       
   171  */
   153  */
       
   154 int search_in_datatype_list(symbol_c *datatype, std::vector <symbol_c *> candidate_datatypes) {
       
   155 	for(unsigned int i = 0; i < candidate_datatypes.size(); i++)
       
   156 		if (is_type_equal(datatype, candidate_datatypes[i]))
       
   157 			return i;
       
   158 	/* Not found ! */
       
   159 	return -1;
       
   160 }
       
   161 
   172 
   162 
   173 /* A helper function... */
   163 /* A helper function... */
   174 bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol) {
   164 bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol) {
   175   if (type_symbol == NULL) {return false;}
   165   if (type_symbol == NULL) {return false;}
   176   return is_ANY_MAGNITUDE_type(type_symbol)
   166   return is_ANY_MAGNITUDE_type(type_symbol)
   330 /* A helper function... */
   320 /* A helper function... */
   331 bool is_ANY_INT_compatible(symbol_c *type_symbol) {
   321 bool is_ANY_INT_compatible(symbol_c *type_symbol) {
   332   if (type_symbol == NULL) {return false;}
   322   if (type_symbol == NULL) {return false;}
   333   if (is_ANY_INT_type    (type_symbol))              {return true;}
   323   if (is_ANY_INT_type    (type_symbol))              {return true;}
   334   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
   324   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
   335   if (is_literal_integer_type(type_symbol))          {return true;}
   325 //   if (is_literal_integer_type(type_symbol))          {return true;}
   336   return false;
   326   return false;
   337 }
   327 }
   338 
   328 
   339 /* A helper function... */
   329 /* A helper function... */
   340 bool is_ANY_REAL_type(symbol_c *type_symbol) {
   330 bool is_ANY_REAL_type(symbol_c *type_symbol) {
   355 /* A helper function... */
   345 /* A helper function... */
   356 bool is_ANY_REAL_compatible(symbol_c *type_symbol) {
   346 bool is_ANY_REAL_compatible(symbol_c *type_symbol) {
   357   if (type_symbol == NULL) {return false;}
   347   if (type_symbol == NULL) {return false;}
   358   if (is_ANY_REAL_type    (type_symbol))              {return true;}
   348   if (is_ANY_REAL_type    (type_symbol))              {return true;}
   359   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
   349   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
   360   if (is_literal_real_type(type_symbol))              {return true;}
   350 //   if (is_literal_real_type(type_symbol))              {return true;}
   361   return false;
   351   return false;
   362 }
   352 }
   363 
   353 
   364 /* A helper function... */
   354 /* A helper function... */
   365 bool is_ANY_BIT_type(symbol_c *type_symbol) {
   355 bool is_ANY_BIT_type(symbol_c *type_symbol) {
   386 /* A helper function... */
   376 /* A helper function... */
   387 bool is_ANY_BIT_compatible(symbol_c *type_symbol) {
   377 bool is_ANY_BIT_compatible(symbol_c *type_symbol) {
   388   if (type_symbol == NULL) {return false;}
   378   if (type_symbol == NULL) {return false;}
   389   if (is_ANY_BIT_type    (type_symbol))              {return true;}
   379   if (is_ANY_BIT_type    (type_symbol))              {return true;}
   390   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
   380   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
   391   if (is_nonneg_literal_integer_type(type_symbol))   {return true;}
   381 //   if (is_nonneg_literal_integer_type(type_symbol))   {return true;}
   392   if (is_literal_bool_type(type_symbol))             {return true;}
   382 //   if (is_literal_bool_type(type_symbol))             {return true;}
   393   return false;
   383   return false;
   394 }
   384 }
   395 
   385 
   396 /* A helper function... */
   386 /* A helper function... */
   397 bool is_BOOL_type(symbol_c *type_symbol) {
   387 bool is_BOOL_type(symbol_c *type_symbol) {
   410 /* A helper function... */
   400 /* A helper function... */
   411 bool is_ANY_BOOL_compatible(symbol_c *type_symbol) {
   401 bool is_ANY_BOOL_compatible(symbol_c *type_symbol) {
   412   if (type_symbol == NULL) {return false;}
   402   if (type_symbol == NULL) {return false;}
   413   if (is_BOOL_type    (type_symbol))              {return true;}
   403   if (is_BOOL_type    (type_symbol))              {return true;}
   414   if (is_SAFEBOOL_type(type_symbol))              {return true;}
   404   if (is_SAFEBOOL_type(type_symbol))              {return true;}
   415   if (is_literal_bool_type(type_symbol))              {return true;}
   405 //   if (is_literal_bool_type(type_symbol))              {return true;}
   416   return false;
   406   return false;
   417 }
   407 }
   418 
   408 
       
   409 
       
   410 
       
   411 #if 0
   419 /* A helper function... */
   412 /* A helper function... */
   420 bool is_literal_integer_type(symbol_c *type_symbol) {
   413 bool is_literal_integer_type(symbol_c *type_symbol) {
   421   if (type_symbol == NULL) {return false;}
   414   if (type_symbol == NULL) {return false;}
   422   if (typeid(*type_symbol) == typeid(neg_integer_c))        {return true;}
   415   if (typeid(*type_symbol) == typeid(neg_integer_c))        {return true;}
   423   return is_nonneg_literal_integer_type(type_symbol);
   416   return is_nonneg_literal_integer_type(type_symbol);
   602  */
   595  */
   603 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
   596 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
   604   if (first_type == NULL || second_type == NULL) {return false;}
   597   if (first_type == NULL || second_type == NULL) {return false;}
   605   return (NULL != common_type(first_type, second_type));
   598   return (NULL != common_type(first_type, second_type));
   606 }
   599 }
       
   600 #endif
   607 
   601 
   608 bool is_type_equal(symbol_c *first_type, symbol_c *second_type)
   602 bool is_type_equal(symbol_c *first_type, symbol_c *second_type)
   609 {
   603 {
   610     if (first_type == NULL || second_type == NULL) {
   604     if (first_type == NULL || second_type == NULL) {
   611         return false;
   605         return false;