本文整理汇总了Golang中go/ast.Ident类的典型用法代码示例。如果您正苦于以下问题:Golang Ident类的具体用法?Golang Ident怎么用?Golang Ident使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Ident类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compileIdent
func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type {
_, _, def := a.block.Lookup(x.Name())
if def == nil {
a.diagAt(x, "%s: undefined", x.Name())
return nil
}
switch def := def.(type) {
case *Constant:
a.diagAt(x, "constant %v used as type", x.Name())
return nil
case *Variable:
a.diagAt(x, "variable %v used as type", x.Name())
return nil
case *NamedType:
if !allowRec && def.incomplete {
a.diagAt(x, "illegal recursive type")
return nil
}
if !def.incomplete && def.Def == nil {
// Placeholder type from an earlier error
return nil
}
return def
case Type:
return def
}
log.Crashf("name %s has unknown type %T", x.Name(), def)
return nil
}
示例2: createDef
func createDef(obj types.Object, ident *ast.Ident, ctx *getDefinitionsContext, isType bool) *Definition {
fullName := getFullName(obj, ctx, isType)
if def, ok := ctx.defs[fullName]; ok {
return def
}
def := new(Definition)
def.Name = fullName
def.Pkg = obj.Pkg()
def.IsExported = obj.Exported()
def.TypeOf = reflect.TypeOf(obj)
def.SimpleName = obj.Name()
def.Usages = make([]*Usage, 0)
def.InterfacesDefs = make([]*Definition, 0)
if ident != nil {
position := ctx.fset.Position(ident.Pos())
def.File = position.Filename
def.Line = position.Line
def.Offset = position.Offset
def.Col = position.Column
}
if !types.IsInterface(obj.Type()) {
fillInterfaces(def, obj, ctx)
}
ctx.defs[def.Name] = def
logDefinition(def, obj, ident, ctx)
return def
}
示例3: findLexicalLabel
func (a *stmtCompiler) findLexicalLabel(name *ast.Ident, pred func(*label) bool, errOp, errCtx string) *label {
bc := a.blockCompiler
for ; bc != nil; bc = bc.parent {
if bc.label == nil {
continue
}
l := bc.label
if name == nil && pred(l) {
return l
}
if name != nil && l.name == name.Name() {
if !pred(l) {
a.diag("cannot %s to %s %s", errOp, l.desc, l.name)
return nil
}
return l
}
}
if name == nil {
a.diag("%s outside %s", errOp, errCtx)
} else {
a.diag("%s label %s not defined", errOp, name.Name())
}
return nil
}
示例4: emitFieldSelection
// emitFieldSelection emits to f code to select the index'th field of v.
//
// If wantAddr, the input must be a pointer-to-struct and the result
// will be the field's address; otherwise the result will be the
// field's value.
// Ident id is used for position and debug info.
//
func emitFieldSelection(f *Function, v Value, index int, wantAddr bool, id *ast.Ident) Value {
fld := deref(v.Type()).Underlying().(*types.Struct).Field(index)
if isPointer(v.Type()) {
instr := &FieldAddr{
X: v,
Field: index,
}
instr.setPos(id.Pos())
instr.setType(types.NewPointer(fld.Type()))
v = f.emit(instr)
// Load the field's value iff we don't want its address.
if !wantAddr {
v = emitLoad(f, v)
}
} else {
instr := &Field{
X: v,
Field: index,
}
instr.setPos(id.Pos())
instr.setType(fld.Type())
v = f.emit(instr)
}
emitDebugRef(f, id, v, wantAddr)
return v
}
示例5: DeclLhsPos
// DeclLhsPos returns the position of the ident's variable on the left hand
// side of the assignment operator with which it was declared.
func DeclLhsPos(ident *ast.Ident) (int, error) {
var identPos int
switch n := ident.Obj.Decl.(type) {
case *ast.AssignStmt:
// find position of ident on lhs of assignment
for i, exp := range n.Lhs {
if a, ok := exp.(*ast.Ident); ok && SameIdent(a, ident) {
identPos = i
break
}
}
case *ast.ValueSpec:
// find position of ident on lhs of assignment
for i, name := range n.Names {
if name.String() == ident.String() {
identPos = i
break
}
}
default:
return 0, errors.New("could not get lhs position of ident: unknown decl type")
}
return identPos, nil
}
示例6: Ident
func (s *Styler) Ident(id *ast.Ident) (text []byte, tag printer.HTMLTag) {
text = strings.Bytes(id.Name())
if s.highlight == id.Name() {
tag = printer.HTMLTag{"<span class=highlight>", "</span>"}
}
return
}
示例7: Ident
func (s *snippetStyler) Ident(id *ast.Ident) (text []byte, tag printer.HTMLTag) {
text = []byte(id.Name())
if s.highlight == id {
tag = printer.HTMLTag{"<span class=highlight>", "</span>"}
}
return
}
示例8: visitIdent
func (x *Indexer) visitIdent(kind SpotKind, id *ast.Ident) {
if id == nil {
return
}
name := x.intern(id.Name)
switch kind {
case TypeDecl, FuncDecl, ConstDecl, VarDecl:
x.curPkgExports[name] = kind
}
lists, found := x.words[name]
if !found {
lists = new(IndexResult)
x.words[name] = lists
}
if kind == Use || x.decl == nil {
if x.c.IndexGoCode {
// not a declaration or no snippet required
info := makeSpotInfo(kind, x.current.Line(id.Pos()), false)
lists.Others = append(lists.Others, Spot{x.file, info})
}
} else {
// a declaration with snippet
index := x.addSnippet(NewSnippet(x.fset, x.decl, id))
info := makeSpotInfo(kind, index, true)
lists.Decls = append(lists.Decls, Spot{x.file, info})
}
x.stats.Spots++
}
示例9: collectMethods
// collectMethods collects the method declarations from an AST File and
// returns a mapping from receiver types to their method FuncDecl's.
func (c *checker) collectMethods(file *ast.File) {
for _, decl := range file.Decls {
if funcdecl, ok := decl.(*ast.FuncDecl); ok && funcdecl.Recv != nil {
recvField := funcdecl.Recv.List[0]
var recv *ast.Ident
switch typ := recvField.Type.(type) {
case *ast.StarExpr:
recv = typ.X.(*ast.Ident)
case *ast.Ident:
recv = typ
case *ast.BadExpr:
return
}
if recv.Obj == nil {
// error reported elsewhere.
return
}
if recv.Obj.Kind != ast.Typ {
c.errorf(recv.Pos(), "%s is not a type", recv.Name)
return
}
// The Obj field of the funcdecl wll be nil, so we'll have to
// create a new one.
funcdecl.Name.Obj = ast.NewObj(ast.Fun, funcdecl.Name.String())
funcdecl.Name.Obj.Decl = funcdecl
c.methods[recv.Obj] = append(c.methods[recv.Obj], funcdecl.Name.Obj)
}
}
}
示例10: SameIdent
// SameIdent returns true if a and b are the same.
func SameIdent(a, b *ast.Ident) bool {
// TODO(waigani) Don't rely on name, it could change and still be the same
// ident.
if a.String() != b.String() {
return false
}
// TODO(waigani) this happens if ident decl is outside of current
// file. We need to use the FileSet to find it.
if a.Obj == nil && b.Obj == nil {
return true
}
pa, err := DeclPos(a)
if err != nil {
// TODO(waigani) log error
return false
}
pb, err := DeclPos(b)
if err != nil {
// TODO(waigani) log error
return false
}
if pa != pb {
return false
}
return true
}
示例11: Visit
func (v *visitor) Visit(node ast.Node) bool {
descend := true
var ident *ast.Ident
var kind string
switch t := node.(type) {
case *ast.FuncDecl:
kind = "func"
ident = t.Name
descend = false
case *ast.TypeSpec:
kind = "type"
ident = t.Name
descend = false
}
if ident != nil && strings.Contains(strings.ToLower(ident.Name), v.query) {
f := v.fset.File(ident.Pos())
v.syms = append(v.syms, symbol{
Package: v.pkg.Name,
Path: f.Name(),
Name: ident.Name,
Kind: kind,
Line: f.Line(ident.Pos()) - 1,
})
}
return descend
}
示例12: findField
// findField returns the object with the given name if visible in the type's scope.
// If no such object is found, an error is reported and a bad object is returned instead.
func (tc *typechecker) findField(typ *ast.Type, name *ast.Ident) (obj *ast.Object) {
// TODO(gri) This is simplistic at the moment and ignores anonymous fields.
obj = typ.Scope.Lookup(name.Name)
if obj == nil {
tc.Errorf(name.Pos(), "%s not declared", name.Name)
obj = ast.NewObj(ast.Bad, name.Name)
}
return
}
示例13: declInScope
// declInScope declares an object of a given kind and name in scope and sets the object's Decl and N fields.
// It returns the newly allocated object. If an object with the same name already exists in scope, an error
// is reported and the object is not inserted.
// (Objects with _ name are always inserted into a scope without errors, but they cannot be found.)
func (tc *typechecker) declInScope(scope *ast.Scope, kind ast.Kind, name *ast.Ident, decl interface{}, n int) *ast.Object {
obj := ast.NewObj(kind, name.Name)
obj.Decl = decl
obj.N = n
name.Obj = obj
if alt := scope.Insert(obj); alt != obj {
tc.Errorf(name.Pos(), "%s already declared at %s", name.Name, objPos(alt))
}
return obj
}
示例14: makeFunc
func (c *compiler) makeFunc(ident *ast.Ident, ftyp *types.Signature) *LLVMValue {
fname := ident.String()
if ftyp.Recv() == nil && fname == "init" {
// Make "init" functions anonymous.
fname = ""
} else {
var pkgname string
if recv := ftyp.Recv(); recv != nil {
var recvname string
switch recvtyp := recv.Type().(type) {
case *types.Pointer:
if named, ok := recvtyp.Elem().(*types.Named); ok {
obj := named.Obj()
recvname = "*" + obj.Name()
pkgname = obj.Pkg().Path()
}
case *types.Named:
named := recvtyp
obj := named.Obj()
recvname = obj.Name()
pkgname = obj.Pkg().Path()
}
if recvname != "" {
fname = fmt.Sprintf("%s.%s", recvname, fname)
} else {
// If the receiver is an unnamed struct, we're
// synthesising a method for an unnamed struct
// type. There's no meaningful name to give the
// function, so leave it up to LLVM.
fname = ""
}
} else {
obj := c.typeinfo.Objects[ident]
pkgname = obj.Pkg().Path()
}
if fname != "" {
fname = pkgname + "." + fname
}
}
// gcimporter may produce multiple AST objects for the same function.
llvmftyp := c.types.ToLLVM(ftyp)
var fn llvm.Value
if fname != "" {
fn = c.module.Module.NamedFunction(fname)
}
if fn.IsNil() {
llvmfptrtyp := llvmftyp.StructElementTypes()[0].ElementType()
fn = llvm.AddFunction(c.module.Module, fname, llvmfptrtyp)
}
fn = llvm.ConstInsertValue(llvm.ConstNull(llvmftyp), fn, []uint32{0})
return c.NewValue(fn, ftyp)
}
示例15: newSnippet
func newSnippet(fset *token.FileSet, decl ast.Decl, id *ast.Ident) *Snippet {
// TODO instead of pretty-printing the node, should use the original source instead
var buf1 bytes.Buffer
writeNode(&buf1, fset, decl)
// wrap text with <pre> tag
var buf2 bytes.Buffer
buf2.WriteString("<pre>")
FormatText(&buf2, buf1.Bytes(), -1, true, id.Name, nil)
buf2.WriteString("</pre>")
return &Snippet{fset.Position(id.Pos()).Line, buf2.String()}
}