File: resolve-symbols.cc

package info (click to toggle)
rumur 2020.12.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,292 kB
  • sloc: cpp: 17,090; ansic: 2,537; objc: 1,542; python: 1,120; sh: 538; yacc: 536; lex: 229; lisp: 15; makefile: 5
file content (612 lines) | stat: -rw-r--r-- 14,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include <cstddef>
#include <cassert>
#include "../../common/isa.h"
#include <gmpxx.h>
#include "location.hh"
#include <memory>
#include <rumur/Boolean.h>
#include <rumur/Decl.h>
#include <rumur/Expr.h>
#include <rumur/Model.h>
#include <rumur/Node.h>
#include <rumur/Number.h>
#include <rumur/Ptr.h>
#include <rumur/resolve-symbols.h>
#include <rumur/Rule.h>
#include <rumur/Stmt.h>
#include <rumur/Symtab.h>
#include <rumur/traverse.h>
#include <rumur/TypeExpr.h>
#include <rumur/validate.h>
#include <string>
#include <utility>

namespace rumur {

namespace {

class Resolver : public Traversal {

 private:
  Symtab symtab;

 public:
  Resolver() {

    // Open a global scope
    symtab.open_scope();

    // Teach the symbol table the built ins
    auto td = Ptr<TypeDecl>::make("boolean", Boolean, location());
    symtab.declare("boolean", td);
    mpz_class index = 0;
    for (const std::pair<std::string, location> &m : Boolean->members) {
      symtab.declare(m.first, Ptr<ConstDecl>::make("boolean",
        Ptr<Number>::make(index, location()), Boolean, location()));
      index++;
    }
  }

  void visit_add(Add &n) final {
    visit_bexpr(n);
  }

  void visit_aliasdecl(AliasDecl &n) final {
    dispatch(*n.value);
    disambiguate(n.value);
  }

  void visit_aliasrule(AliasRule &n) final {
    symtab.open_scope();
    for (auto &a : n.aliases) {
      dispatch(*a);
      symtab.declare(a->name, a);
    }
    for (auto &r : n.rules)
      dispatch(*r);
    symtab.close_scope();
  }

  void visit_aliasstmt(AliasStmt &n) final {
    symtab.open_scope();
    for (auto &a : n.aliases) {
      dispatch(*a);
      symtab.declare(a->name, a);
    }
    for (auto &s : n.body)
      dispatch(*s);
    symtab.close_scope();
  }

  void visit_ambiguousamp(AmbiguousAmp &n) final {
    visit_bexpr(n);
  }

  void visit_ambiguouspipe(AmbiguousPipe &n) final {
    visit_bexpr(n);
  }

  void visit_and(And &n) final {
    visit_bexpr(n);
  }

  void visit_assignment(Assignment &n) final {
    dispatch(*n.lhs);
    dispatch(*n.rhs);
    disambiguate(n.lhs);
    disambiguate(n.rhs);
  }

  void visit_band(Band &n) final {
    visit_bexpr(n);
  }

  void visit_bnot(Bnot &n) final {
    visit_uexpr(n);
  }

  void visit_bor(Bor &n) final {
    visit_bexpr(n);
  }

  void visit_clear(Clear &n) final {
    dispatch(*n.rhs);
    disambiguate(n.rhs);
  }

  void visit_constdecl(ConstDecl &n) final {
    dispatch(*n.value);
    disambiguate(n.value);
  }

  void visit_div(Div &n) final {
    visit_bexpr(n);
  }

  void visit_element(Element &n) final {
    dispatch(*n.array);
    dispatch(*n.index);
    disambiguate(n.array);
    disambiguate(n.index);
  }

  void visit_enum(Enum &n) final {
    auto e = Ptr<Enum>::make(n);

    // register all the enum members so they can be referenced later
    mpz_class index = 0;
    size_t id = e->unique_id + 1;
    for (const std::pair<std::string, location> &m : n.members) {
      auto cd = Ptr<ConstDecl>::make(m.first,
        Ptr<Number>::make(index, m.second), e, m.second);
      // assign this member a unique id so that referrers can use it if need be
      assert(id < e->unique_id_limit && "number of enum members exceeds what "
        "was expected");
      cd->unique_id = id;
      symtab.declare(m.first, cd);
      index++;
      id++;
    }
  }

  void visit_eq(Eq &n) final {
    visit_bexpr(n);
  }

  void visit_exists(Exists &n) final {
    symtab.open_scope();
    dispatch(n.quantifier);
    dispatch(*n.expr);
    symtab.close_scope();
    disambiguate(n.expr);
  }

  void visit_exprid(ExprID &n) final {
    if (n.value == nullptr) {
      // This reference is unresolved

      Ptr<ExprDecl> d = symtab.lookup<ExprDecl>(n.id, n.loc);
      if (d == nullptr)
        throw Error("unknown symbol \"" + n.id + "\"", n.loc);

      n.value = d;
    }
  }

  void visit_field(Field &n) final {
    dispatch(*n.record);
    disambiguate(n.record);
  }

  void visit_for(For &n) final {
    symtab.open_scope();
    dispatch(n.quantifier);
    for (auto &s : n.body)
      dispatch(*s);
    symtab.close_scope();
  }

  void visit_forall(Forall &n) final {
    symtab.open_scope();
    dispatch(n.quantifier);
    dispatch(*n.expr);
    symtab.close_scope();
    disambiguate(n.expr);
  }

  void visit_function(Function &n) final {
    symtab.open_scope();
    for (auto &p : n.parameters)
      dispatch(*p);
    if (n.return_type != nullptr)
      dispatch(*n.return_type);
    // register the function itself, even though its body has not yet been
    // resolved, in order to allow contained function calls to resolve to the
    // containing function, supporting recursion
    symtab.declare(n.name, Ptr<Function>::make(n));
    // only register the function parameters now, to avoid their names shadowing
    // anything that needs to be resolved during symbol resolution of another
    // parameter or the return type
    for (auto &p : n.parameters)
      symtab.declare(p->name, p);
    for (auto &d : n.decls) {
      dispatch(*d);
      symtab.declare(d->name, d);
    }
    for (auto &s : n.body)
      dispatch(*s);
    symtab.close_scope();
  }

  void visit_functioncall(FunctionCall &n) final {
    if (n.function == nullptr) {
      // This reference is unresolved

      Ptr<Function> f = symtab.lookup<Function>(n.name, n.loc);
      if (f == nullptr)
        throw Error("unknown function call \"" + n.name + "\"", n.loc);

      n.function = f;
    }
    for (auto &a : n.arguments)
      dispatch(*a);

    for (Ptr<Expr> &a : n.arguments)
      disambiguate(a);
  }

  void visit_geq(Geq &n) final {
    visit_bexpr(n);
  }

  void visit_gt(Gt &n) final {
    visit_bexpr(n);
  }

  void visit_ifclause(IfClause &n) final {
    if (n.condition != nullptr)
      dispatch(*n.condition);
    for (auto &s : n.body)
      dispatch(*s);
    if (n.condition != nullptr)
      disambiguate(n.condition);
  }

  void visit_implication(Implication &n) final {
    visit_bexpr(n);
  }

  void visit_isundefined(IsUndefined &n) final {
    visit_uexpr(n);
  }

  void visit_leq(Leq &n) final {
    visit_bexpr(n);
  }

  void visit_lsh(Lsh &n) final {
    visit_bexpr(n);
  }

  void visit_lt(Lt &n) final {
    visit_bexpr(n);
  }

  void visit_model(Model &n) final {

    // running marker of offset in the global state data
    mpz_class offset = 0;

    /* whether we have not yet hit any problems that make offset calculation
     * impossible
     */
    bool ok = true;

    for (Ptr<Node> &c : n.children) {
      dispatch(*c);

      /* if this was a variable declaration, we now know enough to determine its
       * offset in the global state data
       */
      if (ok) {
        if (auto v = dynamic_cast<VarDecl*>(c.get())) {

          /* If the declaration or one of its children does not validate, it is
           * unsafe to call width().
           */
          try {
            validate(*v);
          } catch (Error&) {
            /* Skip this and future offset calculations and assume our caller
             * will eventually discover the underlying reason when they call
             * n.validate().
             */
            ok = false;
          }

          if (ok) {
            v->offset = offset;
            offset += v->type->width();
          }
        }
      }

      if (auto d = dynamic_cast<Decl*>(c.get()))
        symtab.declare(d->name, c);
      if (auto f = dynamic_cast<Function*>(c.get()))
        symtab.declare(f->name, c);
    }
  }

  void visit_mod(Mod &n) final {
    visit_bexpr(n);
  }

  void visit_mul(Mul &n) final {
    visit_bexpr(n);
  }

  void visit_negative(Negative &n) final {
    visit_uexpr(n);
  }

  void visit_neq(Neq &n) final {
    visit_bexpr(n);
  }

  void visit_not(Not &n) final {
    visit_uexpr(n);
  }

  void visit_or(Or &n) final {
    visit_bexpr(n);
  }

  void visit_property(Property &n) final {
    dispatch(*n.expr);
    disambiguate(n.expr);
  }

  void visit_put(Put &n) final {
    if (n.expr != nullptr) {
      dispatch(*n.expr);
      disambiguate(n.expr);
    }
  }

  void visit_quantifier(Quantifier &n) final {
    if (n.type != nullptr) {
      // wrap symbol resolution within the type in a dummy scope to suppress any
      // declarations (primarily enum members) as these will be duplicated in
      // when we descend into decl below
      symtab.open_scope();
      dispatch(*n.type);
      symtab.close_scope();
    }
    if (n.from != nullptr)
      dispatch(*n.from);
    if (n.to != nullptr)
      dispatch(*n.to);
    if (n.step != nullptr)
      dispatch(*n.step);

    if (n.from != nullptr)
      disambiguate(n.from);
    if (n.to != nullptr)
      disambiguate(n.to);
    if (n.step != nullptr)
      disambiguate(n.step);

    // if the bounds for this iteration are now known to be constant, we can
    // narrow its VarDecl
    if (n.from != nullptr && n.from->constant() &&
        n.to != nullptr && n.to->constant()) {
      auto r = dynamic_cast<Range*>(n.decl->type.get());
      assert(r != nullptr && "non-range type used for inferred loop decl");
      // the range may have been given as either an up count or down count
      if (n.from->constant_fold() <= n.to->constant_fold()) {
        r->min = n.from;
        r->max = n.to;
      } else {
        r->min = n.to;
        r->max = n.from;
      }
    }

    dispatch(*n.decl);

    symtab.declare(n.name, n.decl);
  }

  void visit_range(Range &n) final {
    dispatch(*n.min);
    disambiguate(n.min);
    dispatch(*n.max);
    disambiguate(n.max);
  }

  void visit_return(Return &n) final {
    if (n.expr != nullptr) {
      dispatch(*n.expr);
      disambiguate(n.expr);
    }
  }

  void visit_rsh(Rsh &n) final {
    visit_bexpr(n);
  }

  void visit_ruleset(Ruleset &n) final {
    symtab.open_scope();
    for (Quantifier &q : n.quantifiers)
      dispatch(q);
    for (auto &r : n.rules)
      dispatch(*r);
    symtab.close_scope();
  }

  void visit_scalarset(Scalarset &n) final {
    dispatch(*n.bound);
    disambiguate(n.bound);
  }

  void visit_simplerule(SimpleRule &n) final {
    symtab.open_scope();
    for (Quantifier &q : n.quantifiers)
      dispatch(q);
    if (n.guard != nullptr)
      dispatch(*n.guard);
    for (auto &d : n.decls) {
      dispatch(*d);
      symtab.declare(d->name, d);
    }
    for (auto &s : n.body)
      dispatch(*s);
    symtab.close_scope();
    if (n.guard != nullptr)
      disambiguate(n.guard);
  }

  void visit_startstate(StartState &n) final {
    symtab.open_scope();
    for (Quantifier &q : n.quantifiers)
      dispatch(q);
    for (auto &d : n.decls) {
      dispatch(*d);
      symtab.declare(d->name, d);
    }
    for (auto &s : n.body)
      dispatch(*s);
    symtab.close_scope();
  }

