source: src/AST/Print.cpp@ f9128a5

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since f9128a5 was d908563, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Merge branch 'master' into cleanup-dtors

  • Property mode set to 100644
File size: 31.2 KB
RevLine 
[461046f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Print.cpp --
8//
9// Author : Thierry Delisle
10// Created On : Tue May 21 16:20:15 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#include "Print.hpp"
17
18#include "Decl.hpp"
19#include "Expr.hpp"
20#include "Stmt.hpp"
21#include "Type.hpp"
22#include "TypeSubstitution.hpp"
23
[a2e758e]24#include "Common/utility.h" // for group_iterate
25
[5902625]26using namespace std;
[461046f]27
28namespace ast {
29
30template <typename C, typename... T>
31constexpr auto make_array(T&&... values) ->
[5902625]32 array<C,sizeof...(T)>
[461046f]33{
[5902625]34 return array<C,sizeof...(T)>{
35 forward<T>(values)...
[461046f]36 };
37}
38
39class Printer : public Visitor {
40public:
[5902625]41 ostream & os;
[461046f]42 Indenter indent;
[5902625]43 bool short_mode;
[461046f]44
[5902625]45 Printer(ostream & os, Indenter indent, bool short_mode) : os( os ), indent( indent ), short_mode(short_mode) {}
[461046f]46
47private:
48 template< typename C >
49 void printAll( const C & c ) {
50 for ( const auto & i : c ) {
51 if ( i ) {
52 os << indent;
53 i->accept( *this );
54 // need an endl after each element because it's not
55 // easy to know when each individual item should end
[5902625]56 os << endl;
[461046f]57 } // if
58 } // for
59 }
60
[20a5977]61 /// call if mandatory field is missing
62 void undefined() {
63 os << "UNDEFINED";
64 }
65
66 /// call for fields that should be mandatory
67 void safe_print( const ast::Node * n ) {
68 if ( n ) n->accept( *this );
69 else undefined();
70 }
71
72 /// call to print short form. Incorporates features of safe_print()
73 void short_print( const ast::Node * n ) {
74 if ( ! n ) { undefined(); return; }
75 bool old_short = short_mode; short_mode = true;
76 n->accept( *this );
77 short_mode = old_short;
78 }
79
[461046f]80
81 static const char* Names[];
82
83 struct Names {
84 static constexpr auto FuncSpecifiers = make_array<const char*>(
85 "inline", "_Noreturn", "fortran"
86 );
87
88 static constexpr auto StorageClasses = make_array<const char*>(
89 "extern", "static", "auto", "register", "_Thread_local"
90 );
91
92 static constexpr auto Qualifiers = make_array<const char*>(
93 "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic"
94 );
95 };
96
97 template<typename storage_t, size_t N>
[5902625]98 void print(const storage_t & storage, const array<const char *, N> & Names ) {
[461046f]99 if ( storage.any() ) {
100 for ( size_t i = 0; i < Names.size(); i += 1 ) {
101 if ( storage[i] ) {
102 os << Names[i] << ' ';
103 }
104 }
105 }
106 }
107
108 void print( const ast::Function::Specs & specs ) {
109 print(specs, Names::FuncSpecifiers);
110 }
111
112 void print( const ast::Storage::Classes & storage ) {
113 print(storage, Names::StorageClasses);
114 }
115
116 void print( const ast::CV::Qualifiers & qualifiers ) {
117 print(qualifiers, Names::Qualifiers);
118 }
119
[94b1f718]120 void print( const std::vector<ast::Label> & labels ) {
121 if ( labels.empty() ) return;
122 os << indent << "... Labels: {";
123 bool isFirst = true;
124 for ( const Label & l : labels ) {
125 if ( isFirst ) { isFirst = false; } else { os << ","; }
126 os << l;
127 }
128 os << "}" << endl;
129 }
130
[20a5977]131 void print( const ast::Expr::InferUnion & inferred, unsigned level = 0 ) {
132 switch ( inferred.mode ) {
133 case ast::Expr::InferUnion::Empty: return;
134 case ast::Expr::InferUnion::Slots: {
[d908563]135 os << indent << "with " << inferred.data.resnSlots.size()
[94b1f718]136 << " pending inference slots" << endl;
[20a5977]137 return;
138 }
139 case ast::Expr::InferUnion::Params: {
[94b1f718]140 os << indent << "with inferred parameters " << level << ":" << endl;
[20a5977]141 ++indent;
142 for ( const auto & i : inferred.data.inferParams ) {
143 os << indent;
144 short_print( Decl::fromId( i.second.decl ) );
[94b1f718]145 os << endl;
[20a5977]146 print( i.second.expr->inferred, level+1 );
147 }
148 --indent;
149 return;
150 }
151 }
152 }
153
[b0ec971]154 void print( const ast::ParameterizedType::ForallList & forall ) {
[d908563]155 if ( forall.empty() ) return;
[94b1f718]156 os << "forall" << endl;
[b0ec971]157 ++indent;
158 printAll( forall );
159 os << indent;
160 --indent;
161 }
162
163 void print( const std::vector<ptr<Attribute>> & attrs ) {
164 if ( attrs.empty() ) return;
[94b1f718]165 os << "with attributes" << endl;
[b0ec971]166 ++indent;
167 printAll( attrs );
168 --indent;
169 }
170
171 void print( const std::vector<ptr<Expr>> & params ) {
172 if ( params.empty() ) return;
[94b1f718]173 os << endl << indent << "... with parameters" << endl;
[b0ec971]174 ++indent;
175 printAll( params );
176 --indent;
177 }
178
[5902625]179 void print( const ast::AggregateDecl * node ) {
180 os << node->typeString() << " " << node->name << ":";
181 if ( node->linkage != Linkage::Cforall ) {
182 os << " " << Linkage::name( node->linkage );
183 } // if
184 os << " with body : " << (node->body ? "yes " : "no ");
185
186 if ( ! node->params.empty() ) {
187 os << endl << indent << "... with parameters" << endl;
188 ++indent;
189 printAll( node->params );
190 --indent;
191 } // if
192 if ( ! node->members.empty() ) {
193 os << endl << indent << "... with members" << endl;
194 ++indent;
195 printAll( node->members );
196 --indent;
197 } // if
198 if ( ! node->attributes.empty() ) {
199 os << endl << indent << "... with attributes" << endl;
200 ++indent;
201 printAll( node->attributes );
202 --indent;
203 } // if
204 os << endl;
205 }
206
207 void print( const ast::NamedTypeDecl * node ) {
208 if ( !node->name.empty() ) os << node->name << ": ";
209
210 if ( node->linkage != Linkage::Cforall ) {
211 os << Linkage::name( node->linkage ) << " ";
212 } // if
213 print( node->storage );
214 os << node->typeString();
215 if ( node->base ) {
216 os << " for ";
217 ++indent;
218 node->base->accept( *this );
219 --indent;
220 } // if
221 if ( ! node->params.empty() ) {
222 os << endl << indent << "... with parameters" << endl;
223 ++indent;
224 printAll( node->params );
225 --indent;
226 } // if
227 if ( ! node->assertions.empty() ) {
228 os << endl << indent << "... with assertions" << endl;
229 ++indent;
230 printAll( node->assertions );
231 --indent;
232 } // if
233 }
[b0ec971]234
[20a5977]235 void postprint( const ast::Expr * node ) {
236 print( node->inferred );
237
238 if ( node->env ) {
[94b1f718]239 os << endl << indent << "... with environment:" << endl;
[20a5977]240 ++indent;
241 node->env->accept( *this );
242 --indent;
243 }
[a16e246]244
[20a5977]245 if ( node->extension ) {
[94b1f718]246 os << endl << indent << "... with extension";
[20a5977]247 }
248 }
249
250 void preprint( const ast::Type * node ) {
251 print( node->qualifiers );
252 }
253
254 void preprint( const ast::ParameterizedType * node ) {
255 print( node->forall );
256 print( node->qualifiers );
257 }
258
259 void preprint( const ast::ReferenceToType * node ) {
260 print( node->forall );
261 print( node->attributes );
262 print( node->qualifiers );
263 }
264
[461046f]265public:
[5902625]266 virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) {
[a2e758e]267 if ( !node->name.empty() ) os << node->name << ": ";
[461046f]268
269 if ( node->linkage != Linkage::Cforall ) {
270 os << Linkage::name( node->linkage ) << " ";
271 } // if
272
273 print( node->storage );
274
275 if ( node->type ) {
276 node->type->accept( *this );
277 } else {
[20a5977]278 os << "untyped entity";
[461046f]279 } // if
280
281 if ( node->init ) {
282 os << " with initializer (" << (
283 node->init->maybeConstructed
284 ? "maybe constructed"
285 : "not constructed"
[5902625]286 ) << ")" << endl << indent+1;
[461046f]287
288 ++indent;
289 node->init->accept( *this );
290 --indent;
[5902625]291 os << endl;
[461046f]292 } // if
293
294 if ( ! node->attributes.empty() ) {
[5902625]295 os << endl << indent << "... with attributes:" << endl;
[461046f]296 ++indent;
297 printAll( node->attributes );
298 --indent;
299 }
300
301 if ( node->bitfieldWidth ) {
302 os << indent << " with bitfield width ";
303 node->bitfieldWidth->accept( *this );
304 } // if
305 return node;
306 }
307
[5902625]308 virtual const ast::DeclWithType * visit( const ast::FunctionDecl * node ) {
309 if ( !node->name.empty() ) {
310 os << node->name << ": ";
311 } // if
312 if ( node->linkage != Linkage::Cforall ) {
313 os << Linkage::name( node->linkage ) << " ";
314 } // if
315
316 printAll( node->attributes );
317
318 print( node->storage );
319 print( node->funcSpec );
320
321 if ( node->type ) {
322 node->type->accept( *this );
323 } else {
[20a5977]324 os << "untyped entity";
[5902625]325 } // if
326
327 if ( node->stmts ) {
328 os << indent << "... with body" << endl << indent+1;
329 ++indent;
330 node->stmts->accept( *this );
331 --indent;
332 } // if
[461046f]333 return node;
334 }
335
[5902625]336 virtual const ast::Decl * visit( const ast::StructDecl * node ) {
337 print(node);
[461046f]338 return node;
339 }
340
[5902625]341 virtual const ast::Decl * visit( const ast::UnionDecl * node ) {
342 print(node);
[461046f]343 return node;
344 }
345
[5902625]346 virtual const ast::Decl * visit( const ast::EnumDecl * node ) {
347 print(node);
[461046f]348 return node;
349 }
350
[5902625]351 virtual const ast::Decl * visit( const ast::TraitDecl * node ) {
352 print(node);
[461046f]353 return node;
354 }
355
[5902625]356 virtual const ast::Decl * visit( const ast::TypeDecl * node ) {
357 print( node );
358 if ( node->init ) {
359 os << endl << indent << "with type initializer: ";
360 ++indent;
361 node->init->accept( *this );
362 --indent;
363 }
[461046f]364 return node;
365 }
366
[5902625]367 virtual const ast::Decl * visit( const ast::TypedefDecl * node ) {
368 print( node );
[461046f]369 return node;
370 }
371
[5902625]372 virtual const ast::AsmDecl * visit( const ast::AsmDecl * node ) {
373 node->stmt->accept( *this );
[461046f]374 return node;
375 }
376
[5902625]377 virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) {
378 os << "Static Assert with condition: ";
379 ++indent;
380 node->cond->accept( *this );
381 --indent;
382 os << endl << indent << "and message: ";
383 ++indent;
384 node->msg->accept( *this );
385 --indent;
386 os << endl;
[461046f]387 return node;
388 }
389
[5902625]390 virtual const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) {
[94b1f718]391 os << "Compound Statement:" << endl;
[5902625]392 ++indent;
393 printAll( node->kids );
394 --indent;
[461046f]395 return node;
396 }
397
[5902625]398 virtual const ast::Stmt * visit( const ast::ExprStmt * node ) {
399 ++indent;
400 os << "Expression Statement:" << endl << indent;
[20a5977]401 safe_print( node->expr );
[5902625]402 --indent;
[461046f]403 return node;
404 }
405
[5902625]406 virtual const ast::Stmt * visit( const ast::AsmStmt * node ) {
407 os << "Assembler Statement:" << endl;
408 ++indent;
[94b1f718]409 os << indent-1 << "instruction:" << endl << indent;
410 safe_print( node->instruction );
[5902625]411 if ( ! node->output.empty() ) {
[94b1f718]412 os << endl << indent << "output:" << endl;
[5902625]413 printAll( node->output );
414 } // if
415 if ( ! node->input.empty() ) {
[94b1f718]416 os << indent << "input:" << endl;
[5902625]417 printAll( node->input );
418 } // if
419 if ( ! node->clobber.empty() ) {
[94b1f718]420 os << indent << "clobber:" << endl;
[5902625]421 printAll( node->clobber );
422 } // if
423 --indent;
[461046f]424 return node;
425 }
426
[5902625]427 virtual const ast::Stmt * visit( const ast::DirectiveStmt * node ) {
[94b1f718]428 os << "GCC Directive: " << node->directive << endl;
[461046f]429 return node;
430 }
431
[5902625]432 virtual const ast::Stmt * visit( const ast::IfStmt * node ) {
[94b1f718]433 os << "If on condition:" << endl;
[5902625]434 ++indent;
[94b1f718]435 os << indent;
[20a5977]436 safe_print( node->cond );
[5902625]437 --indent;
438
[94b1f718]439 if ( ! node->inits.empty() ) {
440 os << indent << "... with initialization:" << endl;
[5902625]441 ++indent;
[94b1f718]442 for ( const ast::Stmt * stmt : node->inits ) {
[5902625]443 os << indent;
[94b1f718]444 safe_print( stmt );
[5902625]445 }
446 --indent;
447 os << endl;
448 }
449
[94b1f718]450 os << indent << "... then:" << endl;
[5902625]451
452 ++indent;
453 os << indent;
[20a5977]454 safe_print( node->thenPart );
[5902625]455 --indent;
456
457 if ( node->elsePart != 0 ) {
[94b1f718]458 os << indent << "... else:" << endl;
[5902625]459 ++indent;
460 os << indent;
461 node->elsePart->accept( *this );
462 --indent;
463 } // if
[461046f]464 return node;
465 }
466
[5902625]467 virtual const ast::Stmt * visit( const ast::WhileStmt * node ) {
[94b1f718]468 if ( node->isDoWhile ) { os << "Do-"; }
469 os << "While on condition:" << endl;
470 ++indent;
471 safe_print( node->cond );
472 os << indent-1 << "... with body:" << endl;
473 safe_print( node->body );
474
475 if ( ! node->inits.empty() ) {
476 os << indent-1 << "... with inits:" << endl;
477 printAll( node->inits );
478 }
479 --indent;
480
[461046f]481 return node;
482 }
483
[5902625]484 virtual const ast::Stmt * visit( const ast::ForStmt * node ) {
[94b1f718]485 os << "For Statement" << endl;
486
487 if ( ! node->inits.empty() ) {
488 os << indent << "... initialization:" << endl;
489 ++indent;
490 for ( const ast::Stmt * stmt : node->inits ) {
491 os << indent+1;
492 safe_print( stmt );
493 }
494 --indent;
495 }
496
497 if ( node->cond ) {
498 os << indent << "... condition:" << endl;
499 ++indent;
500 os << indent;
501 node->cond->accept( *this );
502 --indent;
503 }
504
505 if ( node->inc ) {
506 os << indent << "... increment:" << endl;
507 ++indent;
508 os << indent;
509 node->inc->accept( *this );
510 --indent;
511 }
512
513 if ( node->body ) {
514 os << indent << "... with body:" << endl;
515 ++indent;
516 os << indent;
517 node->body->accept( *this );
518 --indent;
519 }
520 os << endl;
521 print( node->labels );
522
[461046f]523 return node;
524 }
525
[5902625]526 virtual const ast::Stmt * visit( const ast::SwitchStmt * node ) {
[94b1f718]527 os << "Switch on condition: ";
528 safe_print( node->cond );
529 os << endl;
530
531 ++indent;
532 for ( const ast::Stmt * stmt : node->stmts ) {
533 stmt->accept( *this );
534 }
535 --indent;
536
[461046f]537 return node;
538 }
539
[5902625]540 virtual const ast::Stmt * visit( const ast::CaseStmt * node ) {
[94b1f718]541 if ( node->isDefault() ) {
542 os << indent << "Default ";
543 } else {
544 os << indent << "Case ";
545 safe_print( node->cond );
546 } // if
547 os << endl;
548
549 ++indent;
550 for ( const ast::Stmt * stmt : node->stmts ) {
551 os << indent;
552 stmt->accept( *this );
553 }
554 --indent;
555
[461046f]556 return node;
557 }
558
[5902625]559 virtual const ast::Stmt * visit( const ast::BranchStmt * node ) {
[94b1f718]560 os << "Branch (" << node->kindName() << ")" << endl;
561 ++indent;
562 if ( ! node->target.empty() ) {
563 os << indent << "with target: " << node->target << endl;
564 }
565
566 if ( ! node->originalTarget.empty() ) {
567 os << indent << "with original target: " << node->originalTarget << endl;
568 }
569
570 if ( node->computedTarget ) {
571 os << indent << "with computed target: ";
572 node->computedTarget->accept( *this );
573 os << endl;
574 }
575 --indent;
576
[461046f]577 return node;
578 }
579
[5902625]580 virtual const ast::Stmt * visit( const ast::ReturnStmt * node ) {
[94b1f718]581 os << "Return Statement, returning";
582 if ( node->expr ) {
583 ++indent;
584 os << ":" << endl << indent;
585 node->expr->accept( *this );
586 --indent;
587 } else {
588 os << " void";
589 }
590 os << endl;
591
[461046f]592 return node;
593 }
594
[5902625]595 virtual const ast::Stmt * visit( const ast::ThrowStmt * node ) {
[461046f]596 return node;
597 }
598
[5902625]599 virtual const ast::Stmt * visit( const ast::TryStmt * node ) {
[461046f]600 return node;
601 }
602
[5902625]603 virtual const ast::Stmt * visit( const ast::CatchStmt * node ) {
[461046f]604 return node;
605 }
606
[5902625]607 virtual const ast::Stmt * visit( const ast::FinallyStmt * node ) {
[461046f]608 return node;
609 }
610
[5902625]611 virtual const ast::Stmt * visit( const ast::WaitForStmt * node ) {
[461046f]612 return node;
613 }
614
[5902625]615 virtual const ast::Stmt * visit( const ast::WithStmt * node ) {
[461046f]616 return node;
617 }
618
[5902625]619 virtual const ast::NullStmt * visit( const ast::NullStmt * node ) {
[94b1f718]620 os << "Null Statement" << endl;
621 print( node->labels );
622
[461046f]623 return node;
624 }
625
[5902625]626 virtual const ast::Stmt * visit( const ast::DeclStmt * node ) {
[461046f]627 return node;
628 }
629
[5902625]630 virtual const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) {
[461046f]631 return node;
632 }
633
[5902625]634 virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) {
[20a5977]635 ++indent;
[94b1f718]636 os << "Application of" << endl << indent;
[20a5977]637 safe_print( node->func );
[94b1f718]638 os << endl;
[20a5977]639 if ( ! node->args.empty() ) {
[94b1f718]640 os << indent << "... to arguments" << endl;
[20a5977]641 printAll( node->args );
642 }
643 --indent;
644 postprint( node );
645
[461046f]646 return node;
647 }
648
[5902625]649 virtual const ast::Expr * visit( const ast::UntypedExpr * node ) {
[20a5977]650 ++indent;
[94b1f718]651 os << "Applying untyped:" << endl;
[20a5977]652 os << indent;
653 safe_print( node->func );
[94b1f718]654 os << endl << indent-1 << "...to:" << endl;
[20a5977]655 printAll( node->args );
656 --indent;
657 postprint( node );
658
[461046f]659 return node;
660 }
661
[5902625]662 virtual const ast::Expr * visit( const ast::NameExpr * node ) {
[20a5977]663 os << "Name: " << node->name;
664 postprint( node );
[d908563]665
[461046f]666 return node;
667 }
668
[5902625]669 virtual const ast::Expr * visit( const ast::AddressExpr * node ) {
[94b1f718]670 os << "Address of:" << endl;
[20a5977]671 ++indent;
672 os << indent;
673 safe_print( node->arg );
674
675 --indent;
676
[461046f]677 return node;
678 }
679
[5902625]680 virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
[20a5977]681 os << "Address of label:" << node->arg;
682
[461046f]683 return node;
684 }
685
[5902625]686 virtual const ast::Expr * visit( const ast::CastExpr * node ) {
[20a5977]687 ++indent;
[94b1f718]688 os << (node->isGenerated ? "Generated" : "Explicit") << " cast of:" << endl << indent;
[20a5977]689 safe_print( node->arg );
[94b1f718]690 os << endl << indent-1 << "... to:";
[20a5977]691 if ( ! node->result ) {
692 os << " ";
693 undefined();
694 } else if ( node->result->isVoid() ) {
695 os << " nothing";
696 } else {
[94b1f718]697 os << endl << indent;
[20a5977]698 node->result->accept( *this );
699 } // if
700 --indent;
701 postprint( node );
702
[461046f]703 return node;
704 }
705
[5902625]706 virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) {
[20a5977]707 ++indent;
[94b1f718]708 os << "Keyword Cast of:" << endl << indent;
[20a5977]709 safe_print( node->arg );
710 --indent;
[94b1f718]711 os << endl << indent << "... to: " << node->targetString();
[20a5977]712 postprint( node );
713
[461046f]714 return node;
715 }
716
[5902625]717 virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) {
[20a5977]718 ++indent;
[94b1f718]719 os << "Virtual Cast of:" << endl << indent;
[20a5977]720 safe_print( node->arg );
[94b1f718]721 os << endl << indent-1 << "... to:";
[20a5977]722 if ( ! node->result ) {
723 os << " unknown";
724 } else {
[94b1f718]725 os << endl << indent;
[20a5977]726 node->result->accept( *this );
727 }
728 --indent;
729 postprint( node );
730
[461046f]731 return node;
732 }
733
[5902625]734 virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) {
[20a5977]735 ++indent;
[94b1f718]736 os << "Untyped Member Expression, with field: " << endl << indent;
[20a5977]737 safe_print( node->member );
[94b1f718]738 os << indent-1 << "... from aggregate:" << endl << indent;
[20a5977]739 safe_print( node->aggregate );
740 --indent;
741 postprint( node );
742
[461046f]743 return node;
744 }
745
[5902625]746 virtual const ast::Expr * visit( const ast::MemberExpr * node ) {
[20a5977]747 ++indent;
[94b1f718]748 os << "Member Expression, with field:" << endl << indent;
[20a5977]749 safe_print( node->member );
[94b1f718]750 os << endl << indent-1 << "... from aggregate:" << endl << indent;
[20a5977]751 safe_print( node->aggregate );
752 --indent;
753 postprint( node );
754
[461046f]755 return node;
756 }
757
[5902625]758 virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
[20a5977]759 os << "Variable Expression: ";
760 short_print( node->var );
761 postprint( node );
762
[461046f]763 return node;
764 }
765
[5902625]766 virtual const ast::Expr * visit( const ast::ConstantExpr * node ) {
[20a5977]767 os << "Constant Expression (" << node->rep;
768 if ( node->result ) {
769 os << ": ";
770 node->result->accept( *this );
771 }
772 os << ")";
773 postprint( node );
774
[461046f]775 return node;
776 }
777
[5902625]778 virtual const ast::Expr * visit( const ast::SizeofExpr * node ) {
[a16e246]779 os << "Sizeof Expression on: ";
780 ++indent;
781 if ( node->type ) node->type->accept( *this );
782 else safe_print( node->expr );
783 --indent;
784 postprint( node );
785
[461046f]786 return node;
787 }
788
[5902625]789 virtual const ast::Expr * visit( const ast::AlignofExpr * node ) {
[a16e246]790 os << "Alignof Expression on: ";
791 ++indent;
792 if ( node->type ) node->type->accept( *this );
793 else safe_print( node->expr );
794 --indent;
795 postprint( node );
[d908563]796
[461046f]797 return node;
798 }
799
[5902625]800 virtual const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) {
[a16e246]801 os << "Untyped Offsetof Expression on member " << node->member << " of ";
802 ++indent;
803 safe_print( node->type );
804 --indent;
805 postprint( node );
806
[461046f]807 return node;
808 }
809
[5902625]810 virtual const ast::Expr * visit( const ast::OffsetofExpr * node ) {
[a16e246]811 os << "Offsetof Expression on member " << node->member->name << " of ";
812 ++indent;
813 safe_print( node->type );
814 --indent;
815 postprint( node );
816
[461046f]817 return node;
818 }
819
[5902625]820 virtual const ast::Expr * visit( const ast::OffsetPackExpr * node ) {
[a16e246]821 os << "Offset Pack Expression on: ";
822 ++indent;
823 safe_print( node->type );
824 --indent;
825 postprint( node );
826
[461046f]827 return node;
828 }
829
[5902625]830 virtual const ast::Expr * visit( const ast::LogicalExpr * node ) {
[a16e246]831 os << "Short-circuited operation (" << (node->isAnd ? "and" : "or") << ") on: ";
832 safe_print( node->arg1 );
833 os << " and ";
834 safe_print( node->arg2 );
835 postprint( node );
836
[461046f]837 return node;
838 }
839
[5902625]840 virtual const ast::Expr * visit( const ast::ConditionalExpr * node ) {
[a16e246]841 ++indent;
[94b1f718]842 os << "Conditional expression on:" << endl << indent;
[a16e246]843 safe_print( node->arg1 );
[94b1f718]844 os << indent-1 << "First alternative:" << endl << indent;
[a16e246]845 safe_print( node->arg2 );
[94b1f718]846 os << indent-1 << "Second alternative:" << endl << indent;
[a16e246]847 safe_print( node->arg3 );
848 --indent;
849 postprint( node );
850
[461046f]851 return node;
852 }
853
[5902625]854 virtual const ast::Expr * visit( const ast::CommaExpr * node ) {
[a16e246]855 ++indent;
[94b1f718]856 os << "Comma Expression:" << endl << indent;
[a16e246]857 safe_print( node->arg1 );
[94b1f718]858 os << endl << indent;
[a16e246]859 safe_print( node->arg2 );
860 --indent;
861 postprint( node );
862
[461046f]863 return node;
864 }
865
[5902625]866 virtual const ast::Expr * visit( const ast::TypeExpr * node ) {
[a16e246]867 safe_print( node->type );
868 postprint( node );
869
[461046f]870 return node;
871 }
872
[5902625]873 virtual const ast::Expr * visit( const ast::AsmExpr * node ) {
[94b1f718]874 os << "Asm Expression:" << endl;
[a16e246]875 ++indent;
876 if ( node->inout ) node->inout->accept( *this );
877 if ( node->constraint ) node->constraint->accept( *this );
878 if ( node->operand ) node->operand->accept( *this );
879 --indent;
[d908563]880
[461046f]881 return node;
882 }
883
[5902625]884 virtual const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) {
[a16e246]885 ++indent;
[94b1f718]886 os << "Implicit Copy Constructor Expression:" << endl << indent;
[a16e246]887 safe_print( node->callExpr );
888 --indent;
889 postprint( node );
890
[461046f]891 return node;
892 }
893
[5902625]894 virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) {
[94b1f718]895 os << "Constructor Expression:" << endl << indent+1;
[a16e246]896 indent += 2;
897 safe_print( node->callExpr );
898 indent -= 2;
899 postprint( node );
900
[461046f]901 return node;
902 }
903
[5902625]904 virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) {
[a16e246]905 ++indent;
[94b1f718]906 os << "Compound Literal Expression: " << endl << indent;
[a16e246]907 safe_print( node->result );
908 os << indent;
909 safe_print( node->init );
910 --indent;
911 postprint( node );
912
[461046f]913 return node;
914 }
915
[5902625]916 virtual const ast::Expr * visit( const ast::RangeExpr * node ) {
[a16e246]917 os << "Range Expression: ";
918 safe_print( node->low );
919 os << " ... ";
920 safe_print( node->high );
921 postprint( node );
922
[461046f]923 return node;
924 }
925
[5902625]926 virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) {
[94b1f718]927 os << "Untyped Tuple:" << endl;
[a16e246]928 ++indent;
929 printAll( node->exprs );
930 --indent;
931 postprint( node );
932
[461046f]933 return node;
934 }
935
[5902625]936 virtual const ast::Expr * visit( const ast::TupleExpr * node ) {
[94b1f718]937 os << "Tuple:" << endl;
[a16e246]938 ++indent;
939 printAll( node->exprs );
940 --indent;
941 postprint( node );
942
[461046f]943 return node;
944 }
945
[5902625]946 virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) {
[94b1f718]947 os << "Tuple Index Expression, with tuple:" << endl;
[a16e246]948 ++indent;
949 os << indent;
950 safe_print( node->tuple );
[94b1f718]951 os << indent << "with index: " << node->index << endl;
[a16e246]952 --indent;
953 postprint( node );
[d908563]954
[461046f]955 return node;
956 }
957
[5902625]958 virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) {
[94b1f718]959 os << "Tuple Assignment Expression, with stmt expr:" << endl;
[a16e246]960 ++indent;
961 os << indent;
962 safe_print( node->stmtExpr );
963 --indent;
964 postprint( node );
965
[461046f]966 return node;
967 }
968
[5902625]969 virtual const ast::Expr * visit( const ast::StmtExpr * node ) {
[a16e246]970 ++indent;
[94b1f718]971 os << "Statement Expression:" << endl << indent;
[a16e246]972 safe_print( node->stmts );
973 if ( ! node->returnDecls.empty() ) {
974 os << indent << "... with returnDecls: ";
975 printAll( node->returnDecls );
976 }
977 if ( ! node->dtors.empty() ) {
978 os << indent << "... with dtors: ";
979 printAll( node->dtors );
980 }
981 --indent;
982 postprint( node );
983
[461046f]984 return node;
985 }
986
[5902625]987 virtual const ast::Expr * visit( const ast::UniqueExpr * node ) {
[a16e246]988 ++indent;
[94b1f718]989 os << "Unique Expression with id: " << node->id << endl << indent;
[a16e246]990 safe_print( node->expr );
991 if ( node->object ) {
992 os << indent-1 << "... with decl: ";
993 short_print( node->object );
994 }
995 --indent;
996 postprint( node );
997
[461046f]998 return node;
999 }
1000
[5902625]1001 virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) {
[a16e246]1002 ++indent;
[94b1f718]1003 os << "Untyped Init Expression" << endl << indent;
[a16e246]1004 safe_print( node->expr );
1005 if ( ! node->initAlts.empty() ) {
1006 for ( const InitAlternative & alt : node->initAlts ) {
1007 os << indent << "InitAlternative: ";
1008 safe_print( alt.type );
1009 safe_print( alt.designation );
1010 }
1011 }
1012 --indent;
1013
[461046f]1014 return node;
1015 }
1016
[5902625]1017 virtual const ast::Expr * visit( const ast::InitExpr * node ) {
[a16e246]1018 ++indent;
[94b1f718]1019 os << "Init Expression" << endl << indent;
[a16e246]1020 safe_print( node->expr );
1021 os << indent << "... with designation: ";
1022 safe_print( node->designation );
1023 --indent;
1024
[461046f]1025 return node;
1026 }
1027
[5902625]1028 virtual const ast::Expr * visit( const ast::DeletedExpr * node ) {
[a16e246]1029 ++indent;
[94b1f718]1030 os << "Deleted Expression" << endl << indent;
[a16e246]1031 safe_print( node->expr );
[94b1f718]1032 os << endl << indent << "... deleted by: ";
[a16e246]1033 safe_print( node->deleteStmt );
1034 --indent;
1035
[461046f]1036 return node;
1037 }
1038
[5902625]1039 virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) {
[a16e246]1040 ++indent;
[94b1f718]1041 os << "Default Argument Expression" << endl << indent;
[a16e246]1042 safe_print( node->expr );
1043 --indent;
1044
[461046f]1045 return node;
1046 }
1047
[5902625]1048 virtual const ast::Expr * visit( const ast::GenericExpr * node ) {
[a16e246]1049 ++indent;
[94b1f718]1050 os << "C11 _Generic Expression" << endl << indent;
[a16e246]1051 safe_print( node->control );
[94b1f718]1052 os << endl << indent << "... with associations:" << endl;
[a16e246]1053 for ( const auto & assoc : node->associations ) {
1054 os << indent;
1055 if ( assoc.type ) {
1056 os << "... type: ";
1057 assoc.type->accept( *this );
[94b1f718]1058 os << endl << indent << "... expression: ";
[a16e246]1059 safe_print( assoc.expr );
1060 } else {
1061 os << "... default: ";
1062 safe_print( assoc.expr );
1063 }
[94b1f718]1064 os << endl;
[a16e246]1065 }
1066 --indent;
1067
[461046f]1068 return node;
1069 }
1070
[5902625]1071 virtual const ast::Type * visit( const ast::VoidType * node ) {
[b0ec971]1072 preprint( node );
1073 os << "void";
[461046f]1074 return node;
1075 }
1076
[5902625]1077 virtual const ast::Type * visit( const ast::BasicType * node ) {
[b0ec971]1078 preprint( node );
1079 os << ast::BasicType::typeNames[ node->kind ];
[461046f]1080 return node;
1081 }
1082
[5902625]1083 virtual const ast::Type * visit( const ast::PointerType * node ) {
[b0ec971]1084 preprint( node );
1085 if ( ! node->isArray() ) {
1086 os << "pointer to ";
1087 } else {
1088 os << "decayed ";
1089 if ( node->isStatic ) {
1090 os << "static ";
1091 }
1092
1093 if ( node->isVarLen ) {
1094 os << "variable length array of ";
1095 } else if ( node->dimension ) {
1096 os << "array of ";
1097 node->dimension->accept( *this );
1098 os << " ";
1099 }
1100 }
[20a5977]1101 safe_print( node->base );
[b0ec971]1102
[461046f]1103 return node;
1104 }
1105
[5902625]1106 virtual const ast::Type * visit( const ast::ArrayType * node ) {
[b0ec971]1107 preprint( node );
1108 if ( node->isStatic ) {
1109 os << "static ";
1110 }
1111
1112 if ( node->isVarLen ) {
1113 os << "variable length array of ";
1114 } else if ( node->dimension ) {
1115 os << "array of ";
1116 } else {
1117 os << "open array of ";
1118 }
1119
[20a5977]1120 safe_print( node->base );
[b0ec971]1121
1122 if ( node->dimension ) {
1123 os << " with dimension of ";
1124 node->dimension->accept( *this );
1125 }
1126
[461046f]1127 return node;
1128 }
1129
[5902625]1130 virtual const ast::Type * visit( const ast::ReferenceType * node ) {
[b0ec971]1131 preprint( node );
1132 os << "reference to ";
[20a5977]1133 safe_print( node->base );
[b0ec971]1134
[461046f]1135 return node;
1136 }
1137
[5902625]1138 virtual const ast::Type * visit( const ast::QualifiedType * node ) {
[b0ec971]1139 preprint( node );
1140 ++indent;
[94b1f718]1141 os << "Qualified Type:" << endl << indent;
[20a5977]1142 safe_print( node->parent );
[94b1f718]1143 os << endl << indent;
[20a5977]1144 safe_print( node->child );
[94b1f718]1145 os << endl;
[b0ec971]1146 --indent;
1147
[461046f]1148 return node;
1149 }
1150
[5902625]1151 virtual const ast::Type * visit( const ast::FunctionType * node ) {
[b0ec971]1152 preprint( node );
[d908563]1153
[94b1f718]1154 os << "function" << endl;
[b0ec971]1155 if ( ! node->params.empty() ) {
[94b1f718]1156 os << indent << "... with parameters" << endl;
[b0ec971]1157 ++indent;
1158 printAll( node->params );
1159 if ( node->isVarArgs ) {
[94b1f718]1160 os << indent << "and a variable number of other arguments" << endl;
[b0ec971]1161 }
1162 --indent;
1163 } else if ( node->isVarArgs ) {
[94b1f718]1164 os << indent+1 << "accepting unspecified arguments" << endl;
[b0ec971]1165 }
1166
1167 os << indent << "... returning";
1168 if ( node->returns.empty() ) {
[94b1f718]1169 os << " nothing" << endl;
[b0ec971]1170 } else {
[94b1f718]1171 os << endl;
[b0ec971]1172 ++indent;
1173 printAll( node->returns );
1174 --indent;
1175 }
1176
[461046f]1177 return node;
1178 }
1179
[5902625]1180 virtual const ast::Type * visit( const ast::StructInstType * node ) {
[b0ec971]1181 preprint( node );
1182 os << "instance of struct " << node->name;
1183 if ( node->base ) {
1184 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1185 }
1186 print( node->params );
1187
[461046f]1188 return node;
1189 }
1190
[5902625]1191 virtual const ast::Type * visit( const ast::UnionInstType * node ) {
[b0ec971]1192 preprint( node );
1193 os << "instance of union " << node->name;
1194 if ( node->base ) {
1195 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1196 }
1197 print( node->params );
1198
[461046f]1199 return node;
1200 }
1201
[5902625]1202 virtual const ast::Type * visit( const ast::EnumInstType * node ) {
[b0ec971]1203 preprint( node );
1204 os << "instance of enum " << node->name;
1205 if ( node->base ) {
1206 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1207 }
1208 print( node->params );
1209
[461046f]1210 return node;
1211 }
1212
[5902625]1213 virtual const ast::Type * visit( const ast::TraitInstType * node ) {
[b0ec971]1214 preprint( node );
1215 os << "instance of trait " << node->name;
1216 print( node->params );
1217
[461046f]1218 return node;
1219 }
1220
[5902625]1221 virtual const ast::Type * visit( const ast::TypeInstType * node ) {
[b0ec971]1222 preprint( node );
[d908563]1223 os << "instance of type " << node->name
[b0ec971]1224 << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
1225 print( node->params );
1226
[461046f]1227 return node;
1228 }
1229
[5902625]1230 virtual const ast::Type * visit( const ast::TupleType * node ) {
[b0ec971]1231 preprint( node );
[94b1f718]1232 os << "tuple of types" << endl;
[b0ec971]1233 ++indent;
1234 printAll( node->types );
1235 --indent;
1236
[461046f]1237 return node;
1238 }
1239
[5902625]1240 virtual const ast::Type * visit( const ast::TypeofType * node ) {
[b0ec971]1241 preprint( node );
1242 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
1243 os << "type-of expression ";
[20a5977]1244 safe_print( node->expr );
[b0ec971]1245
[461046f]1246 return node;
1247 }
1248
[5902625]1249 virtual const ast::Type * visit( const ast::VarArgsType * node ) {
[b0ec971]1250 preprint( node );
1251 os << "builtin var args pack";
[461046f]1252 return node;
1253 }
1254
[5902625]1255 virtual const ast::Type * visit( const ast::ZeroType * node ) {
[b0ec971]1256 preprint( node );
1257 os << "zero_t";
[461046f]1258 return node;
1259 }
1260
[5902625]1261 virtual const ast::Type * visit( const ast::OneType * node ) {
[b0ec971]1262 preprint( node );
1263 os << "one_t";
[461046f]1264 return node;
1265 }
1266
[5902625]1267 virtual const ast::Type * visit( const ast::GlobalScopeType * node ) {
[b0ec971]1268 preprint( node );
1269 os << "Global Scope Type";
[461046f]1270 return node;
1271 }
1272
[5902625]1273 virtual const ast::Designation * visit( const ast::Designation * node ) {
[c957e7f]1274 if ( node->designators.empty() ) return node;
[94b1f718]1275 os << "... designated by: " << endl;
[c957e7f]1276 ++indent;
1277 for ( const ast::Expr * d : node->designators ) {
1278 os << indent;
1279 d->accept( *this );
[94b1f718]1280 os << endl;
[c957e7f]1281 }
1282 --indent;
[461046f]1283 return node;
1284 }
1285
[5902625]1286 virtual const ast::Init * visit( const ast::SingleInit * node ) {
[c957e7f]1287 os << "Simple Initializer: ";
[20a5977]1288 safe_print( node->value );
[461046f]1289 return node;
1290 }
1291
[5902625]1292 virtual const ast::Init * visit( const ast::ListInit * node ) {
[94b1f718]1293 os << "Compound initializer: " << endl;
[c957e7f]1294 ++indent;
1295 for ( auto p : group_iterate( node->designations, node->initializers ) ) {
1296 const ast::Designation * d = std::get<0>(p);
1297 const ast::Init * init = std::get<1>(p);
1298 os << indent;
1299 init->accept( *this );
[94b1f718]1300 os << endl;
[c957e7f]1301 if ( ! d->designators.empty() ) {
1302 os << indent;
1303 d->accept( *this );
1304 }
1305 }
1306 --indent;
[461046f]1307 return node;
1308 }
1309
[5902625]1310 virtual const ast::Init * visit( const ast::ConstructorInit * node ) {
[94b1f718]1311 os << "Constructor initializer: " << endl;
[c957e7f]1312 if ( node->ctor ) {
1313 os << indent << "... initially constructed with ";
1314 ++indent;
1315 node->ctor->accept( *this );
1316 --indent;
1317 }
1318
1319 if ( node->dtor ) {
1320 os << indent << "... destructed with ";
1321 ++indent;
1322 node->dtor->accept( *this );
1323 --indent;
1324 }
1325
1326 if ( node->init ) {
1327 os << indent << "... with fallback C-style initializer: ";
1328 ++indent;
1329 node->init->accept( *this );
1330 --indent;
1331 }
[461046f]1332 return node;
1333 }
1334
[5902625]1335 virtual const ast::Attribute * visit( const ast::Attribute * node ) {
[489bacf]1336 if ( node->empty() ) return node;
1337 os << "Attribute with name: " << node->name;
1338 if ( node->params.empty() ) return node;
[94b1f718]1339 os << " with parameters: " << endl;
[489bacf]1340 ++indent;
1341 printAll( node->params );
1342 --indent;
[461046f]1343 return node;
1344 }
1345
[5902625]1346 virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) {
[94b1f718]1347 os << indent << "Types:" << endl;
[76ed81f]1348 for ( const auto& i : *node ) {
1349 os << indent+1 << i.first << " -> ";
1350 indent += 2;
[20a5977]1351 safe_print( i.second );
[76ed81f]1352 indent -= 2;
[94b1f718]1353 os << endl;
[76ed81f]1354 }
[94b1f718]1355 os << indent << "Non-types:" << endl;
[76ed81f]1356 for ( auto i = node->beginVar(); i != node->endVar(); ++i ) {
1357 os << indent+1 << i->first << " -> ";
1358 indent += 2;
[20a5977]1359 safe_print( i->second );
[76ed81f]1360 indent -= 2;
[94b1f718]1361 os << endl;
[76ed81f]1362 }
[461046f]1363 return node;
1364 }
1365
1366};
1367
[5902625]1368void print( ostream & os, const ast::Node * node, Indenter indent ) {
1369 Printer printer { os, indent, false };
1370 node->accept(printer);
1371}
1372
1373void printShort( ostream & os, const ast::Node * node, Indenter indent ) {
1374 Printer printer { os, indent, true };
[461046f]1375 node->accept(printer);
1376}
1377
1378// Annoyingly these needed to be defined out of line to avoid undefined references.
1379// The size here needs to be explicit but at least the compiler will produce an error
1380// if the wrong size is specified
[5902625]1381constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
1382constexpr array<const char*, 5> Printer::Names::StorageClasses;
1383constexpr array<const char*, 6> Printer::Names::Qualifiers;
[461046f]1384}
Note: See TracBrowser for help on using the repository browser.