| 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 | // GenType.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Wed May  1 15:24:00 2019
 | 
|---|
| 13 | // Update Count     : 23
 | 
|---|
| 14 | //
 | 
|---|
| 15 | #include "GenType.h"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <cassert>                // for assert, assertf
 | 
|---|
| 18 | #include <list>                   // for _List_iterator, _List_const_iterator
 | 
|---|
| 19 | #include <sstream>                // for operator<<, ostringstream, basic_os...
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "CodeGenerator.h"        // for CodeGenerator
 | 
|---|
| 22 | #include "SynTree/Declaration.h"  // for DeclarationWithType
 | 
|---|
| 23 | #include "SynTree/Expression.h"   // for Expression
 | 
|---|
| 24 | #include "SynTree/Type.h"         // for PointerType, Type, FunctionType
 | 
|---|
| 25 | #include "SynTree/Visitor.h"      // for Visitor
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace CodeGen {
 | 
|---|
| 28 |         struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
 | 
|---|
| 29 |                 std::string typeString;
 | 
|---|
| 30 |                 GenType( const std::string &typeString, const Options &options );
 | 
|---|
| 31 | 
 | 
|---|
| 32 |                 void previsit( BaseSyntaxNode * );
 | 
|---|
| 33 |                 void postvisit( BaseSyntaxNode * );
 | 
|---|
| 34 | 
 | 
|---|
| 35 |                 void postvisit( FunctionType * funcType );
 | 
|---|
| 36 |                 void postvisit( VoidType * voidType );
 | 
|---|
| 37 |                 void postvisit( BasicType * basicType );
 | 
|---|
| 38 |                 void postvisit( PointerType * pointerType );
 | 
|---|
| 39 |                 void postvisit( ArrayType * arrayType );
 | 
|---|
| 40 |                 void postvisit( ReferenceType * refType );
 | 
|---|
| 41 |                 void postvisit( StructInstType * structInst );
 | 
|---|
| 42 |                 void postvisit( UnionInstType * unionInst );
 | 
|---|
| 43 |                 void postvisit( EnumInstType * enumInst );
 | 
|---|
| 44 |                 void postvisit( TypeInstType * typeInst );
 | 
|---|
| 45 |                 void postvisit( TupleType  * tupleType );
 | 
|---|
| 46 |                 void postvisit( VarArgsType * varArgsType );
 | 
|---|
| 47 |                 void postvisit( ZeroType * zeroType );
 | 
|---|
| 48 |                 void postvisit( OneType * oneType );
 | 
|---|
| 49 |                 void postvisit( GlobalScopeType * globalType );
 | 
|---|
| 50 |                 void postvisit( TraitInstType * inst );
 | 
|---|
| 51 |                 void postvisit( TypeofType * typeof );
 | 
|---|
| 52 |                 void postvisit( QualifiedType * qualType );
 | 
|---|
| 53 | 
 | 
|---|
| 54 |           private:
 | 
|---|
| 55 |                 void handleQualifiers( Type *type );
 | 
|---|
| 56 |                 std::string handleGeneric( ReferenceToType * refType );
 | 
|---|
| 57 |                 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
 | 
|---|
| 58 | 
 | 
|---|
| 59 |                 Options options;
 | 
|---|
| 60 |         };
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         std::string genType( Type *type, const std::string &baseString, const Options &options ) {
 | 
|---|
| 63 |                 PassVisitor<GenType> gt( baseString, options );
 | 
|---|
| 64 |                 std::ostringstream os;
 | 
|---|
| 65 | 
 | 
|---|
| 66 |                 if ( ! type->get_attributes().empty() ) {
 | 
|---|
| 67 |                         PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 68 |                         cg.pass.genAttributes( type->get_attributes() );
 | 
|---|
| 69 |                 } // if
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                 type->accept( gt );
 | 
|---|
| 72 |                 return os.str() + gt.pass.typeString;
 | 
|---|
| 73 |         }
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
 | 
