Debian Codegraph @

core/pass.cpp

raw
1 /*
2 Copyright 2015 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include "pass.h"
18
19 namespace jsonnet::internal {
20
21 void CompilerPass::fodder(Fodder &fodder)
22 {
23 for (auto &f : fodder)
24 fodderElement(f);
25 }
26
27 void CompilerPass::specs(std::vector<ComprehensionSpec> &specs)
28 {
29 for (auto &spec : specs) {
30 fodder(spec.openFodder);
31 switch (spec.kind) {
32 case ComprehensionSpec::FOR:
33 fodder(spec.varFodder);
34 fodder(spec.inFodder);
35 expr(spec.expr);
36 break;
37 case ComprehensionSpec::IF: expr(spec.expr); break;
38 }
39 }
40 }
41
42 void CompilerPass::params(Fodder &fodder_l, ArgParams &params, Fodder &fodder_r)
43 {
44 fodder(fodder_l);
45 for (auto &param : params) {
46 fodder(param.idFodder);
47 if (param.expr) {
48 fodder(param.eqFodder);
49 expr(param.expr);
50 }
51 fodder(param.commaFodder);
52 }
53 fodder(fodder_r);
54 }
55
56 void CompilerPass::fieldParams(ObjectField &field)
57 {
58 if (field.methodSugar) {
59 params(field.fodderL, field.params, field.fodderR);
60 }
61 }
62
63 void CompilerPass::fields(ObjectFields &fields)
64 {
65 for (auto &field : fields) {
66 switch (field.kind) {
67 case ObjectField::LOCAL: {
68 fodder(field.fodder1);
69 fodder(field.fodder2);
70 fieldParams(field);
71 fodder(field.opFodder);
72 expr(field.expr2);
73 } break;
74
75 case ObjectField::FIELD_ID:
76 case ObjectField::FIELD_STR:
77 case ObjectField::FIELD_EXPR: {
78 if (field.kind == ObjectField::FIELD_ID) {
79 fodder(field.fodder1);
80
81 } else if (field.kind == ObjectField::FIELD_STR) {
82 expr(field.expr1);
83
84 } else if (field.kind == ObjectField::FIELD_EXPR) {
85 fodder(field.fodder1);
86 expr(field.expr1);
87 fodder(field.fodder2);
88 }
89 fieldParams(field);
90 fodder(field.opFodder);
91 expr(field.expr2);
92
93 } break;
94
95 case ObjectField::ASSERT: {
96 fodder(field.fodder1);
97 expr(field.expr2);
98 if (field.expr3 != nullptr) {
99 fodder(field.opFodder);
100 expr(field.expr3);
101 }
102 } break;
103 }
104
105 fodder(field.commaFodder);
106 }
107 }
108
109 void CompilerPass::expr(AST *&ast_)
110 {
111 fodder(ast_->openFodder);
112 visitExpr(ast_);
113 }
114
115 void CompilerPass::visit(Apply *ast)
116 {
117 expr(ast->target);
118 params(ast->fodderL, ast->args, ast->fodderR);
119 if (ast->tailstrict) {
120 fodder(ast->tailstrictFodder);
121 }
122 }
123
124 void CompilerPass::visit(ApplyBrace *ast)
125 {
126 expr(ast->left);
127 expr(ast->right);
128 }
129
130 void CompilerPass::visit(Array *ast)
131 {
132 for (auto &element : ast->elements) {
133 expr(element.expr);
134 fodder(element.commaFodder);
135 }
136 fodder(ast->closeFodder);
137 }
138
139 void CompilerPass::visit(ArrayComprehension *ast)
140 {
141 expr(ast->body);
142 fodder(ast->commaFodder);
143 specs(ast->specs);
144 fodder(ast->closeFodder);
145 }
146
147 void CompilerPass::visit(Assert *ast)
148 {
149 expr(ast->cond);
150 if (ast->message != nullptr) {
151 fodder(ast->colonFodder);
152 expr(ast->message);
153 }
154 fodder(ast->semicolonFodder);
155 expr(ast->rest);
156 }
157
158 void CompilerPass::visit(Binary *ast)
159 {
160 expr(ast->left);
161 fodder(ast->opFodder);
162 expr(ast->right);
163 }
164
165 void CompilerPass::visit(Conditional *ast)
166 {
167 expr(ast->cond);
168 fodder(ast->thenFodder);
169 if (ast->branchFalse != nullptr) {
170 expr(ast->branchTrue);
171 fodder(ast->elseFodder);
172 expr(ast->branchFalse);
173 } else {
174 expr(ast->branchTrue);
175 }
176 }
177
178 void CompilerPass::visit(Error *ast)
179 {
180 expr(ast->expr);
181 }
182
183 void CompilerPass::visit(Function *ast)
184 {
185 params(ast->parenLeftFodder, ast->params, ast->parenRightFodder);
186 expr(ast->body);
187 }
188
189 void CompilerPass::visit(Import *ast)
190 {
191 visit(ast->file);
192 }
193
194 void CompilerPass::visit(Importstr *ast)
195 {
196 visit(ast->file);
197 }
198
199 void CompilerPass::visit(Importbin *ast)
200 {
201 visit(ast->file);
202 }
203
204 void CompilerPass::visit(InSuper *ast)
205 {
206 expr(ast->element);
207 }
208
209 void CompilerPass::visit(Index *ast)
210 {
211 expr(ast->target);
212 if (ast->id != nullptr) {
213 } else {
214 if (ast->isSlice) {
215 if (ast->index != nullptr)
216 expr(ast->index);
217 if (ast->end != nullptr)
218 expr(ast->end);
219 if (ast->step != nullptr)
220 expr(ast->step);
221 } else {
222 expr(ast->index);
223 }
224 }
225 }
226
227 void CompilerPass::visit(Local *ast)
228 {
229 assert(ast->binds.size() > 0);
230 for (auto &bind : ast->binds) {
231 fodder(bind.varFodder);
232 if (bind.functionSugar) {
233 params(bind.parenLeftFodder, bind.params, bind.parenRightFodder);
234 }
235 fodder(bind.opFodder);
236 expr(bind.body);
237 fodder(bind.closeFodder);
238 }
239 expr(ast->body);
240 }
241
242 void CompilerPass::visit(Object *ast)
243 {
244 fields(ast->fields);
245 fodder(ast->closeFodder);
246 }
247
248 void CompilerPass::visit(DesugaredObject *ast)
249 {
250 for (AST *assert : ast->asserts) {
251 expr(assert);
252 }
253 for (auto &field : ast->fields) {
254 expr(field.name);
255 expr(field.body);
256 }
257 }
258
259 void CompilerPass::visit(ObjectComprehension *ast)
260 {
261 fields(ast->fields);
262 specs(ast->specs);
263 fodder(ast->closeFodder);
264 }
265
266 void CompilerPass::visit(ObjectComprehensionSimple *ast)
267 {
268 expr(ast->field);
269 expr(ast->value);
270 expr(ast->array);
271 }
272
273 void CompilerPass::visit(Parens *ast)
274 {
275 expr(ast->expr);
276 fodder(ast->closeFodder);
277 }
278
279 void CompilerPass::visit(SuperIndex *ast)
280 {
281 if (ast->id != nullptr) {
282 } else {
283 expr(ast->index);
284 }
285 }
286
287 void CompilerPass::visit(Unary *ast)
288 {
289 expr(ast->expr);
290 }
291
292 #define VISIT(var,astType,astClass) \
293 case astType: { \
294 assert(dynamic_cast<astClass *>(var)); \
295 auto *ast = static_cast<astClass *>(var); \
296 visit(ast); \
297 } break
298
299 void CompilerPass::visitExpr(AST *&ast_)
300 {
301 switch(ast_->type) {
302 VISIT(ast_, AST_APPLY, Apply);
303 VISIT(ast_, AST_APPLY_BRACE, ApplyBrace);
304 VISIT(ast_, AST_ARRAY, Array);
305 VISIT(ast_, AST_ARRAY_COMPREHENSION, ArrayComprehension);
306 // VISIT(ast_, AST_ARRAY_COMPREHENSION, ArrayComprehensionSimple);
307 VISIT(ast_, AST_ASSERT, Assert);
308 VISIT(ast_, AST_BINARY, Binary);
309 VISIT(ast_, AST_BUILTIN_FUNCTION, BuiltinFunction);
310 VISIT(ast_, AST_BUILTIN_FUNCTION_BODY, BuiltinFunctionBody);
311 VISIT(ast_, AST_CONDITIONAL, Conditional);
312 VISIT(ast_, AST_DESUGARED_OBJECT, DesugaredObject);
313 VISIT(ast_, AST_DOLLAR, Dollar);
314 VISIT(ast_, AST_ERROR, Error);
315 VISIT(ast_, AST_FUNCTION, Function);
316 VISIT(ast_, AST_IMPORT, Import);
317 VISIT(ast_, AST_IMPORTSTR, Importstr);
318 VISIT(ast_, AST_IMPORTBIN, Importbin);
319 VISIT(ast_, AST_INDEX, Index);
320 VISIT(ast_, AST_IN_SUPER, InSuper);
321 VISIT(ast_, AST_LITERAL_BOOLEAN, LiteralBoolean);
322 VISIT(ast_, AST_LITERAL_NULL, LiteralNull);
323 VISIT(ast_, AST_LITERAL_NUMBER, LiteralNumber);
324 VISIT(ast_, AST_LITERAL_STRING, LiteralString);
325 VISIT(ast_, AST_LOCAL, Local);
326 VISIT(ast_, AST_OBJECT, Object);
327 VISIT(ast_, AST_OBJECT_COMPREHENSION, ObjectComprehension);
328 VISIT(ast_, AST_OBJECT_COMPREHENSION_SIMPLE, ObjectComprehensionSimple);
329 VISIT(ast_, AST_PARENS, Parens);
330 VISIT(ast_, AST_SELF, Self);
331 VISIT(ast_, AST_SUPER_INDEX, SuperIndex);
332 VISIT(ast_, AST_UNARY, Unary);
333 VISIT(ast_, AST_VAR, Var);
334 default:
335 std::cerr << "INTERNAL ERROR: Unknown AST: " << ast_ << std::endl;
336 std::abort();
337 break;
338 }
339 }
340
341 void CompilerPass::file(AST *&body, Fodder &final_fodder)
342 {
343 expr(body);
344 fodder(final_fodder);
345 }
346
347 /** A pass that clones the AST it is given. */
348 class ClonePass : public CompilerPass {
349 public:
350 ClonePass(Allocator &alloc) : CompilerPass(alloc) {}
351 virtual void expr(AST *&ast);
352 };
353
354 #define CLONE(var,astType,astClass) \
355 case astType: { \
356 assert(dynamic_cast<astClass *>(var)); \
357 auto *ast = static_cast<astClass *>(var); \
358 var = alloc.clone(ast); \
359 } break
360
361 void ClonePass::expr(AST *&ast_)
362 {
363 switch(ast_->type) {
364 CLONE(ast_, AST_APPLY, Apply);
365 CLONE(ast_, AST_APPLY_BRACE, ApplyBrace);
366 CLONE(ast_, AST_ARRAY, Array);
367 CLONE(ast_, AST_ARRAY_COMPREHENSION, ArrayComprehension);
368 // CLONE(ast_, AST_ARRAY_COMPREHENSION, ArrayComprehensionSimple);
369 CLONE(ast_, AST_ASSERT, Assert);
370 CLONE(ast_, AST_BINARY, Binary);
371 CLONE(ast_, AST_BUILTIN_FUNCTION, BuiltinFunction);
372 CLONE(ast_, AST_CONDITIONAL, Conditional);
373 CLONE(ast_, AST_DESUGARED_OBJECT, DesugaredObject);
374 CLONE(ast_, AST_DOLLAR, Dollar);
375 CLONE(ast_, AST_ERROR, Error);
376 CLONE(ast_, AST_FUNCTION, Function);
377 CLONE(ast_, AST_IMPORT, Import);
378 CLONE(ast_, AST_IMPORTSTR, Importstr);
379 CLONE(ast_, AST_IMPORTBIN, Importbin);
380 CLONE(ast_, AST_INDEX, Index);
381 CLONE(ast_, AST_IN_SUPER, InSuper);
382 CLONE(ast_, AST_LITERAL_BOOLEAN, LiteralBoolean);
383 CLONE(ast_, AST_LITERAL_NULL, LiteralNull);
384 CLONE(ast_, AST_LITERAL_NUMBER, LiteralNumber);
385 CLONE(ast_, AST_LITERAL_STRING, LiteralString);
386 CLONE(ast_, AST_LOCAL, Local);
387 CLONE(ast_, AST_OBJECT, Object);
388 CLONE(ast_, AST_OBJECT_COMPREHENSION, ObjectComprehension);
389 CLONE(ast_, AST_OBJECT_COMPREHENSION_SIMPLE, ObjectComprehensionSimple);
390 CLONE(ast_, AST_PARENS, Parens);
391 CLONE(ast_, AST_SELF, Self);
392 CLONE(ast_, AST_SUPER_INDEX, SuperIndex);
393 CLONE(ast_, AST_UNARY, Unary);
394 CLONE(ast_, AST_VAR, Var);
395 default:
396 std::cerr << "INTERNAL ERROR: Unknown AST: " << ast_ << std::endl;
397 std::abort();
398 break;
399 }
400
401 CompilerPass::expr(ast_);
402 }
403
404 AST *clone_ast(Allocator &alloc, AST *ast)
405 {
406 AST *r = ast;
407 ClonePass(alloc).expr(r);
408 return r;
409 }
410
411 } // namespace jsonnet::internal