@@ -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,40 +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- Type * elementType = translate (ar->getElementType (), avoid);
116+ std::shared_ptr< Type> elementType = translate (ar->getElementType (), avoid);
110117 if (elementType == nullptr ) {
111118 llvm::errs () << " Failed to translate array type "
112- << ar->getElementType ().getAsString ()
113- << " \n " ;
114- elementType = new PrimitiveType (" Byte" );
119+ << ar->getElementType ().getAsString () << " \n " ;
120+ elementType = std::make_shared<PrimitiveType>(" Byte" );
115121 }
116122
117- return new ArrayType (elementType, size);
123+ return std::make_shared< ArrayType> (elementType, size);
118124}
119125
120- Type * TypeTranslator::translate (const clang::QualType &qtpe,
121- const std::string *avoid) {
126+ std::shared_ptr< Type> TypeTranslator::translate (const clang::QualType &qtpe,
127+ const std::string *avoid) {
122128
123129 const clang::Type *tpe = qtpe.getTypePtr ();
124130
125131 if (typeEquals (tpe, avoid)) {
126132 // This is a type that we want to avoid the usage.
127133 // Êxample: A struct that has a pointer to itself
128134 uint64_t size = ctx->getTypeSize (tpe);
129- return new ArrayType (new PrimitiveType (" Byte" ), size);
135+ return std::make_shared<ArrayType>(
136+ std::make_shared<PrimitiveType>(" Byte" ), size);
130137 }
131138
132139 if (tpe->isFunctionPointerType ()) {
@@ -154,15 +161,15 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
154161
155162 auto found = typeMap.find (qtpe.getUnqualifiedType ().getAsString ());
156163 if (found != typeMap.end ()) {
157- return new PrimitiveType (found->second );
164+ return std::make_shared< PrimitiveType> (found->second );
158165 } else {
159166 return ir.getTypeDefWithName (
160167 qtpe.getUnqualifiedType ().getAsString ());
161168 }
162169 }
163170}
164171
165- void TypeTranslator::addAlias (std::string cName, Type * type) {
172+ void TypeTranslator::addAlias (std::string cName, std::shared_ptr< Type> type) {
166173 aliasesMap[cName] = type;
167174}
168175
0 commit comments