|---|
| 76 |                 return genType( type, baseString, Options(pretty, genC, lineMarks, false ) );
 | 
|---|
| 77 |         }
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         std::string genPrettyType( Type * type, const std::string & baseString ) {
 | 
|---|
| 80 |                 return genType( type, baseString, true, false );
 | 
|---|
| 81 |         }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         GenType::GenType( const std::string &typeString, const Options &options ) : typeString( typeString ), options( options ) {}
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         // *** BaseSyntaxNode
 | 
|---|
| 86 |         void GenType::previsit( BaseSyntaxNode * ) {
 | 
|---|
| 87 |                 // turn off automatic recursion for all nodes, to allow each visitor to
 | 
|---|
| 88 |                 // precisely control the order in which its children are visited.
 | 
|---|
| 89 |                 visit_children = false;
 | 
|---|
| 90 |         }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         void GenType::postvisit( BaseSyntaxNode * node ) {
 | 
|---|
| 93 |                 std::stringstream ss;
 | 
|---|
| 94 |                 node->print( ss );
 | 
|---|
| 95 |                 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
 | 
|---|
| 96 |         }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         void GenType::postvisit( VoidType * voidType ) {
 | 
|---|
| 99 |                 typeString = "void " + typeString;
 | 
|---|
| 100 |                 handleQualifiers( voidType );
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         void GenType::postvisit( BasicType * basicType ) {
 | 
|---|
| 104 |                 BasicType::Kind kind = basicType->kind;
 | 
|---|
| 105 |                 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
 | 
|---|
| 106 |                 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
 | 
|---|
| 107 |                 handleQualifiers( basicType );
 | 
|---|
| 108 |         }
 | 
|---|
| 109 | 
 | 
|---|
| 110 |         void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
 | 
|---|
| 111 |                 std::ostringstream os;
 | 
|---|
| 112 |                 if ( typeString != "" ) {
 | 
|---|
| 113 |                         if ( typeString[ 0 ] == '*' ) {
 | 
|---|
| 114 |                                 os << "(" << typeString << ")";
 | 
|---|
| 115 |                         } else {
 | 
|---|
| 116 |                                 os << typeString;
 | 
|---|
| 117 |                         } // if
 | 
|---|
| 118 |                 } // if
 | 
|---|
| 119 |                 os << "[";
 | 
|---|
| 120 | 
 | 
|---|
| 121 |                 if ( isStatic ) {
 | 
|---|
| 122 |                         os << "static ";
 | 
|---|
| 123 |                 } // if
 | 
|---|
| 124 |                 if ( qualifiers.is_const ) {
 | 
|---|
| 125 |                         os << "const ";
 | 
|---|
| 126 |                 } // if
 | 
|---|
| 127 |                 if ( qualifiers.is_volatile ) {
 | 
|---|
| 128 |                         os << "volatile ";
 | 
|---|
| 129 |                 } // if
 | 
|---|
| 130 |                 if ( qualifiers.is_restrict ) {
 | 
|---|
| 131 |                         os << "__restrict ";
 | 
|---|
| 132 |                 } // if
 | 
|---|
| 133 |                 if ( qualifiers.is_atomic ) {
 | 
|---|
| 134 |                         os << "_Atomic ";
 | 
|---|
| 135 |                 } // if
 | 
|---|
| 136 |                 if ( dimension != 0 ) {
 | 
|---|
| 137 |                         PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 138 |                         dimension->accept( cg );
 | 
|---|
| 139 |                 } else if ( isVarLen ) {
 | 
|---|
| 140 |                         // no dimension expression on a VLA means it came in with the * token
 | 
|---|
| 141 |                         os << "*";
 | 
|---|
| 142 |                 } // if
 | 
|---|
| 143 |                 os << "]";
 | 
|---|
| 144 | 
 | 
|---|
| 145 |                 typeString = os.str();
 | 
|---|
| 146 | 
 | 
|---|
| 147 |                 base->accept( *visitor );
 | 
|---|
| 148 |         }
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         void GenType::postvisit( PointerType * pointerType ) {
 | 
|---|
| 151 |                 assert( pointerType->base != 0);
 | 
|---|
| 152 |                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
 | 
|---|
| 153 |                         genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
 | 
|---|
| 154 |                 } else {
 | 
|---|
| 155 |                         handleQualifiers( pointerType );
 | 
|---|
| 156 |                         if ( typeString[ 0 ] == '?' ) {
 | 
|---|
| 157 |                                 typeString = "* " + typeString;
 | 
|---|
| 158 |                         } else {
 | 
|---|
| 159 |                                 typeString = "*" + typeString;
 | 
|---|
| 160 |                         } // if
 | 
|---|
| 161 |                         pointerType->base->accept( *visitor );
 | 
|---|
| 162 |                 } // if
 | 
|---|
| 163 |         }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         void GenType::postvisit( ArrayType * arrayType ) {
 | 
|---|
| 166 |                 genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
 | 
|---|
| 167 |         }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |         void GenType::postvisit( ReferenceType * refType ) {
 | 
|---|
| 170 |                 assert( refType->base != 0);
 | 
|---|
| 171 |                 assertf( ! options.genC, "Reference types should not reach code generation." );
 | 
|---|
| 172 |                 handleQualifiers( refType );
 | 
|---|
| 173 |                 typeString = "&" + typeString;
 | 
|---|
| 174 |                 refType->base->accept( *visitor );
 | 
|---|
| 175 |         }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         void GenType::postvisit( FunctionType * funcType ) {
 | 
|---|
| 178 |                 std::ostringstream os;
 | 
|---|
| 179 | 
 | 
|---|
| 180 |                 if ( typeString != "" ) {
 | 
|---|
| 181 |                         if ( typeString[ 0 ] == '*' ) {
 | 
|---|
| 182 |                                 os << "(" << typeString << ")";
 | 
|---|
| 183 |                         } else {
 | 
|---|
| 184 |                                 os << typeString;
 | 
|---|
| 185 |                         } // if
 | 
|---|
| 186 |                 } // if
 | 
|---|
| 187 | 
 | 
|---|
| 188 |                 /************* parameters ***************/
 | 
|---|
| 189 | 
 | 
|---|
| 190 |                 const std::list<DeclarationWithType *> &pars = funcType->parameters;
 | 
|---|
| 191 | 
 | 
|---|
| 192 |                 if ( pars.empty() ) {
 | 
|---|
| 193 |                         if ( funcType->get_isVarArgs() ) {
 | 
|---|
| 194 |                                 os << "()";
 | 
|---|
| 195 |                         } else {
 | 
|---|
| 196 |                                 os << "(void)";
 | 
|---|
| 197 |                         } // if
 | 
|---|
| 198 |                 } else {
 | 
|---|
| 199 |                         PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 200 |                         os << "(" ;
 | 
|---|
| 201 | 
 | 
|---|
| 202 |                         cg.pass.genCommaList( pars.begin(), pars.end() );
 | 
|---|
| 203 | 
 | 
|---|
| 204 |                         if ( funcType->get_isVarArgs() ) {
 | 
|---|
| 205 |                                 os << ", ...";
 | 
|---|
| 206 |                         } // if
 | 
|---|
| 207 |                         os << ")";
 | 
|---|
| 208 |                 } // if
 | 
|---|
| 209 | 
 | 
|---|
| 210 |                 typeString = os.str();
 | 
|---|
| 211 | 
 | 
|---|
| 212 |                 if ( funcType->returnVals.size() == 0 ) {
 | 
|---|
| 213 |                         typeString = "void " + typeString;
 | 
|---|
| 214 |                 } else {
 | 
|---|
| 215 |                         funcType->returnVals.front()->get_type()->accept( *visitor );
 | 
|---|
| 216 |                 } // if
 | 
|---|
| 217 | 
 | 
|---|
| 218 |                 // add forall
 | 
|---|
| 219 |                 if( ! funcType->forall.empty() && ! options.genC ) {
 | 
|---|
| 220 |                         // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
 | 
|---|
| 221 |                         std::ostringstream os;
 | 
|---|
| 222 |                         PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 223 |                         os << "forall(";
 | 
|---|
| 224 |                         cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
 | 
|---|
| 225 |                         os << ")" << std::endl;
 | 
|---|
| 226 |                         typeString = os.str() + typeString;
 | 
|---|
| 227 |                 }
 | 
|---|
| 228 |         }
 | 
