[3b3491b6] | 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>
|
---|
[40cd873] | 25 | #include <utility>
|
---|
[3b3491b6] | 26 | #include <vector>
|
---|
| 27 |
|
---|
| 28 | #include "Common/PassVisitor.h"
|
---|
[4d59ff9] | 29 | #include "Common/utility.h"
|
---|
[3b3491b6] | 30 | #include "CodeGen/OperatorTable.h"
|
---|
| 31 | #include "SynTree/Declaration.h"
|
---|
| 32 | #include "SynTree/Expression.h"
|
---|
[4d59ff9] | 33 | #include "SynTree/Initializer.h"
|
---|
[3b3491b6] | 34 | #include "SynTree/Statement.h"
|
---|
| 35 | #include "SynTree/Type.h"
|
---|
| 36 |
|
---|
| 37 | namespace CodeTools {
|
---|
| 38 |
|
---|
| 39 | /// Visitor for dumping resolver prototype output
|
---|
| 40 | class ProtoDump : public WithShortCircuiting, public WithVisitorRef<ProtoDump> {
|
---|
[a4a000d] | 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
|
---|
[3b3491b6] | 47 |
|
---|
| 48 | public:
|
---|
| 49 | /// Default constructor for root ProtoDump
|
---|
[a4a000d] | 50 | ProtoDump() : decls(), exprs(), subs(), closed(), parent(nullptr), rtnType(nullptr) {}
|
---|
[3b3491b6] | 51 |
|
---|
| 52 | /// Child constructor
|
---|
[a4a000d] | 53 | ProtoDump(const ProtoDump* p, Type* r)
|
---|
| 54 | : decls(), exprs(), subs(), closed(p->closed), parent(p), rtnType(r) {}
|
---|
[4d59ff9] | 55 |
|
---|
| 56 | // Fix copy issues
|
---|
[a4a000d] | 57 | ProtoDump(const ProtoDump& o)
|
---|
| 58 | : decls(o.decls), exprs(o.exprs), subs(o.subs), closed(o.closed), parent(o.parent),
|
---|
[4d59ff9] | 59 | rtnType(maybeClone(o.rtnType.get())) {}
|
---|
| 60 | ProtoDump( ProtoDump&& ) = default;
|
---|
| 61 |
|
---|
[a4a000d] | 62 | ProtoDump& operator= (const ProtoDump& o) {
|
---|
[4d59ff9] | 63 | if ( this == &o ) return *this;
|
---|
| 64 |
|
---|
| 65 | decls = o.decls;
|
---|
| 66 | exprs = o.exprs;
|
---|
| 67 | subs = o.subs;
|
---|
[a4a000d] | 68 | closed = o.closed;
|
---|
[4d59ff9] | 69 | parent = o.parent;
|
---|
| 70 | rtnType.reset( maybeClone(o.rtnType.get()) );
|
---|
| 71 |
|
---|
| 72 | return *this;
|
---|
| 73 | }
|
---|
[a4a000d] | 74 | ProtoDump& operator= (ProtoDump&&) = default;
|
---|
[3b3491b6] | 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 ) {
|
---|
[b181639] | 91 | if ( ! s.empty() ) { exprs.emplace_back( s ); }
|
---|
[3b3491b6] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /// adds a new subscope to this scope, returning a reference
|
---|
[40cd873] | 95 | void addSub( PassVisitor<ProtoDump>&& sub ) {
|
---|
| 96 | subs.emplace_back( std::move(sub.pass) );
|
---|
[3b3491b6] | 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 |
|
---|
[4c42a5f] | 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);
|
---|
[3b3491b6] | 202 | }
|
---|
| 203 |
|
---|
| 204 | /// ensures type inst names are uppercase
|
---|
| 205 | static void ti_name( const std::string& name, std::stringstream& ss ) {
|
---|
[4c42a5f] | 206 | unsigned i = 0;
|
---|
| 207 | while ( i < name.size() && name[i] == '_' ) { ++i; }
|
---|
| 208 | if ( i == name.size() ) {
|
---|
| 209 | ss << "Anon";
|
---|
| 210 | return;
|
---|
| 211 | }
|
---|
| 212 | ss << (char)std::toupper( static_cast<unsigned char>(name[i]) )
|
---|
| 213 | << (name.c_str() + i + 1);
|
---|
[3b3491b6] | 214 | }
|
---|
| 215 |
|
---|
| 216 | /// Visitor for printing types
|
---|
| 217 | struct TypePrinter : public WithShortCircuiting, WithVisitorRef<TypePrinter>, WithGuards {
|
---|
[a4a000d] | 218 | std::stringstream& ss; ///< Output to print to
|
---|
| 219 | const std::unordered_set<std::string>& closed; ///< Closed type variables
|
---|
| 220 | unsigned depth; ///< Depth of nesting from root type
|
---|
[3b3491b6] | 221 |
|
---|
[a4a000d] | 222 | TypePrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss )
|
---|
| 223 | : ss(ss), closed(closed), depth(0) {}
|
---|
[3b3491b6] | 224 |
|
---|
| 225 | // basic type represented as integer type
|
---|
| 226 | // TODO maybe hard-code conversion graph and make named type
|
---|
| 227 | void previsit( BasicType* bt ) { ss << (int)bt->get_kind(); }
|
---|
| 228 |
|
---|
[4c42a5f] | 229 | // pointers (except function pointers) represented as generic type
|
---|
| 230 | void previsit( PointerType* pt ) {
|
---|
| 231 | if ( ! dynamic_cast<FunctionType*>(pt->base) ) { ss << "#$ptr<"; ++depth; }
|
---|
| 232 | }
|
---|
| 233 | void postvisit( PointerType* pt ) {
|
---|
| 234 | if ( ! dynamic_cast<FunctionType*>(pt->base) ) { --depth; ss << '>'; }
|
---|
| 235 | }
|
---|
[3b3491b6] | 236 |
|
---|
[4c42a5f] | 237 | // arrays represented as generic pointers
|
---|
[b181639] | 238 | void previsit( ArrayType* at ) {
|
---|
[4c42a5f] | 239 | ss << "#$ptr<";
|
---|
[b181639] | 240 | ++depth;
|
---|
| 241 | at->base->accept( *visitor );
|
---|
| 242 | --depth;
|
---|
| 243 | ss << '>';
|
---|
| 244 | visit_children = false;
|
---|
| 245 | }
|
---|
[3b3491b6] | 246 |
|
---|
| 247 | // ignore top-level reference types, they're mostly transparent to resolution
|
---|
| 248 | void previsit( ReferenceType* ) {
|
---|
| 249 | if ( depth > 0 ) { ss << "#$ref<"; }
|
---|
| 250 | ++depth;
|
---|
| 251 | }
|
---|
| 252 | void postvisit( ReferenceType* ) {
|
---|
| 253 | --depth;
|
---|
| 254 | if ( depth > 0 ) { ss << '>'; }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[4c42a5f] | 257 | // print function types using prototype syntax
|
---|
[3b3491b6] | 258 | void previsit( FunctionType* ft ) {
|
---|
[4c42a5f] | 259 | ss << '[';
|
---|
[3b3491b6] | 260 | ++depth;
|
---|
[4c42a5f] | 261 | build( *visitor, from_decls( ft->returnVals ), ss, preceded );
|
---|
| 262 | ss << " : ";
|
---|
| 263 | build( *visitor, from_decls( ft->parameters ), ss, terminated );
|
---|
[3b3491b6] | 264 | --depth;
|
---|
[4c42a5f] | 265 | ss << ']';
|
---|
[3b3491b6] | 266 | visit_children = false;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private:
|
---|
| 270 | // prints aggregate type name as NamedType with optional paramters
|
---|
| 271 | void handleAggregate( ReferenceToType* at ) {
|
---|
| 272 | ss << '#' << at->name;
|
---|
| 273 | if ( ! at->parameters.empty() ) {
|
---|
| 274 | ss << '<';
|
---|
| 275 | ++depth;
|
---|
| 276 | build( *visitor, from_exprs( at->parameters ), ss );
|
---|
| 277 | --depth;
|
---|
| 278 | ss << '>';
|
---|
| 279 | }
|
---|
| 280 | visit_children = false;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | public:
|
---|
| 284 | // handle aggregate types using NamedType
|
---|
| 285 | void previsit( StructInstType* st ) { handleAggregate( st ); }
|
---|
| 286 | void previsit( UnionInstType* ut ) { handleAggregate( ut ); }
|
---|
| 287 |
|
---|
| 288 | // replace enums with int
|
---|
| 289 | void previsit( EnumInstType* ) { ss << (int)BasicType::SignedInt; }
|
---|
| 290 |
|
---|
| 291 | void previsit( TypeInstType* vt ) {
|
---|
[a4a000d] | 292 | // print closed variables as named types
|
---|
| 293 | if ( closed.count( vt->name ) ) { ss << '#' << vt->name; }
|
---|
| 294 | // otherwise make sure first letter is capitalized
|
---|
| 295 | else { ti_name( vt->name, ss ); }
|
---|
[3b3491b6] | 296 | }
|
---|
| 297 |
|
---|
| 298 | // flattens empty and singleton tuples
|
---|
| 299 | void previsit( TupleType* tt ) {
|
---|
| 300 | ++depth;
|
---|
| 301 | buildAsTuple( *visitor, tt->types, ss );
|
---|
| 302 | --depth;
|
---|
| 303 | visit_children = false;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | // TODO support VarArgsType
|
---|
| 307 |
|
---|
| 308 | // replace 0 and 1 with int
|
---|
| 309 | // TODO support 0 and 1 with their proper type names and conversions
|
---|
| 310 | void previsit( ZeroType* ) { ss << (int)BasicType::SignedInt; }
|
---|
| 311 | void previsit( OneType* ) { ss << (int)BasicType::SignedInt; }
|
---|
| 312 |
|
---|
| 313 | // only print void type if not at top level
|
---|
| 314 | void previsit( VoidType* ) {
|
---|
| 315 | if ( depth > 0 ) { ss << "#void"; }
|
---|
| 316 | }
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | /// builds description of function
|
---|
| 320 | void build( const std::string& name, FunctionType* fnTy, std::stringstream& ss ) {
|
---|
[a4a000d] | 321 | PassVisitor<TypePrinter> printTy{ closed, ss };
|
---|
| 322 | // print return values
|
---|
[3b3491b6] | 323 | build( printTy, from_decls( fnTy->returnVals ), ss, terminated );
|
---|
[a4a000d] | 324 | // print name
|
---|
[3b3491b6] | 325 | rp_name( name, ss );
|
---|
[a4a000d] | 326 | // print parameters
|
---|
[3b3491b6] | 327 | build( printTy, from_decls( fnTy->parameters ), ss, preceded );
|
---|
[a4a000d] | 328 | // print assertions
|
---|
| 329 | for ( TypeDecl* tyvar : fnTy->forall ) {
|
---|
| 330 | for ( DeclarationWithType* assn : tyvar->assertions ) {
|
---|
| 331 | ss << " | ";
|
---|
| 332 | build( assn->name, assn->get_type(), ss );
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
[3b3491b6] | 335 | }
|
---|
| 336 |
|
---|
| 337 | /// builds description of a variable (falls back to function if function type)
|
---|
| 338 | void build( const std::string& name, Type* ty, std::stringstream& ss ) {
|
---|
| 339 | // ignore top-level references
|
---|
| 340 | Type *norefs = ty->stripReferences();
|
---|
| 341 |
|
---|
| 342 | // fall back to function declaration if function type
|
---|
| 343 | if ( PointerType* pTy = dynamic_cast< PointerType* >(norefs) ) {
|
---|
| 344 | if ( FunctionType* fnTy = dynamic_cast< FunctionType* >(pTy->base) ) {
|
---|
| 345 | build( name, fnTy, ss );
|
---|
| 346 | return;
|
---|
| 347 | }
|
---|
| 348 | } else if ( FunctionType* fnTy = dynamic_cast< FunctionType* >(norefs) ) {
|
---|
| 349 | build( name, fnTy, ss );
|
---|
| 350 | return;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[4c42a5f] | 353 | // print variable declaration in prototype syntax
|
---|
[a4a000d] | 354 | PassVisitor<TypePrinter> printTy{ closed, ss };
|
---|
[3b3491b6] | 355 | norefs->accept( printTy );
|
---|
[4c42a5f] | 356 | ss << " &";
|
---|
[3b3491b6] | 357 | rp_name( name, ss );
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /// builds description of a field access
|
---|
| 361 | void build( const std::string& name, AggregateDecl* agg, Type* ty, std::stringstream& ss ) {
|
---|
| 362 | // ignore top-level references
|
---|
| 363 | Type *norefs = ty->stripReferences();
|
---|
| 364 |
|
---|
| 365 | // print access as new field name
|
---|
[a4a000d] | 366 | PassVisitor<TypePrinter> printTy{ closed, ss };
|
---|
[3b3491b6] | 367 | norefs->accept( printTy );
|
---|
| 368 | ss << ' ';
|
---|
| 369 | rp_name( name, ss, "$field_" );
|
---|
| 370 | ss << " #" << agg->name;
|
---|
| 371 | // handle type parameters
|
---|
| 372 | if ( ! agg->parameters.empty() ) {
|
---|
| 373 | ss << '<';
|
---|
| 374 | auto it = agg->parameters.begin();
|
---|
| 375 | while (true) {
|
---|
| 376 | ti_name( (*it)->name, ss );
|
---|
| 377 | if ( ++it == agg->parameters.end() ) break;
|
---|
| 378 | ss << ' ';
|
---|
| 379 | }
|
---|
| 380 | ss << '>';
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /// Visitor for printing expressions
|
---|
| 385 | struct ExprPrinter : WithShortCircuiting, WithVisitorRef<ExprPrinter> {
|
---|
| 386 | // TODO change interface to generate multiple expression candidates
|
---|
[a4a000d] | 387 | const std::unordered_set<std::string>& closed; ///< set of closed type vars
|
---|
| 388 | std::stringstream& ss; ///< Output to print to
|
---|
[3b3491b6] | 389 |
|
---|
[a4a000d] | 390 | ExprPrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss )
|
---|
| 391 | : closed(closed), ss(ss) {}
|
---|
[3b3491b6] | 392 |
|
---|
[4c42a5f] | 393 | /// Names handled as name expressions
|
---|
[3b3491b6] | 394 | void previsit( NameExpr* expr ) {
|
---|
[4c42a5f] | 395 | ss << '&';
|
---|
[3b3491b6] | 396 | rp_name( expr->name, ss );
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /// Calls handled as calls
|
---|
| 400 | void previsit( UntypedExpr* expr ) {
|
---|
| 401 | // TODO handle name extraction more generally
|
---|
| 402 | NameExpr* name = dynamic_cast<NameExpr*>(expr->function);
|
---|
| 403 |
|
---|
| 404 | // fall back on just resolving call to function name
|
---|
| 405 | // TODO incorporate function type into resolv-proto
|
---|
| 406 | if ( ! name ) {
|
---|
| 407 | expr->function->accept( *visitor );
|
---|
| 408 | visit_children = false;
|
---|
| 409 | return;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | rp_name( name->name, ss );
|
---|
| 413 | if ( expr->args.empty() ) {
|
---|
| 414 | ss << "()";
|
---|
| 415 | } else {
|
---|
| 416 | ss << "( ";
|
---|
| 417 | auto it = expr->args.begin();
|
---|
| 418 | while (true) {
|
---|
| 419 | (*it)->accept( *visitor );
|
---|
| 420 | if ( ++it == expr->args.end() ) break;
|
---|
| 421 | ss << ' ';
|
---|
| 422 | }
|
---|
| 423 | ss << " )";
|
---|
| 424 | }
|
---|
| 425 | visit_children = false;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[4c42a5f] | 428 | /// Already-resolved calls skipped
|
---|
| 429 | void previsit( ApplicationExpr* ) {
|
---|
| 430 | visit_children = false;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[3b3491b6] | 433 | /// Address-of handled as operator
|
---|
| 434 | void previsit( AddressExpr* expr ) {
|
---|
| 435 | ss << "$addr( ";
|
---|
| 436 | expr->arg->accept( *visitor );
|
---|
| 437 | ss << " )";
|
---|
| 438 | visit_children = false;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[b181639] | 441 | /// Casts replaced with result type
|
---|
| 442 | /// TODO put cast target functions in, and add second expression for target
|
---|
| 443 | void previsit( CastExpr* cast ) {
|
---|
[a4a000d] | 444 | PassVisitor<TypePrinter> tyPrinter{ closed, ss };
|
---|
[b181639] | 445 | cast->result->accept( tyPrinter );
|
---|
| 446 | visit_children = false;
|
---|
| 447 | }
|
---|
[3b3491b6] | 448 |
|
---|
| 449 | /// Member access handled as function from aggregate to member
|
---|
| 450 | void previsit( UntypedMemberExpr* expr ) {
|
---|
| 451 | // TODO handle name extraction more generally
|
---|
| 452 | NameExpr* name = dynamic_cast<NameExpr*>(expr->member);
|
---|
| 453 |
|
---|
| 454 | // fall back on just resolving call to member name
|
---|
| 455 | // TODO incorporate function type into resolv-proto
|
---|
| 456 | if ( ! name ) {
|
---|
| 457 | expr->member->accept( *visitor );
|
---|
| 458 | visit_children = false;
|
---|
| 459 | return;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | rp_name( name->name, ss, "$field_" );
|
---|
| 463 | ss << "( ";
|
---|
| 464 | expr->aggregate->accept( *visitor );
|
---|
| 465 | ss << " )";
|
---|
| 466 | visit_children = false;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | /// Constant expression replaced by its type
|
---|
| 470 | void previsit( ConstantExpr* expr ) {
|
---|
[a4a000d] | 471 | PassVisitor<TypePrinter> tyPrinter{ closed, ss };
|
---|
[3b3491b6] | 472 | expr->constant.get_type()->accept( tyPrinter );
|
---|
| 473 | visit_children = false;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /// sizeof( ... ), alignof( ... ), offsetof( ... ) replaced by unsigned long constant
|
---|
| 477 | /// TODO extra expression to resolve argument
|
---|
| 478 | void previsit( SizeofExpr* ) {
|
---|
| 479 | ss << (int)BasicType::LongUnsignedInt;
|
---|
| 480 | visit_children = false;
|
---|
| 481 | }
|
---|
| 482 | void previsit( AlignofExpr* ) {
|
---|
| 483 | ss << (int)BasicType::LongUnsignedInt;
|
---|
| 484 | visit_children = false;
|
---|
| 485 | }
|
---|
| 486 | void previsit( UntypedOffsetofExpr* ) {
|
---|
| 487 | ss << (int)BasicType::LongUnsignedInt;
|
---|
| 488 | visit_children = false;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | /// Logical expressions represented as operators
|
---|
| 492 | void previsit( LogicalExpr* expr ) {
|
---|
| 493 | ss << '$' << ( expr->get_isAnd() ? "and" : "or" ) << "( ";
|
---|
| 494 | expr->arg1->accept( *visitor );
|
---|
| 495 | ss << ' ';
|
---|
| 496 | expr->arg2->accept( *visitor );
|
---|
| 497 | ss << " )";
|
---|
| 498 | visit_children = false;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | /// Conditional expression represented as operator
|
---|
| 502 | void previsit( ConditionalExpr* expr ) {
|
---|
| 503 | ss << "$if( ";
|
---|
| 504 | expr->arg1->accept( *visitor );
|
---|
| 505 | ss << ' ';
|
---|
| 506 | expr->arg2->accept( *visitor );
|
---|
| 507 | ss << ' ';
|
---|
| 508 | expr->arg3->accept( *visitor );
|
---|
| 509 | ss << " )";
|
---|
| 510 | visit_children = false;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /// Comma expression represented as operator
|
---|
| 514 | void previsit( CommaExpr* expr ) {
|
---|
| 515 | ss << "$seq( ";
|
---|
| 516 | expr->arg1->accept( *visitor );
|
---|
| 517 | ss << ' ';
|
---|
| 518 | expr->arg2->accept( *visitor );
|
---|
| 519 | ss << " )";
|
---|
| 520 | visit_children = false;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | // TODO handle ignored ImplicitCopyCtorExpr and below
|
---|
| 524 | };
|
---|
| 525 |
|
---|
[4d59ff9] | 526 | void build( Initializer* init, std::stringstream& ss ) {
|
---|
| 527 | if ( SingleInit* si = dynamic_cast<SingleInit*>(init) ) {
|
---|
[a4a000d] | 528 | PassVisitor<ExprPrinter> exprPrinter{ closed, ss };
|
---|
[4d59ff9] | 529 | si->value->accept( exprPrinter );
|
---|
| 530 | ss << ' ';
|
---|
| 531 | } else if ( ListInit* li = dynamic_cast<ListInit*>(init) ) {
|
---|
| 532 | for ( Initializer* it : li->initializers ) {
|
---|
| 533 | build( it, ss );
|
---|
| 534 | ss << ' ';
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | /// Adds an object initializer to the list of expressions
|
---|
| 540 | void build( const std::string& name, Initializer* init, std::stringstream& ss ) {
|
---|
| 541 | ss << "$constructor( ";
|
---|
| 542 | rp_name( name, ss );
|
---|
| 543 | ss << "() ";
|
---|
| 544 | build( init, ss );
|
---|
| 545 | ss << ')';
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | /// Adds a return expression to the list of expressions
|
---|
| 549 | void build( Type* rtnType, Expression* expr, std::stringstream& ss ) {
|
---|
| 550 | ss << "$constructor( ";
|
---|
[a4a000d] | 551 | PassVisitor<TypePrinter> tyPrinter{ closed, ss };
|
---|
[4d59ff9] | 552 | rtnType->accept( tyPrinter );
|
---|
| 553 | ss << ' ';
|
---|
[a4a000d] | 554 | PassVisitor<ExprPrinter> exprPrinter{ closed, ss };
|
---|
[4d59ff9] | 555 | expr->accept( exprPrinter );
|
---|
| 556 | ss << " )";
|
---|
| 557 | }
|
---|
| 558 |
|
---|
[3b3491b6] | 559 | /// Adds all named declarations in a list to the local scope
|
---|
| 560 | void addAll( const std::list<DeclarationWithType*>& decls ) {
|
---|
| 561 | for ( auto decl : decls ) {
|
---|
| 562 | // skip anonymous decls
|
---|
| 563 | if ( decl->name.empty() ) continue;
|
---|
| 564 |
|
---|
| 565 | // handle objects
|
---|
| 566 | if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >( decl ) ) {
|
---|
| 567 | previsit( obj );
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | /// encode field access as function
|
---|
| 573 | void addAggregateFields( AggregateDecl* agg ) {
|
---|
| 574 | // make field names functions
|
---|
| 575 | for ( Declaration* member : agg->members ) {
|
---|
| 576 | if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >(member) ) {
|
---|
| 577 | std::stringstream ss;
|
---|
| 578 | build( obj->name, agg, obj->type, ss );
|
---|
| 579 | addDecl( ss.str() );
|
---|
| 580 | }
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | visit_children = false;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | public:
|
---|
| 587 | void previsit( ObjectDecl *obj ) {
|
---|
| 588 | // add variable as declaration
|
---|
| 589 | std::stringstream ss;
|
---|
| 590 | build( obj->name, obj->type, ss );
|
---|
| 591 | addDecl( ss.str() );
|
---|
[4d59ff9] | 592 |
|
---|
| 593 | // add initializer as expression if applicable
|
---|
| 594 | if ( obj->init ) {
|
---|
| 595 | std::stringstream ss;
|
---|
| 596 | build( obj->name, obj->init, ss );
|
---|
| 597 | addExpr( ss.str() );
|
---|
| 598 | }
|
---|
[3b3491b6] | 599 | }
|
---|
| 600 |
|
---|
| 601 | void previsit( FunctionDecl *decl ) {
|
---|
[4c42a5f] | 602 | // skip decls with ftype parameters
|
---|
| 603 | for ( TypeDecl* tyvar : decl->type->forall ) {
|
---|
| 604 | if ( tyvar->get_kind() == TypeDecl::Ftype ) {
|
---|
| 605 | visit_children = false;
|
---|
| 606 | return;
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[3b3491b6] | 610 | // add function as declaration
|
---|
| 611 | std::stringstream ss;
|
---|
| 612 | build( decl->name, decl->type, ss );
|
---|
| 613 | addDecl( ss.str() );
|
---|
| 614 |
|
---|
| 615 | // add body if available
|
---|
| 616 | if ( decl->statements ) {
|
---|
[4d59ff9] | 617 | std::list<Type*> rtns = from_decls( decl->type->returnVals );
|
---|
| 618 | Type* rtn = nullptr;
|
---|
| 619 | if ( rtns.size() == 1 ) {
|
---|
| 620 | if ( ! dynamic_cast<VoidType*>(rtns.front()) ) rtn = rtns.front()->clone();
|
---|
| 621 | } else if ( rtns.size() > 1 ) {
|
---|
| 622 | rtn = new TupleType{ Type::Qualifiers{}, rtns };
|
---|
| 623 | }
|
---|
| 624 | PassVisitor<ProtoDump> body{ this, rtn };
|
---|
[a4a000d] | 625 |
|
---|
| 626 | for ( TypeDecl* tyvar : decl->type->forall ) {
|
---|
| 627 | // add set of "closed" types to body so that it can print them as NamedType
|
---|
| 628 | body.pass.closed.insert( tyvar->name );
|
---|
| 629 |
|
---|
| 630 | // add assertions to local scope as declarations as well
|
---|
| 631 | for ( DeclarationWithType* assn : tyvar->assertions ) {
|
---|
| 632 | assn->accept( body );
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
[3b3491b6] | 635 |
|
---|
| 636 | // add named parameters and returns to local scope
|
---|
| 637 | body.pass.addAll( decl->type->returnVals );
|
---|
| 638 | body.pass.addAll( decl->type->parameters );
|
---|
| 639 |
|
---|
| 640 | // add contents of function to new scope
|
---|
| 641 | decl->statements->accept( body );
|
---|
[40cd873] | 642 |
|
---|
| 643 | // store sub-scope
|
---|
| 644 | addSub( std::move(body) );
|
---|
[3b3491b6] | 645 | }
|
---|
| 646 |
|
---|
| 647 | visit_children = false;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | void previsit( StructDecl* sd ) { addAggregateFields(sd); }
|
---|
| 651 | void previsit( UnionDecl* ud ) { addAggregateFields(ud); }
|
---|
| 652 |
|
---|
| 653 | void previsit( EnumDecl* ed ) {
|
---|
| 654 | std::unique_ptr<Type> eType =
|
---|
| 655 | std::make_unique<BasicType>( Type::Qualifiers{}, BasicType::SignedInt );
|
---|
| 656 |
|
---|
| 657 | // add field names directly to enclosing scope
|
---|
| 658 | for ( Declaration* member : ed->members ) {
|
---|
| 659 | if ( ObjectDecl* obj = dynamic_cast< ObjectDecl* >(member) ) {
|
---|
| 660 | previsit(obj);
|
---|
| 661 | }
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | visit_children = false;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[4d59ff9] | 667 | void previsit( ReturnStmt* stmt ) {
|
---|
| 668 | // do nothing for void-returning functions or statements returning nothing
|
---|
| 669 | if ( ! rtnType || ! stmt->expr ) return;
|
---|
| 670 |
|
---|
| 671 | // otherwise construct the return type from the expression
|
---|
| 672 | std::stringstream ss;
|
---|
| 673 | build( rtnType.get(), stmt->expr, ss );
|
---|
| 674 | addExpr( ss.str() );
|
---|
| 675 | visit_children = false;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[3b3491b6] | 678 | void previsit( Expression* expr ) {
|
---|
| 679 | std::stringstream ss;
|
---|
[a4a000d] | 680 | PassVisitor<ExprPrinter> exPrinter{ closed, ss };
|
---|
[3b3491b6] | 681 | expr->accept( exPrinter );
|
---|
| 682 | addExpr( ss.str() );
|
---|
| 683 | visit_children = false;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[a4a000d] | 686 | /// Print non-prelude global declarations for resolv proto
|
---|
| 687 | void printGlobals() const {
|
---|
| 688 | std::cout << "#ptr<T> $addr T" << std::endl; // &?
|
---|
| 689 | int i = (int)BasicType::SignedInt;
|
---|
| 690 | std::cout << i << " $and " << i << ' ' << i << std::endl; // ?&&?
|
---|
| 691 | std::cout << i << " $or " << i << ' ' << i << std::endl; // ?||?
|
---|
| 692 | std::cout << "T $if " << i << " T T" << std::endl; // ternary operator
|
---|
| 693 | std::cout << "T $seq X T" << std::endl; // ?,?
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[3b3491b6] | 696 | public:
|
---|
| 697 | /// Prints this ProtoDump instance
|
---|
| 698 | void print(unsigned indent = 0) const {
|
---|
[a4a000d] | 699 | // print globals at root level
|
---|
| 700 | if ( ! parent ) printGlobals();
|
---|
[3b3491b6] | 701 | // print decls
|
---|
[a4a000d] | 702 | std::string tab( indent, '\t' );
|
---|
[3b3491b6] | 703 | for ( const std::string& d : decls ) {
|
---|
| 704 | std::cout << tab << d << std::endl;
|
---|
| 705 | }
|
---|
| 706 | // print divider
|
---|
[4d59ff9] | 707 | std::cout << '\n' << tab << "%%\n" << std::endl;
|
---|
[3b3491b6] | 708 | // print top-level expressions
|
---|
| 709 | for ( const std::string& e : exprs ) {
|
---|
| 710 | std::cout << tab << e << std::endl;
|
---|
| 711 | }
|
---|
| 712 | // print child scopes
|
---|
| 713 | ++indent;
|
---|
| 714 | for ( const PassVisitor<ProtoDump>& s : subs ) {
|
---|
| 715 | std::cout << tab << '{' << std::endl;
|
---|
| 716 | s.pass.print( indent );
|
---|
| 717 | std::cout << tab << '}' << std::endl;
|
---|
| 718 | }
|
---|
| 719 | }
|
---|
| 720 | };
|
---|
| 721 |
|
---|
| 722 | void dumpAsResolvProto( std::list< Declaration * > &translationUnit ) {
|
---|
| 723 | PassVisitor<ProtoDump> dump;
|
---|
| 724 | acceptAll( translationUnit, dump );
|
---|
| 725 | dump.pass.print();
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | } // namespace CodeTools
|
---|
| 729 |
|
---|
| 730 | // Local Variables: //
|
---|
| 731 | // tab-width: 4 //
|
---|
| 732 | // mode: c++ //
|
---|
| 733 | // compile-command: "make install" //
|
---|
| 734 | // End: //
|
---|