| 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 | // ResolveTypeof.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 12:12:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue May 19 16:49:04 2015 | 
|---|
| 13 | // Update Count     : 3 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "ResolveTypeof.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <cassert>               // for assert | 
|---|
| 19 |  | 
|---|
| 20 | #include "Common/PassVisitor.h"  // for PassVisitor | 
|---|
| 21 | #include "Resolver.h"            // for resolveInVoidContext | 
|---|
| 22 | #include "SynTree/Expression.h"  // for Expression | 
|---|
| 23 | #include "SynTree/Mutator.h"     // for Mutator | 
|---|
| 24 | #include "SynTree/Type.h"        // for TypeofType, Type | 
|---|
| 25 |  | 
|---|
| 26 | namespace SymTab { | 
|---|
| 27 | class Indexer; | 
|---|
| 28 | }  // namespace SymTab | 
|---|
| 29 |  | 
|---|
| 30 | namespace ResolvExpr { | 
|---|
| 31 | namespace { | 
|---|
| 32 | #if 0 | 
|---|
| 33 | void | 
|---|
| 34 | printAlts( const AltList &list, std::ostream &os, int indent = 0 ) | 
|---|
| 35 | { | 
|---|
| 36 | for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { | 
|---|
| 37 | i->print( os, indent ); | 
|---|
| 38 | os << std::endl; | 
|---|
| 39 | } | 
|---|
| 40 | } | 
|---|
| 41 | #endif | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | class ResolveTypeof : public WithShortCircuiting { | 
|---|
| 45 | public: | 
|---|
| 46 | ResolveTypeof( const SymTab::Indexer &indexer ) : indexer( indexer ) {} | 
|---|
| 47 | void premutate( TypeofType *typeofType ); | 
|---|
| 48 | Type * postmutate( TypeofType *typeofType ); | 
|---|
| 49 |  | 
|---|
| 50 | private: | 
|---|
| 51 | const SymTab::Indexer &indexer; | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) { | 
|---|
| 55 | PassVisitor<ResolveTypeof> mutator( indexer ); | 
|---|
| 56 | return type->acceptMutator( mutator ); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | void ResolveTypeof::premutate( TypeofType * ) { | 
|---|
| 60 | visit_children = false; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | Type * ResolveTypeof::postmutate( TypeofType *typeofType ) { | 
|---|
| 64 | #if 0 | 
|---|
| 65 | std::cerr << "resolving typeof: "; | 
|---|
| 66 | typeofType->print( std::cerr ); | 
|---|
| 67 | std::cerr << std::endl; | 
|---|
| 68 | #endif | 
|---|
| 69 | // pass on null expression | 
|---|
| 70 | if ( ! typeofType->expr ) return typeofType; | 
|---|
| 71 |  | 
|---|
| 72 | bool isBasetypeof = typeofType->is_basetypeof; | 
|---|
| 73 | auto oldQuals = typeofType->get_qualifiers().val; | 
|---|
| 74 |  | 
|---|
| 75 | Type* newType; | 
|---|
| 76 | if ( TypeExpr* tyExpr = dynamic_cast<TypeExpr*>(typeofType->expr) ) { | 
|---|
| 77 | // typeof wrapping type | 
|---|
| 78 | newType = tyExpr->type; | 
|---|
| 79 | tyExpr->type = nullptr; | 
|---|
| 80 | delete tyExpr; | 
|---|
| 81 | } else { | 
|---|
| 82 | // typeof wrapping expression | 
|---|
| 83 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer ); | 
|---|
| 84 | assert( newExpr->result && ! newExpr->result->isVoid() ); | 
|---|
| 85 | newType = newExpr->result; | 
|---|
| 86 | newExpr->result = nullptr; | 
|---|
| 87 | delete typeofType; | 
|---|
| 88 | delete newExpr; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | // clear qualifiers for base, combine with typeoftype quals in any case | 
|---|
| 92 | if ( isBasetypeof ) { | 
|---|
| 93 | // replace basetypeof(<enum>) by int | 
|---|
| 94 | if ( dynamic_cast<EnumInstType*>(newType) ) { | 
|---|
| 95 | Type* newerType = | 
|---|
| 96 | new BasicType{ newType->get_qualifiers(), BasicType::SignedInt, | 
|---|
| 97 | newType->attributes }; | 
|---|
| 98 | delete newType; | 
|---|
| 99 | newType = newerType; | 
|---|
| 100 | } | 
|---|
| 101 | newType->get_qualifiers().val | 
|---|
| 102 | = ( newType->get_qualifiers().val & ~Type::Qualifiers::Mask ) | oldQuals; | 
|---|
| 103 | } else { | 
|---|
| 104 | newType->get_qualifiers().val |= oldQuals; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | return newType; | 
|---|
| 108 | } | 
|---|
| 109 | } // namespace ResolvExpr | 
|---|
| 110 |  | 
|---|
| 111 | // Local Variables: // | 
|---|
| 112 | // tab-width: 4 // | 
|---|
| 113 | // mode: c++ // | 
|---|
| 114 | // compile-command: "make install" // | 
|---|
| 115 | // End: // | 
|---|