practical_functions.h 11.1 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Buildsupport is (c) 2008-2015 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
#ifndef PRACTICAL_FUNCTIONS
#define PRACTICAL_FUNCTIONS

#include "my_types.h"
#include "c_ast_construction.h"

#define USE_PO_HI_ADA(fv) (fv->system_ast->context->polyorb_hi_c == 0)
#define USE_PO_HI_C(fv) (fv->system_ast->context->polyorb_hi_c == 1)

#define C_DRIVER (get_context()->polyorb_hi_c)

/* If defined, return the output path, otherwise an empty string */
//#define OUTPUT_PATH(fv) (NULL!=fv->system_ast->context->output?fv->system_ast->context->output:".")
#define OUTPUT_PATH (NULL!=get_context()->output?get_context()->output:".")

/* Return a string representing the selected encoding rules for a Parameter */
#define BINARY_ENCODING(p) (native==p->encoding)?"NATIVE":(uper==p->encoding)?"UPER":(acn==p->encoding)?"ACN":"#ERROR#"

Maxime Perrotin's avatar
Maxime Perrotin committed
22
#define LANGUAGE(fv) ada==fv->language?"Ada": c==fv->language?"C": sdl==fv->language?"OG": rtds==fv->language?"RTDS": scade==fv->language?"SCADE6":simulink==fv->language?"SIMULINK": blackbox_device==fv->language? "C": rhapsody==fv->language? "C": vhdl==fv->language?"VHDL": system_c==fv->language?"SYSTEM_C":gui==fv->language?"GUI": qgenc==fv->language?"QGenC": qgenada==fv->language?"QGenAda": cpp==fv->language?"CPP": "UNSUPPORTED_LANGUAGE!"
Maxime Perrotin's avatar
Maxime Perrotin committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

#define RCM_KIND(IF) (protected==IF->rcm?"protected":unprotected==IF->rcm?"unprotected":sporadic==IF->rcm?"sporadic":cyclic==IF->rcm?"cyclic":variator==IF->rcm?"variator":"unknown")

#define NATURE(fv) thread_runtime==fv->runtime_nature?"thread":passive_runtime==fv->runtime_nature?"passive":"unknown"

#define DIRECTION(i) PI==i->direction?"PI":"RI" 

#define PARAM_DIRECTION(p) param_in==p->param_direction?"param_in":"param_out"

#define SYNCHRONISM(i) synch==i->synchronism?"synch":"asynch"

#define BASIC_TYPE(p) sequenceof==p->basic_type?"sequenceof":sequence==p->basic_type?"sequence":enumerated==p->basic_type?"enumerated":set==p->basic_type?"set":setof==p->basic_type?"setof":integer==p->basic_type?"integer":boolean==p->basic_type?"boolean":real==p->basic_type?"real":choice==p->basic_type?"choice":octetstring==p->basic_type?"octetstring":string==p->basic_type?"string":"None"

#define DEBUG_MSG (get_context()->debug)

/* Error reporting */
#define ERROR(...) fprintf(stderr, __VA_ARGS__)
#define WARNING ERROR
#define INFO ERROR

/* Convert ASN.1 types: change '-' to '_' */
char *asn2underscore(char *, size_t);

/* Convert underscore ('_')to dash ('-') - create a new string */
char *underscore_to_dash(char *s, size_t len);

/* Remove any _objXXX suffixes possibly added by TASTE-IV/TASTE-CV */
size_t remove_objXXX_suffix (char *string, size_t len);

/* create a new lowercase string */
char *string_to_lower(char *str);

/* Create a string including malloc */
char *make_string(const char *fmt, ...);

/* Build a textual string */
char *build_string(char **, char *, size_t);

/* Build a textual string made of elements separated by commas */
char *build_comma_string(char **, char *, size_t);

/* Build a call to QGen init function, if it exists in generated code*/
char *Build_QGen_Init(char **, char *, char *, Language);

/* Checks if the string str is found in the file filename (return 1) or not (return 0) */
int Find_Str_In_File(char *filename, char *str);

/* Create a new file in a subdirectory */
int create_file (char *, char *, FILE **);

/* Checks if a file exists in a subdirectory */
int file_exists(char *, char *);

/* Close a file and reset the FILE handler */
void close_file (FILE **);

/* Free memory used by a Parameter type */
void Clear_Parameter(Parameter *);

/* Add an IN or OUT parameter to the list of parameters for the current PI or RI */
void Add_Param (Parameter **, Parameter_list **);

