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