[a32b204] | 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 | // |
---|
[906e24d] | 7 | // ResolveTypeof.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:12:20 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[a08ba92] | 12 | // Last Modified On : Tue May 19 16:49:04 2015 |
---|
| 13 | // Update Count : 3 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "ResolveTypeof.h" |
---|
[ea6332d] | 17 | |
---|
| 18 | #include <cassert> // for assert |
---|
| 19 | |
---|
[9d79f93] | 20 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[ea6332d] | 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 |
---|
[51b7345] | 29 | |
---|
| 30 | namespace ResolvExpr { |
---|
[a08ba92] | 31 | namespace { |
---|
[51b7345] | 32 | #if 0 |
---|
[a32b204] | 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 | } |
---|
[51b7345] | 41 | #endif |
---|
[a08ba92] | 42 | } |
---|
[51b7345] | 43 | |
---|
[9d79f93] | 44 | class ResolveTypeof : public WithShortCircuiting { |
---|
[a08ba92] | 45 | public: |
---|
[a32b204] | 46 | ResolveTypeof( const SymTab::Indexer &indexer ) : indexer( indexer ) {} |
---|
[9d79f93] | 47 | void premutate( TypeofType *typeofType ); |
---|
| 48 | Type * postmutate( TypeofType *typeofType ); |
---|
[51b7345] | 49 | |
---|
[a08ba92] | 50 | private: |
---|
[a32b204] | 51 | const SymTab::Indexer &indexer; |
---|
[a08ba92] | 52 | }; |
---|
[51b7345] | 53 | |
---|
[9d79f93] | 54 | Type * resolveTypeof( Type *type, const SymTab::Indexer &indexer ) { |
---|
| 55 | PassVisitor<ResolveTypeof> mutator( indexer ); |
---|
[a32b204] | 56 | return type->acceptMutator( mutator ); |
---|
[a08ba92] | 57 | } |
---|
[51b7345] | 58 | |
---|
[9d79f93] | 59 | void ResolveTypeof::premutate( TypeofType * ) { |
---|
| 60 | visit_children = false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | Type * ResolveTypeof::postmutate( TypeofType *typeofType ) { |
---|
[d9a0e76] | 64 | #if 0 |
---|
[9d79f93] | 65 | std::cerr << "resolving typeof: "; |
---|
| 66 | typeofType->print( std::cerr ); |
---|
| 67 | std::cerr << std::endl; |
---|
[d9a0e76] | 68 | #endif |
---|
[f441c88] | 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 |
---|
[9d79f93] | 83 | Expression * newExpr = resolveInVoidContext( typeofType->expr, indexer ); |
---|
| 84 | assert( newExpr->result && ! newExpr->result->isVoid() ); |
---|
[f441c88] | 85 | newType = newExpr->result; |
---|
[9d79f93] | 86 | newExpr->result = nullptr; |
---|
[a32b204] | 87 | delete typeofType; |
---|
[c93bc28] | 88 | delete newExpr; |
---|
[f441c88] | 89 | } |
---|
| 90 | |
---|
| 91 | // clear qualifiers for base, combine with typeoftype quals in any case |
---|
| 92 | if ( isBasetypeof ) { |
---|
[8e04794] | 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 | } |
---|
[f441c88] | 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; |
---|
[a08ba92] | 108 | } |
---|
[51b7345] | 109 | } // namespace ResolvExpr |
---|
[a32b204] | 110 | |
---|
| 111 | // Local Variables: // |
---|
| 112 | // tab-width: 4 // |
---|
| 113 | // mode: c++ // |
---|
| 114 | // compile-command: "make install" // |
---|
| 115 | // End: // |
---|