/* Free the memory of a Param_list type */
void Clear_Param_List (Parameter_list *);

/* Allocate memory for a new Interface */
void Create_Interface (Interface **);

/* Free the memory of an Interface data structure */
void Clear_Interface(Interface *);

/* Free the memory of an Interface_list chained list */
void Clear_Interfaces_List(Interface_list *);

/* Clear a FV_list type */
void Clear_FV_List (FV_list *p);

/* Allocate memory for a new FV */
void Create_FV (FV **);

/* Free the memory of an FV structure */
void Clear_FV(FV *);

void Clear_Device (Device *dev);
void Clear_Devices_List(Device_list * p);

void Create_APLC_binding(Aplc_binding **a);

/* Free memory used by an APLC binding type */
void Clear_APLC_binding(Aplc_binding *a);

/* Remove a binding from a process */
void Remove_Binding (FV *fv);

/* Allocate memory for a Process type */
void Create_Process (Process **p);

/* Free memory used by a Process type */
void Clear_Process(Process *p);

/* Clear a Processes_list type */
void Clear_Processes_List (Process_list *p);

/* Clear an Aplc_bindings_list type */
void Clear_Aplc_bindings_List (Aplc_binding_list *p);

/* Check if an interface has input or output parameters (can be used with ForEachWithParam)
 Like that:
  int hasparam=0;
  ForEachWithParam element of fv->interfaces, execute CheckForAsn1Params and update &hasparam thanks;
    if (hasparam){
      (...)
    }
*/
void CheckForAsn1Params (Interface *i, int *result);

/* Check if a list of input parameters match a list of output parameters. */
void CheckInOutParams (Parameter *p_in, Parameter_list *p_out);

/* Check if a FV has context parameters (exclude Directives, Timers, etc.) */
bool has_context_param(FV *fv);

/* Check if a FV contains a timer in its context parameters */
bool has_timer(FV *fv);

/* Return the list of timers of a given FV */
String_list *timers(FV *fv);

151 152
/* Return the number of Cyclic and Sporadic interfaces from a list */
int CountActivePI(Interface_list *interfaces);
Maxime Perrotin's avatar
Maxime Perrotin committed
153

154
/*
Maxime Perrotin's avatar
Maxime Perrotin committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  ForEachWithParam function : Write to file the list of parameters in Ada 
 (form IN/OUT_paramName: interface.c.char_array, IN/OUT_paramNamesize: [access] Integer) 
*/
void List_Ada_Param_Types_And_Names(Parameter *p, FILE **file);

/*
   ForEachWithParam function : Write to file the list of parameters, separated by commas
*/
void List_Ada_Param_Names(Parameter *p, FILE **file);

/* 
  ForEachWithParam function : Write to file the list of parameters in Ada for QGen 
 (form IN/OUT_paramName: interface.c.char_array) 
*/
void List_QGen_Param_Types_And_Names(Parameter *p, FILE **file);

/*
   ForEachWithParam function : Write to file the list of parameters, separated by commas for QGen
*/
void List_QGen_Param_Names(Parameter *p, FILE **file, Language lang);

/* Create a string in the form "name.all",
 * as expected by QGencAda for arguments of the comp function*/
void Create_QGenAda_Argument(char * name, char **result, bool deref);

/* For a given sychronous RI, add "with RI_wrappers;" statement to a file
  Note: TODO: We should check if the line is unique
  (low priority: having several identical "with Package;" lines does not affect the compiler)
*/
void Add_With_RI_Wrapper_to_Ada(Interface *i, FILE **file);

/* Copy an interface */
Interface *Duplicate_Interface (IF_type direction, Interface *i, FV *fv);

/* Duplicate all the elements of a list of parameters */
Parameter_list *Duplicate_Param_List(Parameter_list *p_list, Interface *i);

/* Duplicate a parameter, with the exception of the "interface" field, which is context-dependent */
Parameter *Duplicate_Parameter (Parameter *p);

/* Write to file the list of parameters with their type, separated with commas
 * (form "const asn1SccTYPE1 IN_param1, ...") */
void List_C_Types_And_Params_With_Pointers(Parameter *p, FILE **file);

/* ForEachWithParam function : Write to file the list of parameters separated with commas (form "IN_param1, IN_param2, &OUT_param1....") */
void List_C_Params(Parameter *p, FILE **file);


