| 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 : Thu Feb  2 13:53:43 2017
 | 
|---|
| 13 | // Update Count     : 20
 | 
|---|
| 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 );
 | 
|---|
| 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 |                 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
 | 
|---|
| 51 | 
 | 
|---|
| 52 |                 std::string typeString;
 | 
|---|
| 53 |                 bool pretty = false; // pretty print
 | 
|---|
| 54 |         };
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         std::string genType( Type *type, const std::string &baseString, bool pretty ) {
 | 
|---|
| 57 |                 GenType gt( baseString, pretty );
 | 
|---|
| 58 |                 std::ostringstream os;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |                 if ( ! type->get_attributes().empty() ) {
 | 
|---|
| 61 |                         CodeGenerator cg( os, pretty );
 | 
|---|
| 62 |                         cg.genAttributes( type->get_attributes() );
 | 
|---|
| 63 |                 } // if
 | 
|---|
| 64 | 
 | 
|---|
| 65 |                 type->accept( gt );
 | 
|---|
| 66 |                 return os.str() + gt.get_typeString();
 | 
|---|
| 67 |         }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   std::string genPrettyType( Type * type, const std::string & baseString ) {
 | 
|---|
| 70 |         return genType( type, baseString, true );
 | 
|---|
| 71 |   }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         GenType::GenType( const std::string &typeString, bool pretty ) : typeString( typeString ), pretty( pretty ) {}
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         void GenType::visit( VoidType *voidType ) {
 | 
|---|
| 76 |                 typeString = "void " + typeString;
 | 
|---|
| 77 |                 handleQualifiers( voidType );
 | 
|---|
| 78 |         }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         void GenType::visit( BasicType *basicType ) {
 | 
|---|
| 81 |                 BasicType::Kind kind = basicType->get_kind();
 | 
|---|
| 82 |                 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
 | 
|---|
| 83 |                 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
 | 
|---|
| 84 |                 handleQualifiers( basicType );
 | 
|---|
| 85 |         }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |         void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
 | 
|---|
| 88 |                 std::ostringstream os;
 | 
|---|
| 89 |                 if ( typeString != "" ) {
 | 
|---|
| 90 |                         if ( typeString[ 0 ] == '*' ) {
 | 
|---|
| 91 |                                 os << "(" << typeString << ")";
 | 
|---|
| 92 |                         } else {
 | 
|---|
| 93 |                                 os << typeString;
 | 
|---|
| 94 |                         } // if
 | 
|---|
| 95 |                 } // if
 | 
|---|
| 96 |                 os << "[";
 | 
|---|
| 97 | 
 | 
|---|
| 98 |                 if ( isStatic ) {
 | 
|---|
| 99 |                         os << "static ";
 | 
|---|
| 100 |                 } // if
 | 
|---|
| 101 |                 if ( qualifiers.isConst ) {
 | 
|---|
| 102 |                         os << "const ";
 | 
|---|
| 103 |                 } // if
 | 
|---|
| 104 |                 if ( qualifiers.isVolatile ) {
 | 
|---|
| 105 |                         os << "volatile ";
 | 
|---|
| 106 |                 } // if
 | 
|---|
| 107 |                 if ( qualifiers.isRestrict ) {
 | 
|---|
| 108 |                         os << "__restrict ";
 | 
|---|
| 109 |                 } // if
 | 
|---|
| 110 |                 if ( qualifiers.isAtomic ) {
 | 
|---|
| 111 |                         os << "_Atomic ";
 | 
|---|
| 112 |                 } // if
 | 
|---|
| 113 |                 if ( dimension != 0 ) {
 | 
|---|
| 114 |                         CodeGenerator cg( os, pretty );
 | 
|---|
| 115 |                         dimension->accept( cg );
 | 
|---|
| 116 |                 } else if ( isVarLen ) {
 | 
|---|
| 117 |                         // no dimension expression on a VLA means it came in with the * token
 | 
|---|
| 118 |                         os << "*";
 | 
|---|
| 119 |                 } // if
 | 
|---|
| 120 |                 os << "]";
 | 
