source: translator/CodeGen/GenType.cc @ 91b216b4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 91b216b4 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * $Id: GenType.cc,v 1.6 2005/08/29 20:14:12 rcbilson Exp $
5 *
6 */
7
8#include <strstream>
9#include <cassert>
10
11#include "GenType.h"
12#include "CodeGenerator2.h"
13#include "SynTree/Visitor.h"
14#include "SynTree/Type.h"
15#include "SynTree/Expression.h"
16
17namespace CodeGen {
18
19class GenType : public Visitor
20{
21public:
22  GenType( const std::string &typeString );
23  std::string get_typeString() const { return typeString; }
24  void set_typeString( const std::string &newValue ) { typeString = newValue; }
25 
26  virtual void visit( FunctionType *funcType );
27  virtual void visit( VoidType *voidType );
28  virtual void visit( BasicType *basicType );
29  virtual void visit( PointerType *pointerType );
30  virtual void visit( ArrayType *arrayType );
31  virtual void visit( StructInstType *structInst );
32  virtual void visit( UnionInstType *unionInst );
33  virtual void visit( EnumInstType *enumInst );
34  virtual void visit( TypeInstType *typeInst );
35 
36private:
37  void handleQualifiers( Type *type );
38  void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
39 
40  std::string typeString;
41};
42
43std::string
44genType( Type *type, const std::string &baseString )
45{
46  GenType gt( baseString );
47  type->accept( gt );
48  return gt.get_typeString();
49}
50
51GenType::GenType( const std::string &typeString )
52  : typeString( typeString )
53{
54}
55
56void GenType::visit(VoidType *voidType){
57  typeString = "void " + typeString;
58  handleQualifiers( voidType );
59}
60
61void GenType::visit(BasicType *basicType)
62{
63  std::string typeWords;
64  switch(basicType->get_kind()){
65  case BasicType::Bool:
66    typeWords = "bool";
67    break;
68  case BasicType::Char:
69  case BasicType::SignedChar:
70    typeWords = "char";
71    break;
72  case BasicType::UnsignedChar:
73    typeWords = "unsigned char";
74    break;
75  case BasicType::ShortSignedInt:
76    typeWords = "short";
77    break;
78  case BasicType::ShortUnsignedInt:
79    typeWords = "short unsigned";
80    break;
81  case BasicType::SignedInt:
82    typeWords = "int";
83    break;
84  case BasicType::UnsignedInt:
85    typeWords = "unsigned int";
86    break;
87  case BasicType::LongSignedInt:
88    typeWords = "long int";
89    break;
90  case BasicType::LongUnsignedInt:
91    typeWords = "long unsigned int";
92    break;
93  case BasicType::LongLongSignedInt:
94    typeWords = "long long int";
95    break;
96  case BasicType::LongLongUnsignedInt:
97    typeWords = "long long unsigned int";
98    break;
99  case BasicType::Float:
100    typeWords = "float";
101    break;
102  case BasicType::Double:
103    typeWords = "double";
104    break;
105  case BasicType::LongDouble:
106    typeWords = "long double";
107    break;
108  case BasicType::FloatComplex:
109    typeWords = "float _Complex";
110    break;
111  case BasicType::DoubleComplex:
112    typeWords = "double _Complex";
113    break;
114  case BasicType::LongDoubleComplex:
115    typeWords = "long double _Complex";
116    break;
117  case BasicType::FloatImaginary:
118    typeWords = "float _Imaginary";
119    break;
120  case BasicType::DoubleImaginary:
121    typeWords = "double _Imaginary";
122    break;
123  case BasicType::LongDoubleImaginary:
124    typeWords = "long double _Imaginary";
125    break;
126  default:
127    assert( false );
128  }
129  typeString = typeWords + " " + typeString;
130  handleQualifiers( basicType );
131}
132
133void
134GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic )
135{
136  std::ostrstream os;
137  if( typeString != "" ) {
138    if( typeString[ 0 ] == '*' ) {
139      os << "(" << typeString << ")";
140    } else {
141      os << typeString;
142    }
143  }
144  os << "[";
145  if( isStatic ) {
146    os << "static ";
147  }
148  if( qualifiers.isConst ) {
149    os << "const ";
150  }
151  if( qualifiers.isVolatile ) {
152    os << "volatile ";
153  }
154  if( isVarLen ) {
155    os << "*";
156  }
157  if( dimension != 0 ) {
158    CodeGenerator2 cg( os );
159    dimension->accept( cg );
160  }
161  os << "]";
162 
163  typeString = std::string( os.str(), os.pcount() );
164 
165  base->accept ( *this );
166}
167
168void GenType::visit(PointerType *pointerType) {
169  assert(pointerType->get_base() != 0);
170  if( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
171    genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
172  } else {
173    handleQualifiers( pointerType );
174    if( typeString[ 0 ] == '?' ) {
175      typeString = "* " + typeString;
176    } else {
177      typeString = "*" + typeString;
178    }
179    pointerType->get_base()->accept ( *this );
180  }
181}
182
183void GenType::visit(ArrayType *arrayType){
184  genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
185}
186
187void GenType::visit(FunctionType *funcType) {
188  std::ostrstream os;
189
190  if( typeString != "" ) {
191    if( typeString[ 0 ] == '*' ) {
192      os << "(" << typeString << ")";
193    } else {
194      os << typeString;
195    }
196  }
197 
198  /************* parameters ***************/
199
200  const std::list<DeclarationWithType*> &pars = funcType->get_parameters();
201
202  if( pars.empty() ) {
203    if( funcType->get_isVarArgs() ) {
204      os << "()";
205    } else {
206      os << "(void)";
207    }
208  } else {
209    CodeGenerator2 cg( os );
210    os << "(" ;
211
212    cg.genCommaList( pars.begin(), pars.end() );
213   
214    if( funcType->get_isVarArgs() ){
215      os << ", ...";
216    }
217    os << ")";
218  }
219 
220  typeString = std::string( os.str(), os.pcount() );
221
222  if( funcType->get_returnVals().size() == 0 ) {
223    typeString = "void " + typeString;
224  } else {
225
226    funcType->get_returnVals().front()->get_type()->accept( *this );
227
228  }
229 
230}
231
232void GenType::visit( StructInstType *structInst ) 
233{
234  typeString = "struct " + structInst->get_name() + " " + typeString;
235  handleQualifiers( structInst );
236}
237
238void 
239GenType::visit( UnionInstType *unionInst )
240{
241  typeString = "union " + unionInst->get_name() + " " + typeString;
242  handleQualifiers( unionInst );
243}
244
245void 
246GenType::visit( EnumInstType *enumInst )
247{
248  typeString = "enum " + enumInst->get_name() + " " + typeString;
249  handleQualifiers( enumInst );
250}
251
252void 
253GenType::visit( TypeInstType *typeInst )
254{
255  typeString = typeInst->get_name() + " " + typeString;
256  handleQualifiers( typeInst );
257}
258
259void 
260GenType::handleQualifiers( Type *type )
261{
262  if( type->get_isConst() ) {
263    typeString = "const " + typeString;
264  }
265  if( type->get_isVolatile() ) {
266    typeString = "volatile " + typeString;
267  }
268  if( type->get_isRestrict() ) {
269    typeString = "__restrict " + typeString;
270  }
271}
272
273} // namespace CodeGen
Note: See TracBrowser for help on using the repository browser.