| 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 |
|
|---|
| 16 | #include <sstream>
|
|---|
| 17 | #include <cassert>
|
|---|
| 18 |
|
|---|
| 19 | #include "GenType.h"
|
|---|
| 20 | #include "CodeGenerator.h"
|
|---|
| 21 |
|
|---|
| 22 | #include "SynTree/Declaration.h"
|
|---|
| 23 | #include "SynTree/Expression.h"
|
|---|
| 24 | #include "SynTree/Type.h"
|
|---|
| 25 | #include "SynTree/Visitor.h"
|
|---|
| 26 |
|
|---|
| 27 | namespace CodeGen {
|
|---|
| 28 | class GenType : public Visitor {
|
|---|
| 29 | public:
|
|---|
| 30 | GenType( const std::string &typeString, bool pretty = false, bool genC = false );
|
|---|
| 31 | std::string get_typeString() const { return typeString; }
|
|---|
| 32 | void set_typeString( const std::string &newValue ) { typeString = newValue; }
|
|---|
| 33 |
|
|---|
| 34 | virtual void visit( FunctionType *funcType );
|
|---|
| 35 | virtual void visit( VoidType *voidType );
|
|---|
| 36 | virtual void visit( BasicType *basicType );
|
|---|
| 37 | virtual void visit( PointerType *pointerType );
|
|---|
| 38 | virtual void visit( ArrayType *arrayType );
|
|---|
| 39 | virtual void visit( StructInstType *structInst );
|
|---|
| 40 | virtual void visit( UnionInstType *unionInst );
|
|---|
| 41 | virtual void visit( EnumInstType *enumInst );
|
|---|
| 42 | virtual void visit( TypeInstType *typeInst );
|
|---|
| 43 | virtual void visit( TupleType * tupleType );
|
|---|
| 44 | virtual void visit( VarArgsType *varArgsType );
|
|---|
| 45 | virtual void visit( ZeroType *zeroType );
|
|---|
| 46 | virtual void visit( OneType *oneType );
|
|---|
| 47 |
|
|---|
| 48 | private:
|
|---|
| 49 | void handleQualifiers( Type *type );
|
|---|
| 50 | std::string handleGeneric( ReferenceToType * refType );
|
|---|
| 51 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
|
|---|
| 52 |
|
|---|
| 53 | std::string typeString;
|
|---|
| 54 | bool pretty = false; // pretty print
|
|---|
| 55 | bool genC = false; // generating C code?
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC ) {
|
|---|
| 59 | GenType gt( baseString, pretty, genC );
|
|---|
| 60 | std::ostringstream os;
|
|---|
| 61 |
|
|---|
| 62 | if ( ! type->get_attributes().empty() ) {
|
|---|
| 63 | CodeGenerator cg( os, pretty, genC );
|
|---|
| 64 | cg.genAttributes( type->get_attributes() );
|
|---|
| 65 | } // if
|
|---|
| 66 |
|
|---|
| 67 | type->accept( gt );
|
|---|
| 68 | return os.str() + gt.get_typeString();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | std::string genPrettyType( Type * type, const std::string & baseString ) {
|
|---|
| 72 | return genType( type, baseString, true, false );
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | GenType::GenType( const std::string &typeString, bool pretty, bool genC ) : typeString( typeString ), pretty( pretty ), genC( genC ) {}
|
|---|
| 76 |
|
|---|
| 77 | void GenType::visit( VoidType *voidType ) {
|
|---|
| 78 | typeString = "void " + typeString;
|
|---|
| 79 | handleQualifiers( voidType );
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void GenType::visit( BasicType *basicType ) {
|
|---|
| 83 | BasicType::Kind kind = basicType->get_kind();
|
|---|
| 84 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
|
|---|
| 85 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
|
|---|
| 86 | handleQualifiers( basicType );
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
|
|---|
| 90 | std::ostringstream os;
|
|---|
| 91 | if ( typeString != "" ) {
|
|---|
| 92 | if ( typeString[ 0 ] == '*' ) {
|
|---|
| 93 | os << "(" << typeString << ")";
|
|---|
| 94 | } else {
|
|---|
| 95 | os << typeString;
|
|---|
| 96 | } // if
|
|---|
| 97 | } // if
|
|---|
| 98 | os << "[";
|
|---|
| 99 |
|
|---|
| 100 | if ( isStatic ) {
|
|---|
| 101 | os << "static ";
|
|---|
| 102 | } // if
|
|---|
| 103 | if ( qualifiers.is_const ) {
|
|---|
| 104 | os << "const ";
|
|---|
| 105 | } // if
|
|---|
| 106 | if ( qualifiers.is_volatile ) {
|
|---|
| 107 | os << "volatile ";
|
|---|
| 108 | } // if
|
|---|
| 109 | if ( qualifiers.is_restrict ) {
|
|---|
| 110 | os << "__restrict ";
|
|---|
| 111 | } // if
|
|---|
| 112 | if ( qualifiers.is_atomic ) {
|
|---|
| 113 | os << "_Atomic ";
|
|---|
| 114 | } // if
|
|---|
| 115 | if ( dimension != 0 ) {
|
|---|
| 116 | CodeGenerator cg( os, pretty, genC );
|
|---|
| 117 | dimension->accept( cg );
|
|---|
| 118 | } else if ( isVarLen ) {
|
|---|
| 119 | // no dimension expression on a VLA means it came in with the * token
|
|---|
| 120 | os << "*";
|
|---|
| 121 | } // if
|
|---|
| 122 | os << "]";
|
|---|
| 123 |
|
|---|
| 124 | typeString = os.str();
|
|---|
| 125 |
|
|---|
| 126 | base->accept( *this );
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void GenType::visit( PointerType *pointerType ) {
|
|---|
| 130 | assert( pointerType->get_base() != 0);
|
|---|
| 131 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
|
|---|
| 132 | genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
|
|---|
| 133 | } else {
|
|---|
| 134 | handleQualifiers( pointerType );
|
|---|
| 135 | if ( typeString[ 0 ] == '?' ) {
|
|---|
| 136 | typeString = "* " + typeString;
|
|---|
| 137 | } else {
|
|---|
| 138 | typeString = "*" + typeString;
|
|---|
| 139 | } // if
|
|---|
| 140 | pointerType->get_base()->accept( *this );
|
|---|
| 141 | } // if
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | void GenType::visit( ArrayType *arrayType ) {
|
|---|
| 145 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void GenType::visit( FunctionType *funcType ) {
|
|---|
| 149 | std::ostringstream os;
|
|---|
| 150 |
|
|---|
| 151 | if ( typeString != "" ) {
|
|---|
| 152 | if ( typeString[ 0 ] == '*' ) {
|
|---|
| 153 | os << "(" << typeString << ")";
|
|---|
| 154 | } else {
|
|---|
| 155 | os << typeString;
|
|---|
| 156 | } // if
|
|---|
| 157 | } // if
|
|---|
| 158 |
|
|---|
| 159 | /************* parameters ***************/
|
|---|
| 160 |
|
|---|
| 161 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
|
|---|
| 162 |
|
|---|
| 163 | if ( pars.empty() ) {
|
|---|
| 164 | if ( funcType->get_isVarArgs() ) {
|
|---|
| 165 | os << "()";
|
|---|
| 166 | } else {
|
|---|
| 167 | os << "(void)";
|
|---|
| 168 | } // if
|
|---|
| 169 | } else {
|
|---|
| 170 | CodeGenerator cg( os, pretty, genC );
|
|---|
| 171 | os << "(" ;
|
|---|
| 172 |
|
|---|
| 173 | cg.genCommaList( pars.begin(), pars.end() );
|
|---|
| 174 |
|
|---|
| 175 | if ( funcType->get_isVarArgs() ) {
|
|---|
| 176 | os << ", ...";
|
|---|
| 177 | } // if
|
|---|
| 178 | os << ")";
|
|---|
| 179 | } // if
|
|---|
| 180 |
|
|---|
| 181 | typeString = os.str();
|
|---|
| 182 |
|
|---|
| 183 | if ( funcType->get_returnVals().size() == 0 ) {
|
|---|
| 184 | typeString = "void " + typeString;
|
|---|
| 185 | } else {
|
|---|
| 186 | funcType->get_returnVals().front()->get_type()->accept( *this );
|
|---|
| 187 | } // if
|
|---|
| 188 |
|
|---|
| 189 | // add forall
|
|---|
| 190 | if( ! funcType->get_forall().empty() && ! genC ) {
|
|---|
| 191 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
|
|---|
| 192 | std::ostringstream os;
|
|---|
| 193 | CodeGenerator cg( os, pretty, genC );
|
|---|
| 194 | os << "forall(";
|
|---|
| 195 | cg.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
|
|---|
| 196 | os << ")" << std::endl;
|
|---|
| 197 | typeString = os.str() + typeString;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | std::string GenType::handleGeneric( ReferenceToType * refType ) {
|
|---|
| 202 | if ( ! refType->get_parameters().empty() ) {
|
|---|
| 203 | std::ostringstream os;
|
|---|
| 204 | CodeGenerator cg( os, pretty, genC );
|
|---|
| 205 | os << "(";
|
|---|
| 206 | cg.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );
|
|---|
| 207 | os << ") ";
|
|---|
| 208 | return os.str();
|
|---|
| 209 | }
|
|---|
| 210 | return "";
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void GenType::visit( StructInstType *structInst ) {
|
|---|
| 214 | typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
|
|---|
| 215 | if ( genC ) typeString = "struct " + typeString;
|
|---|
| 216 | handleQualifiers( structInst );
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | void GenType::visit( UnionInstType *unionInst ) {
|
|---|
| 220 | typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
|
|---|
| 221 | if ( genC ) typeString = "union " + typeString;
|
|---|
| 222 | handleQualifiers( unionInst );
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | void GenType::visit( EnumInstType *enumInst ) {
|
|---|
| 226 | typeString = enumInst->get_name() + " " + typeString;
|
|---|
| 227 | if ( genC ) typeString = "enum " + typeString;
|
|---|
| 228 | handleQualifiers( enumInst );
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | void GenType::visit( TypeInstType *typeInst ) {
|
|---|
| 232 | typeString = typeInst->get_name() + " " + typeString;
|
|---|
| 233 | handleQualifiers( typeInst );
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void GenType::visit( TupleType * tupleType ) {
|
|---|
| 237 | assertf( ! genC, "Tuple types should not reach code generation." );
|
|---|
| 238 | Visitor::visit( tupleType );
|
|---|
| 239 | unsigned int i = 0;
|
|---|
| 240 | std::ostringstream os;
|
|---|
| 241 | os << "[";
|
|---|
| 242 | for ( Type * t : *tupleType ) {
|
|---|
| 243 | i++;
|
|---|
| 244 | os << genType( t, "", pretty, genC ) << (i == tupleType->size() ? "" : ", ");
|
|---|
| 245 | }
|
|---|
| 246 | os << "]";
|
|---|
| 247 | typeString = os.str() + typeString;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | void GenType::visit( VarArgsType *varArgsType ) {
|
|---|
| 251 | typeString = "__builtin_va_list " + typeString;
|
|---|
| 252 | handleQualifiers( varArgsType );
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | void GenType::visit( ZeroType *zeroType ) {
|
|---|
| 256 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints
|
|---|
| 257 | typeString = (pretty ? "zero_t " : "long int ") + typeString;
|
|---|
| 258 | handleQualifiers( zeroType );
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | void GenType::visit( OneType *oneType ) {
|
|---|
| 262 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints
|
|---|
| 263 | typeString = (pretty ? "one_t " : "long int ") + typeString;
|
|---|
| 264 | handleQualifiers( oneType );
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | void GenType::handleQualifiers( Type *type ) {
|
|---|
| 268 | if ( type->get_const() ) {
|
|---|
| 269 | typeString = "const " + typeString;
|
|---|
| 270 | } // if
|
|---|
| 271 | if ( type->get_volatile() ) {
|
|---|
| 272 | typeString = "volatile " + typeString;
|
|---|
| 273 | } // if
|
|---|
| 274 | if ( type->get_restrict() ) {
|
|---|
| 275 | typeString = "__restrict " + typeString;
|
|---|
| 276 | } // if
|
|---|
| 277 | if ( type->get_atomic() ) {
|
|---|
| 278 | typeString = "_Atomic " + typeString;
|
|---|
| 279 | } // if
|
|---|
| 280 | }
|
|---|
| 281 | } // namespace CodeGen
|
|---|
| 282 |
|
|---|
| 283 | // Local Variables: //
|
|---|
| 284 | // tab-width: 4 //
|
|---|
| 285 | // mode: c++ //
|
|---|
| 286 | // compile-command: "make install" //
|
|---|
| 287 | // End: //
|
|---|