use long for trap id
[lttv.git] / genevent-new / parser.c
index 17ebef72373e87de845e524021e659a0b8e6a21e..425d390cfa9745bddce0bdf286ddfeb4fcb8811c 100644 (file)
@@ -3,14 +3,15 @@
 parser.c: Generate helper declarations and functions to trace events
   from an event description file.
 
-Copyright (C) 2002, Xianxiu Yang
-Copyright (C) 2002, Michel Dagenais 
-This program is free software; you can redistribute it and/or modify
+           Copyright (C) 2005, Mathieu Desnoyers
+      Copyright (C) 2002, Xianxiu Yang
+      Copyright (C) 2002, Michel Dagenais 
+   This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-the Free Software Foundation; version 2 of the License.
+   the Free Software Foundation; version 2 of the License.
 
-This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
@@ -498,7 +499,7 @@ void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
                        if(strcmp("field",token))in->error(in,"expecting a field");
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(ev->fields),f);
-                       parseFields(in, f, unnamed_types, named_types);
+                       parseFields(in, f, unnamed_types, named_types, 1);
                        break;
                default:
                        in->error(in, "expecting </event> or <field >");
@@ -535,30 +536,35 @@ void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
  *    f             : field
  *    unnamed_types : array of unamed types
  *    named_types   : array of named types
+ *    tag                                              : is field surrounded by a <field> </field> tag ?
  ****************************************************************************/
 
 void parseFields(parse_file_t *in, field_t *f,
     sequence_t * unnamed_types,
-               table_t * named_types) 
+               table_t * named_types,
+               int tag) 
 {
   char * token;
+       if(tag) {
+               //<field name=field_name> <description> <type> </field>
+               getFieldAttributes(in, f);
+               if(f->name == NULL) in->error(in, "Field not named");
+               getRAnglebracket(in);
 
-  //<field name=field_name> <description> <type> </field>
-  getFieldAttributes(in, f);
-  if(f->name == NULL) in->error(in, "Field not named");
-  getRAnglebracket(in);
-
-  f->description = getDescription(in);
+               f->description = getDescription(in);
+       }
 
   //<int size=...>
   getLAnglebracket(in);
   f->type = parseType(in,NULL, unnamed_types, named_types);
 
-  getLAnglebracket(in);
-  getForwardslash(in);
-  token = getName(in);
-  if(strcmp("field",token))in->error(in,"not a valid field definition");
-  getRAnglebracket(in); //</field>
+       if(tag) {
+               getLAnglebracket(in);
+               getForwardslash(in);
+               token = getName(in);
+               if(strcmp("field",token))in->error(in,"not a valid field definition");
+               getRAnglebracket(in); //</field>
+       }
 }
 
 
@@ -611,7 +617,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(t->fields),f);
 
-      parseFields(in, f, unnamed_types, named_types);
+      parseFields(in, f, unnamed_types, named_types, 1);
       
       //next field
       getLAnglebracket(in);
@@ -634,7 +640,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
     while(strcmp("field",token) == 0){
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(t->fields),f);
-      parseFields(in, f, unnamed_types, named_types);
+      parseFields(in, f, unnamed_types, named_types, 1);
       
       //next field
       getLAnglebracket(in);
@@ -648,13 +654,20 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
   }
   else if(strcmp(token,"array") == 0) {
     t->type = ARRAY;
+    sequence_init(&(t->fields));
     getTypeAttributes(in, t, unnamed_types, named_types);
     if(t->size == 0) in->error(in, "Array has empty size");
     getForwardslash(in);
     getRAnglebracket(in); //<array size=n>
 
-    getLAnglebracket(in); //<type struct> 
-    t->nested_type = parseType(in, NULL, unnamed_types, named_types);
+    //getLAnglebracket(in); //<subtype> 
+               /* subfield */
+               f = (field_t *)memAlloc(sizeof(field_t));
+               sequence_push(&(t->fields),f);
+    parseFields(in, f, unnamed_types, named_types, 0);
+
+    //getLAnglebracket(in); //<type struct> 
+    //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
 
     getLAnglebracket(in); //</array>
     getForwardslash(in);
@@ -664,19 +677,33 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
   }
   else if(strcmp(token,"sequence") == 0) {
     t->type = SEQUENCE;
+    sequence_init(&(t->fields));
     //getTypeAttributes(in, t, unnamed_types, named_types);
     //getForwardslash(in);
     getRAnglebracket(in); //<sequence>
 
-    getLAnglebracket(in); //<type sequence> 
-    t->length_type = parseType(in, NULL, unnamed_types, named_types);
+    //getLAnglebracket(in); //<sequence size type> 
+               /* subfield */
+               f = (field_t *)memAlloc(sizeof(field_t));
+               sequence_push(&(t->fields),f);
+    parseFields(in, f, unnamed_types, named_types, 0);
+
+    //getLAnglebracket(in); //<subtype> 
+               /* subfield */
+               f = (field_t *)memAlloc(sizeof(field_t));
+               sequence_push(&(t->fields),f);
+    parseFields(in, f, unnamed_types, named_types, 0);
 
-    getLAnglebracket(in); //<type sequence> 
+    //getLAnglebracket(in); //<type sequence> 
+    //t->length_type = parseType(in, NULL, unnamed_types, named_types);
 
-    t->nested_type = parseType(in, NULL, unnamed_types, named_types);
+    //getLAnglebracket(in); //<type sequence> 
 
-    if(t->length_type == NULL) in->error(in, "Sequence has no length type");
-               switch(t->length_type->type) {
+    //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
+
+    if(t->fields.position < 1) in->error(in, "Sequence has no length type");
+    if(t->fields.position < 2) in->error(in, "Sequence has no subtype");
+               switch(((field_t*)t->fields.array[0])->type->type) {
                        case UINT_FIXED :
                        case UCHAR :
                        case USHORT :
@@ -689,8 +716,6 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
                                in->error(in, "Wrong length type for sequence");
                }
 
-               
-    if(t->nested_type == NULL) in->error(in, "Sequence has no nested type");
     getLAnglebracket(in); //</sequence>
     getForwardslash(in);
     token = getName(in);
@@ -759,24 +784,28 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
   }
   else if(strcmp(token,"char") == 0) {
     t->type = CHAR;
+               t->size = 1;
     getTypeAttributes(in, t, unnamed_types, named_types);
     getForwardslash(in);
     getRAnglebracket(in); 
   }
   else if(strcmp(token,"uchar") == 0) {
     t->type = UCHAR;
+               t->size = 1;
     getTypeAttributes(in, t, unnamed_types, named_types);
     getForwardslash(in);
     getRAnglebracket(in); 
   }
   else if(strcmp(token,"short") == 0) {
     t->type = SHORT;
+               t->size = 2;
     getTypeAttributes(in, t, unnamed_types, named_types);
     getForwardslash(in);
     getRAnglebracket(in); 
   }
   else if(strcmp(token,"ushort") == 0) {
     t->type = USHORT;
+               t->size = 2;
     getTypeAttributes(in, t, unnamed_types, named_types);
     getForwardslash(in);
     getRAnglebracket(in); 
@@ -844,17 +873,14 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
   }
   else if(strcmp(token,"typeref") == 0){
     // Must be a named type
-    if(inType != NULL) 
-      in->error(in,"Named type cannot refer to a named type");
-    else {
-      free(t);
-      sequence_pop(unnamed_types);
-      token = getNameAttribute(in);
-      t = find_named_type(token, named_types);
-      getForwardslash(in);  //<typeref name=type_name/>
-      getRAnglebracket(in);
-      return t;
-    }
+               free(t);
+               sequence_pop(unnamed_types);
+               token = getNameAttribute(in);
+               t = find_named_type(token, named_types);
+               if(t == NULL) in->error(in,"Named referred to must be pre-declared.");
+               getForwardslash(in);  //<typeref name=type_name/>
+               getRAnglebracket(in);
+               return t;
   }else in->error(in,"not a valid type");
 
   return t;
@@ -875,16 +901,22 @@ type_descriptor_t * find_named_type(char *name, table_t * named_types)
   type_descriptor_t *t;
 
   t = table_find(named_types,name);
-  if(t == NULL) {
-    t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
-    t->type_name = allocAndCopy(name);
-    t->type = NONE;
-    t->fmt = NULL;
-    table_insert(named_types,t->type_name,t);
-    //    table_insert(named_types,allocAndCopy(name),t);
-  }
+
   return t;
-}  
+}
+
+type_descriptor_t * create_named_type(char *name, table_t * named_types)
+{
+  type_descriptor_t *t;
+
+       t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
+       t->type_name = allocAndCopy(name);
+       t->type = NONE;
+       t->fmt = NULL;
+       table_insert(named_types,t->type_name,t);
+       //    table_insert(named_types,allocAndCopy(name),t);
+       return t;
+}
 
 /*****************************************************************************
  *Function name
@@ -903,7 +935,7 @@ void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types,
 
   token = getNameAttribute(in);
   if(token == NULL) in->error(in, "Type has empty name");
-  t = find_named_type(token, named_types);
+  t = create_named_type(token, named_types);
 
   if(t->type != NONE) in->error(in,"redefinition of named type");
   getRAnglebracket(in); //<type name=type_name>
@@ -1202,7 +1234,7 @@ void checkNamedTypesImplemented(table_t * named_types)
  ****************************************************************************/
 
 void generateChecksum(char* facName,
-    unsigned long * checksum, sequence_t * events)
+    unsigned int * checksum, sequence_t * events)
 {
   unsigned long crc ;
   int pos;
@@ -1306,12 +1338,12 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type)
       flag = 1;
       break;
     case ARRAY:
-      sprintf(buf,"%llu", type->size);
+      sprintf(buf,"%zu", type->size);
       str = appendString("array ",buf);
       flag = 1;
       break;
     case SEQUENCE:
-      sprintf(buf,"%llu", type->size);
+      sprintf(buf,"%zu", type->size);
       str = appendString("sequence ",buf);
       flag = 1;
       break;
@@ -1333,9 +1365,12 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type)
 
   if(type->fmt) crc = partial_crc32(type->fmt,crc);
     
-  if(type->type == ARRAY || type->type == SEQUENCE){
-    crc = getTypeChecksum(crc,type->nested_type);
-  }else if(type->type == STRUCT || type->type == UNION){
+  if(type->type == ARRAY){
+    crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
+  } else if(type->type ==SEQUENCE) {
+    crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
+    crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type);
+       } else if(type->type == STRUCT || type->type == UNION){
     for(pos =0; pos < type->fields.position; pos++){
       fld = (field_t *) type->fields.array[pos];
       crc = partial_crc32(fld->name,crc);
This page took 0.033709 seconds and 4 git commands to generate.