Coverage Report

Created: 2024-04-23 06:32

/src/testdir/build/lua-master/source/lparser.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lparser.c $
3
** Lua Parser
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lparser_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <limits.h>
14
#include <string.h>
15
16
#include "lua.h"
17
18
#include "lcode.h"
19
#include "ldebug.h"
20
#include "ldo.h"
21
#include "lfunc.h"
22
#include "llex.h"
23
#include "lmem.h"
24
#include "lobject.h"
25
#include "lopcodes.h"
26
#include "lparser.h"
27
#include "lstate.h"
28
#include "lstring.h"
29
#include "ltable.h"
30
31
32
33
/* maximum number of local variables per function (must be smaller
34
   than 250, due to the bytecode format) */
35
31.8k
#define MAXVARS   200
36
37
38
400k
#define hasmultret(k)   ((k) == VCALL || (k) == VVARARG)
39
40
41
/* because all strings are unified by the scanner, the parser
42
   can use pointer equality for string equality */
43
325M
#define eqstr(a,b)  ((a) == (b))
44
45
46
/*
47
** nodes for block list (list of active blocks)
48
*/
49
typedef struct BlockCnt {
50
  struct BlockCnt *previous;  /* chain */
51
  int firstlabel;  /* index of first label in this block */
52
  int firstgoto;  /* index of first pending goto in this block */
53
  lu_byte nactvar;  /* # active locals outside the block */
54
  lu_byte upval;  /* true if some variable in the block is an upvalue */
55
  lu_byte isloop;  /* true if 'block' is a loop */
56
  lu_byte insidetbc;  /* true if inside the scope of a to-be-closed var. */
57
} BlockCnt;
58
59
60
61
/*
62
** prototypes for recursive non-terminal functions
63
*/
64
static void statement (LexState *ls);
65
static void expr (LexState *ls, expdesc *v);
66
67
68
33
static l_noret error_expected (LexState *ls, int token) {
69
33
  luaX_syntaxerror(ls,
70
33
      luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
71
33
}
72
73
74
5
static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
75
5
  lua_State *L = fs->ls->L;
76
5
  const char *msg;
77
5
  int line = fs->f->linedefined;
78
5
  const char *where = (line == 0)
79
5
                      ? "main function"
80
5
                      : luaO_pushfstring(L, "function at line %d", line);
81
5
  msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
82
5
                             what, limit, where);
83
5
  luaX_syntaxerror(fs->ls, msg);
84
5
}
85
86
87
61.4k
static void checklimit (FuncState *fs, int v, int l, const char *what) {
88
61.4k
  if (v > l) errorlimit(fs, l, what);
89
61.4k
}
90
91
92
/*
93
** Test whether next token is 'c'; if so, skip it.
94
*/
95
2.04M
static int testnext (LexState *ls, int c) {
96
2.04M
  if (ls->t.token == c) {
97
1.79M
    luaX_next(ls);
98
1.79M
    return 1;
99
1.79M
  }
100
255k
  else return 0;
101
2.04M
}
102
103
104
/*
105
** Check that next token is 'c'.
106
*/
107
26.4M
static void check (LexState *ls, int c) {
108
26.4M
  if (ls->t.token != c)
109
14
    error_expected(ls, c);
110
26.4M
}
111
112
113
/*
114
** Check that next token is 'c' and skip it.
115
*/
116
186k
static void checknext (LexState *ls, int c) {
117
186k
  check(ls, c);
118
186k
  luaX_next(ls);
119
186k
}
120
121
122
200k
#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
123
124
125
/*
126
** Check that next token is 'what' and skip it. In case of error,
127
** raise an error that the expected 'what' should match a 'who'
128
** in line 'where' (if that is not the current line).
129
*/
130
134k
static void check_match (LexState *ls, int what, int who, int where) {
131
134k
  if (l_unlikely(!testnext(ls, what))) {
132
62
    if (where == ls->linenumber)  /* all in the same line? */
133
19
      error_expected(ls, what);  /* do not need a complex message */
134
43
    else {
135
43
      luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
136
43
             "%s expected (to close %s at line %d)",
137
43
              luaX_token2str(ls, what), luaX_token2str(ls, who), where));
138
43
    }
139
62
  }
140
134k
}
141
142
143
26.2M
static TString *str_checkname (LexState *ls) {
144
26.2M
  TString *ts;
145
26.2M
  check(ls, TK_NAME);
146
26.2M
  ts = ls->t.seminfo.ts;
147
26.2M
  luaX_next(ls);
148
26.2M
  return ts;
149
26.2M
}
150
151
152
53.9M
static void init_exp (expdesc *e, expkind k, int i) {
153
53.9M
  e->f = e->t = NO_JUMP;
154
53.9M
  e->k = k;
155
53.9M
  e->u.info = i;
156
53.9M
}
157
158
159
26.4M
static void codestring (expdesc *e, TString *s) {
160
26.4M
  e->f = e->t = NO_JUMP;
161
26.4M
  e->k = VKSTR;
162
26.4M
  e->u.strval = s;
163
26.4M
}
164
165
166
14.0k
static void codename (LexState *ls, expdesc *e) {
167
14.0k
  codestring(e, str_checkname(ls));
168
14.0k
}
169
170
171
/*
172
** Register a new local variable in the active 'Proto' (for debug
173
** information).
174
*/
175
30.7k
static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
176
30.7k
  Proto *f = fs->f;
177
30.7k
  int oldsize = f->sizelocvars;
178
30.7k
  luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars,
179
30.7k
                  LocVar, SHRT_MAX, "local variables");
180
78.3k
  while (oldsize < f->sizelocvars)
181
47.6k
    f->locvars[oldsize++].varname = NULL;
182
30.7k
  f->locvars[fs->ndebugvars].varname = varname;
183
30.7k
  f->locvars[fs->ndebugvars].startpc = fs->pc;
184
30.7k
  luaC_objbarrier(ls->L, f, varname);
185
0
  return fs->ndebugvars++;
186
30.7k
}
187
188
189
/*
190
** Create a new local variable with the given 'name' and given 'kind'.
191
** Return its index in the function.
192
*/
193
31.8k
static int new_localvarkind (LexState *ls, TString *name, int kind) {
194
31.8k
  lua_State *L = ls->L;
195
31.8k
  FuncState *fs = ls->fs;
196
31.8k
  Dyndata *dyd = ls->dyd;
197
31.8k
  Vardesc *var;
198
31.8k
  checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
199
31.8k
                 MAXVARS, "local variables");
200
31.8k
  luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
201
31.8k
                  dyd->actvar.size, Vardesc, USHRT_MAX, "local variables");
202
31.8k
  var = &dyd->actvar.arr[dyd->actvar.n++];
203
31.8k
  var->vd.kind = kind;  /* default */
204
31.8k
  var->vd.name = name;
205
31.8k
  return dyd->actvar.n - 1 - fs->firstlocal;
206
31.8k
}
207
208
209
/*
210
** Create a new local variable with the given 'name' and regular kind.
211
*/
212
17.6k
static int new_localvar (LexState *ls, TString *name) {
213
17.6k
  return new_localvarkind(ls, name, VDKREG);
214
17.6k
}
215
216
#define new_localvarliteral(ls,v) \
217
4.01k
    new_localvar(ls,  \
218
4.01k
      luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1));
219
220
221
222
/*
223
** Return the "variable description" (Vardesc) of a given variable.
224
** (Unless noted otherwise, all variables are referred to by their
225
** compiler indices.)
226
*/
227
300M
static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
228
300M
  return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx];
229
300M
}
230
231
232
/*
233
** Convert 'nvar', a compiler index level, to its corresponding
234
** register. For that, search for the highest variable below that level
235
** that is in a register and uses its register index ('ridx') plus one.
236
*/
237
70.2M
static int reglevel (FuncState *fs, int nvar) {
238
83.8M
  while (nvar-- > 0) {
239
29.4M
    Vardesc *vd = getlocalvardesc(fs, nvar);  /* get previous variable */
240
29.4M
    if (vd->vd.kind != RDKCTC)  /* is in a register? */
241
15.7M
      return vd->vd.ridx + 1;
242
29.4M
  }
243
54.4M
  return 0;  /* no variables in registers */
244
70.2M
}
245
246
247
/*
248
** Return the number of variables in the register stack for the given
249
** function.
250
*/
251
70.1M
int luaY_nvarstack (FuncState *fs) {
252
70.1M
  return reglevel(fs, fs->nactvar);
253
70.1M
}
254
255
256
/*
257
** Get the debug-information entry for current variable 'vidx'.
258
*/
259
20.4k
static LocVar *localdebuginfo (FuncState *fs, int vidx) {
260
20.4k
  Vardesc *vd = getlocalvardesc(fs,  vidx);
261
20.4k
  if (vd->vd.kind == RDKCTC)
262
431
    return NULL;  /* no debug info. for constants */
263
20.0k
  else {
264
20.0k
    int idx = vd->vd.pidx;
265
20.0k
    lua_assert(idx < fs->ndebugvars);
266
20.0k
    return &fs->f->locvars[idx];
267
20.0k
  }
268
20.4k
}
269
270
271
/*
272
** Create an expression representing variable 'vidx'
273
*/
274
74.7k
static void init_var (FuncState *fs, expdesc *e, int vidx) {
275
74.7k
  e->f = e->t = NO_JUMP;
276
74.7k
  e->k = VLOCAL;
277
74.7k
  e->u.var.vidx = vidx;
278
74.7k
  e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx;
279
74.7k
}
280
281
282
/*
283
** Raises an error if variable described by 'e' is read only
284
*/
285
103k
static void check_readonly (LexState *ls, expdesc *e) {
286
103k
  FuncState *fs = ls->fs;
287
103k
  TString *varname = NULL;  /* to be set if variable is const */
288
103k
  switch (e->k) {
289
0
    case VCONST: {
290
0
      varname = ls->dyd->actvar.arr[e->u.info].vd.name;
291
0
      break;
292
0
    }
293
4.44k
    case VLOCAL: {
294
4.44k
      Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx);
295
4.44k
      if (vardesc->vd.kind != VDKREG)  /* not a regular variable? */
296
0
        varname = vardesc->vd.name;
297
4.44k
      break;
298
0
    }
299
8.54k
    case VUPVAL: {
300
8.54k
      Upvaldesc *up = &fs->f->upvalues[e->u.info];
301
8.54k
      if (up->kind != VDKREG)
302
0
        varname = up->name;
303
8.54k
      break;
304
0
    }
305
90.0k
    default:
306
90.0k
      return;  /* other cases cannot be read-only */
307
103k
  }
308
12.9k
  if (varname) {
309
0
    const char *msg = luaO_pushfstring(ls->L,
310
0
       "attempt to assign to const variable '%s'", getstr(varname));
311
0
    luaK_semerror(ls, msg);  /* error */
312
0
  }
