@@ -32,22 +32,24 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
3232 typeMap[" double" ] = " native.CDouble" ;
3333}
3434
35- Type *TypeTranslator::translateFunctionPointer (const clang::QualType &qtpe,
36- const std::string *avoid) {
35+ std::shared_ptr<Type>
36+ TypeTranslator::translateFunctionPointer (const clang::QualType &qtpe,
37+ const std::string *avoid) {
3738 const auto *ptr = qtpe.getTypePtr ()->getAs <clang::PointerType>();
3839 const clang::QualType &inner = ptr->getPointeeType ();
3940
4041 if (inner->isFunctionProtoType ()) {
4142 const auto *fc = inner->getAs <clang::FunctionProtoType>();
42- Type *returnType = translate (fc->getReturnType (), avoid);
43- std::vector<Type *> parametersTypes;
43+ std::shared_ptr<Type> returnType =
44+ translate (fc->getReturnType (), avoid);
45+ std::vector<std::shared_ptr<Type>> parametersTypes;
4446
4547 for (const clang::QualType ¶m : fc->param_types ()) {
4648 parametersTypes.push_back (translate (param, avoid));
4749 }
4850
49- return new FunctionPointerType (returnType, parametersTypes,
50- fc->isVariadic ());
51+ return std::make_shared< FunctionPointerType>(
52+ returnType, parametersTypes, fc->isVariadic ());
5153
5254 } else {
5355 llvm::errs () << " Unsupported function pointer type: "
@@ -57,29 +59,31 @@ Type *TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
5759 }
5860}
5961
60- Type *TypeTranslator::translatePointer (const clang::QualType &pte,
61- const std::string *avoid) {
62+ std::shared_ptr<Type>
63+ TypeTranslator::translatePointer (const clang::QualType &pte,
64+ const std::string *avoid) {
6265
6366 if (pte->isBuiltinType ()) {
6467 const clang::BuiltinType *as = pte->getAs <clang::BuiltinType>();
6568
6669 // Take care of void*
6770 if (as->getKind () == clang::BuiltinType::Void) {
68- return new PointerType (new PrimitiveType (" Byte" ));
71+ return std::make_shared<PointerType>(
72+ std::make_shared<PrimitiveType>(" Byte" ));
6973 }
7074
7175 // Take care of char*
7276 if (as->getKind () == clang::BuiltinType::Char_S ||
7377 as->getKind () == clang::BuiltinType::SChar) {
7478 // TODO: new PointerType(new PrimitiveType("native.CChar"))
75- return new PrimitiveType (" native.CString" );
79+ return std::make_shared< PrimitiveType> (" native.CString" );
7680 }
7781 }
7882
79- return new PointerType (translate (pte, avoid));
83+ return std::make_shared< PointerType> (translate (pte, avoid));
8084}
8185
82- Type *
86+ std::shared_ptr< Type>
8387TypeTranslator::translateStructOrUnionOrEnum (const clang::QualType &qtpe) {
8488 std::string name = qtpe.getUnqualifiedType ().getAsString ();
8589
@@ -93,32 +97,43 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
9397 return ir.getTypeDefWithName (name);
9498}
9599
96- Type *TypeTranslator::translateStructOrUnion (const clang::QualType &qtpe) {
100+ std::shared_ptr<Type>
101+ TypeTranslator::translateStructOrUnion (const clang::QualType &qtpe) {
97102 if (qtpe->hasUnnamedOrLocalType ()) {
98103 // TODO: Verify that the local part is not a problem
99104 uint64_t size = ctx->getTypeSize (qtpe);
100- return new ArrayType (new PrimitiveType (" Byte" ), size);
105+ return std::make_shared<ArrayType>(
106+ std::make_shared<PrimitiveType>(" Byte" ), size);
101107 }
102108
103109 return translateStructOrUnionOrEnum (qtpe);
104110}
105111
106- Type *TypeTranslator::translateConstantArray (const clang::ConstantArrayType *ar,
107- const std::string *avoid) {
112+ std::shared_ptr<Type>
113+ TypeTranslator::translateConstantArray (const clang::ConstantArrayType *ar,
114+ const std::string *avoid) {
108115 const uint64_t size = ar->getSize ().getZExtValue ();
109- return new ArrayType (translate (ar->getElementType (), avoid), size);
116+ std::shared_ptr<Type> elementType = translate (ar->getElementType (), avoid);
117+ if (elementType == nullptr ) {
118+ llvm::errs () << " Failed to translate array type "
119+ << ar->getElementType ().getAsString () << " \n " ;
120+ elementType = std::make_shared<PrimitiveType>(" Byte" );
121+ }
122+
123+ return std::make_shared<ArrayType>(elementType, size);
110124}
111125
112- Type * TypeTranslator::translate (const clang::QualType &qtpe,
113- const std::string *avoid) {
126+ std::shared_ptr< Type> TypeTranslator::translate (const clang::QualType &qtpe,
127+ const std::string *avoid) {
114128
115129 const clang::Type *tpe = qtpe.getTypePtr ();
116130
117131 if (typeEquals (tpe, avoid)) {
118132 // This is a type that we want to avoid the usage.
119133 // Êxample: A struct that has a pointer to itself
120134 uint64_t size = ctx->getTypeSize (tpe);
121- return new ArrayType (new PrimitiveType (" Byte" ), size);
135+ return std::make_shared<ArrayType>(
136+ std::make_shared<PrimitiveType>(" Byte" ), size);
122137 }
123138
124139 if (tpe->isFunctionPointerType ()) {
@@ -146,15 +161,15 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
146161
147162 auto found = typeMap.find (qtpe.getUnqualifiedType ().getAsString ());
148163 if (found != typeMap.end ()) {
149- return new PrimitiveType (found->second );
164+ return std::make_shared< PrimitiveType> (found->second );
150165 } else {
151166 return ir.getTypeDefWithName (
152167 qtpe.getUnqualifiedType ().getAsString ());
153168 }
154169 }
155170}
156171
157- void TypeTranslator::addAlias (std::string cName, Type * type) {
172+ void TypeTranslator::addAlias (std::string cName, std::shared_ptr< Type> type) {
158173 aliasesMap[cName] = type;
159174}
160175
0 commit comments