CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

Correct behavior of IsTemplate() in Class

Open deadlocklogic opened this issue 2 years ago • 5 comments
trafficstars

Consider a dummy template:

template <typename T>
struct Test {
};

Checking Class.IsTemplate() on the parsed entity returns false. Apparently the declaration should have IsDependent = true in order to be eligible for checking if being a template. What is meant by IsDependent? Are these specific for CSharp? Thanks.

deadlocklogic avatar Jul 30 '23 13:07 deadlocklogic

Checking Class.IsTemplate() on the parsed entity returns false.

This seems because the template parameters list on Class is empty, even though its correct in ClassTemplate.

Looking at Class.TemplateParameters comments:

        /// If this class is a template, this list contains all of its template parameters.
        /// <see cref="ClassTemplate"/> cannot be relied upon to contain all of them because
        /// ClassTemplateDecl in Clang is not a complete declaration, it only serves to forward template classes.
        public List<Declaration> TemplateParameters

This seems pretty messy, we should only have a single list of parameters, not two.

Also:

        public bool IsTemplate => IsDependent && TemplateParameters.Count > 0;

I am not sure why this checks if TemplateParameters.Count > 0, maybe it can be removed.

What is meant by IsDependent?

https://github.com/mono/CppSharp/blob/3c31179db1e8a4a2f008612fb005124b946cc793/src/CppParser/Parser.cpp#L3980 https://github.com/mono/CppSharp/blob/3c31179db1e8a4a2f008612fb005124b946cc793/src/CppParser/Parser.cpp#L1008

IsDependent comes from Clang, it means either a type or the context is dependent on a template parameter.

Are these specific for CSharp?

No.

tritao avatar Jul 30 '23 16:07 tritao

Well looks like the template semantics needs heavy refactoring then. Is anyone willing to work on this issue because right now template manipulation is so messy/obscure.

deadlocklogic avatar Jul 31 '23 02:07 deadlocklogic

Well looks like the template semantics needs heavy refactoring then. Is anyone willing to work on this issue because right now template manipulation is so messy/obscure.

I'll probably try to clean this up a bit, but won't do any too heavy refactoring as that would take too much time. I think getting rid of the duplicated lists and linking Class and ClassTemplate would take care of most of it?

tritao avatar Jul 31 '23 11:07 tritao

I'll probably try to clean this up a bit, but won't do any too heavy refactoring as that would take too much time. I think getting rid of the duplicated lists and linking Class and ClassTemplate would take care of most of it?

I think this will be sufficient. The most important thing is to be able to query template parameters accurately and in a universal way. Maybe adding tests is viable too but requires more time of course.

deadlocklogic avatar Aug 01 '23 03:08 deadlocklogic

After stumbling upon TypeAlias which has a DescribedAliasTemplate property, an elegant solution would be adding a DescribedTemplate property in Declaration which can be null if the declaration is not a template, and a property which casts to the adequat type in any templated declaration like: Class, Function, Method, Variable etc...

deadlocklogic avatar Oct 06 '23 23:10 deadlocklogic