313
12.9k
}
314
315
316
/*
317
** Start the scope for the last 'nvars' created variables.
318
*/
319
32.8k
static void adjustlocalvars (LexState *ls, int nvars) {
320
32.8k
  FuncState *fs = ls->fs;
321
32.8k
  int reglevel = luaY_nvarstack(fs);
322
32.8k
  int i;
323
63.5k
  for (i = 0; i < nvars; i++) {
324
30.7k
    int vidx = fs->nactvar++;
325
30.7k
    Vardesc *var = getlocalvardesc(fs, vidx);
326
30.7k
    var->vd.ridx = reglevel++;
327
30.7k
    var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
328
30.7k
  }
329
32.8k
}
330
331
332
/*
333
** Close the scope for all variables up to level 'tolevel'.
334
** (debug info.)
335
*/
336
40.2k
static void removevars (FuncState *fs, int tolevel) {
337
40.2k
  fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
338
58.7k
  while (fs->nactvar > tolevel) {
339
18.4k
    LocVar *var = localdebuginfo(fs, --fs->nactvar);
340
18.4k
    if (var)  /* does it have debug information? */
341
18.0k
      var->endpc = fs->pc;
342
18.4k
  }
343
40.2k
}
344
345
346
/*
347
** Search the upvalues of the function 'fs' for one
348
** with the given 'name'.
349
*/
350
51.8M
static int searchupvalue (FuncState *fs, TString *name) {
351
51.8M
  int i;
352
51.8M
  Upvaldesc *up = fs->f->upvalues;
353
80.9M
  for (i = 0; i < fs->nups; i++) {
354
54.0M
    if (eqstr(up[i].name, name)) return i;
355
54.0M
  }
356
26.9M
  return -1;  /* not found */
357
51.8M
}
358
359
360
29.6k
static Upvaldesc *allocupvalue (FuncState *fs) {
361
29.6k
  Proto *f = fs->f;
362
29.6k
  int oldsize = f->sizeupvalues;
363
29.6k
  checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
364
29.6k
  luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
365
29.6k
                  Upvaldesc, MAXUPVAL, "upvalues");
366
112k
  while (oldsize < f->sizeupvalues)
367
82.9k
    f->upvalues[oldsize++].name = NULL;
368
29.6k
  return &f->upvalues[fs->nups++];
369
29.6k
}
370
371
372
29.0k
static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
373
29.0k
  Upvaldesc *up = allocupvalue(fs);
374
29.0k
  FuncState *prev = fs->prev;
375
29.0k
  if (v->k == VLOCAL) {
376
6.66k
    up->instack = 1;
377
6.66k
    up->idx = v->u.var.ridx;
378
6.66k
    up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind;
379
6.66k
    lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name));
380
6.66k
  }
381
22.3k
  else {
382
22.3k
    up->instack = 0;
383
22.3k
    up->idx = cast_byte(v->u.info);
384
22.3k
    up->kind = prev->f->upvalues[v->u.info].kind;
385
22.3k
    lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name));
386
22.3k
  }
387
29.0k
  up->name = name;
388
29.0k
  luaC_objbarrier(fs->ls->L, fs->f, name);
389
0
  return fs->nups - 1;
390
29.0k
}
391
392
393
/*
394
** Look for an active local variable with the name 'n' in the
395
** function 'fs'. If found, initialize 'var' with it and return
396
** its expression kind; otherwise return -1.
397
*/
398
53.1M
static int searchvar (FuncState *fs, TString *n, expdesc *var) {
399
53.1M
  int i;
400
323M
  for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
401
271M
    Vardesc *vd = getlocalvardesc(fs, i);
402
271M
    if (eqstr(n, vd->vd.name)) {  /* found? */
403
1.25M
      if (vd->vd.kind == RDKCTC)  /* compile-time constant? */
404
1.18M
        init_exp(var, VCONST, fs->firstlocal + i);
405
74.7k
      else  /* real variable */
406
74.7k
        init_var(fs, var, i);
407
1.25M
      return var->k;
408
1.25M
    }
409
271M
  }
410
51.8M
  return -1;  /* not found */
411
53.1M
}
412
413
414
/*
415
** Mark block where variable at given level was defined
416
** (to emit close instructions later).
417
*/
418
6.66k
static void markupval (FuncState *fs, int level) {
419
6.66k
  BlockCnt *bl = fs->bl;
420
16.2k
  while (bl->nactvar > level)
421
9.62k
    bl = bl->previous;
422
6.66k
  bl->upval = 1;
423
6.66k
  fs->needclose = 1;
424
6.66k
}
425
426
427
/*
428
** Mark that current block has a to-be-closed variable.
429
*/
430
1.15k
static void marktobeclosed (FuncState *fs) {
431
1.15k
  BlockCnt *bl = fs->bl;
432
1.15k
  bl->upval = 1;
433
1.15k
  bl->insidetbc = 1;
434
1.15k
  fs->needclose = 1;
435
1.15k
}
436
437
438
/*
439
** Find a variable with the given name 'n'. If it is an upvalue, add
440
** this upvalue into all intermediate functions. If it is a global, set
441
** 'var' as 'void' as a flag.
442
*/
443
79.2M
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
444
79.2M
  if (fs == NULL)  /* no more levels? */
445
26.0M
    init_exp(var, VVOID, 0);  /* default is global */
446
53.1M
  else {
447
53.1M
    int v = searchvar(fs, n, var);  /* look up locals at current level */
448
53.1M
    if (v >= 0) {  /* found? */
449
1.25M
      if (v == VLOCAL && !base)
450
6.66k
        markupval(fs, var->u.var.vidx);  /* local will be used as an upval */
451
1.25M
    }
452
51.8M
    else {  /* not found as local at current level; try upvalues */
453
51.8M
      int idx = searchupvalue(fs, n);  /* try existing upvalues */
454
51.8M
      if (idx < 0) {  /* not found? */
455
26.9M
        singlevaraux(fs->prev, n, var, 0);  /* try upper levels */
456
26.9M
        if (var->k == VLOCAL || var->k == VUPVAL)  /* local or upvalue? */
457
29.0k
          idx  = newupvalue(fs, n, var);  /* will be a new upvalue */
458
26.9M
        else  /* it is a global or a constant */
459
26.9M
          return;  /* don't need to do anything at this level */
460
26.9M
      }
461
24.9M
      init_exp(var, VUPVAL, idx);  /* new or old upvalue */
462
24.9M
    }
463
53.1M
  }
464
79.2M
}
465
466
467
/*
468
** Find a variable with the given name 'n', handling global variables
469
** too.
470
*/
471
26.2M
static void singlevar (LexState *ls, expdesc *var) {
472
26.2M
  TString *varname = str_checkname(ls);
473
26.2M
  FuncState *fs = ls->fs;
474
26.2M
  singlevaraux(fs, varname, var, 1);
475
26.2M
  if (var->k == VVOID) {  /* global name? */
476
26.0M
    expdesc key;
477
26.0M
    singlevaraux(fs, ls->envn, var, 1);  /* get environment variable */
478
26.0M
    lua_assert(var->k != VVOID);  /* this one must exist */
479
26.0M
    luaK_exp2anyregup(fs, var);  /* but could be a constant */
480
26.0M
    codestring(&key, varname);  /* key is variable name */
481
26.0M
    luaK_indexed(fs, var, &key);  /* env[varname] */
482
26.0M
  }
483
26.2M
}
484
485
486
/*
487
** Adjust the number of results from an expression list 'e' with 'nexps'
488
** expressions to 'nvars' values.
489
*/
490
21.9k
static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
491
21.9k
  FuncState *fs = ls->fs;
492
21.9k
  int needed = nvars - nexps;  /* extra values needed */
493
21.9k
  if (hasmultret(e->k)) {  /* last expression has multiple returns? */
494
598
    int extra = needed + 1;  /* discount last expression itself */
495
598
    if (extra < 0)
496
117
      extra = 0;
497
598
    luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
498
598
  }
499
21.3k
  else {
500
21.3k
    if (e->k != VVOID)  /* at least one expression? */
501
20.1k
      luaK_exp2nextreg(fs, e);  /* close last expression */
502
21.3k
    if (needed > 0)  /* missing values? */
503
5.94k
      luaK_nil(fs, fs->freereg, needed);  /* complete with nils */
504
21.3k
  }
505
21.9k
  if (needed > 0)
506
6.02k
    luaK_reserveregs(fs, needed);  /* registers for extra values */
507
15.9k
  else  /* adding 'needed' is actually a subtraction */
508
15.9k
    fs->freereg += needed;  /* remove extra values */
509
21.9k
}
510
511
512
27.9M
#define enterlevel(ls)  luaE_incCstack(ls->L)
513
514
515
27.9M
#define leavelevel(ls) ((ls)->L->nCcalls--)
516
517
518
/*
519
** Generates an error that a goto jumps into the scope of some
520
** local variable.
521
*/
522
0
static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) {
523
0
  TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name;
524
0
  const char *varname = getstr(tsname);
525
0
  const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'";
526
0
  msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname);
527
0
  luaK_semerror(ls, msg);  /* raise the error */
528
0
}
529
530
531
/*
532
** Solves the goto at index 'g' to given 'label' and removes it
533
** from the list of pending gotos.
534
** If it jumps into the scope of some variable, raises an error.
535
*/
536
23.4k
static void solvegoto (LexState *ls, int g, Labeldesc *label) {
537
23.4k
  int i;
538
23.4k
  Labellist *gl = &ls->dyd->gt;  /* list of gotos */
539
23.4k
  Labeldesc *gt = &gl->arr[g];  /* goto to be resolved */
540
23.4k
  lua_assert(eqstr(gt->name, label->name));
541
23.4k
  if (l_unlikely(gt->nactvar < label->nactvar))  /* enter some scope? */
542
0
    jumpscopeerror(ls, gt);
543
23.4k
  luaK_patchlist(ls->fs, gt->pc, label->pc);
544
141M
  for (i = g; i < gl->n - 1; i++)  /* remove goto from pending list */
545
141M
    gl->arr[i] = gl->arr[i + 1];
546
23.4k
  gl->n--;
547
23.4k
}
548
549
550
/*
551
** Search for an active label with the given name.
552
*/
553
25.4k
static Labeldesc *findlabel (LexState *ls, TString *name) {
554
25.4k
  int i;
555
25.4k
  Dyndata *dyd = ls->dyd;
556
  /* check labels in current function for a match */
557
26.1k
  for (i = ls->fs->firstlabel; i < dyd->label.n; i++) {
558
681
    Labeldesc *lb = &dyd->label.arr[i];
559
681
    if (eqstr(lb->name, name))  /* correct label? */
560
0
      return lb;
561
681
  }
562
25.4k
  return NULL;  /* label not found */
563
25.4k
}
564
565
566
/*
567
** Adds a new label/goto in the corresponding list.
568
*/
569
static int newlabelentry (LexState *ls, Labellist *l, TString *name,
570
37.7k
                          int line, int pc) {
571
37.7k
  int n = l->n;
572
37.7k
  luaM_growvector(ls->L, l->arr, n, l->size,
573
37.7k
                  Labeldesc, SHRT_MAX, "labels/gotos");
574
37.7k
  l->arr[n].name = name;
575
37.7k
  l->arr[n].line = line;
576
37.7k
  l->arr[n].nactvar = ls->fs->nactvar;
577
37.7k
  l->arr[n].close = 0;
578
37.7k
  l->arr[n].pc = pc;
579
37.7k
  l->n = n + 1;
580
37.7k
  return n;
581
37.7k
}
582
583
584
33.7k
static int newgotoentry (LexState *ls, TString *name, int line, int pc) {
585
33.7k
  return newlabelentry(ls, &ls->dyd->gt, name, line, pc);
586
33.7k
}
587
588
589
/*
590
** Solves forward jumps. Check whether new label 'lb' matches any
591
** pending gotos in current block and solves them. Return true
592
** if any of the gotos need to close upvalues.
593
*/
594
4.01k
static int solvegotos (LexState *ls, Labeldesc *lb) {
595
4.01k
  Labellist *gl = &ls->dyd->gt;
596
4.01k
  int i = ls->fs->bl->firstgoto;
597
4.01k
  int needsclose = 0;
598
28.7k
  while (i < gl->n) {
599
24.7k
    if (eqstr(gl->arr[i].name, lb->name)) {
600
23.4k
      needsclose |= gl->arr[i].close;
601
23.4k
      solvegoto(ls, i, lb);  /* will remove 'i' from the list */
602
23.4k
    }
603
1.29k
    else
604
1.29k
      i++;
605
24.7k
  }
606
4.01k
  return needsclose;
607
4.01k
}
608
609
610
/*
611
** Create a new label with the given 'name' at the given 'line'.
612
** 'last' tells whether label is the last non-op statement in its
613
** block. Solves all pending gotos to this new label and adds
614
** a close instruction if necessary.
615
** Returns true iff it added a close instruction.
616
*/
617
static int createlabel (LexState *ls, TString *name, int line,
618
4.01k
                        int last) {
619
4.01k
  FuncState *fs = ls->fs;
620
4.01k
  Labellist *ll = &ls->dyd->label;
621
4.01k
  int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs));
622
4.01k
  if (last) {  /* label is last no-op statement in the block? */
623
    /* assume that locals are already out of scope */
624
2
    ll->arr[l].nactvar = fs->bl->nactvar;
625
2
  }
626
4.01k
  if (solvegotos(ls, &ll->arr[l])) {  /* need close? */
627
108
    luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0);
628
108
    return 1;
629
108
  }
630
3.90k
  return 0;
631
4.01k
}
632
633
634
/*
635
** Adjust pending gotos to outer level of a block.
636
*/
637
19.3k
static void movegotosout (FuncState *fs, BlockCnt *bl) {
638
19.3k
  int i;
639
19.3k
  Labellist *gl = &fs->ls->dyd->gt;
640
  /* correct pending gotos to current block */
641
51.3k
  for (i = bl->firstgoto; i < gl->n; i++) {  /* for each pending goto */
642
32.0k
    Labeldesc *gt = &gl->arr[i];
643
    /* leaving a variable scope? */
644
32.0k
    if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar))
645
12.2k
      gt->close |= bl->upval;  /* jump may need a close */
646
32.0k
    gt->nactvar = bl->nactvar;  /* update goto level */
647
32.0k
  }
648
19.3k
}
649
650
651
43.6k
static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
652
43.6k
  bl->isloop = isloop;
653
43.6k
  bl->nactvar = fs->nactvar;
654
43.6k
  bl->firstlabel = fs->ls->dyd->label.n;
655
43.6k
  bl->firstgoto = fs->ls->dyd->gt.n;
656
43.6k
  bl->upval = 0;
657
43.6k
  bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc);
658
43.6k
  bl->previous = fs->bl;
659
43.6k
  fs->bl = bl;
660
43.6k
  lua_assert(fs->freereg == luaY_nvarstack(fs));
661
43.6k
}
662
663
664
/*
665
** generates an error for an undefined 'goto'.
666
*/
667
1
static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
668
1
  const char *msg;
669
1
  if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) {
670
0
    msg = "break outside loop at line %d";
671
0
    msg = luaO_pushfstring(ls->L, msg, gt->line);
672
0
  }
673
1
  else {
674
1
    msg = "no visible label '%s' for <goto> at line %d";
675
1
    msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
676
1
  }
677
1
  luaK_semerror(ls, msg);
678
1
}
679
680
681
40.2k
static void leaveblock (FuncState *fs) {
682
40.2k
  BlockCnt *bl = fs->bl;
683
40.2k
  LexState *ls = fs->ls;
684
40.2k
  int hasclose = 0;
685
40.2k
  int stklevel = reglevel(fs, bl->nactvar);  /* level outside the block */
686
40.2k
  removevars(fs, bl->nactvar);  /* remove block locals */
687
40.2k
  lua_assert(bl->nactvar == fs->nactvar);  /* back to level on entry */
688
40.2k
  if (bl->isloop)  /* has to fix pending breaks? */
689
3.97k
    hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0);
690
40.2k
  if (!hasclose && bl->previous && bl->upval)  /* still need a 'close'? */
691
880
    luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0);
692
40.2k
  fs->freereg = stklevel;  /* free registers */
693
40.2k
  ls->dyd->label.n = bl->firstlabel;  /* remove local labels */
694
40.2k
  fs->bl = bl->previous;  /* current block now is previous one */
695
40.2k
  if (bl->previous)  /* was it a nested block? */
696
19.3k
    movegotosout(fs, bl);  /* update pending gotos to enclosing block */
697
20.9k
  else {
698
20.9k
    if (bl->firstgoto < ls->dyd->gt.n)  /* still pending gotos? */
699
1
      undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]);  /* error */
700
20.9k
  }
701
40.2k
}
702
703
704
/*
705
** adds a new prototype into list of prototypes
706
*/
707
21.1k
static Proto *addprototype (LexState *ls) {
708
21.1k
  Proto *clp;
709
21.1k
  lua_State *L = ls->L;
710
21.1k
  FuncState *fs = ls->fs;
711
21.1k
  Proto *f = fs->f;  /* prototype of current function */
712
21.1k
  if (fs->np >= f->sizep) {
713
2.86k
    int oldsize = f->sizep;
714
2.86k
    luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
715
39.4k
    while (oldsize < f->sizep)
716
36.5k
      f->p[oldsize++] = NULL;
717
2.86k
  }
718
21.1k
  f->p[fs->np++] = clp = luaF_newproto(L);
719
21.1k
  luaC_objbarrier(L, f, clp);
720
0
  return clp;
721
21.1k
}
722
723
724
/*
725
** codes instruction to create new closure in parent function.
726
** The OP_CLOSURE instruction uses the last available register,
727
** so that, if it invokes the GC, the GC knows which registers
728
** are in use at that time.
729
730
*/
731
20.8k
static void codeclosure (LexState *ls, expdesc *v) {
732
20.8k
  FuncState *fs = ls->fs->prev;
733
20.8k
  init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
734
20.8k
  luaK_exp2nextreg(fs, v);  /* fix it at the last register */
735
20.8k
}
736
737
738
21.7k
static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
739
21.7k
  Proto *f = fs->f;
740
21.7k
  fs->prev = ls->fs;  /* linked list of funcstates */
741
21.7k
  fs->ls = ls;
742
21.7k
  ls->fs = fs;
743
21.7k
  fs->pc = 0;
744
21.7k
  fs->previousline = f->linedefined;
745
21.7k
  fs->iwthabs = 0;
746
21.7k
  fs->lasttarget = 0;
747
21.7k
  fs->freereg = 0;
748
21.7k
  fs->nk = 0;
749
21.7k
  fs->nabslineinfo = 0;
750
21.7k
  fs->np = 0;
751
21.7k
  fs->nups = 0;
752
21.7k
  fs->ndebugvars = 0;
753
21.7k
  fs->nactvar = 0;
754
21.7k
  fs->needclose = 0;
755
21.7k
  fs->firstlocal = ls->dyd->actvar.n;
756
21.7k
  fs->firstlabel = ls->dyd->label.n;
757
21.7k
  fs->bl = NULL;
758
21.7k
  f->source = ls->source;
759
21.7k
  luaC_objbarrier(ls->L, f, f->source);
760
0
  f->maxstacksize = 2;  /* registers 0/1 are always valid */
761
21.7k
  enterblock(fs, bl, 0);
762
21.7k
}
763
764
765
20.9k
static void close_func (LexState *ls) {
766
20.9k
  lua_State *L = ls->L;
767
20.9k
  FuncState *fs = ls->fs;
768
20.9k
  Proto *f = fs->f;
769
20.9k
  luaK_ret(fs, luaY_nvarstack(fs), 0);  /* final return */
770
20.9k
  leaveblock(fs);
771
20.9k
  lua_assert(fs->bl == NULL);
772
20.9k
  luaK_finish(fs);
773
20.9k
  luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction);
774
20.9k
  luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte);
775
20.9k
  luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo,
776
20.9k
                       fs->nabslineinfo, AbsLineInfo);
777
20.9k
  luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue);
778
20.9k
  luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *);
779
20.9k
  luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
780
20.9k
  luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
781
20.9k
  ls->fs = fs->prev;
782
20.9k
  luaC_checkGC(L);
