Skip to content

Commit 8a6693f

Browse files
committed
Properly handle named constants
1 parent 18f7fa8 commit 8a6693f

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

source/compiler/sc3.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ static int hier2(value *lval)
16821682
swdefault=FALSE;
16831683
firstcase=TRUE;
16841684
do {
1685+
int got_cseval=FALSE; /* true if the case value gets misinterpreted by lex() as a label */
16851686
needtoken(tTERM);
16861687
ident=lex(&val,&st);
16871688
if (ident==')')
@@ -1695,15 +1696,29 @@ static int hier2(value *lval)
16951696
error(16); /* multiple defaults in switch */
16961697
swdefault=TRUE;
16971698
} else {
1698-
if (ident!=tLABEL)
1699+
if (ident!=tLABEL) {
16991700
lexpush();
1700-
else
1701-
error(220); /* expression with tag override must appear between parentheses */
1701+
} else {
1702+
symbol *csesym=findloc(st);
1703+
if (csesym==NULL)
1704+
csesym=findglb(st,sGLOBAL);
1705+
if (csesym!=NULL) {
1706+
markusage(csesym,uREAD);
1707+
ident=csesym->ident;
1708+
val=csesym->addr;
1709+
csetag=csesym->tag;
1710+
got_cseval=TRUE;
1711+
} else {
1712+
error(220); /* expression with tag override must appear between parentheses */
1713+
} /* if */
1714+
} /* if */
17021715
if (swdefault!=FALSE)
17031716
error(15); /* "default" case must be last in switch statement */
17041717
do {
1705-
stgget(&index,&cidx); /* mark position in code generator */
1706-
ident=expression(&val,&csetag,NULL,TRUE);
1718+
if (!got_cseval) {
1719+
stgget(&index,&cidx); /* mark position in code generator */
1720+
ident=expression(&val,&csetag,NULL,TRUE);
1721+
} /* if */
17071722
/* if the next token is ";" or ")", then this must be an implicit default case */
17081723
if (matchtoken(';') || matchtoken(')')) {
17091724
lexpush();
@@ -1715,7 +1730,8 @@ static int hier2(value *lval)
17151730
goto skip_impl_default;
17161731
} /* if */
17171732
casecount++;
1718-
stgdel(index,cidx); /* scratch generated code */
1733+
if (!got_cseval)
1734+
stgdel(index,cidx); /* scratch generated code */
17191735
if (ident!=iCONSTEXPR)
17201736
error(8); /* must be constant expression */
17211737
check_tagmismatch(swtag,csetag,TRUE,-1);
@@ -1740,7 +1756,7 @@ static int hier2(value *lval)
17401756
newval=insert_constval(csp,cse,itoh(lbl_case),val,0);
17411757
if (csp==NULL)
17421758
caselist.first=newval;
1743-
if (matchtoken(tDBLDOT)) {
1759+
if (!got_cseval && matchtoken(tDBLDOT)) {
17441760
cell end;
17451761
stgget(&index,&cidx); /* mark position in code generator */
17461762
ident=expression(&end,&csetag,NULL,TRUE);
@@ -1761,7 +1777,8 @@ static int hier2(value *lval)
17611777
} /* while */
17621778
} /* if */
17631779
} while (matchtoken(','));
1764-
needtoken(':'); /* ':' ends the case */
1780+
if (!got_cseval)
1781+
needtoken(':'); /* ':' ends the case */
17651782
} /* if */
17661783
sc_allowtags=bck_allowtags; /* reset */
17671784
ident=expression(NULL,&exprtag,NULL,FALSE);

0 commit comments

Comments
 (0)