forked from openenclave/oeedger8r-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
352 lines (304 loc) · 8.77 KB
/
utils.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#ifndef UTILS_H
#define UTILS_H
#include <ostream>
#include <sstream>
#include <string>
#include "ast.h"
template <typename A, typename C>
bool in(const A& a, const C& c)
{
return std::find(c.begin(), c.end(), a) != c.end();
}
template <typename C1, typename C2>
void append(C1& c1, const C2& c2)
{
c1.insert(c1.end(), c2.begin(), c2.end());
}
template <typename T>
void autogen_preamble(T& os)
{
os << "/*"
<< " * This file is auto generated by oeedger8r. DO NOT EDIT."
<< " */";
}
template <typename T>
void header(T& os, const std::string& guard)
{
autogen_preamble(os);
// clang-format off
os << "#ifndef " + guard
<< "#define " + guard;
// clang-format on
}
template <typename T>
void footer(T& os, const std::string& guard)
{
os << "#endif // " + guard;
}
inline std::string upper(const std::string& s)
{
std::string t(s);
for (char& ch : t)
ch = static_cast<char>(toupper(ch));
return t;
}
inline std::string atype_str(Type* t)
{
if (t->tag_ == Enum)
return "enum " + t->name_;
if (t->tag_ == Struct)
return "struct " + t->name_;
if (t->tag_ == Union)
return "union " + t->name_;
if (t->tag_ == Const)
return "const " + atype_str(t->t_);
if (t->tag_ == Short)
return "short int";
if (t->tag_ == Long)
return "long int";
if (t->tag_ == Unsigned)
return "unsigned " + atype_str(t->t_);
if (t->tag_ == Ptr)
return atype_str(t->t_) + "*";
if (t->tag_ == Foreign)
return t->name_;
#define TYPE_STR(tag, tstr) \
if (t->tag_ == tag) \
return tstr
TYPE_STR(Bool, "bool");
TYPE_STR(Char, "char");
TYPE_STR(Int, "int");
TYPE_STR(LLong, "long long");
TYPE_STR(Float, "float");
TYPE_STR(Double, "double");
TYPE_STR(LDouble, "long double");
TYPE_STR(Int8, "int8_t");
TYPE_STR(Int16, "int16_t");
TYPE_STR(Int32, "int32_t");
TYPE_STR(Int64, "int64_t");
TYPE_STR(UInt8, "uint8_t");
TYPE_STR(UInt16, "uint16_t");
TYPE_STR(UInt32, "uint32_t");
TYPE_STR(UInt64, "uint64_t");
TYPE_STR(WChar, "wchar_t");
TYPE_STR(Void, "void");
TYPE_STR(SizeT, "size_t");
return "";
}
inline std::string dims_str(Dims* dims, size_t idx = 0)
{
if (dims && idx < dims->size())
return "[" + dims->at(idx) + "]" + dims_str(dims, idx + 1);
return "";
}
inline std::string decl_str(const std::string& name, Type* t, Dims* dims)
{
if (std::isalpha(name[0]) || name[0] == '_')
return atype_str(t) + " " + name + dims_str(dims);
else
return atype_str(t) + name + dims_str(dims);
}
inline std::string replace(
std::string s,
const std::string& p,
const std::string& q)
{
size_t pos = s.find(p);
while (pos != std::string::npos)
{
s.replace(pos, p.size(), q);
pos = s.find(p, pos + q.size());
}
return s;
}
inline std::string mtype_str(Decl* p)
{
if (p->type_->tag_ == Foreign && p->attrs_ && p->attrs_->isary_)
return "/* foreign array of type " + p->type_->name_ + " */ void*";
std::string s = atype_str(p->type_) + (p->dims_ ? "*" : "");
return replace(s, "const ", "");
}
inline std::string mdecl_str(
const std::string& name,
Type* t,
Dims* dims,
Attrs* attrs)
{
if (t->tag_ == Foreign && attrs && attrs->isary_)
return "/* foreign array of type " + t->name_ + " */ void* " + name;
std::string decl = atype_str(t) + (dims ? "* " : " ") + name;
return replace(decl, "const ", "");
}
inline std::string prototype(
const Function* f,
bool ecall = true,
bool gen_t = true,
const std::string& prefix = "")
{
std::string retstr =
(ecall != gen_t) ? "oe_result_t" : atype_str(f->rtype_);
std::vector<std::string> args;
if (ecall && !gen_t)
args.push_back("oe_enclave_t* enclave");
if (ecall != gen_t && f->rtype_->tag_ != Void)
args.push_back(atype_str(f->rtype_) + "* _retval");
for (Decl* p : f->params_)
args.push_back(decl_str(p->name_, p->type_, p->dims_));
std::string argsstr;
if (args.empty())
argsstr = gen_t && !ecall ? "(\n )" : "(void)";
else if (args.size() == 1)
argsstr = "(" + args[0] + ")";
else
{
argsstr = "(\n " + args[0];
for (size_t i = 1; i < args.size(); ++i)
argsstr += ",\n " + args[i];
argsstr += ")";
}
return retstr + " " + prefix + f->name_ + argsstr;
}
inline std::string create_prototype(const std::string& ename)
{
return "oe_result_t oe_create_" + ename + "_enclave(\n" +
" const char* path,\n"
" oe_enclave_type_t type,\n"
" uint32_t flags,\n"
" const oe_enclave_setting_t* settings,\n"
" uint32_t setting_count,\n"
" oe_enclave_t** enclave)";
}
inline std::string btype(Type* t)
{
if (t->tag_ == Const || t->tag_ == Ptr)
return btype(t->t_);
if (t->tag_ == Foreign)
return t->name_;
if (t->tag_ == Enum)
return "enum " + t->name_;
if (t->tag_ == Struct)
return "struct " + t->name_;
if (t->tag_ == Union)
return "union " + t->name_;
return atype_str(t);
}
inline std::string count_attr_str(
const Token& t,
const std::string& prefix = "")
{
if (t.is_name())
return prefix + static_cast<std::string>(t);
else
return t;
}
inline std::string size_attr_str(const Token& t, const std::string& prefix = "")
{
return count_attr_str(t, prefix);
}
inline std::string psize(Decl* p, const std::string& prefix = "")
{
if (p->attrs_ && (p->attrs_->string_ || p->attrs_->wstring_))
return prefix + p->name_ + "_len * sizeof(" + btype(p->type_) + ")";
if (p->dims_ && !p->dims_->empty())
return "sizeof(" + decl_str("", p->type_, p->dims_) + ")";
if (p->type_->tag_ == Foreign && p->attrs_ && p->attrs_->isary_)
return "sizeof(" + p->type_->name_ + ")";
std::string s = "";
if (p->type_->tag_ == Ptr)
s = "sizeof(" + replace(atype_str(p->type_->t_), "const ", "") + ")";
else if (p->type_->tag_ == Foreign && p->attrs_ && p->attrs_->isptr_)
s = "sizeof(*(" + p->type_->name_ + ")0)";
if (!p->attrs_->size_.is_empty() && !p->attrs_->count_.is_empty())
return "(" + size_attr_str(p->attrs_->size_, prefix) + " * " +
count_attr_str(p->attrs_->count_, prefix) + ")";
if (p->attrs_ && !p->attrs_->count_.is_empty())
return "((size_t)" + count_attr_str(p->attrs_->count_, prefix) + " * " +
s + ")";
if (p->attrs_ && !p->attrs_->size_.is_empty())
return size_attr_str(p->attrs_->size_, prefix);
return s;
}
template <typename T>
inline std::string to_str(const T& t)
{
std::ostringstream os;
os << t;
return os.str();
}
inline UserType* get_user_type(
const std::vector<UserType*>& types,
const std::string& name)
{
for (UserType* t : types)
{
if (t->name_ == name)
return t;
}
return nullptr;
}
inline UserType* get_user_type(Edl* edl, const std::string& name)
{
return get_user_type(edl->types_, name);
}
template <typename Action>
void iterate_deep_copyable_fields(UserType* user_type, Action&& action)
{
if (user_type->tag_ != Struct)
return;
for (Decl* prop : user_type->fields_)
{
/*
* The member is deep copyable only if it has attrs_ and the
* attrs->is_size_or_count_ is false (i.e., the member is not used by
* size/count attribute).
*/
if (prop->attrs_ && !prop->attrs_->is_size_or_count_)
action(prop);
}
}
inline UserType* get_user_type_for_deep_copy(
const std::vector<UserType*>& types,
Decl* d)
{
Type* t = d->type_;
UserType* ut = nullptr;
if (t->tag_ != Ptr || !d->attrs_)
return nullptr;
// Unwrap first level * and const.
t = t->t_;
if (t->tag_ == Const)
t = t->t_;
// EDL types can masquerade as foregin types. Depends on what
// the parser wants to do.
if (t->tag_ == Foreign || t->tag_ == Struct)
ut = get_user_type(types, t->name_);
if (!ut)
return nullptr;
bool deep_copyable = false;
iterate_deep_copyable_fields(
ut, [&deep_copyable](Decl*) { deep_copyable = true; });
return deep_copyable ? ut : nullptr;
}
inline UserType* get_user_type_for_deep_copy(Edl* edl, Decl* d)
{
return get_user_type_for_deep_copy(edl->types_, d);
}
inline const char* path_sep()
{
#if _WIN32
return "\\";
#else
return "/";
#endif
}
inline std::string fix_path_separators(const std::string& path)
{
#if _WIN32
return replace(path, "/", "\\");
#else
return replace(path, "\\", "/");
#endif
}
#endif // UTILS_H