783
20.9k
}
784
785
786
787
/*============================================================*/
788
/* GRAMMAR RULES */
789
/*============================================================*/
790
791
792
/*
793
** check whether current token is in the follow set of a block.
794
** 'until' closes syntactical blocks, but do not close scope,
795
** so it is handled in separate.
796
*/
797
259k
static int block_follow (LexState *ls, int withuntil) {
798
259k
  switch (ls->t.token) {
799
5.03k
    case TK_ELSE: case TK_ELSEIF:
800
27.6k
    case TK_END: case TK_EOS:
801
27.6k
      return 1;
802
32
    case TK_UNTIL: return withuntil;
803
231k
    default: return 0;
804
259k
  }
805
259k
}
806
807
808
31.8k
static void statlist (LexState *ls) {
809
  /* statlist -> { stat [';'] } */
810
245k
  while (!block_follow(ls, 1)) {
811
221k
    if (ls->t.token == TK_RETURN) {
812
7.96k
      statement(ls);
813
7.96k
      return;  /* 'return' must be last statement */
814
7.96k
    }
815
213k
    statement(ls);
816
213k
  }
817
31.8k
}
818
819
820
13.7k
static void fieldsel (LexState *ls, expdesc *v) {
821
  /* fieldsel -> ['.' | ':'] NAME */
822
13.7k
  FuncState *fs = ls->fs;
823
13.7k
  expdesc key;
824
13.7k
  luaK_exp2anyregup(fs, v);
825
13.7k
  luaX_next(ls);  /* skip the dot or colon */
826
13.7k
  codename(ls, &key);
827
13.7k
  luaK_indexed(fs, v, &key);
828
13.7k
}
829
830
831
34.5k
static void yindex (LexState *ls, expdesc *v) {
832
  /* index -> '[' expr ']' */
833
34.5k
  luaX_next(ls);  /* skip the '[' */
834
34.5k
  expr(ls, v);
835
34.5k
  luaK_exp2val(ls->fs, v);
836
34.5k
  checknext(ls, ']');
837
34.5k
}
838
839
840
/*
841
** {======================================================================
842
** Rules for Constructors
843
** =======================================================================
844
*/
845
846
847
typedef struct ConsControl {
848
  expdesc v;  /* last list item read */
849
  expdesc *t;  /* table descriptor */
850
  int nh;  /* total number of 'record' elements */
851
  int na;  /* number of array elements already stored */
852
  int tostore;  /* number of array elements pending to be stored */
853
} ConsControl;
854
855
856
11
static void recfield (LexState *ls, ConsControl *cc) {
857
  /* recfield -> (NAME | '['exp']') = exp */
858
11
  FuncState *fs = ls->fs;
859
11
  int reg = ls->fs->freereg;
860
11
  expdesc tab, key, val;
861
11
  if (ls->t.token == TK_NAME) {
862
10
    checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
863
10
    codename(ls, &key);
864
10
  }
865
1
  else  /* ls->t.token == '[' */
866
1
    yindex(ls, &key);
867
11
  cc->nh++;
868
11
  checknext(ls, '=');
869
11
  tab = *cc->t;
870
11
  luaK_indexed(fs, &tab, &key);
871
11
  expr(ls, &val);
872
11
  luaK_storevar(fs, &tab, &val);
873
11
  fs->freereg = reg;  /* free registers */
874
11
}
875
876
877
1.55M
static void closelistfield (FuncState *fs, ConsControl *cc) {
878
1.55M
  if (cc->v.k == VVOID) return;  /* there is no list item */
879
1.55M
  luaK_exp2nextreg(fs, &cc->v);
880
1.55M
  cc->v.k = VVOID;
881
1.55M
  if (cc->tostore == LFIELDS_PER_FLUSH) {
882
31.1k
    luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);  /* flush */
883
31.1k
    cc->na += cc->tostore;
884
31.1k
    cc->tostore = 0;  /* no more items pending */
885
31.1k
  }
886
1.55M
}
887
888
889
4.61k
static void lastlistfield (FuncState *fs, ConsControl *cc) {
890
4.61k
  if (cc->tostore == 0) return;
891
1.03k
  if (hasmultret(cc->v.k)) {
892
408
    luaK_setmultret(fs, &cc->v);
893
408
    luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET);
894
408
    cc->na--;  /* do not count last expression (unknown number of elements) */
895
408
  }
896
625
  else {
897
625
    if (cc->v.k != VVOID)
898
625
      luaK_exp2nextreg(fs, &cc->v);
899
625
    luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
900
625
  }
901
1.03k
  cc->na += cc->tostore;
902
1.03k
}
903
904
905
1.55M
static void listfield (LexState *ls, ConsControl *cc) {
906
  /* listfield -> exp */
907
1.55M
  expr(ls, &cc->v);
908
1.55M
  cc->tostore++;
909
1.55M
}
910
911
912
1.55M
static void field (LexState *ls, ConsControl *cc) {
913
  /* field -> listfield | recfield */
914
1.55M
  switch(ls->t.token) {
915
1.54M
    case TK_NAME: {  /* may be 'listfield' or 'recfield' */
916
1.54M
      if (luaX_lookahead(ls) != '=')  /* expression? */
917
1.54M
        listfield(ls, cc);
918
10
      else
919
10
        recfield(ls, cc);
920
1.54M
      break;
921
0
    }
922
1
    case '[': {
923
1
      recfield(ls, cc);
924
1
      break;
925
0
    }
926
9.06k
    default: {
927
9.06k
      listfield(ls, cc);
928
9.06k
      break;
929
0
    }
930
1.55M
  }
931
1.55M
}
932
933
934
4.80k
static void constructor (LexState *ls, expdesc *t) {
935
  /* constructor -> '{' [ field { sep field } [sep] ] '}'
936
     sep -> ',' | ';' */
937
4.80k
  FuncState *fs = ls->fs;
938
4.80k
  int line = ls->linenumber;
939
4.80k
  int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
940
4.80k
  ConsControl cc;
941
4.80k
  luaK_code(fs, 0);  /* space for extra arg. */
942
4.80k
  cc.na = cc.nh = cc.tostore = 0;
943
4.80k
  cc.t = t;
944
4.80k
  init_exp(t, VNONRELOC, fs->freereg);  /* table will be at stack top */
945
4.80k
  luaK_reserveregs(fs, 1);
946
4.80k
  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
947
4.80k
  checknext(ls, '{');
948
1.56M
  do {
949
1.56M
    lua_assert(cc.v.k == VVOID || cc.tostore > 0);
950
1.56M
    if (ls->t.token == '}') break;
951
1.55M
    closelistfield(fs, &cc);
952
1.55M
    field(ls, &cc);
953
1.55M
  } while (testnext(ls, ',') || testnext(ls, ';'));
954
4.80k
  check_match(ls, '}', '{', line);
955
4.80k
  lastlistfield(fs, &cc);
956
4.80k
  luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh);
957
4.80k
}
958
959
/* }====================================================================== */
960
961
962
926
static void setvararg (FuncState *fs, int nparams) {
963
926
  fs->f->flag |= PF_ISVARARG;
964
926
  luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0);
965
926
}
966
967
968
21.1k
static void parlist (LexState *ls) {
969
  /* parlist -> [ {NAME ','} (NAME | '...') ] */
970
21.1k
  FuncState *fs = ls->fs;
971
21.1k
  Proto *f = fs->f;
972
21.1k
  int nparams = 0;
973
21.1k
  int isvararg = 0;
974
21.1k
  if (ls->t.token != ')') {  /* is 'parlist' not empty? */
975
11.5k
    do {
976
11.5k
      switch (ls->t.token) {
977
11.1k
        case TK_NAME: {
978
11.1k
          new_localvar(ls, str_checkname(ls));
979
11.1k
          nparams++;
980
11.1k
          break;
981
0
        }
982
365
        case TK_DOTS: {
983
365
          luaX_next(ls);
984
365
          isvararg = 1;
985
365
          break;
986
0
        }
987
2
        default: luaX_syntaxerror(ls, "<name> or '...' expected");
988
11.5k
      }
989
11.5k
    } while (!isvararg && testnext(ls, ','));
990
3.04k
  }
991
21.1k
  adjustlocalvars(ls, nparams);
992
21.1k
  f->numparams = cast_byte(fs->nactvar);
993
21.1k
  if (isvararg)
994
365
    setvararg(fs, f->numparams);  /* declared vararg */
995
21.1k
  luaK_reserveregs(fs, fs->nactvar);  /* reserve registers for parameters */
996
21.1k
}
997
998
999
21.1k
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
1000
  /* body ->  '(' parlist ')' block END */
1001
21.1k
  FuncState new_fs;
1002
21.1k
  BlockCnt bl;
1003
21.1k
  new_fs.f = addprototype(ls);
1004
21.1k
  new_fs.f->linedefined = line;
1005
21.1k
  open_func(ls, &new_fs, &bl);
1006
21.1k
  checknext(ls, '(');
1007
21.1k
  if (ismethod) {
1008
18
    new_localvarliteral(ls, "self");  /* create 'self' parameter */
1009
18
    adjustlocalvars(ls, 1);
1010
18
  }
1011
21.1k
  parlist(ls);
1012
21.1k
  checknext(ls, ')');
1013
21.1k
  statlist(ls);
1014
21.1k
  new_fs.f->lastlinedefined = ls->linenumber;
1015
21.1k
  check_match(ls, TK_END, TK_FUNCTION, line);
1016
21.1k
  codeclosure(ls, e);
1017
21.1k
  close_func(ls);
1018
21.1k
}
1019
1020
1021
120k
static int explist (LexState *ls, expdesc *v) {
1022
  /* explist -> expr { ',' expr } */
1023
120k
  int n = 1;  /* at least one expression */
1024
120k
  expr(ls, v);
1025
176k
  while (testnext(ls, ',')) {
1026
56.7k
    luaK_exp2nextreg(ls->fs, v);
1027
56.7k
    expr(ls, v);
1028
56.7k
    n++;
1029
56.7k
  }
1030
120k
  return n;
1031
120k
}
1032
1033
1034
350k
static void funcargs (LexState *ls, expdesc *f) {
1035
350k
  FuncState *fs = ls->fs;
1036
350k
  expdesc args;
1037
350k
  int base, nparams;
1038
350k
  int line = ls->linenumber;
1039
350k
  switch (ls->t.token) {
1040
39.9k
    case '(': {  /* funcargs -> '(' [ explist ] ')' */
1041
39.9k
      luaX_next(ls);
1042
39.9k
      if (ls->t.token == ')')  /* arg list is empty? */
1043
20.7k
        args.k = VVOID;
1044
19.2k
      else {
1045
19.2k
        explist(ls, &args);
1046
19.2k
        if (hasmultret(args.k))
1047
378
          luaK_setmultret(fs, &args);
1048
19.2k
      }
1049
39.9k
      check_match(ls, ')', '(', line);
1050
39.9k
      break;
1051
0
    }
1052
2.56k
    case '{': {  /* funcargs -> constructor */
1053
2.56k
      constructor(ls, &args);
1054
2.56k
      break;
1055
0
    }
1056
308k
    case TK_STRING: {  /* funcargs -> STRING */
1057
308k
      codestring(&args, ls->t.seminfo.ts);
1058
308k
      luaX_next(ls);  /* must use 'seminfo' before 'next' */
1059
308k
      break;
1060
0
    }
1061
2
    default: {
1062
2
      luaX_syntaxerror(ls, "function arguments expected");
1063
0
    }
1064
350k
  }
1065
350k
  lua_assert(f->k == VNONRELOC);
1066
350k
  base = f->u.info;  /* base register for call */
1067
350k
  if (hasmultret(args.k))
1068
378
    nparams = LUA_MULTRET;  /* open call */
1069
349k
  else {
1070
349k
    if (args.k != VVOID)
1071
329k
      luaK_exp2nextreg(fs, &args);  /* close last argument */
1072
349k
    nparams = fs->freereg - (base+1);
1073
349k
  }
1074
350k
  init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
1075
350k
  luaK_fixline(fs, line);
1076
350k
  fs->freereg = base+1;  /* call removes function and arguments and leaves
1077
                            one result (unless changed later) */
1078
350k
}
1079
1080
1081
1082
1083
/*
1084
** {======================================================================
1085
** Expression parsing
1086
** =======================================================================
1087
*/
1088
1089
1090
26.2M
static void primaryexp (LexState *ls, expdesc *v) {
1091
  /* primaryexp -> NAME | '(' expr ')' */
1092
26.2M
  switch (ls->t.token) {
1093
62.5k
    case '(': {
1094
62.5k
      int line = ls->linenumber;
1095
62.5k
      luaX_next(ls);
1096
62.5k
      expr(ls, v);
1097
62.5k
      check_match(ls, ')', '(', line);
1098
62.5k
      luaK_dischargevars(ls->fs, v);
1099
62.5k
      return;
1100
0
    }
1101
26.2M
    case TK_NAME: {
1102
26.2M
      singlevar(ls, v);
1103
26.2M
      return;
1104
0
    }
1105
123
    default: {
1106
123
      luaX_syntaxerror(ls, "unexpected symbol");
1107
0
    }
1108
26.2M
  }
1109
26.2M
}
1110
1111
1112
26.2M
static void suffixedexp (LexState *ls, expdesc *v) {
1113
  /* suffixedexp ->
1114
       primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
1115
26.2M
  FuncState *fs = ls->fs;
1116
26.2M
  primaryexp(ls, v);
1117
26.6M
  for (;;) {
1118
26.6M
    switch (ls->t.token) {
1119
13.6k
      case '.': {  /* fieldsel */
1120
13.6k
        fieldsel(ls, v);
1121
13.6k
        break;
1122
0
      }
