| 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 | // TopLvalue.cc -- Check and force that lvalue is only at the top of types. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Wed Jul 31 15:49:00 2019 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Wed Aug  7 15:36:00 2019 | 
|---|
| 13 | // Update Count     : 0 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <iostream> | 
|---|
| 17 |  | 
|---|
| 18 | #include "Common/PassVisitor.h" | 
|---|
| 19 |  | 
|---|
| 20 | namespace { | 
|---|
| 21 | class TopLvalue : public WithGuards { | 
|---|
| 22 | bool inType = false; | 
|---|
| 23 | public: | 
|---|
| 24 | void previsit( const BaseSyntaxNode * ) { | 
|---|
| 25 | if ( inType ) { | 
|---|
| 26 | GuardValue( inType ); | 
|---|
| 27 | inType = false; | 
|---|
| 28 | } | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | void previsit( const Type * type ) { | 
|---|
| 32 | if ( inType ) { | 
|---|
| 33 | assert( !type->get_lvalue() ); | 
|---|
| 34 | } else { | 
|---|
| 35 | GuardValue( inType ); | 
|---|
| 36 | inType = true; | 
|---|
| 37 | } | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | }; | 
|---|
| 41 |  | 
|---|
| 42 | class ClearLvalue : public WithGuards { | 
|---|
| 43 | bool inType = false; | 
|---|
| 44 | public: | 
|---|
| 45 | void previsit( BaseSyntaxNode * ) { | 
|---|
| 46 | if ( inType ) { | 
|---|
| 47 | GuardValue( inType ); | 
|---|
| 48 | inType = false; | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void previsit( Type * type ) { | 
|---|
| 53 | if ( !inType ) { | 
|---|
| 54 | GuardValue( inType ); | 
|---|
| 55 | inType = true; | 
|---|
| 56 | } else if ( type->get_lvalue() ) { | 
|---|
| 57 | type->set_lvalue( false ); | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 | class TopLvaluePrint : public WithGuards, public WithShortCircuiting { | 
|---|
| 63 | bool failed = false; | 
|---|
| 64 | bool inType = false; | 
|---|
| 65 | bool typeTop = false; | 
|---|
| 66 | public: | 
|---|
| 67 | bool failedAny = false; | 
|---|
| 68 | void previsit() { | 
|---|
| 69 | if ( failed ) { | 
|---|
| 70 | visit_children = false; | 
|---|
| 71 | } else if ( typeTop ) { | 
|---|
| 72 | GuardValue( typeTop ); | 
|---|
| 73 | typeTop = false; | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void previsit( const BaseSyntaxNode * ) { | 
|---|
| 78 | previsit(); | 
|---|
| 79 | if ( inType ) { | 
|---|
| 80 | GuardValue( inType ); | 
|---|
| 81 | inType = false; | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void previsit( const Type * type ) { | 
|---|
| 86 | previsit(); | 
|---|
| 87 | if ( inType ) { | 
|---|
| 88 | if ( type->get_lvalue() ) { | 
|---|
| 89 | failed = true; | 
|---|
| 90 | failedAny = true; | 
|---|
| 91 | visit_children = false; | 
|---|
| 92 | std::cout << type->location << std::endl; | 
|---|
| 93 | } | 
|---|
| 94 | //assert( !type->get_lvalue() ); | 
|---|
| 95 | } else { | 
|---|
| 96 | GuardValue( inType ); | 
|---|
| 97 | inType = true; | 
|---|
| 98 | typeTop = true; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void postvisit( const Type * type ) { | 
|---|
| 103 | if ( typeTop ) { | 
|---|
| 104 | if ( failed ) { | 
|---|
| 105 | std::cout << type->location << std::endl; | 
|---|
| 106 | type->print( std::cout ); | 
|---|
| 107 | //assert( !failed ); | 
|---|
| 108 | failed = false; | 
|---|
| 109 | } | 
|---|
| 110 | typeTop = false; | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 | }; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void assertTopLvalue( const std::list< Declaration * > & translationUnit ) { | 
|---|
| 117 | PassVisitor< TopLvaluePrint > visitor; | 
|---|
| 118 | acceptAll( translationUnit, visitor ); | 
|---|
| 119 | assert( !visitor.pass.failedAny ); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void clearInnerLvalue( std::list< Declaration * > & translationUnit ) { | 
|---|
| 123 | PassVisitor< ClearLvalue > visitor; | 
|---|
| 124 | acceptAll( translationUnit, visitor ); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | // Local Variables: // | 
|---|
| 129 | // tab-width: 4 // | 
|---|
| 130 | // mode: c++ // | 
|---|
| 131 | // compile-command: "make install" // | 
|---|
| 132 | // End: // | 
|---|