Skip to content
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

minor fix for template instantiation #1

Open
wants to merge 25 commits into
base: inst_func
Choose a base branch
from

Conversation

diivm
Copy link

@diivm diivm commented Oct 24, 2020

  1. - void aFunction<bool>(bool);
    + void aFunction<bool>(bool) {}
  2. The class is instantiated, hence it loses its template (meta?) data. It is just a normal class after instantiation, as we would expect. Hence, it wouldn't contain TYPE_REF.
  3. Minor typo 🙂

@kunaltyagi
Linking PointCloudLibrary#7

@kunaltyagi
Copy link
Owner

  1. That's a specialization, not an instantiation. Instantiation is required for having a concrete type to use for bindings, specialization is required to have some special implementation for a specific type.

  2. How are you able to get the template type info in L219 of generate.py?

  3. Yeah, I kept that during testing to check the print, but then added the assert for making it fail better

@diivm
Copy link
Author

diivm commented Oct 24, 2020

Please ignore abbe5f2

@diivm
Copy link
Author

diivm commented Oct 24, 2020

  1. That's a specialization, not an instantiation. Instantiation is required for having a concrete type to use for bindings, specialization is required to have some special implementation for a specific type.

My bad.

  1. How are you able to get the template type info in L219 of generate.py?

If I recall correctly, I added those LOCs for instantiated classes (when working on pcl_base).
Ex. from this gist, PointXYZ is the TYPE_REF:

py::class_<PCLBase<PointXYZ>>(m, "PCLBase_PointXYZ").def(py::init<>());

I used to do that too, until I started just asserting False 😂


I don't know yet why

template
    void aFunction<bool>(bool);

doesn't generate output and

template
    void aFunction<bool>(bool) {}

does, 'cause both are valid c++ snippets.

Can you try dumping the AST and see if that generates? I think it will.

@kunaltyagi
Copy link
Owner

AST dump:

None L0 C0 TRANSLATION_UNIT /tmp/pytest-of-kt/pytest-24/test_template_instantiate_sing0/file.cpp
- /tmp/pytest-of-kt/pytest-24/test_template_instantiate_sing0/file.cpp L3 C12 STRUCT_DECL AStruct
- /tmp/pytest-of-kt/pytest-24/test_template_instantiate_sing0/file.cpp L5 C11 CLASS_DECL AClass

@diivm
Copy link
Author

diivm commented Oct 25, 2020

Running clang -Xclang -ast-dump -fsyntax-only test.cpp produced:

TranslationUnitDecl 0x55f89dff2068 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55f89dff2948 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55f89dff2600 '__int128'
|-TypedefDecl 0x55f89dff29b8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55f89dff2620 'unsigned __int128'
|-TypedefDecl 0x55f89dff2d40 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
| `-RecordType 0x55f89dff2aa0 '__NSConstantString_tag'
|   `-CXXRecord 0x55f89dff2a10 '__NSConstantString_tag'
|-TypedefDecl 0x55f89dff2de8 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55f89dff2da0 'char *'
|   `-BuiltinType 0x55f89dff2100 'char'
|-TypedefDecl 0x55f89e030b90 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
| `-ConstantArrayType 0x55f89e030b30 '__va_list_tag [1]' 1 
|   `-RecordType 0x55f89dff2ed0 '__va_list_tag'
|     `-CXXRecord 0x55f89dff2e40 '__va_list_tag'
|-ClassTemplateDecl 0x55f89e030d50 <test.cpp:1:5, line:2:21> col:12 AStruct
| |-TemplateTypeParmDecl 0x55f89e030be8 <line:1:15, col:24> col:24 typename depth 0 index 0 T
| `-CXXRecordDecl 0x55f89e030cc0 <line:2:5, col:21> col:12 struct AStruct definition
|   |-DefinitionData empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
|   | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
|   | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
|   | |-MoveConstructor exists simple trivial needs_implicit
|   | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
|   | |-MoveAssignment exists simple trivial needs_implicit
|   | `-Destructor simple irrelevant trivial needs_implicit
|   `-CXXRecordDecl 0x55f89e030fb8 <col:5, col:12> col:12 implicit struct AStruct
|-ClassTemplateDecl 0x55f89e031190 <line:4:5, line:5:19> col:11 AClass
| |-TemplateTypeParmDecl 0x55f89e031060 <line:4:15, col:24> col:24 typename depth 0 index 0 U
| `-CXXRecordDecl 0x55f89e031100 <line:5:5, col:19> col:11 class AClass definition
|   |-DefinitionData empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
|   | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
|   | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
|   | |-MoveConstructor exists simple trivial needs_implicit
|   | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
|   | |-MoveAssignment exists simple trivial needs_implicit
|   | `-Destructor simple irrelevant trivial needs_implicit
|   `-CXXRecordDecl 0x55f89e0313f8 <col:5, col:11> col:11 implicit class AClass
`-FunctionTemplateDecl 0x55f89e031630 <line:7:5, line:8:20> col:10 aFunction
  |-TemplateTypeParmDecl 0x55f89e0314a0 <line:7:15, col:24> col:24 typename depth 0 index 0 P
  `-FunctionDecl 0x55f89e031590 <line:8:5, col:20> col:10 aFunction 'void ()'

It did parse.

`-FunctionTemplateDecl 0x55f89e031630 <line:7:5, line:8:20> col:10 aFunction
  |-TemplateTypeParmDecl 0x55f89e0314a0 <line:7:15, col:24> col:24 typename depth 0 index 0 P
  `-FunctionDecl 0x55f89e031590 <line:8:5, col:20> col:10 aFunction 'void ()'

@kunaltyagi
Copy link
Owner

For some reason, the printAST doesn't reflect that. Any ideas why?

@kunaltyagi
Copy link
Owner

I added those LOCs for instantiated classes (when working on pcl_base).

I do recall that. I'm wondering how you were able to get the template parameter to append since the parsed output doesn't have that.

@diivm
Copy link
Author

diivm commented Oct 25, 2020

I do recall that. I'm wondering how you were able to get the template parameter to append since the parsed output doesn't have that.

That comes into the picture when we are instantiating. While defining templates, we get TEMPLATE_TYPE_PARAMETER:

def test_class_template(tmp_path):
file_contents = """
template <typename T>
struct AStruct {};
template <typename U>
class AClass {};
"""
parsed_info = get_parsed_info(tmp_path=tmp_path, file_contents=file_contents)
struct_template = parsed_info["members"][0]
assert struct_template["kind"] == "CLASS_TEMPLATE"
assert struct_template["name"] == "AStruct"
template_type_parameter = struct_template["members"][0]
assert template_type_parameter["kind"] == "TEMPLATE_TYPE_PARAMETER"
assert template_type_parameter["name"] == "T"
assert template_type_parameter["access_specifier"] == "PUBLIC"
class_template = parsed_info["members"][1]
assert class_template["kind"] == "CLASS_TEMPLATE"
assert class_template["name"] == "AClass"
template_type_parameter = class_template["members"][0]
assert template_type_parameter["kind"] == "TEMPLATE_TYPE_PARAMETER"
assert template_type_parameter["name"] == "U"
assert template_type_parameter["access_specifier"] == "PUBLIC"

  • Also, the python API is marking the class and struct specialisations as declarations, as opposed to template declarations in the clang ast-dump.
  • Looks like a limitation of the python API, similar to the default/delete ctor discussion we had some time back.
  • Maybe we should use the C++ API.

@kunaltyagi
Copy link
Owner

Let's move the PR directly onto pcl/clang-bind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants