📄 decl.c
字号:
#include "c.h"
#pragma warning(disable:4100)
char *fname;
#define add(x,n) (x > INT_MAX-(n) ? (overflow=1,x) : x+(n))
#define chkoverflow(x,n) ((void)add(x,n))
#define bits2bytes(n) (((n) + 7)/8)
int DefaultAlignment = 16;
extern int Mflag;
static int regcount;
static List autos, registers;
Symbol cfunc; /* current function */
FunctionDescriptor FunctionInfo;
typedef struct tagDefinedFunctions {
struct tagDefinedFunctions *Next;
Symbol func;
FunctionDescriptor FunctionInfo;
} DefinedFunctionsList;
static DefinedFunctionsList *DefinedFunctions=NULL;
Symbol retv; /* return value location for structs */
static void checkref ARGS((Symbol, void *));
static Symbol dclglobal ARGS((int, char *, Type, Coordinate *, int callAttributes));
static Symbol dcllocal (int, char *, Type, Coordinate *,int);
static Symbol dclparam (int, char *, Type, Coordinate *,int);
static Type dclr ARGS((Type, char **, Symbol **, int, int *));
static Type dclr1 ARGS((char **, Symbol **, int, int *));
static void decl ARGS((Symbol(*) (int, char *, Type, Coordinate *,int)));
extern void doconst ARGS((Symbol, void *));
static void doglobal ARGS((Symbol, void *));
static void doextern ARGS((Symbol, void *));
static void exitparams ARGS((Symbol[]));
static void fields ARGS((Type));
static void funcdefn ARGS((int, char *, Type, Symbol[], Coordinate, int));
static void initglobal ARGS((Symbol, int));
static void oldparam ARGS((Symbol, void *));
static Symbol *parameters ARGS((Type));
static Type specifier ARGS((int *, int *, int *,int *));
static Type structdcl ARGS((int));
static Type tnode ARGS((int, Type));
static void AddToFunctionsList(Symbol s)
{
DefinedFunctionsList *rvp;
if (DefinedFunctions == NULL) {
DefinedFunctions = allocate(sizeof(DefinedFunctionsList),PERM);
rvp = DefinedFunctions;
}
else {
rvp = DefinedFunctions;
while (rvp->Next) {
rvp = rvp->Next;
}
rvp->Next = allocate(sizeof(DefinedFunctionsList),PERM);
rvp = rvp->Next;
}
rvp->func = s;
rvp->Next = NULL;
if (xref) {
memcpy(&rvp->FunctionInfo,&FunctionInfo,sizeof(FunctionDescriptor));
rvp->FunctionInfo.CalledFunctions = ltov(&rvp->FunctionInfo.CalledFunctionsList,PERM);
}
}
void program()
{
int n;
level = GLOBAL;
for (n = 0; t != EOI; n++)
if (kind[t] == CHAR || kind[t] == STATIC
|| t == ID || t == '*' || t == '(') {
decl(dclglobal);
deallocate(STMT);
if (!(glevel >= 3 || xref))
deallocate(FUNC);
}
else if (t == ';') {
warning(StrTab[298]);// <empty declaration\n>
t = gettok();
}
else {
error(StrTab[299]);// <unrecognized declaration\n>
t = gettok();
}
if (n == 0 && Mflag == 0)
warning(StrTab[300]);// <empty input file\n>
}
static Type specifier(int *sclass, int *isRedeclaration, int *attributes,int *dllExport)
{
int cls, cons, sign, size, type, vol,dllExp,dllImp;
int qualifierseen=0;
Type ty = NULL;
int isPascal = 0;
if (isRedeclaration)
*isRedeclaration = 0;
*attributes = 0;
cls = vol = cons = sign = size = type = dllExp = dllImp = 0;
if (sclass == NULL)
cls = AUTO;
for (;;) {
int *p, tt = t;
restart:
switch (t) {
case AUTO:
case REGISTER:
if (level <= GLOBAL && cls == 0)
error(StrTab[301], t);// <invalid use of `%k'\n>
p = &cls;
t = gettok();
qualifierseen = 1;
break;
case STATIC:
case EXTERN:
case TYPEDEF:
p = &cls;
t = gettok();
break;
case CONST:
p = &cons;
t = gettok();
break;
case VOLATILE:
p = &vol;
t = gettok();
break;
case SIGNED:
case UNSIGNED:
p = &sign;
t = gettok();
qualifierseen = 1;
break;
case LONG:
case SHORT:
p = &size;
t = gettok();
qualifierseen = 1;
break;
case VOID:
case CHAR:
case INT:
case FLOAT:
case DOUBLE:
case LONGLONG:
p = &type;
ty = tsym->type;
t = gettok();
qualifierseen = 1;
break;
case ENUM:
p = &type;
ty = enumdcl();
break;
case STRUCT:
case UNION:
p = &type;
ty = structdcl(t);
break;
case DLLEXPORT:
p = &dllExp;
t = gettok();
break;
case DLLIMPORT:
p = &dllImp;
t = '*';
break;
case STDCALL:
*attributes = STDCALLATTRIBUTE;
t = gettok();
goto restart;
case ID:
if (istypename(t, tsym) && type == 0) {
use(tsym, src);
ty = tsym->type;
p = &type;
t = gettok();
}
else
p = NULL;
break;
default:
p = NULL;
}
if (p == NULL)
break;
if (*p)
error(StrTab[302], tt);// <invalid use of `%k'\n>
*p = tt;
}
if (sclass)
*sclass = cls;
if (type == 0) {
if (!qualifierseen && Aflag > 1)
warning(StrTab[303]);// <no type specified. Defaulting to int\n>
type = INT;
ty = inttype;
}
if (cls == EXTERN)
dllExp = 0;
*dllExport = dllExp;
if (size == SHORT && type != INT
|| size == LONG && type != INT && type != DOUBLE
|| sign && type != INT && type != CHAR && type != LONGLONG) {
if (isRedeclaration && type == ID) {
Symbol sy;
sy = lookup(tsym->name, identifiers);
if (sy == NULL)
goto err;
if (sy->sclass != TYPEDEF)
goto err;
if (sy->type == NULL)
goto err;
if (sy->type->op != size) {
if (sy->type->op != ty->op && sy->type->op != sign)
goto err;
}
warning(StrTab[304], sy->name);// <redefinition of %s\n>
*isRedeclaration = 1;
return (sy->type);
}
err:
error(StrTab[305]);// <invalid type specification\n>
}
if (type == CHAR && sign)
ty = sign == UNSIGNED ? unsignedchar : signedchar;
else if (type == LONGLONG && sign)
ty = sign == UNSIGNED ? ulonglongtype : longlongtype;
else if (size == SHORT)
ty = sign == UNSIGNED ? unsignedshort : shorttype;
else if (size == LONG && type == DOUBLE)
ty = longdouble;
else if (size == LONG)
ty = sign == UNSIGNED ? unsignedlong : longtype;
else if (sign == UNSIGNED && type == INT)
ty = unsignedtype;
if (cons == CONST)
ty = qual(CONST, ty);
if (vol == VOLATILE)
ty = qual(VOLATILE, ty);
return ty;
}
static void decl(Symbol(*dcl)(int, char *, Type, Coordinate *, int callAttribute))
{
int sclass, redeclaration, callAttributes,dllExport=0;
Type ty, ty1;
static char stop[] = {CHAR, STATIC, ID, 0};
ty = specifier(&sclass, &redeclaration, &callAttributes,&dllExport);
if (redeclaration) {
test(';', stop);
return;
}
if (t == ID || t == '*' || t == '(' || t == '[') {
char *id;
int dummy;
Coordinate pos;
id = NULL;
pos = src;
if (level == GLOBAL) {
Symbol *params = NULL;
ty1 = dclr(ty, &id, ¶ms, 0, &dummy);
if (dummy) callAttributes = dummy;
if (params && id && isfunc(ty1)
&& (t == '{' || istypename(t, tsym)
|| (kind[t] == STATIC && t != TYPEDEF))) {
if (sclass == TYPEDEF) {
error(StrTab[306]);// <invalid use of `typedef'\n>
sclass = EXTERN;
}
if (ty1->u.f.oldstyle)
exitscope();
if (callAttributes == STDCALLATTRIBUTE) {
if (ty1->op != FUNCTION)
error(StrTab[307]);// <Incorrect usage of _stdcall>
else
ty1->u.f.isStdCall = 1;
}
else if (callAttributes == DLLIMPORTATTRIBUTE) {
warning("Incorrect use of __declspec(dllimport)\n");
callAttributes = 0;
}
funcdefn(sclass,fname = id, ty1, params, pos, callAttributes);
fname = NULL;
if (id && dllExport) {
AddExport(id);
}
return;
}
else if (params)
exitparams(params);
}
else {
int oldAttrib = callAttributes;
ty1 = dclr(ty, &id, NULL, 0, &callAttributes);
if (oldAttrib) callAttributes = oldAttrib;
}
for (;;) {
if (Aflag >= 1 && !hasproto(ty1))
warning(StrTab[308]);// <missing prototype\n>
if (id == NULL)
error(StrTab[309]);// <missing identifier\n>
else if (sclass == TYPEDEF) {
Symbol p = lookup(id, identifiers);
if (p && p->scope == level) {
if (p->type->op == POINTER &&
ty1->op == POINTER) {
warning(StrTab[310], id);// <redeclaration of '%s'\n>
}
else if (p->type->op != ty1->op)
error(StrTab[311], id);// <redeclaration of `%s'\n>
}
p = install(id, &identifiers, level,
level < LOCAL ? PERM : FUNC);
p->type = ty1;
p->sclass = TYPEDEF;
p->src = pos;
if (callAttributes)
p->Flags = 1;
}
else
(void) (*dcl) (sclass, id, ty1, &pos, callAttributes);
if (t != ',')
break;
t = gettok();
id = NULL;
pos = src;
ty1 = dclr(ty, &id, NULL, 0, &callAttributes);
}
if (id && dllExport) {
AddExport(id);
}
}
else if (ty == NULL
|| !(isenum(ty) ||
isstruct(ty) && (*unqual(ty)->u.sym->name < '1' || *unqual(ty)->u.sym->name > '9')))
error(StrTab[312]);// <empty declaration\n>
test(';', stop);
}
static Symbol dclglobal(int sclass, char *id, Type ty, Coordinate * pos, int callAttributes)
{
Symbol p;
if (sclass == 0)
sclass = AUTO;
else if (sclass != EXTERN && sclass != STATIC) {
error(StrTab[313],// <invalid storage class `%k' for `%t %s'\n>
sclass, ty, id);
sclass = AUTO;
}
p = lookup(id, identifiers);
if (p && p->scope == GLOBAL) {
if (p->sclass != TYPEDEF && eqtype(ty, p->type, 1))
ty = compose(ty, p->type);
else
error(StrTab[314], p->name, &p->src);// <redeclaration of `%s' previously declared at %w\n>
if (!isfunc(ty) && p->defined && t == '=')
error(StrTab[315], p->name, &p->src);// <redefinition of `%s' previously defined at %w\n>
if (p->sclass == EXTERN && sclass == STATIC
|| p->sclass == STATIC && sclass == AUTO
|| p->sclass == AUTO && sclass == STATIC)
warning(StrTab[316], p->name, &p->src);// <inconsistent linkage for `%s' previously declared at %w\n>
}
if (p == NULL || p->scope != GLOBAL) {
p = install(id, &globals, GLOBAL, PERM);
p->sclass = sclass;
if (p->sclass != STATIC) {
static int nglobals;
nglobals++;
if (Aflag >= 2 && nglobals == 512)
warning(StrTab[317]);// <more than 511 external identifiers\n>
}
if (callAttributes)
p->Flags = callAttributes;
p->type = ty;
(*IR->defsymbol) (p);
}
else if (p->sclass == EXTERN)
p->sclass = sclass;
p->type = ty;
p->src = *pos;
if (callAttributes) {
p->Flags = callAttributes;
if (p->Flags == DLLEXPORTATTRIBUTE) {
AddExport(p->name);
}
}
{
Symbol q = lookup(p->name, externals);
if (q && (p->sclass == STATIC
|| !eqtype(p->type, q->type, 1)))
warning(StrTab[318], p->name, &q->src);// <declaration of `%s' does not match previous declaration at %w\n>
}
if (t == '=' && isfunc(p->type)) {
error(StrTab[319], p->name);// <illegal initialization for `%s'\n>
t = gettok();
initializer(p->type, 0);
}
else if (t == '=') {
initglobal(p, 0);
if (glevel > 0 && IR->stabsym) {
(*IR->stabsym) (p);
swtoseg(p->u.seg);
}
}
else if (p->sclass == STATIC && !isfunc(p->type)
&& p->type->size == 0)
error(StrTab[320], p->type, p->name);// <undefined size for `%t %s'\n>
return p;
}
static void initglobal(Symbol p, int flag)
{
Type ty;
if (t == '=' || flag) {
p->assigned = 1;
p->references++;
p->firstuse = StatementCount;
if (p->sclass == STATIC) {
for (ty = p->type; isarray(ty); ty = ty->type);
defglobal(p, isconst(ty) ? LIT : DATA);
}
else
defglobal(p, DATA);
if (t == '=')
t = gettok();
ty = initializer(p->type, 0);
if (isarray(p->type) && p->type->size == 0)
p->type = ty;
if (p->sclass == EXTERN)
p->sclass = AUTO;
p->defined = 1;
}
}
void defglobal(Symbol p, int seg)
{
p->u.seg = seg;
swtoseg(p->u.seg);
if (p->sclass != STATIC)
(*IR->export) (p);
(*IR->global) (p);
}
static Type dclr(Type basety, char **id, Symbol ** params, int abstract, int *attributes)
{
Type ty;
*attributes = 0;
ty = dclr1(id, params, abstract, attributes);
for (; ty; ty = ty->type)
switch (ty->op) {
case POINTER:
basety = ptr(basety);
break;
case FUNCTION:
basety = func(basety, ty->u.f.proto, ty->u.f.oldstyle);
break;
case ARRAY:
basety = array(basety, ty->size, 0);
break;
case CONST:
case VOLATILE:
basety = qual(ty->op, basety);
break;
default:
assert(0);
}
if (Aflag >= 2 && basety->size > 32767)
warning(StrTab[321], basety);// <more than 32767 bytes in `%t'\n>
return basety;
}
static Type tnode(int op, Type type)
{
Type ty;
NEW0(ty, STMT);
ty->op = op;
ty->type = type;
return ty;
}
static Type dclr1(char **id, Symbol ** params, int abstract, int *attributes)
{
Type ty = NULL;
Type ty1;
restart:
switch (t) {
case ID:
if (id)
*id = token;
else
error(StrTab[322], token);// <extraneous identifier `%s'\n>
t = gettok();
break;
case '*':
t = gettok();
if (t == CONST || t == VOLATILE) {
ty1 = ty = tnode(t, NULL);
while ((t = gettok()) == CONST || t == VOLATILE)
ty1 = tnode(t, ty1);
ty->type = dclr1(id, params, abstract, attributes);
ty = ty1;
}
else
ty = dclr1(id, params, abstract, attributes);
ty = tnode(POINTER, ty);
break;
case DLLEXPORT:
t = gettok();
*attributes = DLLEXPORTATTRIBUTE;
goto restart;
case DLLIMPORT:
*attributes = DLLIMPORTATTRIBUTE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -