Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The change Arnaud wanted so much: Big Star Eradication (plus some more, check the...
[simgrid.git] / src / gras / DataDesc / ddt_parse.c
index 1fd158f..f41d6fd 100644 (file)
@@ -13,7 +13,8 @@
 #include "gras/DataDesc/datadesc_private.h"
 #include "gras/DataDesc/ddt_parse.yy.h"
 
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_parse,datadesc);
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_parse,datadesc,
+  "Parsing C data structures to build GRAS data description");
 
 typedef struct s_type_modifier{
   short is_unsigned;
@@ -25,19 +26,19 @@ typedef struct s_type_modifier{
   short is_enum;
 
   short is_ref;
-} type_modifier_t;
+} s_type_modifier_t,*type_modifier_t;
 
 typedef struct s_field {
-  gras_datadesc_type_t *type;
+  gras_datadesc_type_t type;
   char *type_name;
   char *name;
-  type_modifier_t tm;
-} identifier_t;
+  s_type_modifier_t tm;
+} s_identifier_t;
  
 extern char *gras_ddt_parse_text; /* text being considered in the parser */
 
 /* local functions */
-static void parse_type_modifier(type_modifier_t        *type_modifier)  {
+static void parse_type_modifier(type_modifier_t type_modifier)  {
   GRAS_IN;
   do {
     if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_STAR) {
@@ -87,7 +88,7 @@ static void parse_type_modifier(type_modifier_t       *type_modifier)  {
   GRAS_OUT;
 }
 
-static void print_type_modifier(type_modifier_t tm) {
+static void print_type_modifier(s_type_modifier_t tm) {
   int i;
 
   GRAS_IN;
@@ -103,73 +104,65 @@ static void print_type_modifier(type_modifier_t tm) {
   GRAS_OUT;
 }
 
-static gras_error_t change_to_fixed_array(gras_dynar_t *dynar, long int size) {
-  gras_error_t errcode;
-  identifier_t former,array;
+static void change_to_fixed_array(gras_dynar_t dynar, long int size) {
+  s_identifier_t former,array;
   memset(&array,0,sizeof(array));
 
   GRAS_IN;
   gras_dynar_pop(dynar,&former);
-  array.type_name=malloc(strlen(former.type->name)+20);
+  array.type_name=(char*)gras_malloc(strlen(former.type->name)+20);
   DEBUG2("Array specification (size=%ld, elm='%s'), change pushed type",
         size,former.type_name);
   sprintf(array.type_name,"%s[%ld]",former.type_name,size);
-  free(former.type_name);
-
-  TRY(gras_datadesc_array_fixed(array.type_name, former.type, size, &array.type)); /* redeclaration are ignored */
+  gras_free(former.type_name);
 
+  array.type = gras_datadesc_array_fixed(array.type_name, former.type, size); /* redeclaration are ignored */
   array.name = former.name;
 
-  TRY(gras_dynar_push(dynar,&array));
+  gras_dynar_push(dynar,&array);
   GRAS_OUT;
-  return no_error;
 }
-static gras_error_t change_to_ref(gras_dynar_t *dynar) {
-  gras_error_t errcode;
-  identifier_t former,ref;
+static void change_to_ref(gras_dynar_t dynar) {
+  s_identifier_t former,ref;
   memset(&ref,0,sizeof(ref));
 
   GRAS_IN;
   gras_dynar_pop(dynar,&former);
-  ref.type_name=malloc(strlen(former.type->name)+2);
+  ref.type_name=(char*)gras_malloc(strlen(former.type->name)+2);
   DEBUG1("Ref specification (elm='%s'), change pushed type", former.type_name);
   sprintf(ref.type_name,"%s*",former.type_name);
-  free(former.type_name);
-
-  TRY(gras_datadesc_ref(ref.type_name, former.type, &ref.type)); /* redeclaration are ignored */
+  gras_free(former.type_name);
 
+  ref.type = gras_datadesc_ref(ref.type_name, former.type); /* redeclaration are ignored */
   ref.name = former.name;
 
-  TRY(gras_dynar_push(dynar,&ref));
+  gras_dynar_push(dynar,&ref);
   GRAS_OUT;
-  return no_error;
 }
 
-static gras_error_t change_to_ref_pop_array(gras_dynar_t *dynar) {
-  gras_error_t errcode;
-  identifier_t former,ref;
+static void change_to_ref_pop_array(gras_dynar_t dynar) {
+  s_identifier_t former,ref;
   memset(&ref,0,sizeof(ref));
 
   GRAS_IN;
   gras_dynar_pop(dynar,&former);
-  TRY(gras_datadesc_ref_pop_arr(former.type,&ref.type)); /* redeclaration are ignored */
-  ref.type_name = strdup(ref.type->name);
+  ref.type = gras_datadesc_ref_pop_arr(former.type); /* redeclaration are ignored */
+  ref.type_name = (char*)strdup(ref.type->name);
   ref.name = former.name;
 
-  free(former.type_name);
+  gras_free(former.type_name);
 
-  TRY(gras_dynar_push(dynar,&ref));
+  gras_dynar_push(dynar,&ref);
   GRAS_OUT;
-  return no_error;
 }
 
 static gras_error_t parse_statement(char        *definition,
-                                   gras_dynar_t *identifiers,
-                                   gras_dynar_t *fields_to_push) {
+                                   gras_dynar_t  identifiers,
+                                   gras_dynar_t  fields_to_push) {
   gras_error_t errcode;
   char buffname[512];
 
-  identifier_t identifier;
+  s_identifier_t identifier;
 
   int expect_id_separator = 0;
 
@@ -219,10 +212,10 @@ static gras_error_t parse_statement(char   *definition,
       strcmp(gras_ddt_parse_text,"int") ) {
 
     /* bastard user, they omited "int" ! */
-    identifier.type_name=strdup("int");
+    identifier.type_name=(char*)strdup("int");
     DEBUG0("the base type is 'int', which were omited (you vicious user)");
   } else {
-    identifier.type_name=strdup(gras_ddt_parse_text);
+    identifier.type_name=(char*)strdup(gras_ddt_parse_text);
     DEBUG1("the base type is '%s'",identifier.type_name);
     gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); 
   }
@@ -236,7 +229,7 @@ static gras_error_t parse_statement(char     *definition,
 
   } else if (identifier.tm.is_struct) {
     sprintf(buffname,"struct %s",identifier.type_name);
-    TRY(gras_datadesc_struct(buffname,&identifier.type)); /* Get created when does not exist */
+    identifier.type = gras_datadesc_struct(buffname); /* Get created when does not exist */
 
   } else if (identifier.tm.is_unsigned) {
     if (!strcmp(identifier.type_name,"int")) {
@@ -296,7 +289,7 @@ static gras_error_t parse_statement(char     *definition,
   /**** look for the symbols of this type ****/
   for(expect_id_separator = 0;
 
-      (//(gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_EMPTY) && FIXME
+      (/*(gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_EMPTY) && FIXME*/
        (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_SEMI_COLON)) ; 
 
       gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()          ) {   
@@ -320,7 +313,7 @@ static gras_error_t parse_statement(char     *definition,
            PARSE_ERROR1("Unparsable size of array (found '%c', expected number)",*end);
 
          /* replace the previously pushed type to an array of it */
-         TRY(change_to_fixed_array(identifiers,size));
+         change_to_fixed_array(identifiers,size);
 
          /* eat the closing bracket */
          gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
@@ -335,7 +328,7 @@ static gras_error_t parse_statement(char     *definition,
 
       } else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) {
        /* Handle annotation */
-       identifier_t array;
+       s_identifier_t array;
        char *keyname = NULL;
        char *keyval  = NULL;
        memset(&array,0,sizeof(array));
@@ -350,7 +343,7 @@ static gras_error_t parse_statement(char     *definition,
 
        if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) 
          PARSE_ERROR1("Unparsable annotation: Expected key name, got '%s'",gras_ddt_parse_text);
-       keyname = strdup(gras_ddt_parse_text);
+       keyname = (char*)strdup(gras_ddt_parse_text);
 
        while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY );
 
@@ -361,7 +354,7 @@ static gras_error_t parse_statement(char     *definition,
 
        if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) 
          PARSE_ERROR1("Unparsable annotation: Expected key value, got '%s'",gras_ddt_parse_text);
-       keyval = strdup(gras_ddt_parse_text);
+       keyval = (char*)strdup(gras_ddt_parse_text);
 
        while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY );
 
@@ -372,14 +365,14 @@ static gras_error_t parse_statement(char   *definition,
 
        DEBUG2("Anotation: %s=%s",keyname,keyval);
        if (!strcmp(keyname,"size")) {
-         free(keyname);
+         gras_free(keyname);
          if (!identifier.tm.is_ref)
            PARSE_ERROR0("Size annotation for a field not being a reference");
          identifier.tm.is_ref--;
 
          if (!strcmp(keyval,"1")) {
-           TRY(change_to_ref(identifiers));
-           free(keyval);
+           change_to_ref(identifiers);
+           gras_free(keyval);
            continue;
          } else {
            char *p;
@@ -388,14 +381,14 @@ static gras_error_t parse_statement(char   *definition,
              if (! isdigit(*p) )
                fixed = 0;
            if (fixed) {
-             TRY(change_to_fixed_array(identifiers,atoi(keyval)));
-             TRY(change_to_ref(identifiers));
-             free(keyval);
+             change_to_fixed_array(identifiers,atoi(keyval));
+             change_to_ref(identifiers);
+             gras_free(keyval);
              continue;
 
            } else {
-             TRY(change_to_ref_pop_array(identifiers));
-             TRY(gras_dynar_push(fields_to_push,&keyval));
+             change_to_ref_pop_array(identifiers);
+             gras_dynar_push(fields_to_push,&keyval);
              continue;
            }
          }
@@ -422,11 +415,11 @@ static gras_error_t parse_statement(char   *definition,
     /* found a symbol name. Build the type and push it to dynar */
     if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) {
 
-      identifier.name=strdup(gras_ddt_parse_text);
+      identifier.name=(char*)strdup(gras_ddt_parse_text);
       DEBUG1("Found the identifier \"%s\"",identifier.name);
       
-      TRY(gras_dynar_push(identifiers, &identifier));
-      DEBUG1("Dynar_len=%d",gras_dynar_length(identifiers));
+      gras_dynar_push(identifiers, &identifier);
+      DEBUG1("Dynar_len=%lu",gras_dynar_length(identifiers));
       expect_id_separator = 1;
       continue;
     }
@@ -438,38 +431,34 @@ static gras_error_t parse_statement(char   *definition,
   return no_error;
 }
 
-static gras_datadesc_type_t *parse_struct(char *definition) {
+static gras_datadesc_type_t parse_struct(char *definition) {
 
   gras_error_t errcode;
   char buffname[32];
   static int anonymous_struct=0;
 
-  gras_dynar_t *identifiers;
-  identifier_t field;
+  gras_dynar_t identifiers;
+  s_identifier_t field;
   int i;
 
-  gras_dynar_t *fields_to_push;
+  gras_dynar_t fields_to_push;
   char *name;
 
-  gras_datadesc_type_t *struct_type;
+  gras_datadesc_type_t struct_type;
 
   GRAS_IN;
-  errcode=gras_dynar_new(&identifiers,sizeof(identifier_t),NULL);
-  errcode=gras_dynar_new(&fields_to_push,sizeof(char*),NULL);
-  if (errcode != no_error) {
-    GRAS_OUT;
-    return NULL;
-  }
+  identifiers = gras_dynar_new(sizeof(s_identifier_t),NULL);
+  fields_to_push = gras_dynar_new(sizeof(char*),NULL);
 
   /* Create the struct descriptor */
   if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) {
-    TRYFAIL(gras_datadesc_struct(gras_ddt_parse_text,&struct_type));
+    struct_type = gras_datadesc_struct(gras_ddt_parse_text);
     VERB1("Parse the struct '%s'", gras_ddt_parse_text);
     gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
   } else {
     sprintf(buffname,"anonymous struct %d",anonymous_struct++); 
     VERB1("Parse the anonymous struct nb %d", anonymous_struct);
-    TRYFAIL(gras_datadesc_struct(buffname,&struct_type));
+    struct_type = gras_datadesc_struct(buffname);
   }
 
   if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_LA)
@@ -481,28 +470,28 @@ static gras_datadesc_type_t *parse_struct(char *definition) {
        errcode == no_error                            ;
        errcode=parse_statement(definition,identifiers,fields_to_push)) {
     
-    DEBUG1("This statement contained %d identifiers",gras_dynar_length(identifiers));
+    DEBUG1("This statement contained %lu identifiers",gras_dynar_length(identifiers));
     /* append the identifiers we've found */
     gras_dynar_foreach(identifiers,i, field) {
       if (field.tm.is_ref)
        PARSE_ERROR2("Not enough GRAS_ANNOTATE to deal with all dereferencing levels of %s (%d '*' left)",
                     field.name,field.tm.is_ref);
 
-      VERB2("Append field '%s' to %p",field.name, struct_type);      
-      TRYFAIL(gras_datadesc_struct_append(struct_type, field.name, field.type));
-      free(field.name);
-      free(field.type_name);
+      VERB2("Append field '%s' to %p",field.name, (void*)struct_type);      
+      gras_datadesc_struct_append(struct_type, field.name, field.type);
+      gras_free(field.name);
+      gras_free(field.type_name);
 
     }
     gras_dynar_reset(identifiers);
-    DEBUG1("struct_type=%p",struct_type);
+    DEBUG1("struct_type=%p",(void*)struct_type);
     
     /* Make sure that all fields declaring a size push it into the cbps */
     gras_dynar_foreach(fields_to_push,i, name) {
-      DEBUG1("struct_type=%p",struct_type);
-      VERB2("Push field '%s' into size stack of %p", name, struct_type);
+      DEBUG1("struct_type=%p",(void*)struct_type);
+      VERB2("Push field '%s' into size stack of %p", name, (void*)struct_type);
       gras_datadesc_cb_field_push(struct_type, name);
-      free(name);
+      gras_free(name);
     }
     gras_dynar_reset(fields_to_push);
   }
@@ -519,18 +508,18 @@ static gras_datadesc_type_t *parse_struct(char *definition) {
 
   gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
 
-  gras_dynar_free(identifiers);
-  gras_dynar_free(fields_to_push);
+  gras_dynar_free(&identifiers);
+  gras_dynar_free(&fields_to_push);
   GRAS_OUT;
   return struct_type;
 }
 
-static gras_datadesc_type_t parse_typedef(char *definition) {
+static gras_datadesc_type_t parse_typedef(char *definition) {
 
-  type_modifier_t tm;
+  s_type_modifier_t tm;
 
-  gras_datadesc_type_t *struct_desc=NULL;
-  gras_datadesc_type_t *typedef_desc=NULL;
+  gras_datadesc_type_t struct_desc=NULL;
+  gras_datadesc_type_t typedef_desc=NULL;
 
   GRAS_IN;
   memset(&tm,0,sizeof(tm));
@@ -565,11 +554,11 @@ static gras_datadesc_type_t * parse_typedef(char *definition) {
  *
  * Create a datadescription from the result of parsing the C type description
  */
-gras_datadesc_type_t *
+gras_datadesc_type_t 
 gras_datadesc_parse(const char            *name,
                    const char            *C_statement) {
 
-  gras_datadesc_type_t res=NULL;
+  gras_datadesc_type_t res=NULL;
   char *definition;
   int semicolon_count=0;
   int def_count,C_count;
@@ -579,7 +568,7 @@ gras_datadesc_parse(const char            *name,
   for (C_count=0; C_statement[C_count] != '\0'; C_count++)
     if (C_statement[C_count] == ';' || C_statement[C_count] == '{')
       semicolon_count++;
-  definition = malloc(C_count + semicolon_count + 1);
+  definition = (char*)gras_malloc(C_count + semicolon_count + 1);
   for (C_count=0,def_count=0; C_statement[C_count] != '\0'; C_count++) {
     definition[def_count++] = C_statement[C_count];
     if (C_statement[C_count] == ';' || C_statement[C_count] == '{') {
@@ -610,7 +599,7 @@ gras_datadesc_parse(const char            *name,
 
   gras_ddt_parse_pointer_string_close();
   VERB0("end of _gras_ddt_type_parse()");
-  free(definition);
+  gras_free(definition);
   /* register it under the name provided as symbol */
   if (strcmp(res->name,name)) {
     ERROR2("In GRAS_DEFINE_TYPE, the provided symbol (here %s) must be the C type name (here %s)",