1123
34.5k
      case '[': {  /* '[' exp ']' */
1124
34.5k
        expdesc key;
1125
34.5k
        luaK_exp2anyregup(fs, v);
1126
34.5k
        yindex(ls, &key);
1127
34.5k
        luaK_indexed(fs, v, &key);
1128
34.5k
        break;
1129
0
      }
1130
313
      case ':': {  /* ':' NAME funcargs */
1131
313
        expdesc key;
1132
313
        luaX_next(ls);
1133
313
        codename(ls, &key);
1134
313
        luaK_self(fs, v, &key);
1135
313
        funcargs(ls, v);
1136
313
        break;
1137
0
      }
1138
350k
      case '(': case TK_STRING: case '{': {  /* funcargs */
1139
350k
        luaK_exp2nextreg(fs, v);
1140
350k
        funcargs(ls, v);
1141
350k
        break;
1142
347k
      }
1143
26.2M
      default: return;
1144
26.6M
    }
1145
26.6M
  }
1146
26.2M
}
1147
1148
1149
27.4M
static void simpleexp (LexState *ls, expdesc *v) {
1150
  /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... |
1151
                  constructor | FUNCTION body | suffixedexp */
1152
27.4M
  switch (ls->t.token) {
1153
70.3k
    case TK_FLT: {
1154
70.3k
      init_exp(v, VKFLT, 0);
1155
70.3k
      v->u.nval = ls->t.seminfo.r;
1156
70.3k
      break;
1157
0
    }
1158
1.18M
    case TK_INT: {
1159
1.18M
      init_exp(v, VKINT, 0);
1160
1.18M
      v->u.ival = ls->t.seminfo.i;
1161
1.18M
      break;
1162
0
    }
1163
41.6k
    case TK_STRING: {
1164
41.6k
      codestring(v, ls->t.seminfo.ts);
1165
41.6k
      break;
1166
0
    }
1167
17.2k
    case TK_NIL: {
1168
17.2k
      init_exp(v, VNIL, 0);
1169
17.2k
      break;
1170
0
    }
1171
4.30k
    case TK_TRUE: {
1172
4.30k
      init_exp(v, VTRUE, 0);
1173
4.30k
      break;
1174
0
    }
1175
62
    case TK_FALSE: {
1176
62
      init_exp(v, VFALSE, 0);
1177
62
      break;
1178
0
    }
1179
2.66k
    case TK_DOTS: {  /* vararg */
1180
2.66k
      FuncState *fs = ls->fs;
1181
2.66k
      check_condition(ls, fs->f->flag & PF_ISVARARG,
1182
2.66k
                      "cannot use '...' outside a vararg function");
1183
2.65k
      init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1));
1184
2.65k
      break;
1185
2.66k
    }
1186
2.24k
    case '{': {  /* constructor */
1187
2.24k
      constructor(ls, v);
1188
2.24k
      return;
1189
2.66k
    }
1190
16.0k
    case TK_FUNCTION: {
1191
16.0k
      luaX_next(ls);
1192
16.0k
      body(ls, v, 0, ls->linenumber);
1193
16.0k
      return;
1194
2.66k
    }
1195
26.1M
    default: {
1196
26.1M
      suffixedexp(ls, v);
1197
26.1M
      return;
1198
2.66k
    }
1199
27.4M
  }
1200
1.32M
  luaX_next(ls);
1201
1.32M
}
1202
1203
1204
27.7M
static UnOpr getunopr (int op) {
1205
27.7M
  switch (op) {
1206
3.90k
    case TK_NOT: return OPR_NOT;
1207
69.4k
    case '-': return OPR_MINUS;
1208
119k
    case '~': return OPR_BNOT;
1209
12.7k
    case '#': return OPR_LEN;
1210
27.4M
    default: return OPR_NOUNOPR;
1211
27.7M
  }
1212
27.7M
}
1213
1214
1215
27.6M
static BinOpr getbinopr (int op) {
1216
27.6M
  switch (op) {
1217
13.8k
    case '+': return OPR_ADD;
1218
104k
    case '-': return OPR_SUB;
1219
117k
    case '*': return OPR_MUL;
1220
97.4k
    case '%': return OPR_MOD;
1221
360k
    case '^': return OPR_POW;
1222
7.02M
    case '/': return OPR_DIV;
1223
47.8k
    case TK_IDIV: return OPR_IDIV;
1224
19.2k
    case '&': return OPR_BAND;
1225
13.6k
    case '|': return OPR_BOR;
1226
150k
    case '~': return OPR_BXOR;
1227
110k
    case TK_SHL: return OPR_SHL;
1228
283k
    case TK_SHR: return OPR_SHR;
1229
69.7k
    case TK_CONCAT: return OPR_CONCAT;
1230
5.09k
    case TK_NE: return OPR_NE;
1231
12.0k
    case TK_EQ: return OPR_EQ;
1232
1.23M
    case '<': return OPR_LT;
1233
9.45k
    case TK_LE: return OPR_LE;
1234
16.0M
    case '>': return OPR_GT;
1235
4.40k
    case TK_GE: return OPR_GE;
1236
24.1k
    case TK_AND: return OPR_AND;
1237
78.9k
    case TK_OR: return OPR_OR;
1238
1.86M
    default: return OPR_NOBINOPR;
1239
27.6M
  }
1240
27.6M
}
1241
1242
1243
/*
1244
** Priority table for binary operators.
1245
*/
1246
static const struct {
1247
  lu_byte left;  /* left priority for each binary operator */
1248
  lu_byte right; /* right priority */
1249
} priority[] = {  /* ORDER OPR */
1250
   {10, 10}, {10, 10},           /* '+' '-' */
1251
   {11, 11}, {11, 11},           /* '*' '%' */
1252
   {14, 13},                  /* '^' (right associative) */
1253
   {11, 11}, {11, 11},           /* '/' '//' */
1254
   {6, 6}, {4, 4}, {5, 5},   /* '&' '|' '~' */
1255
   {7, 7}, {7, 7},           /* '<<' '>>' */
1256
   {9, 8},                   /* '..' (right associative) */
1257
   {3, 3}, {3, 3}, {3, 3},   /* ==, <, <= */
1258
   {3, 3}, {3, 3}, {3, 3},   /* ~=, >, >= */
1259
   {2, 2}, {1, 1}            /* and, or */
1260
};
1261
1262
205k
#define UNARY_PRIORITY  12  /* priority for unary operators */
1263
1264
1265
/*
1266
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
1267
** where 'binop' is any binary operator with a priority higher than 'limit'
1268
*/
1269
27.7M
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
1270
27.7M
  BinOpr op;
1271
27.7M
  UnOpr uop;
1272
27.7M
  enterlevel(ls);
1273
27.7M
  uop = getunopr(ls->t.token);
1274
27.7M
  if (uop != OPR_NOUNOPR) {  /* prefix (unary) operator? */
1275
205k
    int line = ls->linenumber;
1276
205k
    luaX_next(ls);  /* skip operator */
1277
205k
    subexpr(ls, v, UNARY_PRIORITY);
1278
205k
    luaK_prefix(ls->fs, uop, v, line);
1279
205k
  }
1280
27.4M
  else simpleexp(ls, v);
1281
  /* expand while operators have priorities higher than 'limit' */
1282
27.7M
  op = getbinopr(ls->t.token);
1283
53.3M
  while (op != OPR_NOBINOPR && priority[op].left > limit) {
1284
25.6M
    expdesc v2;
1285
25.6M
    BinOpr nextop;
1286
25.6M
    int line = ls->linenumber;
1287
25.6M
    luaX_next(ls);  /* skip operator */
1288
25.6M
    luaK_infix(ls->fs, op, v);
1289
    /* read sub-expression with higher priority */
1290
25.6M
    nextop = subexpr(ls, &v2, priority[op].right);
1291
25.6M
    luaK_posfix(ls->fs, op, v, &v2, line);
1292
25.6M
    op = nextop;
1293
25.6M
  }
1294
27.7M
  leavelevel(ls);
1295
27.7M
  return op;  /* return first untreated operator */
1296
27.7M
}
1297
1298
1299
1.84M
static void expr (LexState *ls, expdesc *v) {
1300
1.84M
  subexpr(ls, v, 0);
1301
1.84M
}
1302
1303
/* }==================================================================== */
1304
1305
1306
1307
/*
1308
** {======================================================================
1309
** Rules for Statements
1310
** =======================================================================
1311
*/
1312
1313
1314
7.36k
static void block (LexState *ls) {
1315
  /* block -> statlist */
1316
7.36k
  FuncState *fs = ls->fs;
1317
7.36k
  BlockCnt bl;
1318
7.36k
  enterblock(fs, &bl, 0);
1319
7.36k
  statlist(ls);
1320
7.36k
  leaveblock(fs);
1321
7.36k
}
1322
1323
1324
/*
1325
** structure to chain all variables in the left-hand side of an
1326
** assignment
1327
*/
1328
struct LHS_assign {
1329
  struct LHS_assign *prev;
1330
  expdesc v;  /* variable (global, local, upvalue, or indexed) */
1331
};
1332
1333
1334
/*
1335
** check whether, in an assignment to an upvalue/local variable, the
1336
** upvalue/local variable is begin used in a previous assignment to a
1337
** table. If so, save original upvalue/local value in a safe place and
1338
** use this safe copy in the previous assignment.
1339
*/
1340
2.31k
static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
1341
2.31k
  FuncState *fs = ls->fs;