|---|
| 121 | 
 | 
|---|
| 122 |                 typeString = os.str();
 | 
|---|
| 123 | 
 | 
|---|
| 124 |                 base->accept( *this );
 | 
|---|
| 125 |         }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         void GenType::visit( PointerType *pointerType ) {
 | 
|---|
| 128 |                 assert( pointerType->get_base() != 0);
 | 
|---|
| 129 |                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
 | 
|---|
| 130 |                         genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
 | 
|---|
| 131 |                 } else {
 | 
|---|
| 132 |                         handleQualifiers( pointerType );
 | 
|---|
| 133 |                         if ( typeString[ 0 ] == '?' ) {
 | 
|---|
| 134 |                                 typeString = "* " + typeString;
 | 
|---|
| 135 |                         } else {
 | 
|---|
| 136 |                                 typeString = "*" + typeString;
 | 
|---|
| 137 |                         } // if
 | 
|---|
| 138 |                         pointerType->get_base()->accept( *this );
 | 
|---|
| 139 |                 } // if
 | 
|---|
| 140 |         }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         void GenType::visit( ArrayType *arrayType ) {
 | 
|---|
| 143 |                 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
 | 
|---|
| 144 |         }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         void GenType::visit( FunctionType *funcType ) {
 | 
|---|
| 147 |                 std::ostringstream os;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |                 if ( typeString != "" ) {
 | 
|---|
| 150 |                         if ( typeString[ 0 ] == '*' ) {
 | 
|---|
| 151 |                                 os << "(" << typeString << ")";
 | 
|---|
| 152 |                         } else {
 | 
|---|
| 153 |                                 os << typeString;
 | 
|---|
| 154 |                         } // if
 | 
|---|
| 155 |                 } // if
 | 
|---|
| 156 | 
 | 
|---|
| 157 |                 /************* parameters ***************/
 | 
|---|
| 158 | 
 | 
|---|
| 159 |                 const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
 | 
|---|
| 160 | 
 | 
|---|
| 161 |                 if ( pars.empty() ) {
 | 
|---|
| 162 |                         if ( funcType->get_isVarArgs() ) {
 | 
|---|
| 163 |                                 os << "()";
 | 
|---|
| 164 |                         } else {
 | 
|---|
| 165 |                                 os << "(void)";
 | 
|---|
| 166 |                         } // if
 | 
|---|
| 167 |                 } else {
 | 
|---|
| 168 |                         CodeGenerator cg( os, pretty );
 | 
|---|
| 169 |                         os << "(" ;
 | 
|---|
| 170 | 
 | 
|---|
| 171 |                         cg.genCommaList( pars.begin(), pars.end() );
 | 
|---|
| 172 | 
 | 
|---|
| 173 |                         if ( funcType->get_isVarArgs() ) {
 | 
|---|
| 174 |                                 os << ", ...";
 | 
|---|
| 175 |                         } // if
 | 
|---|
| 176 |                         os << ")";
 | 
|---|
| 177 |                 } // if
 | 
|---|
| 178 | 
 | 
|---|
| 179 |                 typeString = os.str();
 | 
|---|
| 180 | 
 | 
|---|
| 181 |                 if ( funcType->get_returnVals().size() == 0 ) {
 | 
|---|
| 182 |                         typeString = "void " + typeString;
 | 
|---|
| 183 |                 } else {
 | 
|---|
| 184 |                         funcType->get_returnVals().front()->get_type()->accept( *this );
 | 
|---|
| 185 |                 } // if
 | 
|---|
| 186 |         }
 | 
|---|
| 187 | 
 | 
|---|
| 188 |         void GenType::visit( StructInstType *structInst )  {
 | 
|---|
| 189 |                 typeString = "struct " + structInst->get_name() + " " + typeString;
 | 
|---|
| 190 |                 handleQualifiers( structInst );
 | 
|---|
| 191 |         }
 | 
|---|
| 192 | 
 | 
|---|
| 193 |         void GenType::visit( UnionInstType *unionInst ) {
 | 
|---|
| 194 |                 typeString = "union " + unionInst->get_name() + " " + typeString;
 | 
|---|
| 195 |                 handleQualifiers( unionInst );
 | 
|---|
| 196 |         }
 | 
