lbessard@70: /*
Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3
lbessard@70: *
Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
lbessard@70: *
Edouard@279: * This program is free software: you can redistribute it and/or modify
Edouard@279: * it under the terms of the GNU General Public License as published by
Edouard@279: * the Free Software Foundation, either version 3 of the License, or
Edouard@279: * (at your option) any later version.
Edouard@279: *
Edouard@279: * This program is distributed in the hope that it will be useful,
Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of
Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Edouard@279: * GNU General Public License for more details.
Edouard@279: *
Edouard@279: * You should have received a copy of the GNU General Public License
Edouard@279: * along with this program. If not, see .
Edouard@279: *
lbessard@70: *
lbessard@70: * This code is made available on the understanding that it will not be
lbessard@70: * used in safety-critical situations without a full and competent review.
lbessard@70: */
lbessard@70:
etisserant@139: #include
lbessard@70:
lbessard@70:
lbessard@70:
mjsousa@945: /* This file cotains two main classes:
mjsousa@945: * - generate_c_base_c
mjsousa@945: * - generate_c_base_and_typeid_c
mjsousa@945: *
mjsousa@945: * generate_c_base_c
mjsousa@945: * -----------------
mjsousa@945: * This class generates C code for all literals and varables. In short, all the basic stuff
mjsousa@945: * that will probably be needed in all other code generating classes.
mjsousa@945: * It does not however handle derived datatypes.
mjsousa@945: *
mjsousa@945: * generate_c_base_and_typeid_c
mjsousa@945: * ----------------------------
mjsousa@945: * This is similar to the generate_c_base_c (from which it inherits), but it also handles
mjsousa@945: * all the derived datatypes. Note that it does not generate C code for the declaration of
mjsousa@945: * those datatypes (that is what generate_c_typedecl_c is for), but rather it merely
mjsousa@945: * generates the name/id of a derived datatype.
mjsousa@945: * Note too that not all derived datatypes in the IEC 61131-3 have a name (for example,
mjsousa@945: * VAR a: ARRAY [3..5] of INT END_VAR), in which case an alias for this datatype should
mjsousa@945: * have been previously generated by either generate_c_typedecl_c or generate_implicit_typedecl_c.
mjsousa@945: */
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
lbessard@70:
lbessard@149: typedef struct
lbessard@149: {
laurent@217: symbol_c *param_name;
lbessard@149: symbol_c *param_value;
lbessard@149: symbol_c *param_type;
lbessard@149: function_param_iterator_c::param_direction_t param_direction;
lbessard@149: } FUNCTION_PARAM;
lbessard@149:
laurent@217: #define DECLARE_PARAM_LIST()\
laurent@217: std::list param_list;\
laurent@217: std::list::iterator pt;\
laurent@217: FUNCTION_PARAM *param;
laurent@217:
laurent@217: #define ADD_PARAM_LIST(name, value, type, direction)\
lbessard@149: param = new FUNCTION_PARAM;\
laurent@217: param->param_name = name;\
lbessard@149: param->param_value = value;\
lbessard@149: param->param_type = type;\
lbessard@149: param->param_direction = direction;\
laurent@217: param_list.push_back(param);
laurent@217:
laurent@217: #define PARAM_LIST_ITERATOR() for(pt = param_list.begin(); pt != param_list.end(); pt++)
laurent@217:
laurent@217: #define PARAM_NAME (*pt)->param_name
laurent@217: #define PARAM_VALUE (*pt)->param_value
laurent@217: #define PARAM_TYPE (*pt)->param_type
laurent@217: #define PARAM_DIRECTION (*pt)->param_direction
laurent@217:
laurent@217: #define CLEAR_PARAM_LIST()\
laurent@217: PARAM_LIST_ITERATOR()\
laurent@217: delete *pt;\
laurent@217: param_list.clear();
lbessard@70:
lbessard@70:
mjsousa@945: /* generate_c_base_c
mjsousa@945: * -----------------
mjsousa@945: * This class generates C code for all literals and varables. In short, all the basic stuff
mjsousa@945: * that will probably be needed in all other code generating classes.
mjsousa@945: * It does not however handle derived datatypes.
mjsousa@945: */
lbessard@70: class generate_c_base_c: public iterator_visitor_c {
lbessard@70:
lbessard@70: protected:
lbessard@70: stage4out_c &s4o;
lbessard@70:
lbessard@70: private:
lbessard@70: /* Unlike programs that are mapped onto C++ classes, Function Blocks are mapped onto a data structure type
laurent@217: * and a separate function containing the code. This function is passed a pointer to an instance of the data
lbessard@70: * structure. This means that the code inside the functions must insert a pointer to the data structure whenever
lbessard@70: * it wishes to access a Function Block variable.
lbessard@70: * The variable_prefix_ variable will contain the correct string which needs to be prefixed to all variable accesses.
lbessard@70: * This string is set with the set_variable_prefix() member function.
lbessard@70: */
lbessard@70: const char *variable_prefix_;
lbessard@70:
lbessard@70: public:
laurent@217: generate_c_base_c(stage4out_c *s4o_ptr): s4o(*s4o_ptr) {
laurent@217: variable_prefix_ = NULL;
laurent@217: }
lbessard@70: ~generate_c_base_c(void) {}
lbessard@70:
lbessard@70: void set_variable_prefix(const char *variable_prefix) {variable_prefix_ = variable_prefix;}
lbessard@160: const char *get_variable_prefix(void) {return variable_prefix_;}
lbessard@146: bool is_variable_prefix_null(void) {return variable_prefix_ == NULL;}
lbessard@70: void print_variable_prefix(void) {
lbessard@70: if (variable_prefix_ != NULL)
lbessard@70: s4o.print(variable_prefix_);
lbessard@70: }
lbessard@70:
mjsousa@877: void print_line_directive(symbol_c *symbol) {
mjsousa@878: if (!generate_line_directives__) return; /* global variable generate_line_directives__ is defined in generate_c.cc */
mjsousa@877: s4o.print("#line ");
mjsousa@877: s4o.print(symbol->first_line);
mjsousa@877: s4o.print(" \"");
mjsousa@877: s4o.print(symbol->first_file);
mjsousa@877: s4o.print("\"\n");
mjsousa@877: }
mjsousa@877:
lbessard@70: void *print_token(token_c *token, int offset = 0) {
lbessard@70: return s4o.printupper((token->value)+offset);
lbessard@70: }
lbessard@70:
lbessard@70: void *print_literal(symbol_c *type, symbol_c *value) {
lbessard@70: s4o.print("__");
lbessard@70: type->accept(*this);
lbessard@70: s4o.print("_LITERAL(");
lbessard@70: value->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *print_striped_token(token_c *token, int offset = 0) {
lbessard@70: std::string str = "";
laurent@312: bool leading_zero = true;
laurent@312: for (unsigned int i = offset; i < strlen(token->value); i++) {
mjsousa@927: if (leading_zero
mjsousa@927: && ( token->value[i] != '0'
mjsousa@927: || i == strlen(token->value) - 1
mjsousa@927: || token->value[i + 1] == '.'
mjsousa@927: )
mjsousa@927: )
laurent@312: leading_zero = false;
mjsousa@927: if (!leading_zero && token->value[i] != '_')
lbessard@70: str += token->value[i];
laurent@312: }
lbessard@70: return s4o.printupper(str);
lbessard@70: }
lbessard@70:
lbessard@70: void *print_striped_binary_token(token_c *token, unsigned int offset = 0) {
lbessard@70: /* convert the binary value to hexadecimal format... */
lbessard@70: unsigned char value, bit_mult;
lbessard@70: unsigned int i;
lbessard@70: int total_bits;
lbessard@70: char str[2] = {'A', '\0'}; /* since the s4o object is not prepared to print out one character at a time... */
lbessard@70:
lbessard@70: s4o.print("0x");
lbessard@70:
lbessard@70: total_bits = 0;
lbessard@70: for (i = offset; i < strlen(token->value); i++)
lbessard@70: if (token->value[i] != '_')
mjsousa@927: total_bits++;
lbessard@70:
lbessard@70: value = 0;
lbessard@70: bit_mult = (unsigned char)1 << (((total_bits+3)%4)+1);
lbessard@70: for (i = offset; i < strlen(token->value); i++) {
lbessard@70: if (token->value[i] != '_') {
mjsousa@927: bit_mult /= 2;
mjsousa@927: value += bit_mult * ((token->value[i] == '0')? 0:1);
mjsousa@927: if (bit_mult == 1) {
mjsousa@927: str[0] = (value <= 9)? (char)'0' + value : (char)'A' + value - 10;
mjsousa@927: s4o.print(str);
lbessard@70: bit_mult = 0x10;
lbessard@70: value = 0;
mjsousa@927: }
mjsousa@927: }
lbessard@70: }
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *print_list(list_c *list,
mjsousa@927: std::string pre_elem_str = "",
mjsousa@927: std::string inter_elem_str = "",
mjsousa@927: std::string post_elem_str = "",
mjsousa@927: visitor_c *visitor = NULL) {
lbessard@70: if (visitor == NULL) visitor = this;
lbessard@70:
lbessard@70: if (list->n > 0) {
lbessard@70: //std::cout << "generate_c_base_c::print_list(n = " << list->n << ") 000\n";
lbessard@70: s4o.print(pre_elem_str);
msousa@1041: list->get_element(0)->accept(*visitor);
lbessard@70: }
lbessard@70:
lbessard@70: for(int i = 1; i < list->n; i++) {
lbessard@70: //std::cout << "generate_c_base_c::print_list " << i << "\n";
lbessard@70: s4o.print(inter_elem_str);
msousa@1041: list->get_element(i)->accept(*visitor);
lbessard@70: }
lbessard@70:
lbessard@70: if (list->n > 0)
lbessard@70: s4o.print(post_elem_str);
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: void *print_binary_expression(symbol_c *l_exp,
mjsousa@927: symbol_c *r_exp,
mjsousa@927: const char *operation) {
lbessard@70: s4o.print("(");
lbessard@70: l_exp->accept(*this);
lbessard@70: s4o.print(operation);
lbessard@70: r_exp->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *print_unary_expression(symbol_c *exp,
mjsousa@927: const char *operation) {
lbessard@70: s4o.print(operation);
lbessard@70: s4o.print("(");
lbessard@70: exp->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *print_binary_function(const char *function,
lbessard@70: symbol_c *l_exp,
mjsousa@927: symbol_c *r_exp) {
lbessard@70: s4o.print(function);
lbessard@70: s4o.print("(");
lbessard@70: l_exp->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: r_exp->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
mjsousa@927: }
lbessard@70:
mjsousa@1011: /* Call a standard library function that does a comparison (GT, NE, EQ, LT, ...)
mjsousa@1011: * NOTE: Typically, the function will have the following parameters:
mjsousa@1011: * 1st parameter: EN (enable)
mjsousa@1011: * 2nd parameter: ENO (enable output)
mjsousa@1011: * 3rd parameter: number of operands we will be passing (required because we are calling an extensible standard function!)
mjsousa@1011: * 4th parameter: the left hand side of the comparison expression (in out case, the IL implicit variable)
mjsousa@1011: * 4th parameter: the right hand side of the comparison expression (in out case, current operand)
mjsousa@1011: *
mjsousa@1011: * The 1st and 2nd parameter may not be present, only issue them if NE and ENO are being generated!
mjsousa@1011: * The 3rd parameter must not be generated when the 'NE' function is called (it is not an extensible function!)
mjsousa@1011: *
mjsousa@1011: * NOTE: To implement this correctly, this function should really instantiate a
mjsousa@1011: * function_invocation_c and have the generate_c visitor generate the code automatically for this
mjsousa@1011: * function invocation. However, the code for function invocations is currently duplicated
mjsousa@1011: * for IL and ST. Until this code is not re-formulated into a single piece of general code, for now
mjsousa@1011: * we generate the function call directly here in print_compare_function()
mjsousa@1011: */
lbessard@70: void *print_compare_function(const char *function,
lbessard@70: symbol_c *compare_type,
lbessard@70: symbol_c *l_exp,
lbessard@70: symbol_c *r_exp) {
mjsousa@1011: // Print out the name of the function we will call.
mjsousa@1011: // It will be something like LE_TIME, LE_DATE, GT_DATE, ...
mjsousa@1011: // (in other words, we are calling an overloaded function!)
mjsousa@1011: s4o.print(function); // the GT, LE, ... part
mjsousa@1011: s4o.print("_"); // the '_' part...
mjsousa@1011: compare_type->accept(*this); // the TIME, DATE, ... part.
mjsousa@1011: s4o.print("("); // start of parameters to function call...
mjsousa@1011: // Determine whether this function has the EN parameter
mjsousa@1011: // (we just check the base LE, GT, .. function, as it should have
mjsousa@1011: // the same parameters as the overloaded function!)
mjsousa@1011: function_symtable_t::iterator lower = function_symtable.lower_bound(function);
mjsousa@1011: if (lower == function_symtable.end()) ERROR; // We want to call a function that does not exist!!?? Hmm...
mjsousa@1011: search_var_instance_decl_c search_var(function_symtable.get_value(lower));
mjsousa@1011: identifier_c en_var("EN");
mjsousa@1011: identifier_c eno_var("ENO");
mjsousa@1011: if (search_var.get_vartype(& en_var) == search_var_instance_decl_c::input_vt)
mjsousa@1011: s4o.print("__BOOL_LITERAL(TRUE), "); // function has EN parameter, pass TRUE
mjsousa@1011: if (search_var.get_vartype(&eno_var) == search_var_instance_decl_c::output_vt)
mjsousa@1011: s4o.print("NULL, "); // function has ENO parameter, pass NULL
mjsousa@1011: if (strcmp(function, "NE") != 0) // All comparison library functions are extensible, except for 'NE'!!
mjsousa@1011: s4o.print("2, "); // function is extensible, so must first pass the number of parameters that follow
lbessard@70: l_exp->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: r_exp->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
laurent@217: void *print_check_function(symbol_c *type,
laurent@217: symbol_c *value,
laurent@217: symbol_c *fb_name = NULL,
laurent@217: bool temp = false) {
msousa@854: if (!get_datatype_info_c::is_type_valid(type)) ERROR;
msousa@854: bool is_subrange = get_datatype_info_c::is_subrange(type);
laurent@217: if (is_subrange) {
mjsousa@927: s4o.print("__CHECK_");
mjsousa@927: type->accept(*this);
mjsousa@927: s4o.print("(");
laurent@217: }
laurent@217: if (fb_name != NULL) {
laurent@240: s4o.print(GET_VAR);
laurent@240: s4o.print("(");
laurent@217: print_variable_prefix();
laurent@217: fb_name->accept(*this);
laurent@217: s4o.print(".");
laurent@240: value->accept(*this);
laurent@240: s4o.print(")");
laurent@217: }
laurent@240: else {
laurent@240: if (temp)
mjsousa@927: s4o.print(TEMP_VAR);
laurent@240: value->accept(*this);
laurent@240: }
laurent@217: if (is_subrange)
laurent@240: s4o.print(")");
laurent@217: return NULL;
laurent@217: }
laurent@217:
msousa@267: /********************/
msousa@267: /* 2.1.6 - Pragmas */
msousa@267: /********************/
msousa@267: void *visit(enable_code_generation_pragma_c * symbol) {s4o.enable_output(); return NULL;}
msousa@267: void *visit(disable_code_generation_pragma_c * symbol) {s4o.disable_output(); return NULL;}
msousa@267:
lbessard@70: /* Do not use print_token() as it will change everything into uppercase */
Edouard@634: void *visit(pragma_c *symbol) {
Edouard@634: s4o.print("#define GetFbVar(var,...) __GET_VAR(data__->var,__VA_ARGS__)\n");
Edouard@634: s4o.print(s4o.indent_spaces);
mjsousa@886: s4o.print("#define SetFbVar(var,val,...) __SET_VAR(data__->,var,__VA_ARGS__,val)\n");
Edouard@634: s4o.print(symbol->value);
Edouard@634: s4o.print("\n");
Edouard@634: s4o.print(s4o.indent_spaces);
Edouard@634: s4o.print("#undef GetFbVar\n");
Edouard@634: s4o.print(s4o.indent_spaces);
Edouard@634: s4o.print("#undef SetFbVar\n");
Edouard@634: return NULL;
Edouard@634: }
lbessard@70:
lbessard@70:
lbessard@70: /***************************/
lbessard@70: /* B 0 - Programming Model */
lbessard@70: /***************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: /*************************/
lbessard@70: /* B.1 - Common elements */
lbessard@70: /*************************/
lbessard@70: /*******************************************/
lbessard@70: /* B 1.1 - Letters, digits and identifiers */
lbessard@70: /*******************************************/
mjsousa@958: void *visit( identifier_c *symbol) {return print_token(symbol);}
mjsousa@958: void *visit( poutype_identifier_c *symbol) {return print_token(symbol);}
mjsousa@945: /* If you need the derived_datatype_identifier_c visitor, then you should probably be
mjsousa@945: * inheriting from generate_c_base_and_typeid_c and not generate_c_base_c !!
mjsousa@945: */
mjsousa@945: void *visit(derived_datatype_identifier_c *symbol) {ERROR; return NULL;}
lbessard@70:
lbessard@70: /*********************/
lbessard@70: /* B 1.2 - Constants */
lbessard@70: /*********************/
lbessard@70: /* originally empty... */
lbessard@70:
mjsousa@919: /*********************************/
mjsousa@919: /* B 1.2.XX - Reference Literals */
mjsousa@919: /*********************************/
mjsousa@919: /* defined in IEC 61131-3 v3 - Basically the 'NULL' keyword! */
mjsousa@919: void *visit(ref_value_null_literal_c *symbol) {s4o.print("NULL"); return NULL;}
mjsousa@919:
lbessard@70: /******************************/
lbessard@70: /* B 1.2.1 - Numeric Literals */
lbessard@70: /******************************/
lbessard@70: void *visit(real_c *symbol) {return print_striped_token(symbol);}
lbessard@70: void *visit(integer_c *symbol) {return print_striped_token(symbol);}
lbessard@70: void *visit(binary_integer_c *symbol) {return print_striped_binary_token(symbol, 2);}
lbessard@70: void *visit(octal_integer_c *symbol) {s4o.print("0"); return print_striped_token(symbol, 2);}
lbessard@70: void *visit(hex_integer_c *symbol) {s4o.print("0x"); return print_striped_token(symbol, 3);}
lbessard@70:
msousa@257: void *visit(neg_real_c *symbol) {
msousa@257: s4o.print("-");
msousa@257: symbol->exp->accept(*this);
msousa@257: return NULL;
msousa@257: }
msousa@257:
msousa@257: void *visit(neg_integer_c *symbol) {
msousa@257: s4o.print("-");
msousa@257: symbol->exp->accept(*this);
msousa@257: return NULL;
msousa@257: }
msousa@257:
lbessard@70: void *visit(integer_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
lbessard@70: void *visit(real_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
lbessard@70: void *visit(bit_string_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
msousa@257: void *visit(boolean_literal_c *symbol) {
msousa@257: if (NULL != symbol->type)
msousa@257: return print_literal(symbol->type, symbol->value);
msousa@257: else {
mjsousa@927: bool_type_name_c bool_type;
mjsousa@927: return print_literal(&bool_type, symbol->value);
msousa@257: }
msousa@257: }
lbessard@70:
lbessard@70: /* helper class for boolean_literal_c */
lbessard@70: void *visit(boolean_true_c *symbol) {s4o.print("TRUE"); return NULL;}
lbessard@70: void *visit(boolean_false_c *symbol) {s4o.print("FALSE"); return NULL;}
lbessard@70:
lbessard@98: void *visit(neg_expression_c *symbol) {
lbessard@98: s4o.print("-");
lbessard@98: symbol->exp->accept(*this);
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@70: /*******************************/
lbessard@70: /* B.1.2.2 Character Strings */
lbessard@70: /*******************************/
lbessard@70: void *visit(double_byte_character_string_c *symbol) {
lbessard@70: // TO DO ...
lbessard@70: ERROR;
lbessard@70: return print_token(symbol);
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(single_byte_character_string_c *symbol) {
lbessard@70: std::string str = "";
lbessard@70: unsigned int count = 0;
lbessard@70: str += '"';
lbessard@70: /* we ignore the first and last bytes, they will be the character ' */
lbessard@70: for (unsigned int i = 1; i < strlen(symbol->value) - 1; i++) {
lbessard@70: char c = symbol->value[i];
lbessard@70: if ((c == '\\') || (c == '"'))
lbessard@70: {str += '\\'; str += c; count ++; continue;}
lbessard@70: if (c != '$')
lbessard@70: {str += c; count++; continue;}
lbessard@70: /* this should be safe, since the code has passed the syntax parser!! */
lbessard@70: c = symbol->value[++i];
lbessard@70: switch (c) {
lbessard@70: case '$':
lbessard@70: case '\'':
lbessard@70: {str += c; count++; continue;}
lbessard@70: case 'L':
lbessard@70: case 'l':
lbessard@70: {str += "\x0A"; /* LF */; count++; continue;}
lbessard@70: case 'N':
lbessard@70: case 'n':
lbessard@70: {str += "\\x0A"; /* NL */; count++; continue;}
lbessard@70: case 'P':
lbessard@70: case 'p':
lbessard@70: {str += "\\f"; /* FF */; count++; continue;}
lbessard@70: case 'R':
lbessard@70: case 'r':
lbessard@70: {str += "\\r"; /* CR */; count++; continue;}
lbessard@70: case 'T':
lbessard@70: case 't':
lbessard@70: {str += "\\t"; /* tab */; count++; continue;}
lbessard@70: default: {
lbessard@70: if (isxdigit(c)) {
lbessard@70: /* this should be safe, since the code has passed the syntax parser!! */
mjsousa@927: char c2 = symbol->value[++i];
mjsousa@927: if (isxdigit(c2)) {
mjsousa@927: str += '\\'; str += 'x'; str += c; str += c2;
mjsousa@927: count++; continue;
mjsousa@927: }
mjsousa@927: }
lbessard@70: }
lbessard@70: /* otherwise we have an invalid string!! */
lbessard@70: /* This should not have got through the syntax parser! */
lbessard@70: ERROR;
lbessard@70: } /* switch() */
lbessard@70: } /* for() */
lbessard@70:
lbessard@70: str += '"';
laurent@217: s4o.print("__STRING_LITERAL(");
msousa@594: s4o.print(count);
lbessard@70: s4o.print(",");
lbessard@70: s4o.print(str);
laurent@217: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /***************************/
lbessard@70: /* B 1.2.3 - Time Literals */
lbessard@70: /***************************/
lbessard@70:
lbessard@70: /************************/
lbessard@70: /* B 1.2.3.1 - Duration */
lbessard@70: /************************/
lbessard@70: /* The following output is actually the parameters to the constructor of the TIME class! */
lbessard@70:
lbessard@70: /* SYM_REF0(neg_time_c) */
lbessard@70: void *visit(neg_time_c *symbol) {s4o.print("-1"); /* negative time value */; return NULL;}
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_REF2(duration_c, neg, interval) */
lbessard@70: void *visit(duration_c *symbol) {
lbessard@70: TRACE("duration_c");
lbessard@70: s4o.print("__time_to_timespec(");
msousa@547:
msousa@547: if (NULL == symbol->neg) s4o.print("1"); /* positive time value */
msousa@547: else symbol->neg->accept(*this); /* this will print '-1' :-) */
msousa@547:
msousa@547: s4o.print(", ");
lbessard@70: symbol->interval->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_TOKEN(fixed_point_c) */
laurent@312: void *visit(fixed_point_c *symbol) {return print_striped_token(symbol);}
lbessard@70:
msousa@547: /* SYM_REF5(interval_c, days, hours, minutes, seconds, milliseconds) */
msousa@547: void *visit(interval_c *symbol) {
msousa@547: TRACE("interval_c");
msousa@547: /* s4o.print("0, 0, 0, 0, 0"); // milliseconds, seconds, minutes, hours, days */
msousa@547: if (NULL == symbol->milliseconds) s4o.print("0"); /* milliseconds */
msousa@547: else symbol->milliseconds->accept(*this);
msousa@547: s4o.print(", ");
msousa@547:
msousa@547: if (NULL == symbol->seconds) s4o.print("0"); /* seconds */
msousa@547: else symbol->seconds->accept(*this);
msousa@547: s4o.print(", ");
msousa@547:
msousa@547: if (NULL == symbol->minutes) s4o.print("0"); /* minutes */
msousa@547: else symbol->minutes->accept(*this);
msousa@547: s4o.print(", ");
msousa@547:
msousa@547: if (NULL == symbol->hours) s4o.print("0"); /* hours */
msousa@547: else symbol->hours->accept(*this);
msousa@547: s4o.print(", ");
msousa@547:
msousa@547: if (NULL == symbol->days) s4o.print("0"); /* days */
msousa@547: else symbol->days->accept(*this);
msousa@547:
msousa@547: return NULL;
msousa@547: }
msousa@547:
lbessard@70:
lbessard@70: /************************************/
lbessard@70: /* B 1.2.3.2 - Time of day and Date */
lbessard@70: /************************************/
lbessard@70:
lbessard@70: /* SYM_REF2(time_of_day_c, daytime, unused) */
lbessard@70: void *visit(time_of_day_c *symbol) {
lbessard@70: TRACE("time_of_day_c");
lbessard@70: s4o.print("__tod_to_timespec(");
lbessard@70: symbol->daytime->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_REF4(daytime_c, day_hour, day_minute, day_second, unused) */
lbessard@70: void *visit(daytime_c *symbol) {
lbessard@70: TRACE("daytime_c");
lbessard@70: symbol->day_second->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: symbol->day_minute->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: symbol->day_hour->accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_REF2(date_c, date_literal, unused) */
lbessard@70: void *visit(date_c *symbol) {
lbessard@70: TRACE("date_c");
lbessard@70: s4o.print("__date_to_timespec(");
lbessard@70: symbol->date_literal->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_REF4(date_literal_c, year, month, day, unused) */
lbessard@70: void *visit(date_literal_c *symbol) {
lbessard@70: TRACE("date_literal_c");
lbessard@70: symbol->day->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: symbol->month->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: symbol->year->accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* SYM_REF2(date_and_time_c, date_literal, daytime) */
lbessard@70: void *visit(date_and_time_c *symbol) {
lbessard@70: TRACE("date_and_time_c");
lbessard@70: s4o.print("__dt_to_timespec(");
lbessard@70: symbol->daytime->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: symbol->date_literal->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /**********************/
lbessard@70: /* B.1.3 - Data types */
lbessard@70: /**********************/
lbessard@70: /***********************************/
lbessard@70: /* B 1.3.1 - Elementary Data Types */
lbessard@70: /***********************************/
msousa@257: void *visit(time_type_name_c *symbol) {s4o.print("TIME"); return NULL;}
msousa@257: void *visit(bool_type_name_c *symbol) {s4o.print("BOOL"); return NULL;}
msousa@257: void *visit(sint_type_name_c *symbol) {s4o.print("SINT"); return NULL;}
msousa@257: void *visit(int_type_name_c *symbol) {s4o.print("INT"); return NULL;}
msousa@257: void *visit(dint_type_name_c *symbol) {s4o.print("DINT"); return NULL;}
msousa@257: void *visit(lint_type_name_c *symbol) {s4o.print("LINT"); return NULL;}
msousa@257: void *visit(usint_type_name_c *symbol) {s4o.print("USINT"); return NULL;}
msousa@257: void *visit(uint_type_name_c *symbol) {s4o.print("UINT"); return NULL;}
msousa@257: void *visit(udint_type_name_c *symbol) {s4o.print("UDINT"); return NULL;}
msousa@257: void *visit(ulint_type_name_c *symbol) {s4o.print("ULINT"); return NULL;}
msousa@257: void *visit(real_type_name_c *symbol) {s4o.print("REAL"); return NULL;}
msousa@257: void *visit(lreal_type_name_c *symbol) {s4o.print("LREAL"); return NULL;}
msousa@257: void *visit(date_type_name_c *symbol) {s4o.print("DATE"); return NULL;}
msousa@257: void *visit(tod_type_name_c *symbol) {s4o.print("TOD"); return NULL;}
msousa@257: void *visit(dt_type_name_c *symbol) {s4o.print("DT"); return NULL;}
msousa@257: void *visit(byte_type_name_c *symbol) {s4o.print("BYTE"); return NULL;}
msousa@257: void *visit(word_type_name_c *symbol) {s4o.print("WORD"); return NULL;}
msousa@257: void *visit(lword_type_name_c *symbol) {s4o.print("LWORD"); return NULL;}
msousa@257: void *visit(dword_type_name_c *symbol) {s4o.print("DWORD"); return NULL;}
msousa@257: void *visit(string_type_name_c *symbol) {s4o.print("STRING"); return NULL;}
msousa@257: void *visit(wstring_type_name_c *symbol) {s4o.print("WSTRING"); return NULL;}
msousa@257:
msousa@257: void *visit(safetime_type_name_c *symbol) {s4o.print("TIME"); return NULL;}
msousa@257: void *visit(safebool_type_name_c *symbol) {s4o.print("BOOL"); return NULL;}
msousa@257: void *visit(safesint_type_name_c *symbol) {s4o.print("SINT"); return NULL;}
msousa@257: void *visit(safeint_type_name_c *symbol) {s4o.print("INT"); return NULL;}
msousa@257: void *visit(safedint_type_name_c *symbol) {s4o.print("DINT"); return NULL;}
msousa@257: void *visit(safelint_type_name_c *symbol) {s4o.print("LINT"); return NULL;}
msousa@257: void *visit(safeusint_type_name_c *symbol) {s4o.print("USINT"); return NULL;}
msousa@257: void *visit(safeuint_type_name_c *symbol) {s4o.print("UINT"); return NULL;}
msousa@257: void *visit(safeudint_type_name_c *symbol) {s4o.print("UDINT"); return NULL;}
msousa@257: void *visit(safeulint_type_name_c *symbol) {s4o.print("ULINT"); return NULL;}
msousa@257: void *visit(safereal_type_name_c *symbol) {s4o.print("REAL"); return NULL;}
msousa@257: void *visit(safelreal_type_name_c *symbol) {s4o.print("LREAL"); return NULL;}
msousa@257: void *visit(safedate_type_name_c *symbol) {s4o.print("DATE"); return NULL;}
msousa@257: void *visit(safetod_type_name_c *symbol) {s4o.print("TOD"); return NULL;}
msousa@257: void *visit(safedt_type_name_c *symbol) {s4o.print("DT"); return NULL;}
msousa@257: void *visit(safebyte_type_name_c *symbol) {s4o.print("BYTE"); return NULL;}
msousa@257: void *visit(safeword_type_name_c *symbol) {s4o.print("WORD"); return NULL;}
msousa@257: void *visit(safelword_type_name_c *symbol) {s4o.print("LWORD"); return NULL;}
msousa@257: void *visit(safedword_type_name_c *symbol) {s4o.print("DWORD"); return NULL;}
msousa@257: void *visit(safestring_type_name_c *symbol) {s4o.print("STRING"); return NULL;}
msousa@257: void *visit(safewstring_type_name_c *symbol) {s4o.print("WSTRING"); return NULL;}
lbessard@70:
mjsousa@1016: void *visit(void_type_name_c *symbol) {s4o.print("void"); return NULL;}
mjsousa@1016:
lbessard@70: /********************************/
lbessard@70: /* B.1.3.2 - Generic data types */
lbessard@70: /********************************/
mjsousa@923: /* Currently only used in REF_TO ANY, which is mapped onto (void *) */
mjsousa@923: void *visit(generic_type_any_c *symbol) {s4o.print("void"); return NULL;}
lbessard@70:
mjsousa@945:
lbessard@70: /********************************/
lbessard@70: /* B 1.3.3 - Derived data types */
lbessard@70: /********************************/
mjsousa@945:
mjsousa@860: /* enumerated_type_name '#' identifier */
mjsousa@860: void *visit(enumerated_value_c *symbol) {
mjsousa@860: if (NULL == symbol->datatype) {
mjsousa@860: debug_c::print(symbol);
mjsousa@860: ERROR;
mjsousa@860: }
mjsousa@860:
mjsousa@860: symbol_c *type_name = get_datatype_info_c::get_id(symbol->datatype);
mjsousa@860: if (NULL == type_name) {
mjsousa@860: ERROR_MSG("C code generator does not currently support anonymous enumerated data types.");
mjsousa@860: }
mjsousa@860:
mjsousa@860: type_name->accept(*this);
mjsousa@860: s4o.print("__");
mjsousa@860: symbol->value->accept(*this);
mjsousa@860: return NULL;
mjsousa@860: }
mjsousa@860:
mjsousa@860:
lbessard@70: /*********************/
lbessard@70: /* B 1.4 - Variables */
lbessard@70: /*********************/
lbessard@70: void *visit(symbolic_variable_c *symbol) {
lbessard@70: TRACE("symbolic_variable_c");
lbessard@70: this->print_variable_prefix();
lbessard@70: symbol->var_name->accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
mjsousa@966: /* symbolic_constant_c is used only when a variable is used inside the subrange of an array declaration
mjsousa@966: * e.g.: ARRAY [1 .. maxval] OF INT
mjsousa@966: * where maxval is a CONSTANT variable.
mjsousa@966: * When maxval shows up in the POU body, it will be stored as a standard symbolic_variable_c in the AST.
mjsousa@966: * When maxval shows up in the ARRAY declaration, it will be stored as a symbolic_constant_c in the AST.
mjsousa@966: * This will allow us to more easily handle this special case, without affecting the remaining working code.
mjsousa@966: */
mjsousa@966: // a non-standard extension!!
mjsousa@966: void *visit(symbolic_constant_c *symbol) {
mjsousa@966: TRACE("symbolic_variable_c");
mjsousa@966: if (symbol->const_value. _int64.is_valid()) s4o.print(symbol->const_value. _int64.get());
mjsousa@966: else if (symbol->const_value._uint64.is_valid()) s4o.print(symbol->const_value._uint64.get());
mjsousa@966: else ERROR;
mjsousa@966: return NULL;
mjsousa@966: }
mjsousa@945:
lbessard@70: /********************************************/
lbessard@70: /* B.1.4.1 Directly Represented Variables */
lbessard@70: /********************************************/
lbessard@70: void *visit(direct_variable_c *symbol) {
lbessard@70: TRACE("direct_variable_c");
lbessard@70: /* Do not use print_token() as it will change everything into uppercase */
mjsousa@944: return s4o.printlocation(symbol->value+1); // '+1' so we do not print the '%' in '%IW3.2'
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /*************************************/
lbessard@70: /* B.1.4.2 Multi-element Variables */
lbessard@70: /*************************************/
lbessard@70: /* subscripted_variable '[' subscript_list ']' */
mjsousa@945: //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
mjsousa@945:
lbessard@70:
lbessard@70: /* record_variable '.' field_selector */
lbessard@70: /* WARNING: input and/or output variables of function blocks
lbessard@70: * may be accessed as fields of a structured variable!
lbessard@70: * Code handling a structured_variable_c must take
lbessard@70: * this into account!
lbessard@70: */
lbessard@70: // SYM_REF2(structured_variable_c, record_variable, field_selector)
mjsousa@936: // TODO: It seems to me this code no longer gets to execute, since the function is overloaded in generate_c_st_c and generate_c_il_c
mjsousa@936: // I will have to check this later, and delete this code if the above is really true!
lbessard@70: void *visit(structured_variable_c *symbol) {
lbessard@70: TRACE("structured_variable_c");
lbessard@70: symbol->record_variable->accept(*this);
lbessard@70: s4o.print(".");
lbessard@70: symbol->field_selector->accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /******************************************/
lbessard@70: /* B 1.4.3 - Declaration & Initialisation */
lbessard@70: /******************************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /**************************************/
lbessard@70: /* B.1.5 - Program organization units */
lbessard@70: /**************************************/
lbessard@70: /***********************/
lbessard@70: /* B 1.5.1 - Functions */
lbessard@70: /***********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*****************************/
lbessard@70: /* B 1.5.2 - Function Blocks */
lbessard@70: /*****************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /**********************/
lbessard@70: /* B 1.5.3 - Programs */
lbessard@70: /**********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*********************************************/
lbessard@70: /* B.1.6 Sequential function chart elements */
lbessard@70: /*********************************************/
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 1.7 Configuration elements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /****************************************/
lbessard@70: /* B.2 - Language IL (Instruction List) */
lbessard@70: /****************************************/
lbessard@70: /***********************************/
lbessard@70: /* B 2.1 Instructions and Operands */
lbessard@70: /***********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*******************/
lbessard@70: /* B 2.2 Operators */
lbessard@70: /*******************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70:
lbessard@70: /***************************************/
lbessard@70: /* B.3 - Language ST (Structured Text) */
lbessard@70: /***************************************/
lbessard@70: /***********************/
lbessard@70: /* B 3.1 - Expressions */
lbessard@70: /***********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************/
lbessard@70: /* B 3.2 Statements */
lbessard@70: /********************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*********************************/
lbessard@70: /* B 3.2.1 Assignment Statements */
lbessard@70: /*********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /*****************************************/
lbessard@70: /* B 3.2.2 Subprogram Control Statements */
lbessard@70: /*****************************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.3 Selection Statements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.4 Iteration Statements */
lbessard@70: /********************************/
lbessard@70: /* leave for derived classes... */
lbessard@70:
mario@177:
lbessard@70: }; /* class generate_c_basic_c */
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945: /************************************************************************************************/
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /* generate_c_base_and_typeid_c
mjsousa@945: * ----------------------------
mjsousa@945: * This is similar to the generate_c_base_c (from which it inherits), but it also handles
mjsousa@945: * all the derived datatypes. Note that it does not generate C code for the declaration of
mjsousa@945: * those datatypes (that is what generate_c_typedecl_c is for), but rather it merely
mjsousa@945: * generates the name/id of a derived datatype.
mjsousa@945: * Note too that not all derived datatypes in the IEC 61131-3 have a name (for example,
mjsousa@945: * VAR a: ARRAY [3..5] of INT END_VAR), in which case an alias for this datatype should
mjsousa@945: * have been previously generated by either generate_c_typedecl_c or generate_implicit_typedecl_c.
mjsousa@945: */
mjsousa@945: class generate_c_base_and_typeid_c: public generate_c_base_c {
mjsousa@945:
mjsousa@945: public:
mjsousa@945: generate_c_base_and_typeid_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) {}
mjsousa@945: ~generate_c_base_and_typeid_c(void) {}
mjsousa@945:
mjsousa@945:
mjsousa@945: /*************************/
mjsousa@945: /* B.1 - Common elements */
mjsousa@945: /*************************/
mjsousa@945: /*******************************************/
mjsousa@945: /* B 1.1 - Letters, digits and identifiers */
mjsousa@945: /*******************************************/
mjsousa@945: void *visit(derived_datatype_identifier_c *symbol) {
mjsousa@945: if (get_datatype_info_c::is_array(symbol->datatype)) {
mjsousa@945: return symbol->datatype->accept(*this);
mjsousa@945: }
mjsousa@945: return print_token(symbol);
mjsousa@945: }
mjsousa@945:
mjsousa@945: /*********************/
mjsousa@945: /* B 1.2 - Constants */
mjsousa@945: /*********************/
mjsousa@945: /**********************/
mjsousa@945: /* B.1.3 - Data types */
mjsousa@945: /**********************/
mjsousa@945: /***********************************/
mjsousa@945: /* B 1.3.1 - Elementary Data Types */
mjsousa@945: /***********************************/
mjsousa@945: /********************************/
mjsousa@945: /* B.1.3.2 - Generic data types */
mjsousa@945: /********************************/
mjsousa@945: /* Currently only used in REF_TO ANY, which is mapped onto (void *) */
mjsousa@945: void *visit(generic_type_any_c *symbol) {s4o.print("void"); return NULL;}
mjsousa@945:
mjsousa@945: /********************************/
mjsousa@945: /* B 1.3.3 - Derived data types */
mjsousa@945: /********************************/
mjsousa@945:
mjsousa@945: /* subrange_type_name ':' subrange_spec_init */
mjsousa@945: void *visit(subrange_type_declaration_c *symbol) {return symbol->subrange_type_name->accept(*this);}
mjsousa@945:
mjsousa@945: /* subrange_specification ASSIGN signed_integer */
mjsousa@945: void *visit(subrange_spec_init_c *symbol) {return symbol->subrange_specification->accept(*this);}
mjsousa@945:
mjsousa@945: /* integer_type_name '(' subrange')' */
mjsousa@945: void *visit(subrange_specification_c *symbol) {return symbol->integer_type_name->accept(*this);}
mjsousa@945:
mjsousa@945: /* enumerated_type_name ':' enumerated_spec_init */
mjsousa@945: void *visit(enumerated_type_declaration_c *symbol) {return symbol->enumerated_type_name->accept(*this);}
mjsousa@945:
mjsousa@945: /* enumerated_specification ASSIGN enumerated_value */
mjsousa@945: void *visit(enumerated_spec_init_c *symbol) {return symbol->enumerated_specification->accept(*this);}
mjsousa@945:
mjsousa@945:
mjsousa@945: /* enumerated_type_name '#' identifier */
mjsousa@945: /* Handled by generate_c_base_c class!!
mjsousa@945: void *visit(enumerated_value_c *symbol) {}
mjsousa@945: */
mjsousa@945:
mjsousa@945: /* identifier ':' array_spec_init */
mjsousa@945: void *visit(array_type_declaration_c *symbol) {
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (1 != implicit_id_count) ERROR;
mjsousa@945: return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
mjsousa@945: }
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /* array_specification [ASSIGN array_initialization] */
mjsousa@945: /* array_initialization may be NULL ! */
mjsousa@945: void *visit(array_spec_init_c *symbol) {
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (1 == implicit_id_count) return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
mjsousa@945: if (0 == implicit_id_count) return symbol->datatype->accept(*this);
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
mjsousa@945: void *visit(array_specification_c *symbol) {
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (1 != implicit_id_count) ERROR;
mjsousa@945: return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
mjsousa@945: }
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /* simple_type_name ':' simple_spec_init */
mjsousa@945: void *visit(simple_type_declaration_c *symbol) {return symbol->simple_type_name->accept(*this);}
mjsousa@945:
mjsousa@945: /* simple_specification [ASSIGN constant] */
mjsousa@945: //SYM_REF2(simple_spec_init_c, simple_specification, constant)
mjsousa@945: // may be NULL
mjsousa@945: void *visit(simple_spec_init_c *symbol) {return symbol->simple_specification->accept(*this);}
mjsousa@945:
mjsousa@945: /* structure_type_name ':' structure_specification */
mjsousa@945: //SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
mjsousa@945: void *visit(structure_type_declaration_c *symbol) {return symbol->structure_type_name->accept(*this);}
mjsousa@945:
mjsousa@945: /* structure_type_name ASSIGN structure_initialization */
mjsousa@945: /* structure_initialization may be NULL ! */
mjsousa@945: //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
mjsousa@945: void *visit(initialized_structure_c *symbol) {return symbol->structure_type_name->accept(*this);}
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */
mjsousa@945: // SYM_REF1(ref_spec_c, type_name)
mjsousa@945: void *visit(ref_spec_c *symbol) {
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (implicit_id_count > 1) ERROR;
mjsousa@945: if (implicit_id_count == 1) {
mjsousa@945: /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
mjsousa@945: * has already been defined. So, we simly print out the id of that C datatpe...
mjsousa@945: */
mjsousa@945: return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
mjsousa@945: }
mjsousa@945: /* This is NOT part of an implicitly declared datatype (i.e. we are being called from an visit(ref_type_decl_c *),
mjsousa@945: * through the visit(ref_spec_init_c*)), so we need to simply print out the name of the datatype we reference to.
mjsousa@945: */
mjsousa@945: //debug_c::print(symbol); ERROR;
mjsousa@945: symbol->type_name->accept(*this);
mjsousa@945: s4o.print("*");
mjsousa@945: return NULL;
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* For the moment, we do not support initialising reference data types */
mjsousa@945: /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */
mjsousa@945: /* NOTE: ref_initialization may be NULL!! */
mjsousa@945: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
mjsousa@945: void *visit(ref_spec_init_c *symbol) {
mjsousa@945: /* NOTE An ref_type_decl_c will be created in stage4 for each implicitly defined REF_TO datatype,
mjsousa@945: * and this generate_c_typedecl_c will be called to define that REF_TO datatype in C.
mjsousa@945: * However, every implictly defined REF_TO datatype with the exact same parameters will be mapped
mjsousa@945: * to the same identifier (e.g: __REF_TO_INT).
mjsousa@945: * In order for the C compiler not to find the same datatype being defined two or more times,
mjsousa@945: * we will keep track of the datatypes that have already been declared, and henceforth
mjsousa@945: * only declare the datatypes that have not been previously defined.
mjsousa@945: */
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (1 < implicit_id_count) ERROR;
mjsousa@945: if (1 == implicit_id_count)
mjsousa@945: return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
mjsousa@958: return symbol->ref_spec->accept(*this); // this is probably pointing to an ***_identifier_c !!
mjsousa@945: }
mjsousa@945:
mjsousa@945: /* ref_type_decl: identifier ':' ref_spec_init */
mjsousa@945: // SYM_REF2(ref_type_decl_c, ref_type_name, ref_spec_init)
mjsousa@945: void *visit(ref_type_decl_c *symbol) {
mjsousa@945: TRACE("ref_type_decl_c");
mjsousa@945: /* NOTE An ref_type_decl_c will be created in stage4 for each implicitly defined REF_TO datatype,
mjsousa@945: * and this generate_c_typedecl_c will be called to define that REF_TO datatype in C.
mjsousa@945: * However, every implictly defined REF_TO datatype with the exact same parameters will be mapped
mjsousa@945: * to the same identifier (e.g: __REF_TO_INT).
mjsousa@945: * In order for the C compiler not to find the same datatype being defined two or more times,
mjsousa@945: * we will keep track of the datatypes that have already been declared, and henceforth
mjsousa@945: * only declare the datatypes that have not been previously defined.
mjsousa@945: */
mjsousa@945: int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@945: if (0 != implicit_id_count) ERROR;
mjsousa@945: //symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(generate_c_base);
mjsousa@945: return symbol->ref_type_name->accept(*this);
mjsousa@945: }
mjsousa@945:
mjsousa@945:
mjsousa@945: }; /* class generate_c_base_and_typeid_c */
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945:
mjsousa@945: