File: Decl.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 (158 lines) | stat: -rw-r--r-- 3,417 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
#include <cstddef>
#include <gmpxx.h>
#include <iostream>
#include "location.hh"
#include <memory>
#include <rumur/Decl.h>
#include <rumur/except.h>
#include <rumur/Expr.h>
#include <rumur/Node.h>
#include <rumur/Ptr.h>
#include <rumur/traverse.h>
#include <string>

namespace rumur {

Decl::Decl(const std::string &name_, const location &loc_):
  Node(loc_), name(name_) {
}

Decl::~Decl() {
}

ExprDecl::ExprDecl(const std::string &name_, const location &loc_):
  Decl(name_, loc_) { }

AliasDecl::AliasDecl(const std::string &name_, const Ptr<Expr> &value_,
    const location &loc_):
  ExprDecl(name_, loc_), value(value_) { }

AliasDecl *AliasDecl::clone() const {
  return new AliasDecl(*this);
}

void AliasDecl::visit(BaseTraversal &visitor) {
  visitor.visit_aliasdecl(*this);
}

void AliasDecl::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_aliasdecl(*this);
}

bool AliasDecl::is_lvalue() const {
  return value->is_lvalue();
}

bool AliasDecl::is_readonly() const {
  return value->is_readonly();
}

Ptr<TypeExpr> AliasDecl::get_type() const {
  return value->type();
}

ConstDecl::ConstDecl(const std::string &name_, const Ptr<Expr> &value_,
  const location &loc_):
  ExprDecl(name_, loc_), value(value_) { }

ConstDecl::ConstDecl(const std::string &name_, const Ptr<Expr> &value_,
  const Ptr<TypeExpr> &type_, const location &loc_):
  ExprDecl(name_, loc_), value(value_), type(type_) { }

ConstDecl *ConstDecl::clone() const {
  return new ConstDecl(*this);
}

void ConstDecl::visit(BaseTraversal &visitor) {
  visitor.visit_constdecl(*this);
}

void ConstDecl::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_constdecl(*this);
}

bool ConstDecl::is_lvalue() const {
  return false;
}

bool ConstDecl::is_readonly() const {
  return true;
}

Ptr<TypeExpr> ConstDecl::get_type() const {

  // If this constant has an explicit type (e.g. it's an enum member), use that.
  if (type != nullptr)
    return type;

  /* If this doesn't have an explicit type, fall back on the type of the value
   * it points at. This is irrelevant for numerical constants, but important for
   * boolean constants.
   */
  return value->type();
}

void ConstDecl::validate() const {
  if (!value->constant())
    throw Error("const definition is not a constant", value->loc);
}

TypeDecl::TypeDecl(const std::string &name_, const Ptr<TypeExpr> &value_,
  const location &loc_):
  Decl(name_, loc_), value(value_) {
}

TypeDecl *TypeDecl::clone() const {
  return new TypeDecl(*this);
}

void TypeDecl::visit(BaseTraversal &visitor) {
  visitor.visit_typedecl(*this);
}

void TypeDecl::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_typedecl(*this);
}

VarDecl::VarDecl(const std::string &name_, const Ptr<TypeExpr> &type_,
  const location &loc_):
  ExprDecl(name_, loc_), type(type_) {
}

VarDecl *VarDecl::clone() const {
  return new VarDecl(*this);
}

void VarDecl::visit(BaseTraversal &visitor) {
  visitor.visit_vardecl(*this);
}

void VarDecl::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_vardecl(*this);
}

bool VarDecl::is_lvalue() const {
  return true;
}

bool VarDecl::is_readonly() const {
  return readonly;
}

bool VarDecl::is_in_state() const {
  return offset >= 0;
}

mpz_class VarDecl::width() const {
  return type->width();
}

mpz_class VarDecl::count() const {
  return type->count();
}

Ptr<TypeExpr> VarDecl::get_type() const {
  return type;
}

}