1342
2.31k
  int extra = fs->freereg;  /* eventual position to save local variable */
1343
2.31k
  int conflict = 0;
1344
25.0k
  for (; lh; lh = lh->prev) {  /* check all previous assignments */
1345
22.7k
    if (vkisindexed(lh->v.k)) {  /* assignment to table field? */
1346
10.4k
      if (lh->v.k == VINDEXUP) {  /* is table an upvalue? */
1347
3.06k
        if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) {
1348
1.62k
          conflict = 1;  /* table is the upvalue being assigned now */
1349
1.62k
          lh->v.k = VINDEXSTR;
1350
1.62k
          lh->v.u.ind.t = extra;  /* assignment will use safe copy */
1351
1.62k
        }
1352
3.06k
      }
1353
7.41k
      else {  /* table is a register */
1354
7.41k
        if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) {
1355
851
          conflict = 1;  /* table is the local being assigned now */
1356
851
          lh->v.u.ind.t = extra;  /* assignment will use safe copy */
1357
851
        }
1358
        /* is index the local being assigned? */
1359
7.41k
        if (lh->v.k == VINDEXED && v->k == VLOCAL &&
1360
7.41k
            lh->v.u.ind.idx == v->u.var.ridx) {
1361
231
          conflict = 1;
1362
231
          lh->v.u.ind.idx = extra;  /* previous assignment will use safe copy */
1363
231
        }
1364
7.41k
      }
1365
10.4k
    }
1366
22.7k
  }
1367
2.31k
  if (conflict) {
1368
    /* copy upvalue/local value to a temporary (in position 'extra') */
1369
1.90k
    if (v->k == VLOCAL)
1370
495
      luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0);
1371
1.40k
    else
1372
1.40k
      luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0);
1373
1.90k
    luaK_reserveregs(fs, 1);
1374
1.90k
  }
1375
2.31k
}
1376
1377
/*
1378
** Parse and compile a multiple assignment. The first "variable"
1379
** (a 'suffixedexp') was already read by the caller.
1380
**
1381
** assignment -> suffixedexp restassign
1382
** restassign -> ',' suffixedexp restassign | '=' explist
1383
*/
1384
100k
static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) {
1385
100k
  expdesc e;
1386
100k
  check_condition(ls, vkisvar(lh->v.k), "syntax error");
1387
100k
  check_readonly(ls, &lh->v);
1388
100k
  if (testnext(ls, ',')) {  /* restassign -> ',' suffixedexp restassign */
1389
13.4k
    struct LHS_assign nv;
1390
13.4k
    nv.prev = lh;
1391
13.4k
    suffixedexp(ls, &nv.v);
1392
13.4k
    if (!vkisindexed(nv.v.k))
1393
2.31k
      check_conflict(ls, lh, &nv.v);
1394
13.4k
    enterlevel(ls);  /* control recursion depth */
1395
13.4k
    restassign(ls, &nv, nvars+1);
1396
13.4k
    leavelevel(ls);
1397
13.4k
  }
1398
86.7k
  else {  /* restassign -> '=' explist */
1399
86.7k
    int nexps;
1400
86.7k
    checknext(ls, '=');
1401
86.7k
    nexps = explist(ls, &e);
1402
86.7k
    if (nexps != nvars)
1403
15.5k
      adjust_assign(ls, nvars, nexps, &e);
1404
71.1k
    else {
1405
71.1k
      luaK_setoneret(ls->fs, &e);  /* close last expression */
1406
71.1k
      luaK_storevar(ls->fs, &lh->v, &e);
1407
71.1k
      return;  /* avoid default */
1408
71.1k
    }
1409
86.7k
  }
1410
29.0k
  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
1411
29.0k
  luaK_storevar(ls->fs, &lh->v, &e);
1412
29.0k
}
1413
1414
1415
3.12k
static int cond (LexState *ls) {
1416
  /* cond -> exp */
1417
3.12k
  expdesc v;
1418
3.12k
  expr(ls, &v);  /* read condition */
1419
3.12k
  if (v.k == VNIL) v.k = VFALSE;  /* 'falses' are all equal here */
1420
3.12k
  luaK_goiftrue(ls->fs, &v);
1421
3.12k
  return v.f;
1422
3.12k
}
1423
1424
1425
25.3k
static void gotostat (LexState *ls) {
1426
25.3k
  FuncState *fs = ls->fs;
1427
25.3k
  int line = ls->linenumber;
1428
25.3k
  TString *name = str_checkname(ls);  /* label's name */
1429
25.3k
  Labeldesc *lb = findlabel(ls, name);
1430
25.3k
  if (lb == NULL)  /* no label? */
1431
    /* forward jump; will be resolved when the label is declared */
1432
25.3k
    newgotoentry(ls, name, line, luaK_jump(fs));
1433
0
  else {  /* found a label */
1434
    /* backward jump; will be resolved here */
1435
0
    int lblevel = reglevel(fs, lb->nactvar);  /* label level */
1436
0
    if (luaY_nvarstack(fs) > lblevel)  /* leaving the scope of a variable? */
1437
0
      luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0);
1438
    /* create jump and link it to the label */
1439
0
    luaK_patchlist(fs, luaK_jump(fs), lb->pc);
1440
0
  }
1441
25.3k
}
1442
1443
1444
/*
1445
** Break statement. Semantically equivalent to "goto break".
1446
*/
1447
128
static void breakstat (LexState *ls) {
1448
128
  int line = ls->linenumber;
1449
128
  luaX_next(ls);  /* skip break */
1450
128
  newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs));
1451
128
}
1452
1453
1454
/*
1455
** Check whether there is already a label with the given 'name'.
1456
*/
1457
31
static void checkrepeated (LexState *ls, TString *name) {
1458
31
  Labeldesc *lb = findlabel(ls, name);
1459
31
  if (l_unlikely(lb != NULL)) {  /* already defined? */
1460
0
    const char *msg = "label '%s' already defined on line %d";
1461
0
    msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line);
1462
0
    luaK_semerror(ls, msg);  /* error */
1463
0
  }
1464
31
}
1465
1466
1467
31
static void labelstat (LexState *ls, TString *name, int line) {
1468
  /* label -> '::' NAME '::' */
1469
31
  checknext(ls, TK_DBCOLON);  /* skip double colon */
1470
31
  while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
1471
0
    statement(ls);  /* skip other no-op statements */
1472
31
  checkrepeated(ls, name);  /* check for repeated labels */
1473
31
  createlabel(ls, name, line, block_follow(ls, 0));
1474
31
}
1475
1476
1477
2.96k
static void whilestat (LexState *ls, int line) {
1478
  /* whilestat -> WHILE cond DO block END */
1479
2.96k
  FuncState *fs = ls->fs;
1480
2.96k
  int whileinit;
1481
2.96k
  int condexit;
1482
2.96k
  BlockCnt bl;
1483
2.96k
  luaX_next(ls);  /* skip WHILE */
1484
2.96k
  whileinit = luaK_getlabel(fs);
1485
2.96k
  condexit = cond(ls);
1486
2.96k
  enterblock(fs, &bl, 1);
1487
2.96k
  checknext(ls, TK_DO);
1488
2.96k
  block(ls);
1489
2.96k
  luaK_jumpto(fs, whileinit);
1490
2.96k
  check_match(ls, TK_END, TK_WHILE, line);
1491
2.96k
  leaveblock(fs);
1492
2.96k
  luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
1493
2.96k
}
1494
1495
1496
166
static void repeatstat (LexState *ls, int line) {
1497
  /* repeatstat -> REPEAT block UNTIL cond */
1498
166
  int condexit;
1499
166
  FuncState *fs = ls->fs;
1500
166
  int repeat_init = luaK_getlabel(fs);
1501
166
  BlockCnt bl1, bl2;
1502
166
  enterblock(fs, &bl1, 1);  /* loop block */
1503
166
  enterblock(fs, &bl2, 0);  /* scope block */
1504
166
  luaX_next(ls);  /* skip REPEAT */
1505
166
  statlist(ls);
1506
166
  check_match(ls, TK_UNTIL, TK_REPEAT, line);
1507
166
  condexit = cond(ls);  /* read condition (inside scope block) */
1508
166
  leaveblock(fs);  /* finish scope */
1509
166
  if (bl2.upval) {  /* upvalues? */
1510
108
    int exit = luaK_jump(fs);  /* normal exit must jump over fix */
1511
108
    luaK_patchtohere(fs, condexit);  /* repetition must close upvalues */
1512
108
    luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0);
1513
108
    condexit = luaK_jump(fs);  /* repeat after closing upvalues */
1514
108
    luaK_patchtohere(fs, exit);  /* normal exit comes to here */
1515
108
  }
1516
166
  luaK_patchlist(fs, condexit, repeat_init);  /* close the loop */
1517
166
  leaveblock(fs);  /* finish loop */
1518
166
}
1519
1520
1521
/*
1522
** Read an expression and generate code to put its results in next
1523
** stack slot.
1524
**
1525
*/
1526
1.39k
static void exp1 (LexState *ls) {
1527
1.39k
  expdesc e;
1528
1.39k
  expr(ls, &e);
1529
1.39k
  luaK_exp2nextreg(ls->fs, &e);
1530
1.39k
  lua_assert(e.k == VNONRELOC);
1531
1.39k
}
1532
1533
1534
/*
1535
** Fix for instruction at position 'pc' to jump to 'dest'.
1536
** (Jump addresses are relative in Lua). 'back' true means
1537
** a back jump.
1538
*/
1539
1.93k
static void fixforjump (FuncState *fs, int pc, int dest, int back) {
1540
1.93k
  Instruction *jmp = &fs->f->code[pc];
1541
1.93k
  int offset = dest - (pc + 1);
1542
1.93k
  if (back)
1543
966
    offset = -offset;
1544
1.93k
  if (l_unlikely(offset > MAXARG_Bx))
1545
2
    luaX_syntaxerror(fs->ls, "control structure too long");
1546
1.93k
  SETARG_Bx(*jmp, offset);
1547
1.93k
}
1548
1549
1550
/*
1551
** Generate code for a 'for' loop.
1552
*/
1553
1.52k
static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
1554
  /* forbody -> DO block */
1555
1.52k
  static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP};
1556
1.52k
  static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP};
1557
1.52k
  BlockCnt bl;
1558
1.52k
  FuncState *fs = ls->fs;
1559
1.52k
  int prep, endfor;
1560
1.52k
  checknext(ls, TK_DO);
1561
1.52k
  prep = luaK_codeABx(fs, forprep[isgen], base, 0);
1562
1.52k
  fs->freereg--;  /* both 'forprep' remove one register from the stack */
1563
1.52k
  enterblock(fs, &bl, 0);  /* scope for declared variables */
1564
1.52k
  adjustlocalvars(ls, nvars);
1565
1.52k
  luaK_reserveregs(fs, nvars);
1566
1.52k
  block(ls);
1567
1.52k
  leaveblock(fs);  /* end of scope for declared variables */
1568
1.52k
  fixforjump(fs, prep, luaK_getlabel(fs), 0);
1569
1.52k
  if (isgen) {  /* generic for? */
1570
741
    luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
1571
741
    luaK_fixline(fs, line);
1572
741
  }
1573
1.52k
  endfor = luaK_codeABx(fs, forloop[isgen], base, 0);
1574
1.52k
  fixforjump(fs, endfor, prep + 1, 1);
1575
1.52k
  luaK_fixline(fs, line);
1576
1.52k
}
1577
1578
1579
594
static void fornum (LexState *ls, TString *varname, int line) {
1580
  /* fornum -> NAME = exp,exp[,exp] forbody */
1581
594
  FuncState *fs = ls->fs;
1582
594
  int base = fs->freereg;
1583
594
  new_localvarliteral(ls, "(for state)");
1584
594
  new_localvarliteral(ls, "(for state)");
1585
594
  new_localvarkind(ls, varname, RDKCONST);  /* control variable */
1586
594
  checknext(ls, '=');
1587
594
  exp1(ls);  /* initial value */
1588
594
  checknext(ls, ',');
1589
594
  exp1(ls);  /* limit */
1590
594
  if (testnext(ls, ','))
1591
210
    exp1(ls);  /* optional step */
1592
384
  else {  /* default step = 1 */
1593
384
    luaK_int(fs, fs->freereg, 1);
1594
384
    luaK_reserveregs(fs, 1);
1595
384
  }
1596
594
  adjustlocalvars(ls, 2);  /* start scope for internal variables */
1597
594
  forbody(ls, base, line, 1, 0);
1598
594
}
1599
1600
1601
937
static void forlist (LexState *ls, TString *indexname) {
1602
  /* forlist -> NAME {,NAME} IN explist forbody */
1603
937
  FuncState *fs = ls->fs;
1604
937
  expdesc e;
1605
937
  int nvars = 4;  /* function, state, closing, control */
1606
937
  int line;
1607
937
  int base = fs->freereg;
1608
  /* create internal variables */
1609
937
  new_localvarliteral(ls, "(for state)");  /* iterator function */
1610
937
  new_localvarliteral(ls, "(for state)");  /* state */
1611
937
  new_localvarliteral(ls, "(for state)");  /* closing var. (after swap) */
1612
937
  new_localvarkind(ls, indexname, RDKCONST);  /* control variable */
1613
  /* other declared variables */
1614
1.44k
  while (testnext(ls, ',')) {
1615
506
    new_localvar(ls, str_checkname(ls));
1616
506
    nvars++;
1617
506
  }
1618
937
  checknext(ls, TK_IN);
1619
937
  line = ls->linenumber;
1620
937
  adjust_assign(ls, 4, explist(ls, &e), &e);
1621
937
  adjustlocalvars(ls, 3);  /* start scope for internal variables */
1622
937
  marktobeclosed(fs);  /* last internal var. must be closed */
1623
937
  luaK_checkstack(fs, 2);  /* extra space to call iterator */
1624
937
  forbody(ls, base, line, nvars - 3, 1);
1625
937
}
1626
1627
1628
1.53k
static void forstat (LexState *ls, int line) {
1629
  /* forstat -> FOR (fornum | forlist) END */
1630
1.53k
  FuncState *fs = ls->fs;
1631
1.53k
  TString *varname;
1632
1.53k
  BlockCnt bl;
1633
1.53k
  enterblock(fs, &bl, 1);  /* scope for loop and control variables */
1634
1.53k
  luaX_next(ls);  /* skip 'for' */
1635
1.53k
  varname = str_checkname(ls);  /* first variable name */
1636
1.53k
  switch (ls->t.token) {
1637
594
    case '=': fornum(ls, varname, line); break;
1638
937
    case ',': case TK_IN: forlist(ls, varname); break;
1639
0
    default: luaX_syntaxerror(ls, "'=' or 'in' expected");
1640
1.53k
  }
1641
966
  check_match(ls, TK_END, TK_FOR, line);
1642
966
  leaveblock(fs);  /* loop scope ('break' jumps to this point) */
1643
966
}
1644
1645
1646
8.18k
static void test_then_block (LexState *ls, int *escapelist) {
1647
  /* test_then_block -> [IF | ELSEIF] cond THEN block */
1648
8.18k
  BlockCnt bl;
1649
8.18k
  FuncState *fs = ls->fs;
1650
8.18k
  expdesc v;
1651
8.18k
  int jf;  /* instruction to skip 'then' code (if condition is false) */
1652
8.18k
  luaX_next(ls);  /* skip IF or ELSEIF */
1653
8.18k
  expr(ls, &v);  /* read condition */
1654
8.18k
  checknext(ls, TK_THEN);
1655
8.18k
  if (ls->t.token == TK_BREAK) {  /* 'if x then break' ? */
1656
8.18k
    int line = ls->linenumber;
1657
8.18k
    luaK_goiffalse(ls->fs, &v);  /* will jump if condition is true */
1658
8.18k
    luaX_next(ls);  /* skip 'break' */
1659
8.18k
    enterblock(fs, &bl, 0);  /* must enter block before 'goto' */
1660
8.18k
    newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t);
1661
8.18k
    while (testnext(ls, ';')) {}  /* skip semicolons */
1662
8.18k
    if (block_follow(ls, 0)) {  /* jump is the entire block? */
1663
5.58k
      leaveblock(fs);
1664
5.58k
      return;  /* and that is it */
1665
5.58k
    }
1666
2.59k
    else  /* must skip over 'then' part if condition is false */
1667
2.59k
      jf = luaK_jump(fs);
1668
8.18k
  }
1669
3
  else {  /* regular case (not a break) */
1670
3
    luaK_goiftrue(ls->fs, &v);  /* skip over block if condition is false */
1671
3
    enterblock(fs, &bl, 0);
1672
3
    jf = v.f;
1673
3
  }
1674
2.60k
  statlist(ls);  /* 'then' part */
1675
2.60k
  leaveblock(fs);
1676
2.60k
  if (ls->t.token == TK_ELSE ||
1677
2.60k
      ls->t.token == TK_ELSEIF)  /* followed by 'else'/'elseif'? */
1678
649
    luaK_concat(fs, escapelist, luaK_jump(fs));  /* must jump over it */
1679
2.60k
  luaK_patchtohere(fs, jf);
1680
2.60k
}
1681
1682
1683
3.15k
static void ifstat (LexState *ls, int line) {
1684
  /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1685
3.15k
  FuncState *fs = ls->fs;
1686
3.15k
  int escapelist = NO_JUMP;  /* exit list for finished parts */
1687
3.15k
  test_then_block(ls, &escapelist);  /* IF cond THEN block */
1688
8.18k
  while (ls->t.token == TK_ELSEIF)
1689
5.03k
    test_then_block(ls, &escapelist);  /* ELSEIF cond THEN block */
1690
3.15k
  if (testnext(ls, TK_ELSE))
1691
0
    block(ls);  /* 'else' part */
1692
3.15k
  check_match(ls, TK_END, TK_IF, line);
1693
3.15k
  luaK_patchtohere(fs, escapelist);  /* patch escape list to 'if' end */
1694
3.15k
}
1695
1696
1697
1.97k
static void localfunc (LexState *ls) {
1698
1.97k
  expdesc b;
1699
1.97k
  FuncState *fs = ls->fs;
1700
1.97k
  int fvar = fs->nactvar;  /* function's variable index */
1701
1.97k
  new_localvar(ls, str_checkname(ls));  /* new local variable */
1702
1.97k
  adjustlocalvars(ls, 1);  /* enter its scope */
1703
1.97k
  body(ls, &b, 0, ls->linenumber);  /* function created in next register */
1704
  /* debug information will only see the variable after this point! */
1705
1.97k
  localdebuginfo(fs, fvar)->startpc = fs->pc;
1706
1.97k
}
1707
1708
1709
12.6k
static int getlocalattribute (LexState *ls) {
1710
  /* ATTRIB -> ['<' Name '>'] */
1711
12.6k
  if (testnext(ls, '<')) {
1712
3.02k
    TString *ts = str_checkname(ls);
1713
3.02k
    const char *attr = getstr(ts);
1714
3.02k
    checknext(ls, '>');
1715
3.02k
    if (strcmp(attr, "const") == 0)
1716
2.80k
      return RDKCONST;  /* read-only variable */
1717
224
    else if (strcmp(attr, "close") == 0)
1718
224
      return RDKTOCLOSE;  /* to-be-closed variable */
1719
0
    else
1720
0
      luaK_semerror(ls,
1721
0
        luaO_pushfstring(ls->L, "unknown attribute '%s'", attr));
1722
3.02k
  }
1723
9.60k
  return VDKREG;  /* regular variable */
1724
12.6k
}
1725
1726
1727
6.57k
static void checktoclose (FuncState *fs, int level) {
1728
6.57k
  if (level != -1) {  /* is there a to-be-closed variable? */
1729
224
    marktobeclosed(fs);
1730
224
    luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0);
1731
224
  }
