]> git.corax.cc Git - ccc/commitdiff
parser: Add missing constructors and destructors for constants and other expressions
authorMatthias Kruk <m@m10k.eu>
Sat, 11 Jul 2020 07:46:33 +0000 (16:46 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 11 Jul 2020 07:46:33 +0000 (16:46 +0900)
src/grammar.c
src/grammar.h

index f281d7911748cb50746854e04a3d8fb20684de75..80aa8916002a5a755b9a216f5507892e4a05787b 100644 (file)
@@ -218,6 +218,47 @@ struct primary_expression *primary_expression_new(void)
        return(pexp);
 }
 
+void primary_expression_free(struct primary_expression *pexpr)
+{
+       switch(pexpr->type) {
+       case PRIMARY_EXPR_IDENTIFIER:
+               if(pexpr->data.identifier) {
+                       identifier_free(pexpr->data.identifier);
+               }
+
+               break;
+
+       case PRIMARY_EXPR_CONSTANT:
+               if(pexpr->data.constant) {
+                       constant_free(pexpr->data.constant);
+               }
+
+               break;
+
+       case PRIMARY_EXPR_STRING:
+               if(pexpr->data.string) {
+                       string_free(pexpr->data.string);
+               }
+
+               break;
+
+       case PRIMARY_EXPR_EXPR:
+               if(pexpr->data.expr.expr) {
+                       expression_free(pexpr->data.expr.expr);
+               }
+
+               break;
+
+       default:
+               break;
+       }
+
+       memset(pexpr, 0, sizeof(*pexpr));
+       free(pexpr);
+
+       return;
+}
+
 void primary_expression_debug(struct primary_expression *pexpr)
 {
        switch(pexpr->type) {
@@ -262,6 +303,14 @@ struct integer_constant *integer_constant_new(void)
        return(iconst);
 }
 
+void integer_constant_free(struct integer_constant *iconst)
+{
+       memset(iconst, 0, sizeof(*iconst));
+       free(iconst);
+
+       return;
+}
+
 struct character_constant *character_constant_new(void)
 {
        struct character_constant *cconst;
@@ -275,6 +324,14 @@ struct character_constant *character_constant_new(void)
        return(cconst);
 }
 
+void character_constant_free(struct character_constant *cconst)
+{
+       memset(cconst, 0, sizeof(*cconst));
+       free(cconst);
+
+       return;
+}
+
 struct floating_constant *floating_constant_new(void)
 {
        struct floating_constant *fconst;
@@ -288,6 +345,14 @@ struct floating_constant *floating_constant_new(void)
        return(fconst);
 }
 
+void floating_constant_free(struct floating_constant *fconst)
+{
+       memset(fconst, 0, sizeof(*fconst));
+       free(fconst);
+
+       return;
+}
+
 struct enumeration_constant *enumeration_constant_new(void)
 {
        struct enumeration_constant *econst;
@@ -301,6 +366,14 @@ struct enumeration_constant *enumeration_constant_new(void)
        return(econst);
 }
 
+void enumeration_constant_free(struct enumeration_constant *econst)
+{
+       memset(econst, 0, sizeof(*econst));
+       free(econst);
+
+       return;
+}
+
 struct constant *constant_new(void)
 {
        struct constant *c;
@@ -314,6 +387,35 @@ struct constant *constant_new(void)
        return(c);
 }
 
+void constant_free(struct constant *c)
+{
+       switch(c->type) {
+       case CONST_TYPE_INTEGER:
+               integer_constant_free(c->data.iconst);
+               break;
+
+       case CONST_TYPE_CHARACTER:
+               character_constant_free(c->data.cconst);
+               break;
+
+       case CONST_TYPE_FLOATING:
+               floating_constant_free(c->data.fconst);
+               break;
+
+       case CONST_TYPE_ENUMERATION:
+               enumeration_constant_free(c->data.econst);
+               break;
+
+       default:
+               break;
+       }
+
+       memset(c, 0, sizeof(*c));
+       free(c);
+
+       return;
+}
+
 struct identifier *identifier_new(void)
 {
        struct identifier *id;
@@ -345,6 +447,14 @@ struct string *string_new(void)
        return(s);
 }
 
+void string_free(struct string *str)
+{
+       memset(str, 0, sizeof(*str));
+       free(str);
+
+       return;
+}
+
 struct argument_expression_list* argument_expression_list_new(void)
 {
        struct argument_expression_list *aelist;
@@ -509,6 +619,26 @@ struct conditional_expression *conditional_expression_new(void)
        return(ce);
 }
 
+void conditional_expression_free(struct conditional_expression *expr)
+{
+       if(expr->lorexpr) {
+               logical_or_expression_free(expr->lorexpr);
+       }
+
+       if(expr->expr) {
+               expression_free(expr->expr);
+       }
+
+       if(expr->cexpr) {
+               conditional_expression_free(expr->cexpr);
+       }
+
+       memset(expr, 0, sizeof(*expr));
+       free(expr);
+
+       return;
+}
+
 struct constant_expression *constant_expression_new(void)
 {
        struct constant_expression *ce;
@@ -522,6 +652,18 @@ struct constant_expression *constant_expression_new(void)
        return(ce);
 }
 
+void constant_expression_free(struct constant_expression *cexpr)
+{
+       if(cexpr->cexpr) {
+               conditional_expression_free(cexpr->cexpr);
+       }
+
+       memset(cexpr, 0, sizeof(*cexpr));
+       free(cexpr);
+
+       return;
+}
+
 struct logical_or_expression *logical_or_expression_new(void)
 {
        struct logical_or_expression *loe;
index 59dc61a1159bc67ac75aa809dedd919326871f8a..d59ae796f6587a918e74a60225347198e36791c3 100644 (file)
@@ -450,9 +450,16 @@ void primary_expression_free(struct primary_expression*);
 void primary_expression_debug(struct primary_expression*);
 
 struct integer_constant *integer_constant_new(void);
+void integer_constant_free(struct integer_constant*);
+
 struct character_constant *character_constant_new(void);
+void character_constant_free(struct character_constant*);
+
 struct floating_constant *floating_constant_new(void);
+void floating_constant_free(struct floating_constant*);
+
 struct enumeration_constant *enumeration_constant_new(void);
+void enumeration_constant_free(struct enumeration_constant*);
 
 struct argument_expression_list *argument_expression_list_new(void);
 void argument_expression_list_free(struct argument_expression_list*);
@@ -471,12 +478,10 @@ struct declaration_specifiers* declaration_specifiers_new(void);
 void declaration_specifiers_debug(struct declaration_specifiers*);
 
 struct constant *constant_new(void);
+void constant_free(struct constant*);
 
-struct integer_constant *integer_constant_new(void);
-struct character_constant *character_constant_new(void);
-struct floating_constant *floating_constant_new(void);
-struct enumeration_constant *enueration_constant_new(void);
 struct string *string_new(void);
+void string_free(struct string*);
 
 struct identifier* identifier_new(void);
 void identifier_free(struct identifier*);
@@ -580,4 +585,7 @@ void enumerator_free(struct enumerator*);
 struct enumerator_list *enumerator_list_new(void);
 void enumerator_list_free(struct enumerator_list*);
 
+struct expression *expression_new(void);
+void expression_free(struct expression*);
+
 #endif /* GRAMMAR_H */