diff options
Diffstat (limited to 'src/pkg/go/ast/ast.go')
-rw-r--r-- | src/pkg/go/ast/ast.go | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/pkg/go/ast/ast.go b/src/pkg/go/ast/ast.go index 49b92fe28..b8a964939 100644 --- a/src/pkg/go/ast/ast.go +++ b/src/pkg/go/ast/ast.go @@ -117,8 +117,8 @@ type ( // An Ident node represents an identifier. Ident struct { - token.Position // identifier position - Value string // identifier string (e.g. foobar) + token.Position // identifier position + Obj *Object // denoted object } // An Ellipsis node stands for the "..." type in a @@ -139,6 +139,8 @@ type ( // A single string literal (common case) is represented by a BasicLit // node; StringList nodes are used only if there are two or more string // literals in a sequence. + // TODO(gri) Deprecated. StringLists are only created by exp/parser; + // Remove when exp/parser is removed. // StringList struct { Strings []*BasicLit // list of strings, len(Strings) > 1 @@ -346,6 +348,16 @@ func (x *MapType) exprNode() {} func (x *ChanType) exprNode() {} +// ---------------------------------------------------------------------------- +// Convenience functions for Idents + +// NewIdent creates a new Ident without position and minimal object +// information. Useful for ASTs generated by code other than the Go +// parser. +// +func NewIdent(name string) *Ident { return &Ident{noPos, NewObj(Err, noPos, name)} } + + // IsExported returns whether name is an exported Go symbol // (i.e., whether it begins with an uppercase letter). func IsExported(name string) bool { @@ -353,13 +365,19 @@ func IsExported(name string) bool { return unicode.IsUpper(ch) } -// IsExported returns whether name is an exported Go symbol + +// IsExported returns whether id is an exported Go symbol // (i.e., whether it begins with an uppercase letter). -func (name *Ident) IsExported() bool { return IsExported(name.Value) } +func (id *Ident) IsExported() bool { return id.Obj.IsExported() } + + +// Name returns an identifier's name. +func (id *Ident) Name() string { return id.Obj.Name } + -func (name *Ident) String() string { - if name != nil { - return name.Value +func (id *Ident) String() string { + if id != nil && id.Obj != nil { + return id.Obj.Name } return "<nil>" } |