1732
6.57k
}
1733
1734
1735
6.59k
static void localstat (LexState *ls) {
1736
  /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */
1737
6.59k
  FuncState *fs = ls->fs;
1738
6.59k
  int toclose = -1;  /* index of to-be-closed variable (if any) */
1739
6.59k
  Vardesc *var;  /* last variable */
1740
6.59k
  int vidx;  /* index of last variable */
1741
6.59k
  int nvars = 0;
1742
6.59k
  int nexps;
1743
6.59k
  expdesc e;
1744
12.6k
  do {
1745
12.6k
    TString *vname = str_checkname(ls);
1746
12.6k
    int kind = getlocalattribute(ls);
1747
12.6k
    vidx = new_localvarkind(ls, vname, kind);
1748
12.6k
    if (kind == RDKTOCLOSE) {  /* to-be-closed? */
1749
224
      if (toclose != -1)  /* one already present? */
1750
0
        luaK_semerror(ls, "multiple to-be-closed variables in local list");
1751
224
      toclose = fs->nactvar + nvars;
1752
224
    }
1753
12.6k
    nvars++;
1754
12.6k
  } while (testnext(ls, ','));
1755
6.59k
  if (testnext(ls, '='))
1756
5.40k
    nexps = explist(ls, &e);
1757
1.19k
  else {
1758
1.19k
    e.k = VVOID;
1759
1.19k
    nexps = 0;
1760
1.19k
  }
1761
6.59k
  var = getlocalvardesc(fs, vidx);  /* get last variable */
1762
6.59k
  if (nvars == nexps &&  /* no adjustments? */
1763
6.59k
      var->vd.kind == RDKCONST &&  /* last variable is const? */
1764
6.59k
      luaK_exp2const(fs, &e, &var->k)) {  /* compile-time constant? */
1765
1.09k
    var->vd.kind = RDKCTC;  /* variable is a compile-time constant */
1766
1.09k
    adjustlocalvars(ls, nvars - 1);  /* exclude last variable */
1767
1.09k
    fs->nactvar++;  /* but count it */
1768
1.09k
  }
1769
5.50k
  else {
1770
5.50k
    adjust_assign(ls, nvars, nexps, &e);
1771
5.50k
    adjustlocalvars(ls, nvars);
1772
5.50k
  }
1773
6.59k
  checktoclose(fs, toclose);
1774
6.59k
}
1775
1776
1777
3.16k
static int funcname (LexState *ls, expdesc *v) {
1778
  /* funcname -> NAME {fieldsel} [':' NAME] */
1779
3.16k
  int ismethod = 0;
1780
3.16k
  singlevar(ls, v);
1781
3.19k
  while (ls->t.token == '.')
1782
32
    fieldsel(ls, v);
1783
3.16k
  if (ls->t.token == ':') {
1784
18
    ismethod = 1;
1785
18
    fieldsel(ls, v);
1786
18
  }
1787
3.16k
  return ismethod;
1788
3.16k
}
1789
1790
1791
3.16k
static void funcstat (LexState *ls, int line) {
1792
  /* funcstat -> FUNCTION funcname body */
1793
3.16k
  int ismethod;
1794
3.16k
  expdesc v, b;
1795
3.16k
  luaX_next(ls);  /* skip FUNCTION */
1796
3.16k
  ismethod = funcname(ls, &v);
1797
3.16k
  body(ls, &b, ismethod, line);
1798
3.16k
  check_readonly(ls, &v);
1799
3.16k
  luaK_storevar(ls->fs, &v, &b);
1800
3.16k
  luaK_fixline(ls->fs, line);  /* definition "happens" in the first line */
1801
3.16k
}
1802
1803
1804
105k
static void exprstat (LexState *ls) {
1805
  /* stat -> func | assignment */
1806
105k
  FuncState *fs = ls->fs;
1807
105k
  struct LHS_assign v;
1808
105k
  suffixedexp(ls, &v.v);
1809
105k
  if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
1810
86.7k
    v.prev = NULL;
1811
86.7k
    restassign(ls, &v, 1);
1812
86.7k
  }
1813
19.2k
  else {  /* stat -> func */
1814
19.2k
    Instruction *inst;
1815
19.2k
    check_condition(ls, v.v.k == VCALL, "syntax error");
1816
19.2k
    inst = &getinstruction(fs, &v.v);
1817
19.2k
    SETARG_C(*inst, 1);  /* call statement uses no results */
1818
19.2k
  }
1819
105k
}
1820
1821
1822
7.96k
static void retstat (LexState *ls) {
1823
  /* stat -> RETURN [explist] [';'] */
1824
7.96k
  FuncState *fs = ls->fs;
1825
7.96k
  expdesc e;
1826
7.96k
  int nret;  /* number of values being returned */
1827
7.96k
  int first = luaY_nvarstack(fs);  /* first slot to be returned */
1828
7.96k
  if (block_follow(ls, 1) || ls->t.token == ';')
1829
304
    nret = 0;  /* return no values */
1830
7.66k
  else {
1831
7.66k
    nret = explist(ls, &e);  /* optional return values */
1832
7.66k
    if (hasmultret(e.k)) {
1833
3.45k
      luaK_setmultret(fs, &e);
1834
3.45k
      if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) {  /* tail call? */
1835
654
        SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
1836
654
        lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs));
1837
654
      }
1838
3.45k
      nret = LUA_MULTRET;  /* return all values */
1839
3.45k
    }
1840
4.20k
    else {
1841
4.20k
      if (nret == 1)  /* only one single value? */
1842
2.87k
        first = luaK_exp2anyreg(fs, &e);  /* can use original slot */
1843
1.33k
      else {  /* values must go to the top of the stack */
1844
1.33k
        luaK_exp2nextreg(fs, &e);
1845
1.33k
        lua_assert(nret == fs->freereg - first);
1846
1.33k
      }
1847
4.20k
    }
1848
7.66k
  }
1849
7.96k
  luaK_ret(fs, first, nret);
1850
7.96k
  testnext(ls, ';');  /* skip optional semicolon */
1851
7.96k
}
1852
1853
1854
221k
static void statement (LexState *ls) {
1855
221k
  int line = ls->linenumber;  /* may be needed for error messages */
1856
221k
  enterlevel(ls);
1857
221k
  switch (ls->t.token) {
1858
59.3k
    case ';': {  /* stat -> ';' (empty statement) */
1859
59.3k
      luaX_next(ls);  /* skip ';' */
1860
59.3k
      break;
1861
0
    }
1862
3.15k
    case TK_IF: {  /* stat -> ifstat */
1863
3.15k
      ifstat(ls, line);
1864
3.15k
      break;
1865
0
    }
1866
2.96k
    case TK_WHILE: {  /* stat -> whilestat */
1867
2.96k
      whilestat(ls, line);
1868
2.96k
      break;
1869
0
    }
1870
2.87k
    case TK_DO: {  /* stat -> DO block END */
1871
2.87k
      luaX_next(ls);  /* skip DO */
1872
2.87k
      block(ls);
1873
2.87k
      check_match(ls, TK_END, TK_DO, line);
1874
2.87k
      break;
1875
0
    }
1876
1.53k
    case TK_FOR: {  /* stat -> forstat */
1877
1.53k
      forstat(ls, line);
1878
1.53k
      break;
1879
0
    }
1880
166
    case TK_REPEAT: {  /* stat -> repeatstat */
1881
166
      repeatstat(ls, line);
1882
166
      break;
1883
0
    }
1884
3.16k
    case TK_FUNCTION: {  /* stat -> funcstat */
1885
3.16k
      funcstat(ls, line);
1886
3.16k
      break;
1887
0
    }
1888
8.57k
    case TK_LOCAL: {  /* stat -> localstat */
1889
8.57k
      luaX_next(ls);  /* skip LOCAL */
1890
8.57k
      if (testnext(ls, TK_FUNCTION))  /* local function? */
1891
1.97k
        localfunc(ls);
1892
6.59k
      else
1893
6.59k
        localstat(ls);
1894
8.57k
      break;
1895
0
    }
1896
31
    case TK_DBCOLON: {  /* stat -> label */
1897
31
      luaX_next(ls);  /* skip double colon */
1898
31
      labelstat(ls, str_checkname(ls), line);
1899
31
      break;
1900
0
    }
1901
7.96k
    case TK_RETURN: {  /* stat -> retstat */
1902
7.96k
      luaX_next(ls);  /* skip RETURN */
1903
7.96k
      retstat(ls);
1904
7.96k
      break;
1905
0
    }
1906
128
    case TK_BREAK: {  /* stat -> breakstat */
1907
128
      breakstat(ls);
1908
128
      break;
1909
0
    }
1910
25.3k
    case TK_GOTO: {  /* stat -> 'goto' NAME */
1911
25.3k
      luaX_next(ls);  /* skip 'goto' */
1912
25.3k
      gotostat(ls);
1913
25.3k
      break;
1914
0
    }
1915
105k
    default: {  /* stat -> func | assignment */
1916
105k
      exprstat(ls);
1917
105k
      break;
1918
0
    }
1919
221k
  }
1920
219k
  lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1921
219k
             ls->fs->freereg >= luaY_nvarstack(ls->fs));
1922
219k
  ls->fs->freereg = luaY_nvarstack(ls->fs);  /* free registers */
1923
219k
  leavelevel(ls);
1924
219k
}
1925
1926
/* }====================================================================== */
1927
1928
1929
/*
1930
** compiles the main function, which is a regular vararg function with an
1931
** upvalue named LUA_ENV
1932
*/
1933
561
static void mainfunc (LexState *ls, FuncState *fs) {
1934
561
  BlockCnt bl;
1935
561
  Upvaldesc *env;
1936
561
  open_func(ls, fs, &bl);
1937
561
  setvararg(fs, 0);  /* main function is always declared vararg */
1938
561
  env = allocupvalue(fs);  /* ...set environment upvalue */
1939
561
  env->instack = 1;
1940
561
  env->idx = 0;
1941
561
  env->kind = VDKREG;
1942
561
  env->name = ls->envn;
1943
561
  luaC_objbarrier(ls->L, fs->f, env->name);
1944
0
  luaX_next(ls);  /* read first token */
1945
561
  statlist(ls);  /* parse main body */
1946
561
  check(ls, TK_EOS);
1947
561
  close_func(ls);
1948
561
}
1949
1950
1951
LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1952
561
                       Dyndata *dyd, const char *name, int firstchar) {
1953
561
  LexState lexstate;
1954
561
  FuncState funcstate;
1955
561
  LClosure *cl = luaF_newLclosure(L, 1);  /* create main closure */
1956
561
  setclLvalue2s(L, L->top.p, cl);  /* anchor it (to avoid being collected) */
1957
561
  luaD_inctop(L);
1958
561
  lexstate.h = luaH_new(L);  /* create table for scanner */
1959
561
  sethvalue2s(L, L->top.p, lexstate.h);  /* anchor it */
1960
561
  luaD_inctop(L);
1961
561
  funcstate.f = cl->p = luaF_newproto(L);
1962
561
  luaC_objbarrier(L, cl, cl->p);
1963
0
  funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
1964
561
  luaC_objbarrier(L, funcstate.f, funcstate.f->source);
1965
0
  lexstate.buff = buff;
1966
561
  lexstate.dyd = dyd;
1967
561
  dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
1968
561
  luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
1969
561
  mainfunc(&lexstate, &funcstate);
1970
561
  lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1971
  /* all scopes should be correctly finished */
1972
162
  lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
1973
162
  L->top.p--;  /* remove scanner's table */
1974
162
  return cl;  /* closure is on the stack, too */
1975
162
}
1976