  void visit_sub(Sub &n) final {
    visit_bexpr(n);
  }

  void visit_switch(Switch &n) final {
    dispatch(*n.expr);
    for (SwitchCase &c : n.cases)
      dispatch(c);
    disambiguate(n.expr);
  }

  void visit_switchcase(SwitchCase &n) final {
    for (auto &m : n.matches)
      dispatch(*m);
    for (auto &s : n.body)
      dispatch(*s);
    for (Ptr<Expr> &m : n.matches)
      disambiguate(m);
  }

  void visit_ternary(Ternary &n) {
    dispatch(*n.cond);
    dispatch(*n.lhs);
    dispatch(*n.rhs);
    disambiguate(n.cond);
    disambiguate(n.lhs);
    disambiguate(n.rhs);
  }

  void visit_typeexprid(TypeExprID &n) final {
    if (n.referent == nullptr) {
      // This reference is unresolved

      Ptr<TypeDecl> t = symtab.lookup<TypeDecl>(n.name, n.loc);
      if (t == nullptr)
        throw Error("unknown type symbol \"" + n.name + "\"", n.loc);

      n.referent = t;
    }
  }

  void visit_undefine(Undefine &n) final {
    dispatch(*n.rhs);
    disambiguate(n.rhs);
  }

  void visit_while(While &n) final {
    dispatch(*n.condition);
    for (auto &s : n.body)
      dispatch(*s);
    disambiguate(n.condition);
  }

  void visit_xor(Xor &n) final {
    visit_bexpr(n);
  }

  virtual ~Resolver() = default;

 private:
  void visit_bexpr(BinaryExpr &n) {
    dispatch(*n.lhs);
    dispatch(*n.rhs);
    disambiguate(n.lhs);
    disambiguate(n.rhs);
  }

  void visit_uexpr(UnaryExpr &n) {
    dispatch(*n.rhs);
    disambiguate(n.rhs);
  }

  // detect whether this is an ambiguous node and, if so, resolve it into its
  // more precise AST node type
  void disambiguate(Ptr<Expr> &e) {

    if (auto a = dynamic_cast<const AmbiguousAmp*>(e.get())) {

      // try to get the type of the left hand side
      Ptr<TypeExpr> t;
      try {
        t = a->lhs->type();
      } catch (Error&) {
        // We failed because the left operand is somehow invalid. Silently
        // ignore this, assuming it will be rediscovered during AST validation.
        return;
      }

      // Form an unambiguous replacement node based on the type of the left
      // operand. Note that the types of the left and right operands may be
      // incompatible. However, this will cause an error during AST validation
      // so we do not need to worry about that here.
      Ptr<Expr> replacement;
      if (isa<Range>(t)) {
        replacement = Ptr<Band>::make(a->lhs, a->rhs, a->loc);
      } else {
        replacement = Ptr<And>::make(a->lhs, a->rhs, a->loc);
      }

      // also preserve the identifier which has already been set
      replacement->unique_id = a->unique_id;

      // replace the ambiguous node
      e = replacement;

      return;
    }

    if (auto o = dynamic_cast<const AmbiguousPipe*>(e.get())) {

      // try to get the type of the left hand side
      Ptr<TypeExpr> t;
      try {
        t = o->lhs->type();
      } catch (Error&) {
        // We failed because the left operand is somehow invalid. Silently
        // ignore this, assuming it will be rediscovered during AST validation.
        return;
      }

      // Form an unambiguous replacement node based on the type of the left
      // operand. Note that the types of the left and right operands may be
      // incompatible. However, this will cause an error during AST validation
      // so we do not need to worry about that here.
      Ptr<Expr> replacement;
      if (isa<Range>(t)) {
        replacement = Ptr<Bor>::make(o->lhs, o->rhs, o->loc);
      } else {
        replacement = Ptr<Or>::make(o->lhs, o->rhs, o->loc);
      }

      // also preserve the identifier which has already been set
      replacement->unique_id = o->unique_id;

      // replace the ambiguous node
      e = replacement;

      return;
    }
  }
};

}

void resolve_symbols(Model &m) {
  Resolver r;
  r.dispatch(m);
}

}