summaryrefslogtreecommitdiff
path: root/graphics/asymptote/varinit.h
blob: 55c47665945f79b9fb4ce7d6c25502ca63698d6f (plain)
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