summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/frame.h
blob: ee05005006159861e8953ced95b1c6e927e5d23c (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
/*****
 * frame.h
 * Andy Hammerlindl 2002/07/22
 *
 * Describes the frame levels for the functions of the language.
 * Also creates accesses for the variable for automated loading
 * and saving of variables.
 *****/

#ifndef FRAME_H
#define FRAME_H

#include <cassert>

#include "access.h"

namespace trans {

class frame : public gc {
  frame *parent;
  size_t numFormals;
  Int numLocals;

#ifdef DEBUG_FRAME
  string name;
#endif

public:
  frame(string name, frame *parent, size_t numFormals)
    : parent(parent), numFormals(numFormals), numLocals(0)
#ifdef DEBUG_FRAME
      , name(name)
#endif
  
  {}

  size_t getNumFormals() {
    return numFormals;
  } 
  Int getNumLocals() {
    return numLocals;
  }

  frame *getParent() {
    return parent;
  }

  Int size() {
    return (Int) (1+numFormals+numLocals);
  }

  access *accessFormal(size_t index) {
    assert(index < numFormals);
    return new localAccess((Int) (1 + index), this);
  }

  access *allocLocal() {
    return new localAccess((Int) (1 + numFormals + numLocals++), this);
  }

  // Checks if the frame f is a descendent of this frame.
  // For our purposes, a frame is its own descendant.
  bool isDescendant(frame *f)
  {
    while (f != 0) {
      if (f == this)
        return true;
      f = f->parent;
    }
    return false;
  }
};

inline void print(ostream& out, frame *f) {
  out << f;
  if (f != 0) {
    out << " -> ";
    print(out, f->getParent());
  }
}

} // namespace trans

#endif