From: Matthias Kruk Date: Sat, 11 Jul 2020 07:46:33 +0000 (+0900) Subject: parser: Add missing constructors and destructors for constants and other expressions X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=dd4c6a4b7d387d8d16d7f40f76fbee2aa7163d6e;p=ccc parser: Add missing constructors and destructors for constants and other expressions --- diff --git a/src/grammar.c b/src/grammar.c index f281d79..80aa891 100644 --- a/src/grammar.c +++ b/src/grammar.c @@ -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; diff --git a/src/grammar.h b/src/grammar.h index 59dc61a..d59ae79 100644 --- a/src/grammar.h +++ b/src/grammar.h @@ -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 */