stage4/generate_c/generate_c.cc
changeset 914 be6c89943079
parent 913 1c74da17cb61
child 915 ec3759689efe
equal deleted inserted replaced
913:1c74da17cb61 914:be6c89943079
   752 /***********************************************************************/
   752 /***********************************************************************/
   753 /***********************************************************************/
   753 /***********************************************************************/
   754 /***********************************************************************/
   754 /***********************************************************************/
   755 
   755 
   756 
   756 
   757 identifier_c *generate_unique_id(symbol_c *clone = NULL) {
   757 identifier_c *generate_unique_id(const char *prefix = "", symbol_c *clone = NULL) {
   758   static int counter = 0;
   758   static int counter = 0;
   759   
   759   
   760   counter++;
   760   counter++;
   761   int   len = snprintf(NULL, 0, "__UNIQUE_ID_%d", counter);
   761   int   len = snprintf(NULL, 0, "%s__UID_%d", prefix, counter); // UID -> Unique IDentifier
   762   char *str = (char *)malloc(len+1);
   762   char *str = (char *)malloc(len+1);
   763   if (snprintf(str, len+1, "__UNIQUE_ID_%d", counter) < 0) ERROR;
   763   if (snprintf(str, len+1,      "%s__UID_%d", prefix, counter) < 0) ERROR;
   764   
   764   
   765   identifier_c *id = new identifier_c(str);
   765   identifier_c *id = new identifier_c(str);
   766   if (NULL == id) ERROR;
   766   if (NULL == id) ERROR;
   767   if (NULL != clone)
   767   if (NULL != clone)
   768     *(dynamic_cast<symbol_c *>(id)) = *(dynamic_cast<symbol_c *>(clone));
   768     *(dynamic_cast<symbol_c *>(id)) = *(dynamic_cast<symbol_c *>(clone));
   769   return id;
   769   return id;
   770 }
   770 }
   771 
   771 
   772 
   772 
   773 
   773 identifier_c *generate_unique_id(symbol_c *prefix, symbol_c *clone = NULL) {
       
   774   token_c *token = dynamic_cast<token_c *>(prefix);
       
   775   //if (NULL == token) ERROR;
       
   776   if (NULL == token)
       
   777     return generate_unique_id("", clone);
       
   778   return generate_unique_id(token->value, clone);
       
   779 }
       
   780 
       
   781 
       
   782 /* This class will generate a new datatype for each implicitly declared array datatype
       
   783  * (i.e. arrays declared in a variable declaration, or a struct datatype declaration...)
       
   784  * 
       
   785  * e.g.:
       
   786  *      VAR  a: ARRAY [1..3] OF INT; END_VAR   <---- ARRAY datatype is implicitly declared inside the variable declaration
       
   787  *      TYPE STRUCT
       
   788  *               a: ARRAY [1..3] OF INT;       <---- ARRAY datatype is implicitly declared inside the struct type declaration  
       
   789  *               b: INT;
       
   790  *            END_STRUCT
       
   791  *      END_TYPE
       
   792  */
   774 class generate_c_datatypes_c: public iterator_visitor_c {
   793 class generate_c_datatypes_c: public iterator_visitor_c {
   775   private:
   794   private:
   776     generate_c_typedecl_c generate_c_typedecl;
   795     generate_c_typedecl_c generate_c_typedecl;
       
   796     symbol_c *prefix;
   777   public:
   797   public:
   778     generate_c_datatypes_c(stage4out_c *s4o_incl_ptr)
   798     generate_c_datatypes_c(stage4out_c *s4o_incl_ptr)
   779       : generate_c_typedecl(s4o_incl_ptr) {
   799       : generate_c_typedecl(s4o_incl_ptr) {
       
   800         prefix = NULL;
   780     };
   801     };
   781     virtual ~generate_c_datatypes_c(void) {
   802     virtual ~generate_c_datatypes_c(void) {
   782     }
   803     }
   783 
   804 
   784     /*************************/
   805     /*************************/
   785     /* B.1 - Common elements */
   806     /* B.1 - Common elements */
   786     /*************************/
   807     /*************************/
   787     /*******************************************/
       
   788     /* B 1.1 - Letters, digits and identifiers */
       
   789     /*******************************************/
       
   790 
       
   791     /**********************/
   808     /**********************/
   792     /* B.1.3 - Data types */
   809     /* B.1.3 - Data types */
   793     /**********************/
   810     /**********************/
   794     /***********************************/
   811     /********************************/
   795     /* B 1.3.1 - Elementary Data Types */
   812     /* B 1.3.3 - Derived data types */
   796     /***********************************/
   813     /********************************/
   797     /***********************************/
   814     /*  identifier ':' array_spec_init */
   798     /* B 1.3.2 - Generic Data Types    */
   815     void *visit(array_type_declaration_c *symbol) {return NULL;} // This is not an implicitly defined array!
   799     /***********************************/
   816 
   800 
   817 
   801     /******************************************/
   818     /******************************************/
   802     /* B 1.4.3 - Declaration & Initialization */
   819     /* B 1.4.3 - Declaration & Initialization */
   803     /******************************************/
   820     /******************************************/
   804 
       
   805     void *visit(edge_declaration_c           *symbol) {return NULL;}
   821     void *visit(edge_declaration_c           *symbol) {return NULL;}
   806     void *visit(en_param_declaration_c       *symbol) {return NULL;}
   822     void *visit(en_param_declaration_c       *symbol) {return NULL;}
   807     void *visit(eno_param_declaration_c      *symbol) {return NULL;}
   823     void *visit(eno_param_declaration_c      *symbol) {return NULL;}
   808 
   824 
   809     void *visit(var1_init_decl_c             *symbol) {return NULL;}
   825     void *visit(var1_init_decl_c             *symbol) {return NULL;}
   819       return NULL;
   835       return NULL;
   820     }
   836     }
   821 
   837 
   822     /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   838     /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   823     void *visit(array_specification_c *symbol) {
   839     void *visit(array_specification_c *symbol) {
   824       identifier_c *id = generate_unique_id(symbol);
   840       identifier_c *id = generate_unique_id(prefix, symbol);
   825       /* Warning: The following is dangerous... 
   841       /* Warning: The following is dangerous... 
   826        * We are asking the generate_c_typedecl_c visitor to visit a newly created array_type_declaration_c object
   842        * We are asking the generate_c_typedecl_c visitor to visit a newly created array_type_declaration_c object
   827        * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
   843        * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
   828        * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
   844        * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
   829        */
   845        */
   844 
   860 
   845     /*  var1_list ':' structure_type_name */
   861     /*  var1_list ':' structure_type_name */
   846     //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
   862     //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
   847     void *visit(structured_var_declaration_c *symbol) {return NULL;}
   863     void *visit(structured_var_declaration_c *symbol) {return NULL;}
   848 
   864 
       
   865     /***********************/
       
   866     /* B 1.5.1 - Functions */
       
   867     /***********************/      
       
   868     void *visit(function_declaration_c *symbol) {
       
   869       prefix = symbol->derived_function_name;
       
   870       symbol->var_declarations_list->accept(*this);
       
   871       prefix = NULL;
       
   872       return NULL;
       
   873     }
       
   874     /*****************************/
       
   875     /* B 1.5.2 - Function Blocks */
       
   876     /*****************************/
       
   877     void *visit(function_block_declaration_c *symbol) {
       
   878       prefix = symbol->fblock_name;
       
   879       symbol->var_declarations->accept(*this);
       
   880       prefix = NULL;
       
   881       return NULL;
       
   882     }
       
   883     /**********************/
       
   884     /* B 1.5.3 - Programs */
       
   885     /**********************/    
       
   886     void *visit(program_declaration_c *symbol) {
       
   887       prefix = symbol->program_type_name;
       
   888       symbol->var_declarations->accept(*this);
       
   889       prefix = NULL;
       
   890       return NULL;
       
   891     }
   849 };
   892 };
   850 
   893 
   851 
   894 
   852 
   895 
   853 
   896 
  2294 
  2337 
  2295 /***********************/
  2338 /***********************/
  2296 /* B 1.5.1 - Functions */
  2339 /* B 1.5.1 - Functions */
  2297 /***********************/      
  2340 /***********************/      
  2298     void *visit(function_declaration_c *symbol) {
  2341     void *visit(function_declaration_c *symbol) {
  2299       handle_pou(handle_function,symbol->derived_function_name, symbol->var_declarations_list)
  2342       handle_pou(handle_function,symbol->derived_function_name, symbol)
  2300       return NULL;
  2343       return NULL;
  2301     }
  2344     }
  2302     
  2345     
  2303 /*****************************/
  2346 /*****************************/
  2304 /* B 1.5.2 - Function Blocks */
  2347 /* B 1.5.2 - Function Blocks */
  2305 /*****************************/
  2348 /*****************************/
  2306     void *visit(function_block_declaration_c *symbol) {
  2349     void *visit(function_block_declaration_c *symbol) {
  2307       handle_pou(handle_function_block,symbol->fblock_name, symbol->var_declarations)
  2350       handle_pou(handle_function_block,symbol->fblock_name, symbol)
  2308       return NULL;
  2351       return NULL;
  2309     }
  2352     }
  2310     
  2353     
  2311 /**********************/
  2354 /**********************/
  2312 /* B 1.5.3 - Programs */
  2355 /* B 1.5.3 - Programs */
  2313 /**********************/    
  2356 /**********************/    
  2314     void *visit(program_declaration_c *symbol) {
  2357     void *visit(program_declaration_c *symbol) {
  2315       handle_pou(handle_program,symbol->program_type_name, symbol->var_declarations)
  2358       handle_pou(handle_program,symbol->program_type_name, symbol)
  2316       return NULL;
  2359       return NULL;
  2317     }
  2360     }
  2318     
  2361     
  2319 
  2362 
  2320 /********************************/
  2363 /********************************/