RomanYankovsky / DelphiAST

Abstract syntax tree builder for Delphi
Mozilla Public License 2.0
273 stars 117 forks source link

Generic Methods #213

Open Wosi opened 7 years ago

Wosi commented 7 years ago

Generic type parameters are plain text part of method names. With this PR type parameters of methods are part of AST in the same way as they are in type declarations.

Code:

TGen<T> = class
  procedure Generate<T2>;
end;

AST:

<METHOD begin_line="15" begin_col="5" end_line="16" end_col="3" kind="procedure" name="Generate">
    <TYPEPARAMS line="15" col="23">
        <TYPEPARAM line="15" col="24">
            <TYPE line="15" col="24" name="T2"/>
        </TYPEPARAM>
    </TYPEPARAMS>
</METHOD>

We may need to discuss how the implementations of generic methods are shown in AST.

Code:

procedure TGen<T>.Generate<T2>;
begin

end;

Currently the method's name attribute contains the entire method name TGen<T>.Generate<T2>. This is not the most elegant solution as someone would need to parse the name in order to get the generic type parameters and the name of the current class. I didn't want to break existing projects using DelphiAST so I decided to remove only the method's generic parameters from the name and keep the rest of it untouched: The type name including its generic parameters remains still plain text.

AST:

    <METHOD begin_line="170" begin_col="1" end_line="175" end_col="1" name="TGen&lt;T&gt;.Generate" kind="procedure">
      <TYPEPARAMS line="170" col="27">
        <TYPEPARAM line="170" col="28">
          <TYPE line="170" col="28" name="T2"/>
        </TYPEPARAM>
      </TYPEPARAMS>
      <STATEMENTS begin_line="171" begin_col="1" end_line="173" end_col="4"/>
    </METHOD>
RomanYankovsky commented 7 years ago

Can we keep both? Full method name as a string and also as a subtree?

Wosi commented 7 years ago

Generally yes. I'd like to know what other people think.

JBontes commented 7 years ago

This is implemented in #231. It works pretty much as expected.