/* $Id$ */
-/* DataDesc/ddt_parse.c -- automatic parsing of data structures */
+/* DataDesc/ddt_parse.c -- automatic parsing of data structures */
-/* Copyright (c) 2004 Arnaud Legrand, Martin Quinson. All rights reserved. */
+/* Copyright (c) 2003 Arnaud Legrand. */
+/* Copyright (c) 2003, 2004 Martin Quinson. */
+/* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <ctype.h> /* isdigit */
+#include "xbt/ex.h"
#include "gras/DataDesc/datadesc_private.h"
#include "gras/DataDesc/ddt_parse.yy.h"
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_parse,datadesc,
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_parse,gras_ddt,
"Parsing C data structures to build GRAS data description");
typedef struct s_type_modifier{
- short is_unsigned;
- short is_short;
short is_long;
+ int is_unsigned:1;
+ int is_short:1;
- short is_struct;
- short is_union;
- short is_enum;
+ int is_struct:1;
+ int is_union:1;
+ int is_enum:1;
- short is_ref;
+ int is_ref:1;
+
+ int is_dynar:2;
+ int is_matrix:2;
} s_type_modifier_t,*type_modifier_t;
typedef struct s_field {
XBT_IN;
xbt_dynar_pop(dynar,&former);
- array.type_name=(char*)xbt_malloc(strlen(former.type->name)+20);
+ array.type_name=(char*)xbt_malloc(strlen(former.type->name)+48);
DEBUG2("Array specification (size=%ld, elm='%s'), change pushed type",
size,former.type_name);
- sprintf(array.type_name,"%s[%ld]",former.type_name,size);
- xbt_free(former.type_name);
+ sprintf(array.type_name,"%s%s%s%s[%ld]",
+ (former.tm.is_unsigned?"u ":""),
+ (former.tm.is_short?"s ":""),
+ (former.tm.is_long?"l ":""),
+ former.type_name,
+ size);
+ free(former.type_name);
array.type = gras_datadesc_array_fixed(array.type_name, former.type, size); /* redeclaration are ignored */
array.name = former.name;
ref.type_name=(char*)xbt_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);
- xbt_free(former.type_name);
+ free(former.type_name);
ref.type = gras_datadesc_ref(ref.type_name, former.type); /* redeclaration are ignored */
ref.name = former.name;
ref.type_name = (char*)strdup(ref.type->name);
ref.name = former.name;
- xbt_free(former.type_name);
+ free(former.type_name);
xbt_dynar_push(dynar,&ref);
XBT_OUT;
}
-static xbt_error_t parse_statement(char *definition,
- xbt_dynar_t identifiers,
- xbt_dynar_t fields_to_push) {
+static void change_to_dynar_of(xbt_dynar_t dynar,gras_datadesc_type_t subtype) {
+ s_identifier_t former,ref;
+ memset(&ref,0,sizeof(ref));
+
+ XBT_IN;
+ xbt_dynar_pop(dynar,&former);
+ ref.type = gras_datadesc_dynar(subtype,NULL); /* redeclaration are ignored */
+ ref.type_name = (char*)strdup(ref.type->name);
+ ref.name = former.name;
+
+ free(former.type_name);
+
+ xbt_dynar_push(dynar,&ref);
+ XBT_OUT;
+}
+
+static void change_to_matrix_of(xbt_dynar_t dynar,gras_datadesc_type_t subtype) {
+ s_identifier_t former,ref;
+ memset(&ref,0,sizeof(ref));
+
+ XBT_IN;
+ xbt_dynar_pop(dynar,&former);
+ ref.type = gras_datadesc_matrix(subtype,NULL); /* redeclaration are ignored */
+ ref.type_name = (char*)strdup(ref.type->name);
+ ref.name = former.name;
+
+ free(former.type_name);
+
+ xbt_dynar_push(dynar,&ref);
+ XBT_OUT;
+}
+
+static void add_free_f(xbt_dynar_t dynar,void_f_pvoid_t free_f) {
+ s_identifier_t former,ref;
+ memset(&ref,0,sizeof(ref));
+
+ XBT_IN;
+ xbt_dynar_pop(dynar,&former);
+ memcpy(former.type->extra,free_f, sizeof(free_f));
+ xbt_dynar_push(dynar,&former);
+ XBT_OUT;
+}
+
+static void parse_statement(char *definition,
+ xbt_dynar_t identifiers,
+ xbt_dynar_t fields_to_push) {
char buffname[512];
s_identifier_t identifier;
gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_RA) {
XBT_OUT;
- return mismatch_error; /* end of the englobing structure or union */
+ THROW0(mismatch_error,0,"End of the englobing structure or union");
}
- if (XBT_LOG_ISENABLED(ddt_parse,xbt_log_priority_debug)) {
+ if (XBT_LOG_ISENABLED(gras_ddt_parse,xbt_log_priority_debug)) {
int colon_pos;
for (colon_pos = gras_ddt_parse_col_pos;
definition[colon_pos] != ';';
/* FIXME: This does not detect recursive definitions at all? */
if (identifier.tm.is_union || identifier.tm.is_enum || identifier.tm.is_struct)
- PARSE_ERROR0("Cannot handle recursive type definition yet");
+ PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle recursive type definition yet");
/**** get the base type, giving "short a" the needed love ****/
if (!identifier.tm.is_union &&
/**** build the base type for latter use ****/
if (identifier.tm.is_union) {
- PARSE_ERROR0("Cannot handle union yet (get callback from annotation?)");
+ PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle union yet (get callback from annotation?)");
} else if (identifier.tm.is_enum) {
- PARSE_ERROR0("Cannot handle enum yet");
+ PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle enum yet");
} else if (identifier.tm.is_struct) {
sprintf(buffname,"struct %s",identifier.type_name);
identifier.type = gras_datadesc_by_name("unsigned char");
} else { /* impossible, gcc parses this shit before us */
- RAISE_IMPOSSIBLE;
+ THROW_IMPOSSIBLE;
}
} else if (!strcmp(identifier.type_name, "float")) {
} else {
DEBUG1("Base type is a constructed one (%s)",identifier.type_name);
- identifier.type = gras_datadesc_by_name(identifier.type_name);
- if (!identifier.type)
- PARSE_ERROR1("Unknown base type '%s'",identifier.type_name);
+ if (!strcmp(identifier.type_name,"xbt_matrix_t")) {
+ identifier.tm.is_matrix = 1;
+ } else if (!strcmp(identifier.type_name,"xbt_dynar_t")) {
+ identifier.tm.is_dynar = 1;
+ } else {
+ identifier.type = gras_datadesc_by_name(identifier.type_name);
+ if (!identifier.type)
+ PARSE_ERROR1("Unknown base type '%s'",identifier.type_name);
+ }
}
}
/* Now identifier.type and identifier.name speak about the base type.
Stars are not eaten unless 'int' was omitted.
- We will have to enhance it if we are in fact asked for array or reference */
+ We will have to enhance it if we are in fact asked for array or reference.
+
+ Dynars and matrices also need some extra love (prodiged as annotations)
+ */
/**** look for the symbols of this type ****/
for(expect_id_separator = 0;
/* Handle fixed size arrays */
gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_RB) {
- PARSE_ERROR0("Cannot deal with [] constructs (yet)");
+ PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot deal with [] constructs (yet)");
} else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) {
char *end;
long int size=strtol(gras_ddt_parse_text, &end, 10);
- if (end == gras_ddt_parse_text || *end != '\0')
- PARSE_ERROR1("Unparsable size of array (found '%c', expected number)",*end);
+ if (end == gras_ddt_parse_text || *end != '\0') {
+ /* Not a number. Get the constant value, if any */
+ int *storage=xbt_dict_get_or_null(gras_dd_constants,gras_ddt_parse_text);
+ if (storage) {
+ size = *storage;
+ } else {
+ PARSE_ERROR1("Unparsable size of array. Found '%s', expected number or known constant. Need to use gras_datadesc_set_const(), huh?",
+ gras_ddt_parse_text);
+ }
+ }
/* replace the previously pushed type to an array of it */
change_to_fixed_array(identifiers,size);
while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY );
+ /* get the value */
+
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 = (char*)strdup(gras_ddt_parse_text);
while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY );
- if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RP)
- PARSE_ERROR1("Unparsable annotation: Expected parenthesis, got '%s'",gras_ddt_parse_text);
-
/* Done with parsing the annotation. Now deal with it by replacing previously pushed type with the right one */
DEBUG2("Anotation: %s=%s",keyname,keyval);
if (!strcmp(keyname,"size")) {
- xbt_free(keyname);
+ 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")) {
change_to_ref(identifiers);
- xbt_free(keyval);
- continue;
+ free(keyval);
} else {
char *p;
int fixed = 1;
if (fixed) {
change_to_fixed_array(identifiers,atoi(keyval));
change_to_ref(identifiers);
- xbt_free(keyval);
- continue;
+ free(keyval);
} else {
change_to_ref_pop_array(identifiers);
xbt_dynar_push(fields_to_push,&keyval);
- continue;
}
}
- RAISE_IMPOSSIBLE;
-
+ } else if (!strcmp(keyname,"subtype")) {
+ gras_datadesc_type_t subtype = gras_datadesc_by_name(keyval);
+ if (identifier.tm.is_matrix) {
+ change_to_matrix_of(identifiers,subtype);
+ identifier.tm.is_matrix = -1;
+ } else if (identifier.tm.is_dynar) {
+ change_to_dynar_of(identifiers,subtype);
+ identifier.tm.is_dynar = -1;
+ } else {
+ PARSE_ERROR1("subtype annotation only accepted for dynars and matrices, but passed to '%s'",identifier.type_name);
+ }
+ free(keyval);
+ } else if (!strcmp(keyname,"free_f")) {
+ int *storage=xbt_dict_get_or_null(gras_dd_constants,keyval);
+ if (!storage)
+ PARSE_ERROR1("value for free_f annotation of field %s is not a known constant",identifier.name);
+ if (identifier.tm.is_matrix == -1) {
+ add_free_f(identifiers,*(void_f_pvoid_t*)storage);
+ identifier.tm.is_matrix = 0;
+ } else if (identifier.tm.is_dynar == -1) {
+ add_free_f(identifiers,*(void_f_pvoid_t*)storage);
+ identifier.tm.is_dynar = 0;
+ } else {
+ PARSE_ERROR1("free_f annotation only accepted for dynars and matrices which subtype is already declared (field %s)",
+ identifier.name);
+ }
+ free(keyval);
} else {
+ free(keyval);
PARSE_ERROR1("Unknown annotation type: '%s'",keyname);
}
+
+ /* Get all the multipliers */
+ while (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_STAR) {
+
+ gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump();
+
+ if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD)
+ PARSE_ERROR1("Unparsable annotation: Expected field name after '*', got '%s'",gras_ddt_parse_text);
+
+ keyval = xbt_malloc(strlen(gras_ddt_parse_text)+2);
+ sprintf(keyval,"*%s",gras_ddt_parse_text);
+
+ /* ask caller to push field as a multiplier */
+ xbt_dynar_push(fields_to_push,&keyval);
+
+ /* skip blanks after this block*/
+ while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump())
+ == GRAS_DDT_PARSE_TOKEN_EMPTY );
+ }
+
+ if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RP)
+ PARSE_ERROR1("Unparsable annotation: Expected parenthesis, got '%s'",
+ gras_ddt_parse_text);
+
continue;
/* End of annotation handling */
PARSE_ERROR0("Unparasable symbol (maybe a def struct in a def struct or a parser bug ;)");
}
+ if (identifier.tm.is_matrix>0)
+ PARSE_ERROR0("xbt_matrix_t field without 'subtype' annotation");
+ if (identifier.tm.is_dynar>0)
+ PARSE_ERROR0("xbt_dynar_t field without 'subtype' annotation");
+
XBT_OUT;
- return no_error;
}
static gras_datadesc_type_t parse_struct(char *definition) {
- xbt_error_t errcode;
+ xbt_ex_t e;
+
char buffname[32];
static int anonymous_struct=0;
xbt_dynar_t identifiers;
s_identifier_t field;
- int i;
+ unsigned int iter;
+ int done;
xbt_dynar_t fields_to_push;
char *name;
gras_ddt_parse_text);
/* Parse the identifiers */
- for (errcode=parse_statement(definition,identifiers,fields_to_push);
- errcode == no_error ;
- errcode=parse_statement(definition,identifiers,fields_to_push)) {
+ done = 0;
+ do {
+ TRY {
+ parse_statement(definition,identifiers,fields_to_push);
+ } CATCH(e) {
+ if (e.category != mismatch_error)
+ RETHROW;
+ xbt_ex_free(e);
+ done = 1;
+ }
DEBUG1("This statement contained %lu identifiers",xbt_dynar_length(identifiers));
/* append the identifiers we've found */
- xbt_dynar_foreach(identifiers,i, field) {
+ xbt_dynar_foreach(identifiers,iter, 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, (void*)struct_type);
gras_datadesc_struct_append(struct_type, field.name, field.type);
- xbt_free(field.name);
- xbt_free(field.type_name);
+ free(field.name);
+ free(field.type_name);
}
xbt_dynar_reset(identifiers);
DEBUG1("struct_type=%p",(void*)struct_type);
/* Make sure that all fields declaring a size push it into the cbps */
- xbt_dynar_foreach(fields_to_push,i, name) {
+ xbt_dynar_foreach(fields_to_push,iter, name) {
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);
- xbt_free(name);
+ if (name[0] == '*') {
+ VERB2("Push field '%s' as a multiplier into size stack of %p",
+ name+1, (void*)struct_type);
+ gras_datadesc_cb_field_push_multiplier(struct_type, name+1);
+ } else {
+ VERB2("Push field '%s' into size stack of %p",
+ name, (void*)struct_type);
+ gras_datadesc_cb_field_push(struct_type, name);
+ }
+ free(name);
}
xbt_dynar_reset(fields_to_push);
- }
+ } while (!done);
gras_datadesc_struct_close(struct_type);
- if (errcode != mismatch_error) {
- XBT_OUT;
- return NULL; /* FIXME: LEAK! */
- }
/* terminates */
if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RA)
parse_type_modifier(&tm);
if (tm.is_ref)
- PARSE_ERROR0("Cannot handle reference without annotation");
+ PARSE_ERROR0("GRAS_DEFINE_TYPE cannot handle reference without annotation");
/* get the aliasing name */
if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD)
gras_ddt_parse_text);
/* (FIXME: should) build the alias */
- PARSE_ERROR0("Cannot handle typedef yet");
+ PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle typedef yet");
XBT_OUT;
return typedef_desc;
gras_ddt_parse_pointer_string_close();
VERB0("end of _gras_ddt_type_parse()");
- xbt_free(definition);
+ 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)",
name,res->name);
xbt_abort();
}
+ gras_ddt_parse_lex_destroy();
XBT_OUT;
return res;
}
+xbt_dict_t gras_dd_constants;
+/** \brief Declare a constant to the parsing mecanism. See the "\#define and fixed size array" section */
+void gras_datadesc_set_const(const char*name, int value) {
+ int *stored = xbt_new(int, 1);
+ *stored=value;
+ xbt_dict_set(gras_dd_constants,name, stored, xbt_free_f);
+}