flush
[m6w6/ext-psi] / src / parser.h
index 0f5671d38ebf7782f3b42ae68120df801182487a..94860c5248d0e0fd9e6076b4ae90e5e4ce5a2fe5 100644 (file)
@@ -62,6 +62,7 @@ typedef struct decl_type {
        token_t type;
        struct decl_type *real;
        struct decl_struct *strct;
        token_t type;
        struct decl_type *real;
        struct decl_struct *strct;
+       struct decl_union *unn;
        struct decl_enum *enm;
        struct decl *func;
 } decl_type;
        struct decl_enum *enm;
        struct decl *func;
 } decl_type;
@@ -166,6 +167,9 @@ static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
 }
 
 static inline void free_decl_arg(decl_arg *arg) {
 }
 
 static inline void free_decl_arg(decl_arg *arg) {
+       if (arg->token && arg->token != arg->var->token) {
+               free(arg->token);
+       }
        free_decl_type(arg->type);
        free_decl_var(arg->var);
        if (arg->layout) {
        free_decl_type(arg->type);
        free_decl_var(arg->var);
        if (arg->layout) {
@@ -343,6 +347,7 @@ typedef struct decl_struct {
        char *name;
        decl_args *args;
        size_t size;
        char *name;
        decl_args *args;
        size_t size;
+       size_t align;
        struct {
                void *type;
                void (*dtor)(void *type);
        struct {
                void *type;
                void (*dtor)(void *type);
@@ -394,6 +399,56 @@ static inline void free_decl_structs(decl_structs *ss) {
        free(ss);
 }
 
        free(ss);
 }
 
+typedef struct decl_union {
+       PSI_Token *token;
+       char *name;
+       decl_args *args;
+       size_t size;
+       size_t align;
+} decl_union;
+
+static inline decl_union *init_decl_union(const char *name, decl_args *args) {
+       decl_union *u = calloc(1, sizeof(*u));
+       u->name = strdup(name);
+       u->args = args;
+       return u;
+}
+
+static inline void free_decl_union(decl_union *u) {
+       if (u->token) {
+               free(u->token);
+       }
+       if (u->args) {
+               free_decl_args(u->args);
+       }
+       free(u->name);
+       free(u);
+}
+
+typedef struct decl_unions {
+       decl_union **list;
+       size_t count;
+} decl_unions;
+
+static inline decl_unions *add_decl_union(decl_unions *uu, decl_union *u) {
+       if (!uu) {
+               uu = calloc(1, sizeof(*uu));
+       }
+       uu->list = realloc(uu->list, ++uu->count * sizeof(*uu->list));
+       uu->list[uu->count-1] = u;
+       return uu;
+}
+
+static inline void free_decl_unions(decl_unions *uu) {
+       size_t i;
+
+       for (i = 0; i < uu->count; ++i) {
+               free_decl_union(uu->list[i]);
+       }
+       free(uu->list);
+       free(uu);
+}
+
 typedef struct impl_type {
        char *name;
        token_t type;
 typedef struct impl_type {
        char *name;
        token_t type;
@@ -1354,13 +1409,14 @@ static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_pt
 
 #define PSI_ERROR 16
 #define PSI_WARNING 32
 
 #define PSI_ERROR 16
 #define PSI_WARNING 32
-typedef void (*psi_error_cb)(PSI_Token *token, int type, const char *msg, ...);
+typedef void (*psi_error_cb)(void *context, PSI_Token *token, int type, const char *msg, ...);
 
 #define PSI_DATA(D) ((PSI_Data *) (D))
 #define PSI_DATA_MEMBERS \
        constants *consts; \
        decl_typedefs *defs; \
        decl_structs *structs; \
 
 #define PSI_DATA(D) ((PSI_Data *) (D))
 #define PSI_DATA_MEMBERS \
        constants *consts; \
        decl_typedefs *defs; \
        decl_structs *structs; \
+       decl_unions *unions; \
        decl_enums *enums; \
        decls *decls; \
        impls *impls; \
        decl_enums *enums; \
        decls *decls; \
        impls *impls; \
@@ -1368,7 +1424,9 @@ typedef void (*psi_error_cb)(PSI_Token *token, int type, const char *msg, ...);
                decl_file file; \
                decl_libs libs; \
        } psi; \
                decl_file file; \
                decl_libs libs; \
        } psi; \
-       psi_error_cb error
+       psi_error_cb error; \
+       unsigned errors; \
+       unsigned flags
 typedef struct PSI_Data {
        PSI_DATA_MEMBERS;
 } PSI_Data;
 typedef struct PSI_Data {
        PSI_DATA_MEMBERS;
 } PSI_Data;
@@ -1392,6 +1450,9 @@ static inline void PSI_DataDtor(PSI_Data *data) {
        if (data->structs) {
                free_decl_structs(data->structs);
        }
        if (data->structs) {
                free_decl_structs(data->structs);
        }
+       if (data->unions) {
+               free_decl_unions(data->unions);
+       }
        if (data->enums) {
                free_decl_enums(data->enums);
        }
        if (data->enums) {
                free_decl_enums(data->enums);
        }
@@ -1409,7 +1470,7 @@ typedef struct PSI_Parser {
        FILE *fp;
        token_t num;
        void *proc;
        FILE *fp;
        token_t num;
        void *proc;
-       unsigned flags, errors, line, col;
+       unsigned line, col;
        char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
 } PSI_Parser;
 
        char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
 } PSI_Parser;
 
@@ -1541,6 +1602,7 @@ static inline uint64_t PSI_TokenHash(PSI_Token *t, char *digest_buf) {
 }
 
 #define PSI_PARSER_DEBUG 0x1
 }
 
 #define PSI_PARSER_DEBUG 0x1
+#define PSI_PARSER_SILENT 0x2
 
 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
 
 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);