Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
1f46cb0b
Commit
1f46cb0b
authored
May 23, 2012
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/parser: resolve all parameter types
Fixes #3655. R=golang-dev, r CC=golang-dev
https://golang.org/cl/6213065
parent
f430d0e6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
12 deletions
+79
-12
src/pkg/go/parser/parser.go
src/pkg/go/parser/parser.go
+18
-12
src/pkg/go/parser/parser_test.go
src/pkg/go/parser/parser_test.go
+61
-0
No files found.
src/pkg/go/parser/parser.go
View file @
1f46cb0b
...
@@ -619,10 +619,10 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
...
@@ -619,10 +619,10 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
doc
:=
p
.
leadComment
doc
:=
p
.
leadComment
//
fields
//
FieldDecl
list
,
typ
:=
p
.
parseVarList
(
false
)
list
,
typ
:=
p
.
parseVarList
(
false
)
//
optional t
ag
//
T
ag
var
tag
*
ast
.
BasicLit
var
tag
*
ast
.
BasicLit
if
p
.
tok
==
token
.
STRING
{
if
p
.
tok
==
token
.
STRING
{
tag
=
&
ast
.
BasicLit
{
ValuePos
:
p
.
pos
,
Kind
:
p
.
tok
,
Value
:
p
.
lit
}
tag
=
&
ast
.
BasicLit
{
ValuePos
:
p
.
pos
,
Kind
:
p
.
tok
,
Value
:
p
.
lit
}
...
@@ -637,7 +637,6 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
...
@@ -637,7 +637,6 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
}
else
{
}
else
{
// ["*"] TypeName (AnonymousField)
// ["*"] TypeName (AnonymousField)
typ
=
list
[
0
]
// we always have at least one element
typ
=
list
[
0
]
// we always have at least one element
p
.
resolve
(
typ
)
if
n
:=
len
(
list
);
n
>
1
||
!
isTypeName
(
deref
(
typ
))
{
if
n
:=
len
(
list
);
n
>
1
||
!
isTypeName
(
deref
(
typ
))
{
pos
:=
typ
.
Pos
()
pos
:=
typ
.
Pos
()
p
.
errorExpected
(
pos
,
"anonymous field"
)
p
.
errorExpected
(
pos
,
"anonymous field"
)
...
@@ -649,6 +648,7 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
...
@@ -649,6 +648,7 @@ func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
field
:=
&
ast
.
Field
{
Doc
:
doc
,
Names
:
idents
,
Type
:
typ
,
Tag
:
tag
,
Comment
:
p
.
lineComment
}
field
:=
&
ast
.
Field
{
Doc
:
doc
,
Names
:
idents
,
Type
:
typ
,
Tag
:
tag
,
Comment
:
p
.
lineComment
}
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
resolve
(
typ
)
return
field
return
field
}
}
...
@@ -691,12 +691,15 @@ func (p *parser) parsePointerType() *ast.StarExpr {
...
@@ -691,12 +691,15 @@ func (p *parser) parsePointerType() *ast.StarExpr {
return
&
ast
.
StarExpr
{
Star
:
star
,
X
:
base
}
return
&
ast
.
StarExpr
{
Star
:
star
,
X
:
base
}
}
}
// If the result is an identifier, it is not resolved.
func
(
p
*
parser
)
tryVarType
(
isParam
bool
)
ast
.
Expr
{
func
(
p
*
parser
)
tryVarType
(
isParam
bool
)
ast
.
Expr
{
if
isParam
&&
p
.
tok
==
token
.
ELLIPSIS
{
if
isParam
&&
p
.
tok
==
token
.
ELLIPSIS
{
pos
:=
p
.
pos
pos
:=
p
.
pos
p
.
next
()
p
.
next
()
typ
:=
p
.
tryIdentOrType
(
isParam
)
// don't use parseType so we can provide better error message
typ
:=
p
.
tryIdentOrType
(
isParam
)
// don't use parseType so we can provide better error message
if
typ
==
nil
{
if
typ
!=
nil
{
p
.
resolve
(
typ
)
}
else
{
p
.
error
(
pos
,
"'...' parameter is missing type"
)
p
.
error
(
pos
,
"'...' parameter is missing type"
)
typ
=
&
ast
.
BadExpr
{
From
:
pos
,
To
:
p
.
pos
}
typ
=
&
ast
.
BadExpr
{
From
:
pos
,
To
:
p
.
pos
}
}
}
...
@@ -705,6 +708,7 @@ func (p *parser) tryVarType(isParam bool) ast.Expr {
...
@@ -705,6 +708,7 @@ func (p *parser) tryVarType(isParam bool) ast.Expr {
return
p
.
tryIdentOrType
(
false
)
return
p
.
tryIdentOrType
(
false
)
}
}
// If the result is an identifier, it is not resolved.
func
(
p
*
parser
)
parseVarType
(
isParam
bool
)
ast
.
Expr
{
func
(
p
*
parser
)
parseVarType
(
isParam
bool
)
ast
.
Expr
{
typ
:=
p
.
tryVarType
(
isParam
)
typ
:=
p
.
tryVarType
(
isParam
)
if
typ
==
nil
{
if
typ
==
nil
{
...
@@ -716,6 +720,7 @@ func (p *parser) parseVarType(isParam bool) ast.Expr {
...
@@ -716,6 +720,7 @@ func (p *parser) parseVarType(isParam bool) ast.Expr {
return
typ
return
typ
}
}
// If any of the results are identifiers, they are not resolved.
func
(
p
*
parser
)
parseVarList
(
isParam
bool
)
(
list
[]
ast
.
Expr
,
typ
ast
.
Expr
)
{
func
(
p
*
parser
)
parseVarList
(
isParam
bool
)
(
list
[]
ast
.
Expr
,
typ
ast
.
Expr
)
{
if
p
.
trace
{
if
p
.
trace
{
defer
un
(
trace
(
p
,
"VarList"
))
defer
un
(
trace
(
p
,
"VarList"
))
...
@@ -736,9 +741,7 @@ func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) {
...
@@ -736,9 +741,7 @@ func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) {
}
}
// if we had a list of identifiers, it must be followed by a type
// if we had a list of identifiers, it must be followed by a type
if
typ
=
p
.
tryVarType
(
isParam
);
typ
!=
nil
{
typ
=
p
.
tryVarType
(
isParam
)
p
.
resolve
(
typ
)
}
return
return
}
}
...
@@ -748,7 +751,10 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
...
@@ -748,7 +751,10 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
defer
un
(
trace
(
p
,
"ParameterList"
))
defer
un
(
trace
(
p
,
"ParameterList"
))
}
}
// ParameterDecl
list
,
typ
:=
p
.
parseVarList
(
ellipsisOk
)
list
,
typ
:=
p
.
parseVarList
(
ellipsisOk
)
// analyze case
if
typ
!=
nil
{
if
typ
!=
nil
{
// IdentifierList Type
// IdentifierList Type
idents
:=
p
.
makeIdentList
(
list
)
idents
:=
p
.
makeIdentList
(
list
)
...
@@ -757,10 +763,10 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
...
@@ -757,10 +763,10 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
// Go spec: The scope of an identifier denoting a function
// Go spec: The scope of an identifier denoting a function
// parameter or result variable is the function body.
// parameter or result variable is the function body.
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
resolve
(
typ
)
if
p
.
tok
==
token
.
COMMA
{
if
p
.
tok
==
token
.
COMMA
{
p
.
next
()
p
.
next
()
}
}
for
p
.
tok
!=
token
.
RPAREN
&&
p
.
tok
!=
token
.
EOF
{
for
p
.
tok
!=
token
.
RPAREN
&&
p
.
tok
!=
token
.
EOF
{
idents
:=
p
.
parseIdentList
()
idents
:=
p
.
parseIdentList
()
typ
:=
p
.
parseVarType
(
ellipsisOk
)
typ
:=
p
.
parseVarType
(
ellipsisOk
)
...
@@ -769,18 +775,18 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
...
@@ -769,18 +775,18 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
// Go spec: The scope of an identifier denoting a function
// Go spec: The scope of an identifier denoting a function
// parameter or result variable is the function body.
// parameter or result variable is the function body.
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
declare
(
field
,
nil
,
scope
,
ast
.
Var
,
idents
...
)
p
.
resolve
(
typ
)
if
!
p
.
atComma
(
"parameter list"
)
{
if
!
p
.
atComma
(
"parameter list"
)
{
break
break
}
}
p
.
next
()
p
.
next
()
}
}
}
else
{
}
else
{
// Type { "," Type } (anonymous parameters)
// Type { "," Type } (anonymous parameters)
params
=
make
([]
*
ast
.
Field
,
len
(
list
))
params
=
make
([]
*
ast
.
Field
,
len
(
list
))
for
i
,
x
:=
range
list
{
for
i
,
typ
:=
range
list
{
p
.
resolve
(
x
)
p
.
resolve
(
typ
)
params
[
i
]
=
&
ast
.
Field
{
Type
:
x
}
params
[
i
]
=
&
ast
.
Field
{
Type
:
typ
}
}
}
}
}
...
...
src/pkg/go/parser/parser_test.go
View file @
1f46cb0b
...
@@ -135,6 +135,67 @@ func TestVarScope(t *testing.T) {
...
@@ -135,6 +135,67 @@ func TestVarScope(t *testing.T) {
}
}
}
}
func
TestUnresolved
(
t
*
testing
.
T
)
{
f
,
err
:=
ParseFile
(
fset
,
""
,
`
package p
//
func f1a(int)
func f2a(byte, int, float)
func f3a(a, b int, c float)
func f4a(...complex)
func f5a(a s1a, b ...complex)
//
func f1b(*int)
func f2b([]byte, (int), *float)
func f3b(a, b *int, c []float)
func f4b(...*complex)
func f5b(a s1a, b ...[]complex)
//
type s1a struct { int }
type s2a struct { byte; int; s1a }
type s3a struct { a, b int; c float }
//
type s1b struct { *int }
type s2b struct { byte; int; *float }
type s3b struct { a, b *s3b; c []float }
`
,
0
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
want
:=
"int "
+
// f1a
"byte int float "
+
// f2a
"int float "
+
// f3a
"complex "
+
// f4a
"complex "
+
// f5a
//
"int "
+
// f1b
"byte int float "
+
// f2b
"int float "
+
// f3b
"complex "
+
// f4b
"complex "
+
// f5b
//
"int "
+
// s1a
"byte int "
+
// s2a
"int float "
+
// s3a
//
"int "
+
// s1a
"byte int float "
+
// s2a
"float "
// s3a
// collect unresolved identifiers
var
buf
bytes
.
Buffer
for
_
,
u
:=
range
f
.
Unresolved
{
buf
.
WriteString
(
u
.
Name
)
buf
.
WriteByte
(
' '
)
}
got
:=
buf
.
String
()
if
got
!=
want
{
t
.
Errorf
(
"
\n
got: %s
\n
want: %s"
,
got
,
want
)
}
}
var
imports
=
map
[
string
]
bool
{
var
imports
=
map
[
string
]
bool
{
`"a"`
:
true
,
`"a"`
:
true
,
"`a`"
:
true
,
"`a`"
:
true
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment