Skip to content

Commit 4fc6a2a

Browse files
committed
Go: fill typeref field of tparam kind tags
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
1 parent 3bf0d55 commit 4fc6a2a

2 files changed

Lines changed: 62 additions & 17 deletions

File tree

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
x input.go /^package x$/;" p
2-
T input.go /^func min[T ~int|~float64](x, y T) T {$/;" Z func:x.min nth:0
2+
T input.go /^func min[T ~int|~float64](x, y T) T {$/;" Z func:x.min typeref:constraint:~int|~float64 nth:0
33
min input.go /^func min[T ~int|~float64](x, y T) T {$/;" f package:x typeref:typename:T signature:(x, y T) tparams:[T ~int|~float64]
4-
S1 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc nth:0
5-
S2 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc nth:1
6-
E1 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc nth:2
7-
E2 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc nth:3
4+
S1 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc typeref:constraint:~[]E1 nth:0
5+
S2 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc typeref:constraint:~[]E2 nth:1
6+
E1 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc typeref:constraint:any nth:2
7+
E2 input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" Z func:x.EqualFunc typeref:constraint:any nth:3
88
EqualFunc input.go /^func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {$/;" f package:x typeref:typename:bool signature:(s1 S1, s2 S2, eq func(E1, E2) bool) tparams:[S1 ~[]E1, S2 ~[]E2, E1, E2 any]
9-
L input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 nth:0
10-
M input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 nth:1
11-
G input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 nth:3
9+
L input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 typeref:constraint:~[]M nth:0
10+
M input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 typeref:constraint:comparable nth:1
11+
G input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" Z func:x.F0 typeref:constraint:any nth:3
1212
F0 input.go /^func F0[L ~[]M, M comparable, _, G any](s L, v M) int {$/;" f package:x typeref:typename:int signature:(s L, v M) tparams:[L ~[]M, M comparable, _, G any]
13-
N input.go /^func F1[N ~[]O, O comparable, _ any](s N, v O) int {$/;" Z func:x.F1 nth:0
14-
O input.go /^func F1[N ~[]O, O comparable, _ any](s N, v O) int {$/;" Z func:x.F1 nth:1
13+
N input.go /^func F1[N ~[]O, O comparable, _ any](s N, v O) int {$/;" Z func:x.F1 typeref:constraint:~[]O nth:0
14+
O input.go /^func F1[N ~[]O, O comparable, _ any](s N, v O) int {$/;" Z func:x.F1 typeref:constraint:comparable nth:1
1515
F1 input.go /^func F1[N ~[]O, O comparable, _ any](s N, v O) int {$/;" f package:x typeref:typename:int signature:(s N, v O) tparams:[N ~[]O, O comparable, _ any]

parsers/go.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,29 @@ static tokenInfo * parseReceiver (tokenInfo *const token, int *corkIndex)
820820
return receiver_type_token;
821821
}
822822

823-
static intArray * parseTypeParameters (tokenInfo *const token, collector *collector)
823+
static void attachConstraint (intArray *tparams_indexes, collector *constraint)
824+
{
825+
collectorTruncate (constraint, false);
826+
if (collectorIsEmpty (constraint))
827+
return;
828+
829+
const char *constraint_cstr = vStringValue (constraint->str);
830+
for (unsigned int i = intArrayCount (tparams_indexes); i > 0; i--)
831+
{
832+
int index = intArrayItem (tparams_indexes, i - 1);
833+
tagEntryInfo *e = getEntryInCorkQueue (index);
834+
if (!e)
835+
continue; /* Maybe "_" */
836+
837+
if (e->extensionFields.typeRef [0])
838+
return;
839+
840+
e->extensionFields.typeRef [0] = eStrdup ("constraint");
841+
e->extensionFields.typeRef [1] = eStrdup (constraint_cstr);
842+
}
843+
}
844+
845+
static intArray * parseTypeParameters (tokenInfo *const token, collector *tparams)
824846
{
825847
// TypeParameters = "[" TypeParamList [ "," ] "]" .
826848
// TypeParamList = TypeParamDecl { "," TypeParamDecl } .
@@ -830,40 +852,63 @@ static intArray * parseTypeParameters (tokenInfo *const token, collector *collec
830852

831853
int nest_level = 1;
832854
bool expecting_param = true;
833-
intArray *tparams = intArrayNew ();
855+
intArray *tparams_indexes = intArrayNew ();
856+
bool expecting_constraint = false;
857+
858+
vString *buffer = vStringNew ();
859+
collector constraint = COLLECTOR (buffer);
834860

835861
while (nest_level > 0 && !isType (token, TOKEN_EOF))
836862
{
837-
readTokenFull (token, collector);
863+
readTokenFull (token, tparams);
838864
if (isType (token, TOKEN_OPEN_PAREN)
839865
|| isType (token, TOKEN_OPEN_CURLY)
840866
|| isType (token, TOKEN_OPEN_SQUARE))
867+
{
841868
nest_level++;
869+
collectorAppendToken (&constraint, token);
870+
}
842871
else if (isType (token, TOKEN_CLOSE_PAREN)
843872
|| isType (token, TOKEN_CLOSE_CURLY)
844873
|| isType (token, TOKEN_CLOSE_SQUARE))
874+
{
845875
nest_level--;
876+
if (nest_level == 0)
877+
{
878+
attachConstraint (tparams_indexes, &constraint);
879+
collectorReset(&constraint);
880+
continue;
881+
}
882+
collectorAppendToken (&constraint, token);
883+
}
846884
else if (nest_level == 1
847885
&& isType (token, TOKEN_IDENTIFIER)
848886
&& expecting_param)
849887
{
888+
attachConstraint (tparams_indexes, &constraint);
889+
collectorReset(&constraint);
890+
850891
expecting_param = false;
892+
expecting_constraint = true;
851893
int index = makeTag (token, GOTAG_TPARAM, CORK_NIL, NULL, NULL);
852-
intArrayAdd (tparams, index);
894+
intArrayAdd (tparams_indexes, index);
853895
}
854896
else if (nest_level == 1
855897
&& isType (token, TOKEN_COMMA))
856898
{
857-
/* TODO: put the tokens to typeref:constrain:... of the
858-
* tparam tag */
859899
expecting_param = true;
900+
expecting_constraint = false;
860901
}
902+
else if (expecting_constraint)
903+
collectorAppendToken(&constraint, token);
861904
}
862905

906+
vStringDelete (buffer);
907+
863908
/* Read the next token as the other parser* functions do.
864909
* However, don't put the token to the collector. */
865910
readToken (token);
866-
return tparams;
911+
return tparams_indexes;
867912
}
868913

869914
static void parseFunctionOrMethod (tokenInfo *const token, const int scope)

0 commit comments

Comments
 (0)