|---|
| 229 | 
 | 
|---|
| 230 |         std::string GenType::handleGeneric( ReferenceToType * refType ) {
 | 
|---|
| 231 |                 if ( ! refType->parameters.empty() ) {
 | 
|---|
| 232 |                         std::ostringstream os;
 | 
|---|
| 233 |                         PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 234 |                         os << "(";
 | 
|---|
| 235 |                         cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
 | 
|---|
| 236 |                         os << ") ";
 | 
|---|
| 237 |                         return os.str();
 | 
|---|
| 238 |                 }
 | 
|---|
| 239 |                 return "";
 | 
|---|
| 240 |         }
 | 
|---|
| 241 | 
 | 
|---|
| 242 |         void GenType::postvisit( StructInstType * structInst )  {
 | 
|---|
| 243 |                 typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
 | 
|---|
| 244 |                 if ( options.genC ) typeString = "struct " + typeString;
 | 
|---|
| 245 |                 handleQualifiers( structInst );
 | 
|---|
| 246 |         }
 | 
|---|
| 247 | 
 | 
|---|
| 248 |         void GenType::postvisit( UnionInstType * unionInst ) {
 | 
|---|
| 249 |                 typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
 | 
|---|
| 250 |                 if ( options.genC ) typeString = "union " + typeString;
 | 
|---|
| 251 |                 handleQualifiers( unionInst );
 | 
|---|
| 252 |         }
 | 
|---|
| 253 | 
 | 
|---|
| 254 |         void GenType::postvisit( EnumInstType * enumInst ) {
 | 
|---|
| 255 |                 typeString = enumInst->name + " " + typeString;
 | 
|---|
| 256 |                 if ( options.genC ) typeString = "enum " + typeString;
 | 
|---|
| 257 |                 handleQualifiers( enumInst );
 | 
|---|
| 258 |         }
 | 
|---|
| 259 | 
 | 
|---|
| 260 |         void GenType::postvisit( TypeInstType * typeInst ) {
 | 
|---|
| 261 |                 typeString = typeInst->name + " " + typeString;
 | 
|---|
| 262 |                 handleQualifiers( typeInst );
 | 
|---|
| 263 |         }
 | 
|---|
| 264 | 
 | 
|---|
| 265 |         void GenType::postvisit( TupleType * tupleType ) {
 | 
|---|
| 266 |                 assertf( ! options.genC, "Tuple types should not reach code generation." );
 | 
|---|
| 267 |                 unsigned int i = 0;
 | 
|---|
| 268 |                 std::ostringstream os;
 | 
|---|
| 269 |                 os << "[";
 | 
|---|
| 270 |                 for ( Type * t : *tupleType ) {
 | 
|---|
| 271 |                         i++;
 | 
|---|
| 272 |                         os << genType( t, "", options ) << (i == tupleType->size() ? "" : ", ");
 | 
|---|
| 273 |                 }
 | 
|---|
| 274 |                 os << "] ";
 | 
|---|
| 275 |                 typeString = os.str() + typeString;
 | 
|---|
| 276 |         }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         void GenType::postvisit( VarArgsType * varArgsType ) {
 | 
|---|
| 279 |                 typeString = "__builtin_va_list " + typeString;
 | 
|---|
| 280 |                 handleQualifiers( varArgsType );
 | 
|---|
| 281 |         }
 | 
|---|
| 282 | 
 | 