/* ForEachWithParam function : Write to file the list of parameters + size separated with commas (form "IN_param1, size_IN_param1,.., OUT_parami, &size_OUT_parami....") */
void List_C_Params_And_Size (Parameter *p, FILE **file);

/* Allocate memory for the system (AST root) */
void Create_System (System **s);

/* Free the memory of the System AST */
void Clear_System (System *s);

/* Count a number of FVs in a list */
void CountFV (FV *fv, int *count);

/* Create a new APLC binding type */
void Create_Aplc_binding(Aplc_binding **a);

/* Allocate memory for a Parameter type */
void Create_Parameter (Parameter **p);

/* Dump the model for debug purposes */
void Dump_model(System *);

/* Add an error the the top-level error count */
extern void add_error();



/* function potentially called by the ADD_TO_SET macro */
int Compare_Package(Package *one, Package *two);
int Compare_Protected_Object_Name(Protected_Object_Name *one, Protected_Object_Name *two);
int Compare_Interface (Interface *one, Interface *two);
233
int Compare_FV (FV *one, FV *two);
Maxime Perrotin's avatar
Maxime Perrotin committed
234 235 236 237 238 239 240 241 242 243 244 245 246
int Compare_String (String *one, String *two);
int Compare_ASN1_Filename(ASN1_Filename *one, ASN1_Filename *two);
int Compare_ASN1_Module (ASN1_Module *one, ASN1_Module *two);
int Compare_ASN1_Type (ASN1_Type *one, ASN1_Type *two);
int Compare_Port (Port *one, Port *two);
void Clear_Port (Port ** p);
void Print_FV (FV *fv);


/* New FOREACH macro.. Supports online statements!
Example of use:

        with "MyType_list *l;" and l containing elements
247

Maxime Perrotin's avatar
Maxime Perrotin committed
248 249 250 251 252
                FOREACH(tmp, MyType,l, {
                        printf("%s",tmp);
                        printf(" ");
                        }
                )
253

Maxime Perrotin's avatar
Maxime Perrotin committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
*/
#define FOREACH(var,type,list,stmt) {type##_list *varlist##var##type=list; type *var;while(varlist##var##type!=NULL) { var=varlist##var##type->value; stmt varlist##var##type=varlist##var##type->next; }}

#define CREATE_NEW_LIST(type, list) \
{\
assert(NULL == list);\
list=(type##_list *) malloc(sizeof(type##_list));\
}


#define APPEND_TO_LIST(type, list, val) \
{\
type##_list *newlist=NULL;\
assert(NULL != val);\
CREATE_NEW_LIST(type, newlist);\
assert(NULL!=newlist);\
newlist->value=val;\
newlist->next=NULL;\
if(NULL==list) list = newlist;\
else {\
type##_list *nav=list;\
while (NULL != nav->next) nav=nav->next;\
nav->next=newlist; }\
}


/* 
 * ADD_TO_SET : make sure the list contains UNIQUE elements 
 * The user has to provide the Compare_type function
 */
#define ADD_TO_SET(type, list, val) \
{\
int result = 0; \
FOREACH(elem, type, list, {\
if(Compare_##type(val,elem)) result = 1; \
});\
if (!result) APPEND_TO_LIST(type, list, val);\
}

293 294 295 296 297 298 299 300 301 302
/* return true if value of type sort is in set */
#define IN_SET(sort, set, val) \
__extension__({\
bool result = false; \
FOREACH(elem, sort, set, {\
if(Compare_##sort(val,elem)) result = true; \
});\
result;\
})

Maxime Perrotin's avatar
Maxime Perrotin committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
#define FREE_LIST(type, list) \
{\
type##_list *next=NULL;\
while(NULL != list) {\
Clear_##type(&(list->value));\
free(list->value);\
list->value=NULL;\
next=list->next;\
free(list);\
list=next;}\
}

#define REMOVE_FROM_LIST(type, list, val) \
{\
type##_list *tmp=list;\
if(list->value==val) list=list->next;\
else while (NULL != tmp) {\
if(tmp->next->value==val) {tmp->next=tmp->next->next;break;}\
tmp=tmp->next;}\
}

#define PRINT_LIST(type, list) \
{\
type##_list *next=list;\
do {\
printf("[");\
Print_##type(next->value);\
printf("]");\
next=next->next;\
}\
while (NULL != next);\
}
 

int concat_file (const char* source, const char* dest);

void create_enum_file(FV *fv);

#endif /* PRACTICAL_FUNCTIONS */