|---|
| 197 | 
 | 
|---|
| 198 |         void GenType::visit( EnumInstType *enumInst ) {
 | 
|---|
| 199 |                 typeString = "enum " + enumInst->get_name() + " " + typeString;
 | 
|---|
| 200 |                 handleQualifiers( enumInst );
 | 
|---|
| 201 |         }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |         void GenType::visit( TypeInstType *typeInst ) {
 | 
|---|
| 204 |                 typeString = typeInst->get_name() + " " + typeString;
 | 
|---|
| 205 |                 handleQualifiers( typeInst );
 | 
|---|
| 206 |         }
 | 
|---|
| 207 | 
 | 
|---|
| 208 |         void GenType::visit( TupleType * tupleType ) {
 | 
|---|
| 209 |                 assertf( pretty, "Tuple types should not make it to Code Gen." );
 | 
|---|
| 210 |                 Visitor::visit( tupleType );
 | 
|---|
| 211 |                 unsigned int i = 0;
 | 
|---|
| 212 |                 std::ostringstream os;
 | 
|---|
| 213 |                 os << "[";
 | 
|---|
| 214 |                 for ( Type * t : *tupleType ) {
 | 
|---|
| 215 |                         i++;
 | 
|---|
| 216 |                         os << genType( t, "", pretty ) << (i == tupleType->size() ? "" : ", ");
 | 
|---|
| 217 |                 }
 | 
|---|
| 218 |                 os << "]";
 | 
|---|
| 219 |                 typeString = os.str() + typeString;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         void GenType::visit( VarArgsType *varArgsType ) {
 | 
|---|
| 223 |                 typeString = "__builtin_va_list " + typeString;
 | 
|---|
| 224 |                 handleQualifiers( varArgsType );
 | 
|---|
| 225 |         }
 | 
|---|
| 226 | 
 | 
|---|
| 227 |         void GenType::visit( ZeroType *zeroType ) {
 | 
|---|
| 228 |                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
 | 
|---|
| 229 |                 typeString = (pretty ? "zero_t " : "long int ") + typeString;
 | 
|---|
| 230 |                 handleQualifiers( zeroType );
 | 
|---|
| 231 |         }
 | 
|---|
| 232 | 
 | 
|---|
| 233 |         void GenType::visit( OneType *oneType ) {
 | 
|---|
| 234 |                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
 | 
|---|
| 235 |                 typeString = (pretty ? "one_t " : "long int ") + typeString;
 | 
|---|
| 236 |                 handleQualifiers( oneType );
 | 
|---|
| 237 |         }
 | 
|---|
| 238 | 
 | 
|---|
| 239 |         void GenType::handleQualifiers( Type *type ) {
 | 
|---|
| 240 |                 if ( type->get_isConst() ) {
 | 
|---|
| 241 |                         typeString = "const " + typeString;
 | 
|---|
| 242 |                 } // if
 | 
|---|
| 243 |                 if ( type->get_isVolatile() ) {
 | 
|---|
| 244 |                         typeString = "volatile " + typeString;
 | 
|---|
| 245 |                 } // if
 | 
|---|
| 246 |                 if ( type->get_isRestrict() ) {
 | 
|---|
| 247 |                         typeString = "__restrict " + typeString;
 | 
|---|
| 248 |                 } // if
 | 
|---|
| 249 |                 if ( type->get_isAtomic() ) {
 | 
|---|
| 250 |                         typeString = "_Atomic " + typeString;
 | 
|---|
| 251 |                 } // if
 | 
|---|
| 252 |         }
 | 
|---|
| 253 | } // namespace CodeGen
 | 
|---|
| 254 | 
 | 
|---|
| 255 | // Local Variables: //
 | 
|---|
| 256 | // tab-width: 4 //
 | 
|---|
| 257 | // mode: c++ //
 | 
|---|
| 258 | // compile-command: "make install" //
 | 
|---|
| 259 | // End: //
 | 
|---|