|---|
| 283 |         void GenType::postvisit( ZeroType * zeroType ) {
 | 
|---|
| 284 |                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
 | 
|---|
| 285 |                 typeString = (options.pretty ? "zero_t " : "long int ") + typeString;
 | 
|---|
| 286 |                 handleQualifiers( zeroType );
 | 
|---|
| 287 |         }
 | 
|---|
| 288 | 
 | 
|---|
| 289 |         void GenType::postvisit( OneType * oneType ) {
 | 
|---|
| 290 |                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
 | 
|---|
| 291 |                 typeString = (options.pretty ? "one_t " : "long int ") + typeString;
 | 
|---|
| 292 |                 handleQualifiers( oneType );
 | 
|---|
| 293 |         }
 | 
|---|
| 294 | 
 | 
|---|
| 295 |         void GenType::postvisit( GlobalScopeType * globalType ) {
 | 
|---|
| 296 |                 assertf( ! options.genC, "Global scope type should not reach code generation." );
 | 
|---|
| 297 |                 handleQualifiers( globalType );
 | 
|---|
| 298 |         }
 | 
|---|
| 299 | 
 | 
|---|
| 300 |         void GenType::postvisit( TraitInstType * inst ) {
 | 
|---|
| 301 |                 assertf( ! options.genC, "Trait types should not reach code generation." );
 | 
|---|
| 302 |                 typeString = inst->name + " " + typeString;
 | 
|---|
| 303 |                 handleQualifiers( inst );
 | 
|---|
| 304 |         }
 | 
|---|
| 305 | 
 | 
|---|
| 306 |         void GenType::postvisit( TypeofType * typeof ) {
 | 
|---|
| 307 |                 std::ostringstream os;
 | 
|---|
| 308 |                 PassVisitor<CodeGenerator> cg( os, options );
 | 
|---|
| 309 |                 os << "typeof(";
 | 
|---|
| 310 |                 typeof->expr->accept( cg );
 | 
|---|
| 311 |                 os << ") " << typeString;
 | 
|---|
| 312 |                 typeString = os.str();
 | 
|---|
| 313 |                 handleQualifiers( typeof );
 | 
|---|
| 314 |         }
 | 
|---|
| 315 | 
 | 
|---|
| 316 |         void GenType::postvisit( QualifiedType * qualType ) {
 | 
|---|
| 317 |                 assertf( ! options.genC, "Qualified types should not reach code generation." );
 | 
|---|
| 318 |                 std::ostringstream os;
 | 
|---|
| 319 |                 os << genType( qualType->parent, "", options ) << "." << genType( qualType->child, "", options ) << typeString;
 | 
|---|
| 320 |                 typeString = os.str();
 | 
|---|
| 321 |                 handleQualifiers( qualType );
 | 
|---|
| 322 |         }
 | 
|---|
| 323 | 
 | 
|---|
| 324 |         void GenType::handleQualifiers( Type * type ) {
 | 
|---|
| 325 |                 if ( type->get_const() ) {
 | 
|---|
| 326 |                         typeString = "const " + typeString;
 | 
|---|
| 327 |                 } // if
 | 
|---|
| 328 |                 if ( type->get_volatile() ) {
 | 
|---|
| 329 |                         typeString = "volatile " + typeString;
 | 
|---|
| 330 |                 } // if
 | 
|---|
| 331 |                 if ( type->get_restrict() ) {
 | 
|---|
| 332 |                         typeString = "__restrict " + typeString;
 | 
|---|
| 333 |                 } // if
 | 
|---|
| 334 |                 if ( type->get_atomic() ) {
 | 
|---|
| 335 |                         typeString = "_Atomic " + typeString;
 | 
|---|
| 336 |                 } // if
 | 
|---|
| 337 |         }
 | 
|---|
| 338 | } // namespace CodeGen
 | 
|---|
| 339 | 
 | 
|---|
| 340 | // Local Variables: //
 | 
|---|
| 341 | // tab-width: 4 //
 | 
|---|
| 342 | // mode: c++ //
 | 
|---|
| 343 | // compile-command: "make install" //
 | 
|---|
| 344 | // End: //
 | 
|---|