📄 pexpr.c
字号:
/* "p2c", a Pascal to C translator. Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation. Author's address: daveg@synaptics.com.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation (any version).This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; see the file COPYING. If not, write tothe Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#define PROTO_PEXPR_C#include "trans.h"Expr *dots_n_hats(ex, target)Expr *ex;Type *target;{ Expr *ex2, *ex3; Type *tp, *tp2; Meaning *mp, *tvar; int hassl; for (;;) { if ((ex->val.type->kind == TK_PROCPTR || ex->val.type->kind == TK_CPROCPTR) && curtok != TOK_ASSIGN && ((mp = (tp2 = ex->val.type)->basetype->fbase) == NULL || (mp->isreturn && mp->xnext == NULL) || curtok == TOK_LPAR) && (tp2->basetype->basetype != tp_void || target == tp_void) && (!target || (target->kind != TK_PROCPTR && target->kind != TK_CPROCPTR))) { hassl = tp2->escale; ex2 = ex; ex3 = copyexpr(ex2); if (hassl != 0) ex3 = makeexpr_cast(makeexpr_dotq(ex3, "proc", tp_anyptr), makepointertype(tp2->basetype)); ex = makeexpr_un(EK_SPCALL, tp2->basetype->basetype, ex3); if (mp && mp->isreturn) { /* pointer to buffer for return value */ tvar = makestmttempvar(ex->val.type->basetype, (ex->val.type->basetype->kind == TK_STRING) ? name_STRING : name_TEMP); insertarg(&ex, 1, makeexpr_addr(makeexpr_var(tvar))); mp = mp->xnext; } if (mp) { if (wneedtok(TOK_LPAR)) { ex = p_funcarglist(ex, mp, 0, 0); skipcloseparen(); } } else if (curtok == TOK_LPAR) { gettok(); if (!wneedtok(TOK_RPAR)) skippasttoken(TOK_RPAR); } if (hassl != 1 || hasstaticlinks == 2) { freeexpr(ex2); } else { ex2 = makeexpr_dotq(ex2, "link", tp_anyptr), ex3 = copyexpr(ex); insertarg(&ex3, ex3->nargs, copyexpr(ex2)); tp = maketype(TK_FUNCTION); tp->basetype = tp2->basetype->basetype; tp->fbase = tp2->basetype->fbase; tp->issigned = 1; ex3->args[0]->val.type = makepointertype(tp); ex = makeexpr_cond(makeexpr_rel(EK_NE, ex2, makeexpr_nil()), ex3, ex); } if (tp2->basetype->fbase && tp2->basetype->fbase->isreturn && tp2->basetype->fbase->kind == MK_VARPARAM) ex = makeexpr_hat(ex, 0); /* returns pointer to structured result */ continue; } else if (ex->val.type->kind == TK_FUNCTION && ex->val.type->meaning && ex->val.type->meaning->kind == MK_FUNCTION && ex->val.type->meaning->rectype) { if (curtok == TOK_LPAR || !target || (target->kind != TK_PROCPTR && target->kind != TK_CPROCPTR)) { ex = p_memcall(ex, ex->val.type->meaning); continue; } else { ex = makeexpr_addrf(ex); ex->val.type = tp_cproc; note("Pointer to member function may need to be fixed [343]"); continue; } } switch (curtok) { case TOK_HAT: case TOK_ADDR: gettok(); ex = makeexpr_hat(ex, 1); break; case TOK_LBR: do { gettok(); ex2 = p_ord_expr(); ex = p_index(ex, ex2); } while (curtok == TOK_COMMA); if (!wneedtok(TOK_RBR)) skippasttotoken(TOK_RBR, TOK_SEMI); break; case TOK_DOT: gettok(); if (!wexpecttok(TOK_IDENT)) break; if (ex->val.type->kind == TK_STRING) { if (!strcicmp(curtokbuf, "LENGTH")) { ex = makeexpr_bicall_1("strlen", tp_int, ex); } else if (!strcicmp(curtokbuf, "BODY")) { /* nothing to do */ } gettok(); break; } tp = ex->val.type; if (tp->kind == TK_POINTER && tp->basetype->kind == TK_RECORD && tp->basetype->issigned) { ex = makeexpr_hat(ex, 0); tp = tp->basetype; } while (tp) { mp = curtoksym->fbase; while (mp && mp->rectype != tp) mp = mp->snext; if (mp) break; tp = tp->basetype; } if (tp) ex = makeexpr_dot(ex, mp); else { warning(format_s("No field called %s in that record [288]", curtokbuf)); ex = makeexpr_dotq(ex, curtokcase, tp_integer); } gettok(); break; case TOK_COLONCOLON: gettok(); if (wexpecttok(TOK_IDENT)) { ex = pascaltypecast(curtokmeaning->type, ex); gettok(); } break; default: return ex; } }}Expr *p_index(ex, ex2)Expr *ex, *ex2;{ Expr *ex3; Type *tp, *ot; Meaning *mp; int bits; tp = ex->val.type; if (tp->kind == TK_STRING) { if (checkconst(ex2, 0)) /* is it "s[0]"? */ return makeexpr_bicall_1("strlen", tp_char, ex); else return makeexpr_index(ex, ex2, makeexpr_long(1)); } else if (tp->kind == TK_ARRAY || tp->kind == TK_SMALLARRAY) { if (tp->smax) { ord_range_expr(tp->indextype, &ex3, NULL); if (ex3->kind == EK_VAR) var_reference((Meaning *)ex3->val.i); ex2 = makeexpr_minus(ex2, copyexpr(ex3)); if (!nodependencies(ex2, 0) && *getbitsname == '*') { mp = makestmttempvar(tp_integer, name_TEMP); ex3 = makeexpr_assign(makeexpr_var(mp), ex2); ex2 = makeexpr_var(mp); } else ex3 = NULL; ex = makeexpr_bicall_3(getbitsname, tp_int, ex, ex2, makeexpr_long(tp->escale)); if (tp->kind == TK_ARRAY) { if (tp->basetype == tp_sshort) bits = 4; else bits = 3; insertarg(&ex, 3, makeexpr_long(bits)); } ex = makeexpr_comma(ex3, ex); ot = ord_type(tp->smax->val.type); if (ot->kind == TK_ENUM && ot->meaning && useenum) ex = makeexpr_cast(ex, tp->smax->val.type); ex->val.type = tp->smax->val.type; return ex; } else { ord_range_expr(ex->val.type->indextype, &ex3, NULL); if (ex3->kind == EK_VAR) var_reference((Meaning *)ex3->val.i); if (debug>2) { fprintf(outf, "ord_range_expr returns "); dumpexpr(ex3); fprintf(outf, "\n"); } return makeexpr_index(ex, ex2, copyexpr(ex3)); } } else { warning("Index on a non-array variable [287]"); return makeexpr_bin(EK_INDEX, tp_integer, ex, ex2); }}Expr *fake_dots_n_hats(ex)Expr *ex;{ for (;;) { switch (curtok) { case TOK_HAT: case TOK_ADDR: if (ex->val.type->kind == TK_POINTER) ex = makeexpr_hat(ex, 0); else { ex->val.type = makepointertype(ex->val.type); ex = makeexpr_un(EK_HAT, ex->val.type->basetype, ex); } gettok(); break; case TOK_LBR: do { gettok(); ex = makeexpr_bin(EK_INDEX, tp_integer, ex, p_expr(tp_integer)); } while (curtok == TOK_COMMA); if (!wneedtok(TOK_RBR)) skippasttotoken(TOK_RBR, TOK_SEMI); break; case TOK_DOT: gettok(); if (!wexpecttok(TOK_IDENT)) break; ex = makeexpr_dotq(ex, curtokcase, tp_integer); gettok(); break; case TOK_COLONCOLON: gettok(); if (wexpecttok(TOK_IDENT)) { ex = pascaltypecast(curtokmeaning->type, ex); gettok(); } break; default: return ex; } }}Static void bindnames(ex)Expr *ex;{ int i; Symbol *sp; Meaning *mp; if (ex->kind == EK_NAME) { sp = findsymbol_opt(fixpascalname(ex->val.s)); if (sp) { mp = sp->mbase; while (mp && !mp->isactive) mp = mp->snext; if (mp && !strcmp(mp->name, ex->val.s)) { ex->kind = EK_VAR; ex->val.i = (long)mp; ex->val.type = mp->type; } } } i = ex->nargs; while (--i >= 0) bindnames(ex->args[i]);}void var_reference(mp)Meaning *mp;{ Meaning *mp2; mp->refcount++; if (mp->ctx && mp->ctx->kind == MK_FUNCTION && mp->ctx->needvarstruct && (mp->kind == MK_VAR || mp->kind == MK_VARREF || mp->kind == MK_VARMAC || mp->kind == MK_PARAM || mp->kind == MK_VARPARAM || (mp->kind == MK_CONST && (mp->type->kind == TK_ARRAY || mp->type->kind == TK_RECORD)))) { if (debug>1) { fprintf(outf, "varstruct'ing %s\n", mp->name); } if (!mp->varstructflag) { mp->varstructflag = 1; if (mp->constdefn && /* move init code into function body */ mp->kind != MK_VARMAC) { mp2 = addmeaningafter(mp, curtoksym, MK_VAR); curtoksym->mbase = mp2->snext; /* hide this fake variable */ mp2->snext = mp; /* remember true variable */ mp2->type = mp->type; mp2->constdefn = mp->constdefn; mp2->isforward = 1; /* declare it "static" */ mp2->refcount++; /* so it won't be purged! */ mp->constdefn = NULL; mp->isforward = 0; } } for (mp2 = curctx->ctx; mp2 != mp->ctx; mp2 = mp2->ctx) mp2->varstructflag = 1; mp2->varstructflag = 1; }}Expr *expr_reference(ex)Expr *ex;{ int i; for (i = 0; i < ex->nargs; i++) expr_reference(ex->args[i]); if (ex->kind == EK_VAR) var_reference((Meaning *)ex->val.i); return ex;}Expr *p_variable(target)Type *target;{ Expr *ex, *ex2; Meaning *mp; Symbol *sym; if (curtok != TOK_IDENT) { warning("Expected a variable [289]"); return makeexpr_long(0); } if (!curtokmeaning) { sym = curtoksym; ex = makeexpr_name(curtokcase, tp_integer); gettok(); if (curtok == TOK_LPAR) { ex = makeexpr_bicall_0(ex->val.s, tp_integer); do { gettok(); insertarg(&ex, ex->nargs, p_expr(NULL)); } while (curtok == TOK_COMMA || curtok == TOK_ASSIGN); if (!wneedtok(TOK_RPAR)) skippasttotoken(TOK_RPAR, TOK_SEMI); } if (!tryfuncmacro(&ex, NULL)) undefsym(sym); return fake_dots_n_hats(ex); } var_reference(curtokmeaning); mp = curtokmeaning; if (curtokint >= 0 /*mp->kind == MK_FIELD*/) { if (withexprs[curtokint]) ex = makeexpr_dot(copyexpr(withexprs[curtokint]), mp); else ex = makeexpr_name(mp->name, mp->type); } else if (mp->kind == MK_CONST && mp->type->kind == TK_SET && mp->constdefn) { ex = copyexpr(mp->constdefn); mp = makestmttempvar(ex->val.type, name_SET); ex2 = makeexpr(EK_MACARG, 0); ex2->val.type = ex->val.type; ex = replaceexprexpr(ex, ex2, makeexpr_var(mp), 0); freeexpr(ex2); } else if (mp->kind == MK_CONST && (mp == mp_false || mp == mp_true || mp->anyvarflag || (foldconsts > 0 && (mp->type->kind == TK_INTEGER || mp->type->kind == TK_BOOLEAN || mp->type->kind == TK_CHAR || mp->type->kind == TK_ENUM || mp->type->kind == TK_SUBR || mp->type->kind == TK_REAL)) || (foldstrconsts > 0 && (mp->type->kind == TK_STRING)))) { if (mp->constdefn) { ex = copyexpr(mp->constdefn); if (ex->val.type == tp_int) /* kludge! */ ex->val.type = tp_integer; } else ex = makeexpr_val(copyvalue(mp->val)); } else if (mp->kind == MK_VARPARAM || mp->kind == MK_VARREF) { ex = makeexpr_hat(makeexpr_var(mp), 0); } else if (mp->kind == MK_VARMAC) { ex = copyexpr(mp->constdefn); bindnames(ex); ex = gentle_cast(ex, mp->type); ex->val.type = mp->type; } else if (mp->kind == MK_SPVAR && mp->handler) { gettok(); ex = (*mp->handler)(mp); return dots_n_hats(ex, target); } else if (mp->kind == MK_VAR || mp->kind == MK_CONST || mp->kind == MK_PARAM) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -