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

classfile static methods #1849

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions parser/_testdata/staticmthd2/a.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func .New() *T {
}
24 changes: 24 additions & 0 deletions parser/_testdata/staticmthd2/parser.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

file a.gox
ast.FuncDecl:
Recv:
ast.FieldList:
Name:
ast.Ident:
Name: New
Type:
ast.FuncType:
Params:
ast.FieldList:
Results:
ast.FieldList:
List:
ast.Field:
Type:
ast.StarExpr:
X:
ast.Ident:
Name: T
Body:
ast.BlockStmt:
67 changes: 41 additions & 26 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3495,6 +3495,10 @@ func (p *parser) parseImportSpec(doc *ast.CommentGroup, _ token.Token, _ int) as
return spec
}

func (p *parser) inClassFile() bool {
return p.mode&ParseGoPlusClass != 0
}

func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota int) ast.Spec {
if p.trace {
defer un(trace(p, keyword.String()+"Spec"))
Expand All @@ -3505,7 +3509,7 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota
var typ ast.Expr
var tag *ast.BasicLit
var values []ast.Expr
if p.mode&ParseGoPlusClass != 0 && p.topScope == p.pkgScope && keyword == token.VAR && p.varDeclCnt == 1 {
if p.inClassFile() && p.topScope == p.pkgScope && keyword == token.VAR && p.varDeclCnt == 1 {
var starPos token.Pos
if p.tok == token.MUL {
starPos = p.pos
Expand Down Expand Up @@ -3706,6 +3710,7 @@ func (p *parser) parseOverloadDecl(decl *ast.OverloadFuncDecl) *ast.OverloadFunc
// `func identOrOp = (overloadFuncs)`
//
// `func T.ident(params) results { ... }`
// `func .ident(params) results { ... }` (only in classfile)
//
// `func (recv) identOrOp(params) results { ... }`
// `func (T).identOrOp = (overloadFuncs)`
Expand All @@ -3723,31 +3728,8 @@ func (p *parser) parseFuncDeclOrCall() (ast.Decl, *ast.CallExpr) {
var ident *ast.Ident
var isOp, isStatic, isFunLit, ok bool

if p.tok != token.LPAREN {
// func: `func identOrOp(...) results`
// overload: `func identOrOp = (overloadFuncs)`
// static method: `func T.ident(...) results`
ident, isOp = p.parseIdentOrOp()
switch p.tok {
case token.ASSIGN:
// func identOrOp = (overloadFuncs)
return p.parseOverloadDecl(&ast.OverloadFuncDecl{
Doc: doc,
Func: pos,
Name: ident,
Operator: isOp,
}), nil
case token.PERIOD:
// func T.ident(...) results
if !isOp {
p.next()
recv = &ast.FieldList{List: []*ast.Field{{Type: ident}}}
ident = p.parseIdent()
isStatic = true
}
}
params, results = p.parseSignature(scope)
} else {
switch p.tok {
case token.LPAREN:
// method: `func (recv) identOrOp(params) results { ... }`
// overload: `func (T).identOrOp = (overloadFuncs)`
// funlit: `func (params) results { ... }()`
Expand Down Expand Up @@ -3809,6 +3791,39 @@ func (p *parser) parseFuncDeclOrCall() (ast.Decl, *ast.CallExpr) {
p.expectSemi()
return nil, call
}
case token.PERIOD:
// func .ident(...) results (only in classfile)
if p.inClassFile() {
p.next()
recv = &ast.FieldList{}
isStatic = true
}
ident = p.parseIdent()
params, results = p.parseSignature(scope)
default:
// func: `func identOrOp(...) results`
// overload: `func identOrOp = (overloadFuncs)`
// static method: `func T.ident(...) results`
ident, isOp = p.parseIdentOrOp()
switch p.tok {
case token.ASSIGN:
// func identOrOp = (overloadFuncs)
return p.parseOverloadDecl(&ast.OverloadFuncDecl{
Doc: doc,
Func: pos,
Name: ident,
Operator: isOp,
}), nil
case token.PERIOD:
// func T.ident(...) results
if !isOp {
p.next()
recv = &ast.FieldList{List: []*ast.Field{{Type: ident}}}
ident = p.parseIdent()
isStatic = true
}
}
params, results = p.parseSignature(scope)
}

if isOp {
Expand Down
4 changes: 3 additions & 1 deletion printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,9 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
startCol := p.out.Column - len("func ")
if d.Recv != nil && !d.IsClass {
if d.Static { // static method
p.expr(d.Recv.List[0].Type)
if list := d.Recv.List; len(list) > 0 {
p.expr(list[0].Type)
}
p.print(token.PERIOD)
} else {
p.parameters(d.Recv) // method: print receiver
Expand Down