7 * do a macro definition. tp points to the name being defined in the line
10 dodefine(Tokenrow *trp)
19 if (tp>=trp->lp || tp->type!=NAME) {
20 error(ERROR, "#defined token is not a name");
24 if (np->flag&ISUNCHANGE) {
25 error(ERROR, "#defined token %t can't be redefined", tp);
28 /* collect arguments */
31 if (tp<trp->lp && tp->type==LP && tp->wslen==0) {
36 maketokenrow(2, args);
41 if (tp->type == ELLIPS)
43 else if (tp->type!=NAME) {
49 for (atp=args->bp; atp<args->lp; atp++)
51 && strncmp((char*)atp->t, (char*)tp->t, tp->len)==0)
52 error(ERROR, "Duplicate macro argument");
59 error(ERROR, "arguments after '...' in macro");
60 if (tp->type!=COMMA) {
67 error(ERROR, "Syntax error in macro parameters");
74 if (((trp->lp)-1)->type==NL)
76 def = normtokenrow(trp);
77 if (np->flag&ISDEFINED) {
78 if (comparetokens(def, np->vp)
79 || (np->ap==NULL) != (args==NULL)
80 || np->ap && comparetokens(args, np->ap))
81 error(ERROR, "Macro redefinition of %t", trp->bp+2);
85 tap = normtokenrow(args);
91 np->flag |= ISDEFINED;
97 * Definition received via -D or -U
100 doadefine(Tokenrow *trp, int type)
103 static unsigned char one[] = "1";
104 static Token onetoken[1] = {{ NUMBER, 0, 0, 0, 1, one }};
105 static Tokenrow onetr = { onetoken, onetoken, onetoken+1, 1 };
109 if (trp->lp-trp->tp != 2 || trp->tp->type!=NAME)
111 if ((np = lookup(trp->tp, 0)) == NULL)
113 np->flag &= ~ISDEFINED;
116 if (trp->tp >= trp->lp || trp->tp->type!=NAME)
118 np = lookup(trp->tp, 1);
119 np->flag |= ISDEFINED;
121 if (trp->tp >= trp->lp || trp->tp->type==END) {
125 if (trp->tp->type!=ASGN)
128 if ((trp->lp-1)->type == END)
130 np->vp = normtokenrow(trp);
133 error(FATAL, "Illegal -D or -U argument %r", trp);
137 * Do macro expansion in a row of tokens.
138 * Flag is NULL if more input can be gathered.
141 expandrow(Tokenrow *trp, char *flag, int inmacro)
147 setsource(flag, -1, "");
148 for (tp = trp->tp; tp<trp->lp; ) {
150 || quicklook(tp->t[0], tp->len>1?tp->t[1]:0)==0
151 || (np = lookup(tp, 0))==NULL
152 || (np->flag&(ISDEFINED|ISMAC))==0
153 || tp->hideset && checkhideset(tp->hideset, np)) {
158 if (np->val==KDEFINED) {
160 if ((tp+1)<trp->lp && (tp+1)->type==NAME)
161 (tp+1)->type = NAME1;
162 else if ((tp+3)<trp->lp && (tp+1)->type==LP
163 && (tp+2)->type==NAME && (tp+3)->type==RP)
164 (tp+2)->type = NAME1;
166 error(ERROR, "Incorrect syntax for `defined'");
171 builtin(trp, np->val);
173 expand(trp, np, inmacro);
182 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
183 * Return trp->tp at the first token next to be expanded
184 * (ordinarily the beginning of the expansion)
187 expand(Tokenrow *trp, Nlist *np, int inmacro)
192 Tokenrow *atr[NARG+1];
195 copytokenrow(&ntr, np->vp); /* copy macro value */
196 if (np->ap==NULL) /* parameterless */
199 ntokc = gatherargs(trp, atr, (np->flag&ISVARMAC) ? rowlen(np->ap) : 0, &narg);
200 if (narg<0) { /* not actually a call (no '(') */
201 /* error(WARNING, "%d %r\n", narg, trp); */
202 /* gatherargs has already pushed trp->tr to the next token */
205 if (narg != rowlen(np->ap)) {
206 error(ERROR, "Disagreement in number of macro arguments");
207 trp->tp->hideset = newhideset(trp->tp->hideset, np);
211 substargs(np, &ntr, atr); /* put args into replacement */
212 for (i=0; i<narg; i++) {
218 doconcat(&ntr); /* execute ## operators */
219 hs = newhideset(trp->tp->hideset, np);
220 for (tp=ntr.bp; tp<ntr.lp; tp++) { /* distribute hidesets */
221 if (tp->type==NAME) {
225 tp->hideset = unionhideset(tp->hideset, hs);
229 insertrow(trp, ntokc, &ntr);
230 trp->tp -= rowlen(&ntr);
236 * Gather an arglist, starting in trp with tp pointing at the macro name.
237 * Return total number of tokens passed, stash number of args found.
238 * trp->tp is not changed relative to the tokenrow.
241 gatherargs(Tokenrow *trp, Tokenrow **atr, int dots, int *narg)
250 *narg = -1; /* means that there is no macro call */
255 if (trp->tp >= trp->lp) {
257 if ((trp->lp-1)->type==END) {
258 /* error(WARNING, "reach END\n"); */
265 if (trp->tp->type==LP)
267 if (trp->tp->type!=NL)
274 /* search for the terminating ), possibly extending the row */
277 if (trp->tp >= trp->lp)
283 if (trp->tp->type==END) {
286 error(ERROR, "EOF in macro arglist");
289 if (trp->tp->type==NL) {
297 if (trp->tp->type==LP)
299 else if (trp->tp->type==RP)
305 /* Now trp->tp won't move underneath us */
306 lp = bp = trp->tp+ntokp;
307 for (; parens>=0; lp++) {
308 if (lp->type == LP) {
314 if (lp->type==DSHARP)
315 lp->type = DSHARP1; /* ## not special in arg */
316 if ((lp->type==COMMA && parens==0) || (parens<0 && (lp-1)->type!=LP)) {
317 if (lp->type == COMMA && dots && *narg == dots-1)
320 error(FATAL, "Sorry, too many macro arguments");
321 ttr.bp = ttr.tp = bp;
323 atr[(*narg)++] = normtokenrow(&ttr);
331 * substitute the argument list into the replacement string
332 * This would be simple except for ## and #
335 substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
341 for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
342 if (rtr->tp->type==SHARP) { /* string operator */
345 if ((argno = lookuparg(np, rtr->tp))<0) {
346 error(ERROR, "# not followed by macro parameter");
349 ntok = 1 + (rtr->tp - tp);
351 insertrow(rtr, ntok, stringify(atr[argno]));
354 if (rtr->tp->type==NAME
355 && (argno = lookuparg(np, rtr->tp)) >= 0) {
356 if (rtr->tp < rtr->bp)
357 error(ERROR, "access out of bounds");
358 if ((rtr->tp+1)->type==DSHARP
359 || rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP)
360 insertrow(rtr, 1, atr[argno]);
362 copytokenrow(&tatr, atr[argno]);
363 expandrow(&tatr, "<macro>", Inmacro);
364 insertrow(rtr, 1, &tatr);
374 * Evaluate the ## operators in a tokenrow
377 doconcat(Tokenrow *trp)
383 for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
384 if (trp->tp->type==DSHARP1)
385 trp->tp->type = DSHARP;
386 else if (trp->tp->type==DSHARP) {
390 if (ltp<trp->bp || ntp>=trp->lp) {
391 error(ERROR, "## occurs at border of replacement");
394 len = ltp->len + ntp->len;
395 strncpy((char*)tt, (char*)ltp->t, ltp->len);
396 strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len);
398 setsource("<##>", -1, tt);
399 maketokenrow(3, &ntr);
402 if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS)
403 error(WARNING, "Bad token %r produced by ##", &ntr);
407 insertrow(trp, (ntp-ltp)+1, &ntr);
415 * tp is a potential parameter name of macro mac;
416 * look it up in mac's arglist, and if found, return the
417 * corresponding index in the argname array. Return -1 if not found.
420 lookuparg(Nlist *mac, Token *tp)
424 if (tp->type!=NAME || mac->ap==NULL)
426 if((mac->flag & ISVARMAC) && strcmp((char*)tp->t, "__VA_ARGS__") == 0)
427 return rowlen(mac->ap) - 1;
428 for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
429 if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
430 return ap - mac->ap->bp;
436 * Return a quoted version of the tokenrow (from # arg)
440 stringify(Tokenrow *vp)
442 static Token t = { STRING };
443 static Tokenrow tr = { &t, &t, &t+1, 1 };
450 for (tp = vp->bp; tp < vp->lp; tp++) {
451 instring = tp->type==STRING || tp->type==CCON;
452 if (sp+2*tp->len >= &s[STRLEN-10]) {
453 error(ERROR, "Stringified macro arg is too long");
456 if (tp->wslen /* && (tp->flag&XPWS)==0 */)
458 for (i=0, cp=tp->t; i<tp->len; i++) {
459 if (instring && (*cp=='"' || *cp=='\\'))
467 t.len = strlen((char*)sp);
468 t.t = newstring(sp, t.len, 0);
473 * expand a builtin name
476 builtin(Tokenrow *trp, int biname)
484 /* need to find the real source */
486 while (s && s->fd==-1)
490 /* most are strings */
502 op = outnum(op-1, s->line);
506 strcpy(op, s->filename);
507 op += strlen(s->filename);
511 strncpy(op, curtime+4, 7);
512 strncpy(op+7, curtime+24, 4); /* Plan 9 asctime disobeys standard */
517 strncpy(op, curtime+11, 8);
522 error(ERROR, "cpp botch: unknown internal macro");
525 if (tp->type==STRING)
527 tp->t = (uchar*)outp;