source: src/CodeTools/ResolvProtoDump.cc @ bb0f974

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since bb0f974 was bb0f974, checked in by Aaron Moss <a3moss@…>, 6 years ago

Fix bugs in RPDump

  • Property mode set to 100644
File size: 22.4 KB
Line 
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// ResolvProtoDump.cc -- Translates CFA resolver instances into resolv-proto instances
8//
9// Author           : Aaron Moss
10// Created On       : Tue Sep 11 09:04:00 2018
11// Last Modified By : Aaron Moss
12// Last Modified On : Tue Sep 11 09:04:00 2018
13// Update Count     : 1
14//
15
16#include <algorithm>
17#include <cctype>
18#include <iostream>
19#include <memory>
20#include <list>
21#include <set>
22#include <sstream>
23#include <string>
24#include <unordered_set>
25#include <utility>
26#include <vector>
27
28#include "Common/PassVisitor.h"
29#include "Common/utility.h"
30#include "CodeGen/OperatorTable.h"
31#include "SynTree/Declaration.h"
32#include "SynTree/Expression.h"
33#include "SynTree/Initializer.h"
34#include "SynTree/Statement.h"
35#include "SynTree/Type.h"
36
37namespace CodeTools {
38
39        /// Visitor for dumping resolver prototype output
40        class ProtoDump : public WithShortCircuiting, public WithVisitorRef<ProtoDump> {
41                std::set<std::string> decls;             ///< Declarations in this scope
42                std::vector<std::string> exprs;          ///< Expressions in this scope
43                std::vector<ProtoDump> subs;             ///< Sub-scopes
44                std::unordered_set<std::string> closed;  ///< Closed type variables
45                const ProtoDump* parent;                 ///< Outer lexical scope
46                std::unique_ptr<Type> rtnType;           ///< Return type for this scope
47
48        public:
49                /// Default constructor for root ProtoDump
50                ProtoDump() : decls(), exprs(), subs(), closed(), parent(nullptr), rtnType(nullptr) {}
51
52                /// Child constructor
53                ProtoDump(const ProtoDump* p, Type* r) 
54                        : decls(), exprs(), subs(), closed(p->closed), parent(p), rtnType(r) {}
55
56                // Fix copy issues
57                ProtoDump(const ProtoDump& o) 
58                        : decls(o.decls), exprs(o.exprs), subs(o.subs), closed(o.closed), parent(o.parent), 
59                          rtnType(maybeClone(o.rtnType.get())) {}
60                ProtoDump( ProtoDump&& ) = default;
61
62                ProtoDump& operator= (const ProtoDump& o) {
63                        if ( this == &o ) return *this;
64
65                        decls = o.decls;
66                        exprs = o.exprs;
67                        subs = o.subs;
68                        closed = o.closed;
69                        parent = o.parent;
70                        rtnType.reset( maybeClone(o.rtnType.get()) );
71                       
72                        return *this;
73                }
74                ProtoDump& operator= (ProtoDump&&) = default;
75
76        private:
77                /// checks if this declaration is contained in the scope or one of its parents
78                bool hasDecl( const std::string& s ) const {
79                        if ( decls.count( s ) ) return true;
80                        if ( parent ) return parent->hasDecl( s );
81                        return false;
82                }
83
84                /// adds a new declaration to this scope, providing it does not already exist
85                void addDecl( const std::string& s ) {
86                        if ( ! hasDecl( s ) ) decls.insert( s );
87                }
88
89                /// adds a new expression to this scope
90                void addExpr( const std::string& s ) {
91                        if ( ! s.empty() ) { exprs.emplace_back( s ); }
92                }
93
94                /// adds a new subscope to this scope, returning a reference
95                void addSub( PassVisitor<ProtoDump>&& sub ) {
96                        subs.emplace_back( std::move(sub.pass) );
97                }
98       
99                /// Whether lists should be separated, terminated, or preceded by their separator
100                enum septype { separated, terminated, preceded };
101
102                /// builds space-separated list of types
103                template<typename V>
104                static void build( V& visitor, const std::list< Type* >& tys, std::stringstream& ss, 
105                                septype mode = separated ) {
106                        if ( tys.empty() ) return;
107
108                        if ( mode == preceded ) { ss << ' '; }
109
110                        auto it = tys.begin();
111                        (*it)->accept( visitor );
112
113                        while ( ++it != tys.end() ) {
114                                ss << ' ';
115                                (*it)->accept( visitor );
116                        }
117
118                        if ( mode == terminated ) { ss << ' '; }
119                }
120
121                /// builds list of types wrapped as tuple type
122                template<typename V>
123                static void buildAsTuple( V& visitor, const std::list< Type* >& tys, 
124                                std::stringstream& ss ) {
125                        switch ( tys.size() ) {
126                                case 0: ss << "#void"; break;
127                                case 1: tys.front()->accept( visitor ); break;
128                                default:
129                                        ss << "#$" << tys.size() << '<';
130                                        build( visitor, tys, ss );
131                                        ss << '>';
132                                        break;
133                        }
134                }
135
136                /// gets types from DWT list
137                static std::list< Type* > from_decls( const std::list< DeclarationWithType* >& decls ) {
138                        std::list< Type* > tys;
139                        for ( auto decl : decls ) { tys.emplace_back( decl->get_type() ); }
140                        return tys;
141                }
142
143                /// gets types from TypeExpr list
144                static std::list< Type* > from_exprs( const std::list< Expression* >& exprs ) {
145                        std::list< Type* > tys;
146                        for ( auto expr : exprs ) {
147                                if ( TypeExpr* tyExpr = dynamic_cast<TypeExpr*>(expr) ) {
148                                        tys.emplace_back( tyExpr->type );
149                                }
150                        }
151                        return tys;
152                }
153
154                /// builds prefixes for rp_name
155                static std::string new_prefix( const std::string& old, const char* added ) {
156                        if ( old.empty() ) return std::string{"$"} + added;
157                        return old + added;
158                }
159
160                /// shortens operator names
161                static void op_name( const std::string& name, std::stringstream& ss ) {
162                        if ( name.compare( 0, 10, "_operator_" ) == 0 ) {
163                                ss << name.substr(10);
164                        } else if ( name.compare( "_constructor" ) == 0 
165                                        || name.compare( "_destructor" ) == 0 ) {
166                                ss << name.substr(1);
167                        } else if ( name.compare( 0, 11, "__operator_" ) == 0 ) {
168                                ss << name.substr(11);
169                        } else {
170                                ss << name;
171                        }
172                }
173
174                /// replaces operators with resolv-proto names
175                static void rp_name( const std::string& name, std::stringstream& ss, 
176                                std::string&& pre = "" ) {
177                        // safety check for anonymous names
178                        if ( name.empty() ) {
179                                ss << new_prefix(pre, "anon");
180                                return;
181                        }
182
183                        // replace operator names
184                        CodeGen::OperatorInfo info;
185                        if ( CodeGen::operatorLookup( name, info ) ) {
186                                ss << new_prefix(pre, "");
187                                op_name( info.outputName, ss );
188                                return;
189                        } 
190                       
191                        // replace retval names
192                        if ( name.compare( 0, 8, "_retval_" ) == 0 ) {
193                                ss << new_prefix(pre, "rtn_");
194                                op_name( name.substr(8), ss );
195                                return;
196                        }
197                       
198                        // default to just name, with first character in lowercase
199                        ss << pre
200                           << (char)std::tolower( static_cast<unsigned char>(name[0]) )
201                           << (name.c_str() + 1);
202                }
203
204                /// ensures type inst names are uppercase
205                static void ti_name( const std::string& name, std::stringstream& ss ) {
206                        // replace built-in wide character types with named types
207                        if ( name == "char16_t" || name == "char32_t" || name == "wchar_t" ) {
208                                ss << "#" << name;
209                                return;
210                        }
211
212                        // strip leading underscore
213                        unsigned i = 0;
214                        while ( i < name.size() && name[i] == '_' ) { ++i; }
215                        if ( i == name.size() ) {
216                                ss << "Anon";
217                                return;
218                        }
219
220                        std::string stripped = name.substr(i);
221                        // strip trailing "_generic_" from autogen names (avoids some user-generation issues)
222                        char generic[] = "_generic_"; size_t n_generic = sizeof(generic) - 1;
223                        if ( stripped.size() >= n_generic
224                                        && stripped.substr( stripped.size() - n_generic ) == generic ) {
225                                stripped.resize( stripped.size() - n_generic );
226                        }
227
228                        // uppercase first character
229                        ss << (char)std::toupper( static_cast<unsigned char>(stripped[0]) )
230                           << (stripped.c_str() + 1);
231                }
232
233                /// Visitor for printing types
234                struct TypePrinter : public WithShortCircuiting, WithVisitorRef<TypePrinter>, WithGuards {
235                        std::stringstream& ss;                          ///< Output to print to
236                        const std::unordered_set<std::string>& closed;  ///< Closed type variables
237                        unsigned depth;                                 ///< Depth of nesting from root type
238
239                        TypePrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss ) 
240                                : ss(ss), closed(closed), depth(0) {}
241
242                        // basic type represented as integer type
243                        // TODO maybe hard-code conversion graph and make named type
244                        void previsit( BasicType* bt ) { ss << (int)bt->get_kind(); }
245
246                        // pointers (except function pointers) represented as generic type
247                        void previsit( PointerType* pt ) {
248                                if ( ! dynamic_cast<FunctionType*>(pt->base) ) { ss << "#$ptr<"; ++depth; }
249                        }
250                        void postvisit( PointerType* pt ) {
251                                if ( ! dynamic_cast<FunctionType*>(pt->base) ) { --depth; ss << '>'; }
252                        }
253
254                        // arrays represented as generic pointers
255                        void previsit( ArrayType* at ) {
256                                ss << "#$ptr<";
257                                ++depth;
258                                at->base->accept( *visitor );
259                                --depth;
260                                ss << '>';
261                                visit_children = false;
262                        }
263
264                        // ignore top-level reference types, they're mostly transparent to resolution
265                        void previsit( ReferenceType* ) {
266                                if ( depth > 0 ) { ss << "#$ref<"; }
267                                ++depth;
268                        }
269                        void postvisit( ReferenceType* ) {
270                                --depth;
271                                if ( depth > 0 ) { ss << '>'; }
272                        }
273
274                        // print function types using prototype syntax
275                        void previsit( FunctionType* ft ) {
276                                ss << '[';
277                                ++depth;
278                                build( *visitor, from_decls( ft->returnVals ), ss, preceded );
279                                ss << " : ";
280                                build( *visitor, from_decls( ft->parameters ), ss, terminated );
281                                --depth;
282                                ss << ']';
283                                visit_children = false;
284                        }
285
286                private:
287                        // prints aggregate type name as NamedType with optional paramters
288                        void handleAggregate( ReferenceToType* at ) {
289                                ss << '#' << at->name;
290                                if ( ! at->parameters.empty() ) {
291                                        ss << '<';
292                                        ++depth;
293                                        build( *visitor, from_exprs( at->parameters ), ss );
294                                        --depth;
295                                        ss << '>';
296                                }
297                                visit_children = false;
298                        }
299
300                public:
301                        // handle aggregate types using NamedType
302                        void previsit( StructInstType* st ) { handleAggregate( st ); }
303                        void previsit( UnionInstType* ut ) { handleAggregate( ut ); }
304
305                        // replace enums with int
306                        void previsit( EnumInstType* ) { ss << (int)BasicType::SignedInt; }
307
308                        void previsit( TypeInstType* vt ) {
309                                // print closed variables as named types
310                                if ( closed.count( vt->name ) ) { ss << '#' << vt->name; }
311                                // otherwise make sure first letter is capitalized
312                                else { ti_name( vt->name, ss ); }
313                        }
314
315                        // flattens empty and singleton tuples
316                        void previsit( TupleType* tt ) {
317                                ++depth;
318                                buildAsTuple( *visitor, tt->types, ss );
319                                --depth;
320                                visit_children = false;
321                        }
322
323                        // TODO support VarArgsType
324
325                        // replace 0 and 1 with int
326                        // TODO support 0 and 1 with their proper type names and conversions
327                        void previsit( ZeroType* ) { ss << (int)BasicType::SignedInt; }
328                        void previsit( OneType* ) { ss << (int)BasicType::SignedInt; }
329
330                        // only print void type if not at top level
331                        void previsit( VoidType* ) {
332                                if ( depth > 0 ) { ss << "#void"; }
333                        }
334                };
335       
336                /// builds description of function
337                void build( const std::string& name, FunctionType* fnTy, std::stringstream& ss ) {
338                        PassVisitor<TypePrinter> printTy{ closed, ss };
339                        // print return values
340                        build( printTy, from_decls( fnTy->returnVals ), ss, terminated );
341                        // print name
342                        rp_name( name, ss );
343                        // print parameters
344                        build( printTy, from_decls( fnTy->parameters ), ss, preceded );
345                        // print assertions
346                        for ( TypeDecl* tyvar : fnTy->forall ) {
347                                for ( DeclarationWithType* assn : tyvar->assertions ) {
348                                        ss << " | "; 
349                                        build( assn->name, assn->get_type(), ss );
350                                }
351                        }
352                }
353
354                /// builds description of a variable (falls back to function if function type)
355                void build( const std::string& name, Type* ty, std::stringstream& ss ) {
356                        // ignore top-level references
357                        Type *norefs = ty->stripReferences();
358                       
359                        // fall back to function declaration if function type
360                        if ( PointerType* pTy = dynamic_cast< PointerType* >(norefs) ) {
361                                if ( FunctionType* fnTy = dynamic_cast< FunctionType* >(pTy->base) ) {
362                                        build( name, fnTy, ss );
363                                        return;
364                                }
365                        } else if ( FunctionType* fnTy = dynamic_cast< FunctionType* >(norefs) ) {
366                                build( name, fnTy, ss );
367                                return;
368                        }
369
370                        // print variable declaration in prototype syntax
371                        PassVisitor<TypePrinter> printTy{ closed, ss };
372                        norefs->accept( printTy );
373                        ss << " &";
374                        rp_name( name, ss );
375                }
376
377                /// builds description of a field access
378                void build( const std::string& name, AggregateDecl* agg, Type* ty, std::stringstream& ss ) {
379                        // ignore top-level references
380                        Type *norefs = ty->stripReferences();
381
382                        // print access as new field name
383                        PassVisitor<TypePrinter> printTy{ closed, ss };
384                        norefs->accept( printTy );
385                        ss << ' ';
386                        rp_name( name, ss, "$field_" );
387                        ss << " #" << agg->name;
388                        // handle type parameters
389                        if ( ! agg->parameters.empty() ) {
390                                ss << '<';
391                                auto it = agg->parameters.begin();
392                                while (true) {
393                                        ti_name( (*it)->name, ss );
394                                        if ( ++it == agg->parameters.end() ) break;
395                                        ss << ' ';
396                                }
397                                ss << '>';
398                        }
399                }
400
401                /// Visitor for printing expressions
402                struct ExprPrinter : WithShortCircuiting, WithVisitorRef<ExprPrinter> {
403                        // TODO change interface to generate multiple expression candidates
404                        const std::unordered_set<std::string>& closed;  ///< set of closed type vars
405                        std::stringstream& ss;                          ///< Output to print to
406
407                        ExprPrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss ) 
408                                : closed(closed), ss(ss) {}
409
410                        /// Names handled as name expressions
411                        void previsit( NameExpr* expr ) {
412                                ss << '&';
413                                rp_name( expr->name, ss );
414                        }
415
416                        /// Handle already-resolved variables as type constants
417                        void previsit( VariableExpr* expr ) {
418                                PassVisitor<TypePrinter> tyPrinter{ closed, ss };
419                                expr->var->get_type()->accept( tyPrinter );
420                                visit_children = false;
421                        }
422
423                        /// Calls handled as calls
424                        void previsit( UntypedExpr* expr ) {
425                                // TODO handle name extraction more generally
426                                NameExpr* name = dynamic_cast<NameExpr*>(expr->function);
427
428                                // fall back on just resolving call to function name
429                                // TODO incorporate function type into resolv-proto
430                                if ( ! name ) {
431                                        expr->function->accept( *visitor );
432                                        visit_children = false;
433                                        return;
434                                }
435
436                                rp_name( name->name, ss );
437                                if ( expr->args.empty() ) {
438                                        ss << "()";
439                                } else {
440                                        ss << "( ";
441                                        auto it = expr->args.begin();
442                                        while (true) {
443                                                (*it)->accept( *visitor );
444                                                if ( ++it == expr->args.end() ) break;
445                                                ss << ' ';
446                                        }
447                                        ss << " )";
448                                }
449                                visit_children = false;
450                        }
451
452                        /// Already-resolved calls reduced to their type constant
453                        void previsit( ApplicationExpr* expr ) {
454                                PassVisitor<TypePrinter> tyPrinter{ closed, ss };
455                                expr->result->accept( tyPrinter );
456                                visit_children = false;
457                        }
458
459                        /// Address-of handled as operator
460                        void previsit( AddressExpr* expr ) {
461                                ss << "$addr( ";
462                                expr->arg->accept( *visitor );
463                                ss << " )";
464                                visit_children = false;
465                        }
466
467                        /// Casts replaced with result type
468                        /// TODO put cast target functions in, and add second expression for target
469                        void previsit( CastExpr* cast ) {
470                                PassVisitor<TypePrinter> tyPrinter{ closed, ss };
471                                cast->result->accept( tyPrinter );
472                                visit_children = false;
473                        }
474                       
475                        /// Member access handled as function from aggregate to member
476                        void previsit( UntypedMemberExpr* expr ) {
477                                // TODO handle name extraction more generally
478                                NameExpr* name = dynamic_cast<NameExpr*>(expr->member);
479
480                                // fall back on just resolving call to member name
481                                // TODO incorporate function type into resolv-proto
482                                if ( ! name ) {
483                                        expr->member->accept( *visitor );
484                                        visit_children = false;
485                                        return;
486                                }
487
488                                rp_name( name->name, ss, "$field_" );
489                                ss << "( ";
490                                expr->aggregate->accept( *visitor );
491                                ss << " )";
492                                visit_children = false;
493                        }
494
495                        /// Constant expression replaced by its type
496                        void previsit( ConstantExpr* expr ) {
497                                PassVisitor<TypePrinter> tyPrinter{ closed, ss };
498                                expr->constant.get_type()->accept( tyPrinter );
499                                visit_children = false;
500                        }
501
502                        /// sizeof( ... ), alignof( ... ), offsetof( ... ) replaced by unsigned long constant
503                        /// TODO extra expression to resolve argument
504                        void previsit( SizeofExpr* ) {
505                                ss << (int)BasicType::LongUnsignedInt;
506                                visit_children = false;
507                        }
508                        void previsit( AlignofExpr* ) {
509                                ss << (int)BasicType::LongUnsignedInt;
510                                visit_children = false;
511                        }
512                        void previsit( UntypedOffsetofExpr* ) {
513                                ss << (int)BasicType::LongUnsignedInt;
514                                visit_children = false;
515                        }
516
517                        /// Logical expressions represented as operators
518                        void previsit( LogicalExpr* expr ) {
519                                ss << '$' << ( expr->get_isAnd() ? "and" : "or" ) << "( ";
520                                expr->arg1->accept( *visitor );
521                                ss << ' ';
522                                expr->arg2->accept( *visitor );
523                                ss << " )";
524                                visit_children = false;
525                        }
526
527                        /// Conditional expression represented as operator
528                        void previsit( ConditionalExpr* expr ) {
529                                ss << "$if( ";
530                                expr->arg1->accept( *visitor );
531                                ss << ' ';
532                                expr->arg2->accept( *visitor );
533                                ss << ' ';
534                                expr->arg3->accept( *visitor );
535                                ss << " )";
536                                visit_children = false;
537                        }
538
539                        /// Comma expression represented as operator
540                        void previsit( CommaExpr* expr ) {
541                                ss << "$seq( ";
542                                expr->arg1->accept( *visitor );
543                                ss << ' ';
544                                expr->arg2->accept( *visitor );
545                                ss << " )";
546                                visit_children = false;
547                        }
548
549                        // TODO handle ignored ImplicitCopyCtorExpr and below
550                };
551
552                void build( Initializer* init, std::stringstream& ss ) {
553                        if ( SingleInit* si = dynamic_cast<SingleInit*>(init) ) {
554                                PassVisitor<ExprPrinter> exprPrinter{ closed, ss };
555                                si->value->accept( exprPrinter );
556                                ss << ' ';
557                        } else if ( ListInit* li = dynamic_cast<ListInit*>(init) ) {
558                                for ( Initializer* it : li->initializers ) {
559                                        build( it, ss );
560                                }
561                        }
562                }
563
564                /// Adds an object initializer to the list of expressions
565                void build( const std::string& name, Initializer* init, std::stringstream& ss ) {
566                        ss << "$constructor( &";
567                        rp_name( name, ss );
568                        ss << ' ';
569                        build( init, ss );
570                        ss << ')';
571                }
572
573                /// Adds a return expression to the list of expressions
574                void build( Type* rtnType, Expression* expr, std::stringstream& ss ) {
575                        ss << "$constructor( ";
576                        PassVisitor<TypePrinter> tyPrinter{ closed, ss };
577                        rtnType->accept( tyPrinter );
578                        ss << ' ';
579                        PassVisitor<ExprPrinter> exprPrinter{ closed, ss };
580                        expr->accept( exprPrinter );
581                        ss << " )";
582                }
583
584                /// Adds all named declarations in a list to the local scope
585                void addAll( const std::list<DeclarationWithType*>& decls ) {
586                        for ( auto decl : decls ) {
587                                // skip anonymous decls
588                                if ( decl->name.empty() ) continue;
589
590                                // handle objects
591                                if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >( decl ) ) {
592                                        previsit( obj );
593                                }
594                        }
595                }
596
597                /// encode field access as function
598                void addAggregateFields( AggregateDecl* agg ) {
599                        // make field names functions
600                        for ( Declaration* member : agg->members ) {
601                                if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >(member) ) {
602                                        std::stringstream ss;
603                                        build( obj->name, agg, obj->type, ss );
604                                        addDecl( ss.str() );
605                                }
606                        }
607
608                        visit_children = false;
609                }
610
611        public:
612                void previsit( ObjectDecl *obj ) {
613                        // add variable as declaration
614                        std::stringstream ss;
615                        build( obj->name, obj->type, ss );
616                        addDecl( ss.str() );
617
618                        // add initializer as expression if applicable
619                        if ( obj->init ) {
620                                std::stringstream ss;
621                                build( obj->name, obj->init, ss );
622                                addExpr( ss.str() );
623                        }
624                }
625
626                void previsit( FunctionDecl *decl ) {
627                        // skip decls with ftype parameters
628                        for ( TypeDecl* tyvar : decl->type->forall ) {
629                                if ( tyvar->get_kind() == TypeDecl::Ftype ) {
630                                        visit_children = false;
631                                        return;
632                                }
633                        }
634
635                        // add function as declaration
636                        std::stringstream ss;
637                        build( decl->name, decl->type, ss );
638                        addDecl( ss.str() );
639
640                        // add body if available
641                        if ( decl->statements ) {
642                                std::list<Type*> rtns = from_decls( decl->type->returnVals );
643                                Type* rtn = nullptr;
644                                if ( rtns.size() == 1 ) {
645                                        if ( ! dynamic_cast<VoidType*>(rtns.front()) ) rtn = rtns.front()->clone();
646                                } else if ( rtns.size() > 1 ) {
647                                        rtn = new TupleType{ Type::Qualifiers{}, rtns };
648                                }
649                                PassVisitor<ProtoDump> body{ this, rtn };
650
651                                for ( TypeDecl* tyvar : decl->type->forall ) {
652                                        // add set of "closed" types to body so that it can print them as NamedType
653                                        body.pass.closed.insert( tyvar->name );
654
655                                        // add assertions to local scope as declarations as well
656                                        for ( DeclarationWithType* assn : tyvar->assertions ) {
657                                                assn->accept( body );
658                                        }
659                                }
660                               
661                                // add named parameters and returns to local scope
662                                body.pass.addAll( decl->type->returnVals );
663                                body.pass.addAll( decl->type->parameters );
664
665                                // add contents of function to new scope
666                                decl->statements->accept( body );
667
668                                // store sub-scope
669                                addSub( std::move(body) );
670                        }
671
672                        visit_children = false;
673                }
674
675                void previsit( StructDecl* sd ) { addAggregateFields(sd); }
676                void previsit( UnionDecl* ud ) { addAggregateFields(ud); }
677               
678                void previsit( EnumDecl* ed ) {
679                        std::unique_ptr<Type> eType = 
680                                std::make_unique<BasicType>( Type::Qualifiers{}, BasicType::SignedInt );
681                       
682                        // add field names directly to enclosing scope
683                        for ( Declaration* member : ed->members ) {
684                                if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >(member) ) {
685                                        previsit(obj);
686                                }
687                        }
688
689                        visit_children = false;
690                }
691
692                void previsit( ReturnStmt* stmt ) {
693                        // do nothing for void-returning functions or statements returning nothing
694                        if ( ! rtnType || ! stmt->expr ) return;
695
696                        // otherwise construct the return type from the expression
697                        std::stringstream ss;
698                        build( rtnType.get(), stmt->expr, ss );
699                        addExpr( ss.str() );
700                        visit_children = false;
701                }
702
703                void previsit( AsmStmt* ) {
704                        // skip asm statements
705                        visit_children = false;
706                }
707
708                void previsit( Expression* expr ) {
709                        std::stringstream ss;
710                        PassVisitor<ExprPrinter> exPrinter{ closed, ss };
711                        expr->accept( exPrinter );
712                        addExpr( ss.str() );
713                        visit_children = false;
714                }
715
716                /// Print non-prelude global declarations for resolv proto
717                void printGlobals() const {
718                        std::cout << "#$ptr<T> $addr T" << std::endl;  // &?
719                        int i = (int)BasicType::SignedInt;
720                        std::cout << i << " $and " << i << ' ' << i << std::endl;  // ?&&?
721                        std::cout << i << " $or " << i << ' ' << i << std::endl;  // ?||?
722                        std::cout << "T $if " << i << " T T" << std::endl; // ternary operator
723                        std::cout << "T $seq X T" << std::endl;  // ?,?
724                }
725
726        public:
727                /// Prints this ProtoDump instance
728                void print(unsigned indent = 0) const {
729                        // print globals at root level
730                        if ( ! parent ) printGlobals();
731                        // print decls
732                        std::string tab( indent, '\t' );
733                        for ( const std::string& d : decls ) {
734                                std::cout << tab << d << std::endl;
735                        }
736                        // print divider
737                        std::cout << '\n' << tab << "%%\n" << std::endl;
738                        // print top-level expressions
739                        for ( const std::string& e : exprs ) {
740                                std::cout << tab << e << std::endl;
741                        }
742                        // print child scopes
743                        ++indent;
744                        for ( const PassVisitor<ProtoDump>& s : subs ) {
745                                std::cout << tab << '{' << std::endl;
746                                s.pass.print( indent );
747                                std::cout << tab << '}' << std::endl;
748                        }
749                }
750        };
751
752        void dumpAsResolvProto( std::list< Declaration * > &translationUnit ) {
753                PassVisitor<ProtoDump> dump;
754                acceptAll( translationUnit, dump );
755                dump.pass.print();
756        }
757
758}  // namespace CodeTools
759
760// Local Variables: //
761// tab-width: 4 //
762// mode: c++ //
763// compile-command: "make install" //
764// End: //
Note: See TracBrowser for help on using the repository browser.