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

Template Support #368

Merged
merged 216 commits into from
Jun 23, 2021
Merged

Template Support #368

merged 216 commits into from
Jun 23, 2021

Conversation

oxisto
Copy link
Member

@oxisto oxisto commented Mar 22, 2021

Just to keep track of it.

vfsrfs and others added 30 commits January 15, 2021 13:26
…a new Type

Change TypeResolver to avoid handling of ParameterizedType, as they are not globally unique, but only class unique
Update TypeTests to include tests for ParameterizedType
…arameterizedType in the TypeManager

Add documentation to ParameterizedType
…are performed

Add tests for the CallResolver with implicit casts
# Conflicts:
#	src/main/java/de/fraunhofer/aisec/cpg/graph/types/ParameterizedType.java
@vfsrfs vfsrfs marked this pull request as ready for review June 12, 2021 14:44
@vfsrfs
Copy link
Contributor

vfsrfs commented Jun 12, 2021

This PR adds template support as discussed in #363

Features

  • Supports Type Parameters and Value Parameters as Parameters for the TemplateDeclaration
  • Supports the resolution with default parameters
  • Supports use of a previously defined template parameter as a default value
  • Creates dummy elements when needed
  • CallExpression is extended to include an instantiation Edge to the template that is being instantiated by the call and a template parameter edge that connects the CallExpression to the parameters the template is instantiated with
  • Introduces TypeExpression to model the type keywords provided when instantiating a template

ClassTemplate

  • Supports templates applied to classes and structs
  • Templates can be instantiated either on the stack or on the heap using the new keyword

Example:

classtemplate

template<class Type1, class Type2>
class Pair
{
    public:
        Type1 first;
        Type2 second;
};

int main()
{
    Pair<int,int> point1;
}

FunctionTemplate

  • Can resolve template parameters using auto deduction or implicit casts
  • Prioritizes matching function that already exist instead of instantiating a template
  • Supports function templates within a record

Example:

functiontemplate

(ObjectType int removed for better visualization)

template <class T=int, int N=5>
T fixed_multiply (T val)
{
  auto x = val * N;
  return x;
}

int main() {
  fixed_multiply<int,2>(10);
}

Future Changes

There are some request that weren't addressed in this PR, but we will have to take care of them in the future:

  • Add Support for Template Specialization
  • Add TemplateExpansionPass to realize every template instantiation

@vfsrfs vfsrfs requested a review from konradweiss June 12, 2021 14:46
@sonarcloud
Copy link

sonarcloud bot commented Jun 23, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 54 Code Smells

82.8% 82.8% Coverage
0.0% 0.0% Duplication

@konradweiss konradweiss merged commit e4ea0e3 into master Jun 23, 2021
@konradweiss konradweiss deleted the vfsrfs/templates branch June 23, 2021 21:22
@oxisto oxisto mentioned this pull request Jul 14, 2021
@oxisto oxisto mentioned this pull request Aug 5, 2021
20 tasks
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