Skip to content

Commit

Permalink
Try to avoid problems with declaring static functions with annotation…
Browse files Browse the repository at this point in the history
…s like 'inline'
  • Loading branch information
totalspectrum committed Sep 30, 2023
1 parent d26b728 commit ab0b525
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2199,7 +2199,7 @@ FoldIfConst(AST *expr)
AST *
RemoveTypeModifiers(AST *ast)
{
while(ast && (ast->kind == AST_MODIFIER_CONST || ast->kind == AST_MODIFIER_VOLATILE || ast->kind == AST_MODIFIER_SEND_ARGS || ast->kind == AST_TYPEDEF || ast->kind == AST_REGISTER)) {
while(ast && (ast->kind == AST_MODIFIER_CONST || ast->kind == AST_MODIFIER_VOLATILE || ast->kind == AST_MODIFIER_SEND_ARGS || ast->kind == AST_TYPEDEF || ast->kind == AST_REGISTER || ast->kind == AST_ANNOTATION)) {
ast = ast->left;
}
return ast;
Expand Down
40 changes: 33 additions & 7 deletions frontends/c/cgram.y
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,36 @@ static AST *IsGlobalRegisterDecl(AST *type)
return NULL;
}

bool IsStaticType(AST *type) {
while (type && type->kind == AST_ANNOTATION)
type = type->left;
return type && type->kind == AST_STATIC;
}

AST *ExtractStaticType(AST *type) {
AST *annotations = NULL;
while (type && type->kind == AST_ANNOTATION) {
AST *item = DupAST(type);
item->left = annotations;
item->right = NULL;
annotations = item;
type = type->left;
}
if (type && type->kind == AST_STATIC) {
type = type->left;
}
// preserve the annotations
if (annotations) {
AST *base = type;
type = annotations;
while (annotations->left) {
annotations = annotations->left;
}
annotations->left = base;
}
return type;
}

static AST *
MultipleDeclareVar(AST *first, AST *second)
{
Expand All @@ -402,8 +432,8 @@ MultipleDeclareVar(AST *first, AST *second)
if (module) {
ERROR(first, ":: not supported yet");
}
if (type && type->kind == AST_STATIC) {
stmtlist = AddToList(stmtlist, DeclareStatics(current, type->left, ident));
if (IsStaticType(type)) {
stmtlist = AddToList(stmtlist, DeclareStatics(current, ExtractStaticType(type), ident));
} else if ( NULL != (regtype = IsGlobalRegisterDecl(type)) ) {
/* declare a register global variable */
ident = NewAST(AST_DECLARE_VAR, regtype, ident);
Expand Down Expand Up @@ -869,10 +899,6 @@ MergeOldStyleDeclarationList(AST *orig_funcdecl, AST *decl_list)
return orig_funcdecl;
}

static int IsStatic(AST *ftype) {
return ftype && ftype->kind == AST_STATIC;
}

/* declare a typed function, optionally using a local identifier (if the function is STATIC) */
static AST *
DeclareCTypedFunction(Module *P, AST *ftype, AST *nameAst, int is_public, AST *body, AST *attribute)
Expand All @@ -890,7 +916,7 @@ DeclareCTypedFunction(Module *P, AST *ftype, AST *nameAst, int is_public, AST *b
if (ftype && ftype->kind == AST_EXTERN) {
ftype = ftype->left;
}
if (IsStatic(ftype) && nameAst->kind == AST_IDENTIFIER) {
if (IsStaticType(ftype) && nameAst->kind == AST_IDENTIFIER) {
/* declare a local alias for the name */
const char *nameString = GetIdentifierName(nameAst);
AST *globalName = AstTempIdentifier(nameString);
Expand Down
7 changes: 7 additions & 0 deletions frontends/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,13 @@ DeclareOneGlobalVar(Module *P, AST *ident, AST *type, int inDat)
type = type->left;
is_typedef = 1;
}
while (type && type->kind == AST_ANNOTATION) {
type = type->left;
}
if (!type) {
ERROR(ident, "Internal error, only annotations found");
return;
}
if (type->kind == AST_STATIC) {
// FIXME: this case probably shouldn't happen any more??
WARNING(ident, "Internal error, did not expect static in code");
Expand Down

0 comments on commit ab0b525

Please sign in to comment.