Daily bump.
[official-gcc.git] / libgo / go / go / types / objects.go
blobdcd905b4138d65f807ba327e01c4095c2c474bce
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package types
7 import (
8 "go/ast"
9 "go/token"
12 // An Object describes a named language entity such as a package,
13 // constant, type, variable, function (incl. methods), or label.
14 // All objects implement the Object interface.
16 type Object interface {
17 GetName() string
18 GetType() Type
19 GetPos() token.Pos
21 anObject()
24 // A Package represents the contents (objects) of a Go package.
25 type Package struct {
26 Name string
27 Path string // import path, "" for current (non-imported) package
28 Scope *Scope // package-level scope
29 Imports map[string]*Package // map of import paths to imported packages
30 Complete bool // if set, this package was imported completely
32 spec *ast.ImportSpec
35 // A Const represents a declared constant.
36 type Const struct {
37 Name string
38 Type Type
39 Val interface{}
41 spec *ast.ValueSpec
44 // A TypeName represents a declared type.
45 type TypeName struct {
46 Name string
47 Type Type // *NamedType or *Basic
49 spec *ast.TypeSpec
52 // A Variable represents a declared variable (including function parameters and results).
53 type Var struct {
54 Name string
55 Type Type
57 visited bool // for initialization cycle detection
58 decl interface{}
61 // A Func represents a declared function.
62 type Func struct {
63 Name string
64 Type Type // *Signature or *Builtin
66 decl *ast.FuncDecl
69 func (obj *Package) GetName() string { return obj.Name }
70 func (obj *Const) GetName() string { return obj.Name }
71 func (obj *TypeName) GetName() string { return obj.Name }
72 func (obj *Var) GetName() string { return obj.Name }
73 func (obj *Func) GetName() string { return obj.Name }
75 func (obj *Package) GetType() Type { return Typ[Invalid] }
76 func (obj *Const) GetType() Type { return obj.Type }
77 func (obj *TypeName) GetType() Type { return obj.Type }
78 func (obj *Var) GetType() Type { return obj.Type }
79 func (obj *Func) GetType() Type { return obj.Type }
81 func (obj *Package) GetPos() token.Pos { return obj.spec.Pos() }
82 func (obj *Const) GetPos() token.Pos {
83 for _, n := range obj.spec.Names {
84 if n.Name == obj.Name {
85 return n.Pos()
88 return token.NoPos
90 func (obj *TypeName) GetPos() token.Pos { return obj.spec.Pos() }
91 func (obj *Var) GetPos() token.Pos {
92 switch d := obj.decl.(type) {
93 case *ast.Field:
94 for _, n := range d.Names {
95 if n.Name == obj.Name {
96 return n.Pos()
99 case *ast.ValueSpec:
100 for _, n := range d.Names {
101 if n.Name == obj.Name {
102 return n.Pos()
105 case *ast.AssignStmt:
106 for _, x := range d.Lhs {
107 if ident, isIdent := x.(*ast.Ident); isIdent && ident.Name == obj.Name {
108 return ident.Pos()
112 return token.NoPos
114 func (obj *Func) GetPos() token.Pos { return obj.decl.Name.Pos() }
116 func (*Package) anObject() {}
117 func (*Const) anObject() {}
118 func (*TypeName) anObject() {}
119 func (*Var) anObject() {}
120 func (*Func) anObject() {}
122 // newObj returns a new Object for a given *ast.Object.
123 // It does not canonicalize them (it always returns a new one).
124 // For canonicalization, see check.lookup.
126 // TODO(gri) Once we do identifier resolution completely in
127 // in the typechecker, this functionality can go.
129 func newObj(astObj *ast.Object) Object {
130 name := astObj.Name
131 typ, _ := astObj.Type.(Type)
132 switch astObj.Kind {
133 case ast.Bad:
134 // ignore
135 case ast.Pkg:
136 unreachable()
137 case ast.Con:
138 return &Const{Name: name, Type: typ, Val: astObj.Data, spec: astObj.Decl.(*ast.ValueSpec)}
139 case ast.Typ:
140 return &TypeName{Name: name, Type: typ, spec: astObj.Decl.(*ast.TypeSpec)}
141 case ast.Var:
142 switch astObj.Decl.(type) {
143 case *ast.Field, *ast.ValueSpec, *ast.AssignStmt: // these are ok
144 default:
145 unreachable()
147 return &Var{Name: name, Type: typ, decl: astObj.Decl}
148 case ast.Fun:
149 return &Func{Name: name, Type: typ, decl: astObj.Decl.(*ast.FuncDecl)}
150 case ast.Lbl:
151 unreachable() // for now
153 unreachable()
154 return nil