File: varinit.h

package info (click to toggle)
asymptote 2.47-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,408 kB
  • sloc: cpp: 61,370; python: 8,474; sh: 3,607; ansic: 2,711; perl: 1,563; lisp: 1,363; makefile: 600; yacc: 554; lex: 444
file content (87 lines) | stat: -rw-r--r-- 2,013 bytes parent folder | download | duplicates (8)
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
/*****
 * varinit.h
 * Andy Hammerlindl 2005/07/01
 *
 * Variable initializers are syntax that finish code such as
 *   Int var = ...
 * As such, they are translated to yield a certain type, the type of the
 * variable.  Expressions are a special case that can be translated without an
 * associated variable or its type.
 *****/

#ifndef VARINIT_H
#define VARINIT_H

#include "types.h"
#include "symbol.h"
#include "absyn.h"

namespace absyntax {

using trans::coenv;
using trans::access;
using sym::symbol;
using types::array;

class varinit : public absyn {
public:
  varinit(position pos)
    : absyn(pos) {}

  // This determines what instruction are needed to put the associated
  // value onto the stack, then adds those instructions to the current
  // lambda in e.
  // In some expressions and initializers, the target type needs to be
  // known in order to translate properly.  For most expressions, this is
  // kept to a minimum.
  // For expression, this also allows an implicit cast, hence the name.
  virtual void transToType(coenv &e, types::ty *target) = 0;
};

// A default initializer.  For example:
//   int a;
// is in some sense equivalent to
//   int a=0;
// where the definit for Int is a function that returns 0.
class definit : public varinit {
public:
  definit(position pos)
    : varinit(pos) {}

  void prettyprint(ostream &out, Int indent);

  void transToType(coenv &e, types::ty *target);
};

class arrayinit : public varinit {
  mem::list<varinit *> inits;

  varinit *rest;
public:
  arrayinit(position pos)
    : varinit(pos), rest(0) {}

  virtual ~arrayinit() 
  {}

  void prettyprint(ostream &out, Int indent);

  // Encodes the instructions to make an array from size elements on the stack.
  static void transMaker(coenv &e, Int size, bool rest);

  void transToType(coenv &e, types::ty *target);

  void add(varinit *init) {
    inits.push_back(init);
  }

  void addRest(varinit *init) {
    rest=init;
  }

  friend class joinExp;
};

} // namespace absyntax

#endif