-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store types in ir #68
Conversation
type struct_point = native.CStruct2[native.CInt, native.CInt] | ||
type point_s = native.Ptr[struct_point] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
type toggle_e = enum_toggle_e | ||
type int2int = native.CFunctionPtr1[native.CInt, native.CInt] | ||
type day2string = native.CFunctionPtr1[enum_days, native.CString] | ||
type toggle = native.CFunctionPtr1[toggle_e, Unit] | ||
type enum_days = native.CUnsignedInt | ||
type enum_toggle_e = native.CUnsignedInt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you are doing a lot of refactoring. Would you mind to split out things like reordering of the generated code to a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generated code is reordered because typedefs are now generated earlier. Order of types happens to be more logical but it is not a separate refactoring
bindgen/ir/types/SimpleType.h
Outdated
/** | ||
* For example native.CInt | ||
*/ | ||
class SimpleType : public Type { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to name it PrimitiveType
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name is good
3217dfd
to
51f57bf
Compare
I tried to do memory management properly but got a problem: It is not possible to use Type values instead of pointers because inheritance will not work properly. Using vectors of pointers for everything (so destructors are not called by I decided to not spend too much time on it and leave it as it is. |
Okay, maybe we should create a ticket to investigate sometime in the future. I will take a second look when #62 has been merged and the conflict in this PR has been fixed. |
51f57bf
to
8db4611
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a second pass. Impressive work.
bindgen/CycleDetection.h
Outdated
std::string qtpeString = tpeTransl.Translate(qtpe); | ||
Type *type = tpeTransl.translate(qtpe); | ||
if (type == nullptr) { | ||
/* temp fix for function pointer */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What temporary fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function recursively extracts pointee type from pointer type to add a dependency.
So it extracts FunctionType
from FunctionPointerType
but TypeTranslator
is unable to translate FunctionType
I'll make the comment more clear
@@ -27,38 +30,24 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_) : ctx(ctx_), typeMap() { | |||
typeMap["char32_t"] = "native.CChar32"; | |||
typeMap["float"] = "native.CFloat"; | |||
typeMap["double"] = "native.CDouble"; | |||
typeMap["void*"] = "native.Ptr[Byte]"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
return std::string("native.CFunctionPtr") + std::to_string(counter) + | ||
"[" + params + variad + ret + "]"; | ||
return new FunctionPointerType(returnType, parametersTypes, | ||
fc->isVariadic()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
ir.addEnum(name, typeTranslator.Translate(enumdecl->getIntegerType()), | ||
enumerators); | ||
std::string scalaType = typeTranslator.getTypeFromTypeMap( | ||
enumdecl->getIntegerType().getUnqualifiedType().getAsString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to get rid of this string translation.
bindgen/TypeTranslator.cpp
Outdated
} | ||
|
||
// Take care of char* | ||
if (as->getKind() == clang::BuiltinType::Char_S || | ||
as->getKind() == clang::BuiltinType::SChar) { | ||
return "native.CString"; | ||
return new PrimitiveType("native.CString"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be a constant? Similar for the other "native.$type"
.
Also CString
is technically char *
so more logically would be encoded as new PointerType(new PrimitiveType("native.CChar"))
. Would you mind leaving a FIXME or comment related to this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
bindgen/TypeTranslator.cpp
Outdated
std::string | ||
TypeTranslator::TranslateConstantArray(const clang::ConstantArrayType *ar, | ||
const std::string *avoid) { | ||
Type *TypeTranslator::translateEnum(const clang::QualType &qtpe) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this method is reused actually be used by translateStruct
or even merged with it once the union
specific if
branch is removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} else { | ||
// TODO: Properly handle non-default types | ||
return handleReservedWords(qtpe.getUnqualifiedType().getAsString()); | ||
return ir.getTypeDefWithName( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess here we don't want to go through the alias map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because struct myStruct
will be translated above (the same for enum
and union
)
/** | ||
* Maps C struct, union or enum name to Type alias | ||
*/ | ||
std::map<std::string, Type *> aliasesMap; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite liked the unification in the original typeMap
. Would it make sense to change it to be defined as:
std::map<std::string, Type *> typeMap
populated with
typeMap["void"] = new PrimitiveType("Unit");
typeMap["bool"] = new PrimitiveType("native.CBool");
typeMap["_Bool"] = new PrimitiveType("native.CBool");
// ...
I assume the problem is with the deallocation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, deallocation is the problem (well, these instances are not deallocated now, but changing type of the map will make fixing it harder)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll create an issue for deallocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or simply do it now, because I do not want the code that does not deallocate objects
bindgen/defines/DefineFinder.cpp
Outdated
@@ -109,60 +111,63 @@ void DefineFinder::addNumericConstantDefine(const std::string ¯oName, | |||
const clang::Token &token, | |||
bool positive) { | |||
clang::NumericLiteralParser parser(literal, token.getLocation(), pp); | |||
std::string type; | |||
Type *type = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method could be simpler by using std::string typeName
and still use "native.CLongLong
etc. and do the new PrimitiveType(typeName)
allocation at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
bindgen/ir/types/Type.cpp
Outdated
#include "Type.h" | ||
#include "../../Utils.h" | ||
|
||
std::string Type::str() const { return handleReservedWords(_str()); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this call handleReservedWords()
on stuff like native.CFunctionPtr[...]
? It seems that it over-do it. How hard would it be to selectively call it for any type that returns a simple name
and then get rid of _str()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be a problem
Now all objects are deallocated. I checked it with Valgrind |
But it is a bit ugly because some types are shared between objects ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stellar work!
Closes #52
Added multiple new classes in
ir/types
directory. I decided not to wrapstruct
/union
/enum
into aType
class instance but inherit the classes fromType
.One of changes:
Typedefs for
struct
/union
/enum
are generated right after a declaration was visited.Example of such typedef:
Created typedef points to
struct
/union
/enum
instance and it is added to translation map, so all other declarations that use type of thisstruct
/union
/enum
will point to the created typedef.I will carefully review the code later.