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