11#include " TypeTranslator.h"
22#include " Utils.h"
3+ #include " ir/types/FunctionPointerType.h"
4+ #include " ir/types/PointerType.h"
35
4- TypeTranslator::TypeTranslator (clang::ASTContext *ctx_) : ctx(ctx_), typeMap() {
6+ TypeTranslator::TypeTranslator (clang::ASTContext *ctx_, IR &ir)
7+ : ctx(ctx_), ir(ir), typeMap() {
58
69 // Native Types
710 typeMap[" void" ] = " Unit" ;
@@ -27,38 +30,24 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_) : ctx(ctx_), typeMap() {
2730 typeMap[" char32_t" ] = " native.CChar32" ;
2831 typeMap[" float" ] = " native.CFloat" ;
2932 typeMap[" double" ] = " native.CDouble" ;
30- typeMap[" void*" ] = " native.Ptr[Byte]" ;
3133}
3234
33- std::string
34- TypeTranslator::TranslateFunctionPointer (const clang::QualType &qtpe,
35- const std::string *avoid) {
36- const clang::PointerType *ptr =
37- qtpe.getTypePtr ()->getAs <clang::PointerType>();
35+ Type *TypeTranslator::translateFunctionPointer (const clang::QualType &qtpe,
36+ const std::string *avoid) {
37+ const auto *ptr = qtpe.getTypePtr ()->getAs <clang::PointerType>();
3838 const clang::QualType &inner = ptr->getPointeeType ();
3939
4040 if (inner->isFunctionProtoType ()) {
41- const clang::FunctionProtoType *fc =
42- inner->getAs <clang::FunctionProtoType>();
43- std::string ret = Translate (fc->getReturnType (), avoid);
44- std::string params = " " ;
45- int counter = 0 ;
41+ const auto *fc = inner->getAs <clang::FunctionProtoType>();
42+ Type *returnType = translate (fc->getReturnType (), avoid);
43+ std::vector<Type *> parametersTypes;
4644
4745 for (const clang::QualType ¶m : fc->param_types ()) {
48- params += Translate (param, avoid);
49- params += " , " ;
50- counter++;
46+ parametersTypes.push_back (translate (param, avoid));
5147 }
5248
53- std::string variad = " " ;
54-
55- if (fc->isVariadic ()) {
56- counter++;
57- variad = " native.CVararg, " ;
58- }
59-
60- return std::string (" native.CFunctionPtr" ) + std::to_string (counter) +
61- " [" + params + variad + ret + " ]" ;
49+ return new FunctionPointerType (returnType, parametersTypes,
50+ fc->isVariadic ());
6251
6352 } else {
6453 llvm::errs () << " Unsupported function pointer type: "
@@ -68,111 +57,111 @@ TypeTranslator::TranslateFunctionPointer(const clang::QualType &qtpe,
6857 }
6958}
7059
71- std::string TypeTranslator::TranslatePointer (const clang::QualType &pte,
72- const std::string *avoid) {
60+ Type * TypeTranslator::translatePointer (const clang::QualType &pte,
61+ const std::string *avoid) {
7362
7463 if (pte->isBuiltinType ()) {
7564 const clang::BuiltinType *as = pte->getAs <clang::BuiltinType>();
7665
7766 // Take care of void*
7867 if (as->getKind () == clang::BuiltinType::Void) {
79- return " native.Ptr[ Byte] " ;
68+ return new PointerType ( new PrimitiveType ( " Byte" )) ;
8069 }
8170
8271 // Take care of char*
8372 if (as->getKind () == clang::BuiltinType::Char_S ||
8473 as->getKind () == clang::BuiltinType::SChar) {
85- return " native.CString" ;
74+ // TODO: new PointerType(new PrimitiveType("native.CChar"))
75+ return new PrimitiveType (" native.CString" );
8676 }
8777 }
8878
89- return std::string (" native.Ptr[" ) + Translate (pte, avoid) +
90- std::string (" ]" );
79+ return new PointerType (translate (pte, avoid));
9180}
9281
93- std::string
94- TypeTranslator::TranslateStructOrUnion (const clang::QualType &qtpe) {
95- if (qtpe->hasUnnamedOrLocalType ()) {
96- // TODO: Verify that the local part is not a problem
97- uint64_t size = ctx->getTypeSize (qtpe);
98- return " native.CArray[Byte, " + uint64ToScalaNat (size) + " ]" ;
99- }
100-
82+ Type *
83+ TypeTranslator::translateStructOrUnionOrEnum (const clang::QualType &qtpe) {
10184 std::string name = qtpe.getUnqualifiedType ().getAsString ();
10285
103- // TODO: do it properly
104- size_t f = name.find (std::string (" struct __dirstream" ));
105- if (f != std::string::npos) {
106- return std::string (" native.CArray[Byte, Digit[_3, Digit[_2, _0]]]" );
107- }
108-
109- f = name.find (" " );
110- if (f != std::string::npos) {
111- return name.replace (f, std::string (" " ).length (), " _" );
86+ auto it = aliasesMap.find (name);
87+ if (it != aliasesMap.end ()) {
88+ /* name contains space: struct <name>.
89+ * Use type alias instead struct type */
90+ return (*it).second ;
11291 }
113- return name;
92+ /* type has typedef alias */
93+ return ir.getTypeDefWithName (name);
11494}
11595
116- std::string TypeTranslator::TranslateEnum (const clang::QualType &qtpe) {
117- std::string name = qtpe. getUnqualifiedType (). getAsString ();
118- size_t f = name. find ( " " );
119- if (f != std::string::npos) {
120- return name. replace (f, std::string ( " " ). length (), " _ " );
96+ Type * TypeTranslator::translateStructOrUnion (const clang::QualType &qtpe) {
97+ if ( qtpe-> hasUnnamedOrLocalType ()) {
98+ // TODO: Verify that the local part is not a problem
99+ uint64_t size = ctx-> getTypeSize (qtpe);
100+ return new ArrayType ( new PrimitiveType ( " Byte " ), size );
121101 }
122- return name;
102+
103+ return translateStructOrUnionOrEnum (qtpe);
123104}
124105
125- std::string
126- TypeTranslator::TranslateConstantArray (const clang::ConstantArrayType *ar,
127- const std::string *avoid) {
106+ Type *TypeTranslator::translateConstantArray (const clang::ConstantArrayType *ar,
107+ const std::string *avoid) {
128108 const uint64_t size = ar->getSize ().getZExtValue ();
129- const std::string nat = uint64ToScalaNat (size);
130- return " native.CArray[" + Translate (ar->getElementType (), avoid) + " , " +
131- nat + " ]" ;
109+ return new ArrayType (translate (ar->getElementType (), avoid), size);
132110}
133111
134- std::string TypeTranslator::Translate (const clang::QualType &qtpe,
135- const std::string *avoid) {
112+ Type * TypeTranslator::translate (const clang::QualType &qtpe,
113+ const std::string *avoid) {
136114
137115 const clang::Type *tpe = qtpe.getTypePtr ();
138116
139117 if (typeEquals (tpe, avoid)) {
140118 // This is a type that we want to avoid the usage.
141- // Êxample: A struct that has a pointer to itself
119+ // Êxample: A struct that has a pointer to itself
142120 uint64_t size = ctx->getTypeSize (tpe);
143- return " native.CArray[ Byte, " + uint64ToScalaNat ( size) + " ] " ;
121+ return new ArrayType ( new PrimitiveType ( " Byte" ), size);
144122 }
145123
146124 if (tpe->isFunctionPointerType ()) {
147- return TranslateFunctionPointer (qtpe, avoid);
125+ return translateFunctionPointer (qtpe, avoid);
148126
149127 } else if (tpe->isPointerType ()) {
150- return TranslatePointer (
128+ return translatePointer (
151129 tpe->getAs <clang::PointerType>()->getPointeeType (), avoid);
152130
153- } else if (qtpe->isStructureType () || qtpe->isUnionType ()) {
154- return handleReservedWords (TranslateStructOrUnion (qtpe));
131+ } else if (qtpe->isStructureType ()) {
132+ return translateStructOrUnion (qtpe);
133+
134+ } else if (qtpe->isUnionType ()) {
135+ return translateStructOrUnion (qtpe);
155136
156137 } else if (qtpe->isEnumeralType ()) {
157- return TranslateEnum (qtpe);
138+ return translateStructOrUnionOrEnum (qtpe);
158139
159140 } else if (qtpe->isConstantArrayType ()) {
160- return TranslateConstantArray (ctx->getAsConstantArrayType (qtpe), avoid);
141+ return translateConstantArray (ctx->getAsConstantArrayType (qtpe), avoid);
161142 } else if (qtpe->isArrayType ()) {
162- return TranslatePointer (ctx->getAsArrayType (qtpe)->getElementType (),
143+ return translatePointer (ctx->getAsArrayType (qtpe)->getElementType (),
163144 avoid);
164145 } else {
165146
166147 auto found = typeMap.find (qtpe.getUnqualifiedType ().getAsString ());
167148 if (found != typeMap.end ()) {
168- return handleReservedWords (found->second );
149+ return new PrimitiveType (found->second );
169150 } else {
170- // TODO: Properly handle non-default types
171- return handleReservedWords ( qtpe.getUnqualifiedType ().getAsString ());
151+ return ir. getTypeDefWithName (
152+ qtpe.getUnqualifiedType ().getAsString ());
172153 }
173154 }
174155}
175156
176- void TypeTranslator::AddTranslation (std::string from, std::string to) {
177- typeMap[from] = to;
157+ void TypeTranslator::addAlias (std::string cName, Type *type) {
158+ aliasesMap[cName] = type;
159+ }
160+
161+ std::string TypeTranslator::getTypeFromTypeMap (std::string cType) {
162+ auto it = typeMap.find (cType);
163+ if (it != typeMap.end ()) {
164+ return (*it).second ;
165+ }
166+ return " " ;
178167}
0 commit comments