Skip to content

Commit c3609ee

Browse files
committed
Allow string constants to be initialized from other string constants
1 parent 02afbfd commit c3609ee

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

frontends/common.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ DeclareConstants(Module *P, AST **conlist_ptr)
509509

510510
switch (ast->kind) {
511511
case AST_ASSIGN:
512+
{
512513
if (IsConstExpr(ast->right)) {
513514
if (!IsIdentifier(ast->left)) {
514515
ERROR(ast, "Internal error, bad constant declaration");
@@ -522,8 +523,20 @@ DeclareConstants(Module *P, AST **conlist_ptr)
522523
completed_declarations = AddToList(completed_declarations, upper);
523524
conlist = *conlist_ptr;
524525
} else {
526+
AST *setExpr = ast->right;
527+
// check for declarations of old constants
528+
if (IsIdentifier(setExpr)) {
529+
const char *name = GetIdentifierName(setExpr);
530+
AST *olddecl = FindDeclaration(current->datblock, name);
531+
if (olddecl && olddecl->kind == AST_DECLARE_VAR && olddecl->left && IsConstType(olddecl->left)) {
532+
AST *assign = olddecl->right;
533+
if (assign && assign->right) {
534+
setExpr = ast->right = assign->right;
535+
}
536+
}
537+
}
525538
AST *typ;
526-
typ = ExprType(ast->right);
539+
typ = ExprType(setExpr);
527540
if (typ && (IsStringType(typ) || IsPointerType(typ))) {
528541
if (!IsIdentifier(ast->left)) {
529542
ERROR(ast, "Internal error, bad constant declaration");
@@ -538,6 +551,7 @@ DeclareConstants(Module *P, AST **conlist_ptr)
538551
}
539552
}
540553
break;
554+
}
541555
case AST_ENUMSET:
542556
if (IsConstExpr(ast->left)) {
543557
default_val = EvalConstExpr(ast->left);
@@ -1727,7 +1741,7 @@ GetExprlistLen(AST *list)
17271741
//
17281742
// find any previous declaration of name
17291743
//
1730-
static AST *
1744+
AST *
17311745
FindDeclaration(AST *datlist, const char *name)
17321746
{
17331747
AST *ident;

frontends/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,9 @@ void DeclareAnonymousAliases(Module *Parent, Module *sub, AST *prefix);
10231023
// add a source file to our internal list
10241024
void AddSourceFile(const char *shortName, const char *fullName);
10251025

1026+
// find a constant or other declaration in a list
1027+
AST *FindDeclaration(AST *datlist, const char *name);
1028+
10261029
// external vars
10271030
extern AST *basic_get_float;
10281031
extern AST *basic_get_string;

0 commit comments

Comments
 (0)