-
Notifications
You must be signed in to change notification settings - Fork 8
/
schema.cpp
126 lines (114 loc) · 2.51 KB
/
schema.cpp
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
#include "schema.h"
#include "types.h"
namespace Types
{
Schema::Schema(const std::vector<VarDef>& args) : vars(args) {}
void Schema::CopyToNameStack(NameStack& stack)
{
for (const auto& v : vars)
{
stack.Add(&v);
}
}
const VarDef* Schema::FindVar(const std::string& name) const
{
for (const auto& v : vars)
{
if (v.Name() == name)
{
return &v;
}
}
return 0;
}
int Schema::NameToIndex(const std::string& nm) const
{
int index = 0;
for (auto a : vars)
{
if (a.Name() == nm)
{
return index;
}
index++;
}
return -1;
}
void SchemaRange::DoDump() const
{
std::cerr << "SchemaRange " << start << ".." << name << std::endl;
}
TypeDecl* SchemaRange::Instantiate(const std::vector<int64_t>& vals)
{
int64_t lowVal = start;
if (lowName != "")
{
int idx = schema->NameToIndex(lowName);
if (idx < 0)
{
return 0;
}
lowVal = vals[idx];
}
int idx = schema->NameToIndex(highName);
if (idx < 0)
{
return 0;
}
return new RangeDecl(new Range(lowVal, vals[idx]), baseType);
}
TypeDecl* SchemaArrayDecl::Instantiate(const std::vector<int64_t>& vals)
{
std::vector<RangeBaseDecl*> rv;
for (auto r : Ranges())
{
if (auto sr = llvm::dyn_cast<SchemaRange>(const_cast<RangeBaseDecl*>(r)))
{
auto rr = llvm::dyn_cast<RangeBaseDecl>(sr->Instantiate(vals));
rv.push_back(rr);
}
else
{
rv.push_back(const_cast<RangeBaseDecl*>(r));
}
}
return new ArrayDecl(baseType, rv);
}
TypeDecl* SchemaSetDecl::Instantiate(const std::vector<int64_t>& vals)
{
auto rr = llvm::dyn_cast<SchemaRange>(range)->Instantiate(vals);
return new SetDecl(llvm::cast<RangeBaseDecl>(rr), baseType);
}
bool IsSchema(const TypeDecl* ty)
{
switch (ty->Type())
{
case TypeDecl::TK_SchRange:
case TypeDecl::TK_SchArray:
case TypeDecl::TK_SchSet:
return true;
default:
return false;
}
}
template<typename T>
static TypeDecl* Instantiate(const std::vector<int64_t>& vals, TypeDecl* ty)
{
T* schTy = llvm::dyn_cast<T>(ty);
return schTy->Instantiate(vals);
}
TypeDecl* Instantiate(const std::vector<int64_t>& vals, TypeDecl* ty)
{
switch (ty->Type())
{
case TypeDecl::TK_SchRange:
return Instantiate<SchemaRange>(vals, ty);
case TypeDecl::TK_SchSet:
return Instantiate<SchemaSetDecl>(vals, ty);
case TypeDecl::TK_SchArray:
return Instantiate<SchemaArrayDecl>(vals, ty);
default:
return 0;
}
}
} // namespace Types