当前位置: 首页>>代码示例>>Golang>>正文


Golang types.Scope类代码示例

本文整理汇总了Golang中go/types.Scope的典型用法代码示例。如果您正苦于以下问题:Golang Scope类的具体用法?Golang Scope怎么用?Golang Scope使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Scope类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: deeper

// deeper reports whether block x is lexically deeper than y.
func deeper(x, y *types.Scope) bool {
	if x == y || x == nil {
		return false
	} else if y == nil {
		return true
	} else {
		return deeper(x.Parent(), y.Parent())
	}
}
开发者ID:ChloeTigre,项目名称:golang-tools,代码行数:10,代码来源:check.go

示例2: getTypeStruct

// getTypeStruct will take a type and the package scope, and return the
// (innermost) struct if the type is considered a RR type (currently defined as
// those structs beginning with a RR_Header, could be redefined as implementing
// the RR interface). The bool return value indicates if embedded structs were
// resolved.
func getTypeStruct(t types.Type, scope *types.Scope) (*types.Struct, bool) {
	st, ok := t.Underlying().(*types.Struct)
	if !ok {
		return nil, false
	}
	if st.Field(0).Type() == scope.Lookup("RR_Header").Type() {
		return st, false
	}
	if st.Field(0).Anonymous() {
		st, _ := getTypeStruct(st.Field(0).Type(), scope)
		return st, true
	}
	return nil, false
}
开发者ID:nickschuch,项目名称:dns,代码行数:19,代码来源:types_generate.go

示例3: analyzeCode

func analyzeCode(scope *types.Scope, docs *doc.Package, variable string) (imports.Importer, []fn, error) {
	pkg := docs.Name
	v, ok := scope.Lookup(variable).(*types.Var)
	if v == nil {
		return nil, nil, fmt.Errorf("impossible to find variable %s", variable)
	}
	if !ok {
		return nil, nil, fmt.Errorf("%s must be a variable", variable)
	}
	var vType interface {
		NumMethods() int
		Method(int) *types.Func
	}
	switch t := v.Type().(type) {
	case *types.Interface:
		vType = t
	case *types.Pointer:
		vType = t.Elem().(*types.Named)
	case *types.Named:
		vType = t
		if t, ok := t.Underlying().(*types.Interface); ok {
			vType = t
		}
	default:
		return nil, nil, fmt.Errorf("variable is of an invalid type: %T", v.Type().Underlying())
	}

	importer := imports.New(pkg)
	var funcs []fn
	for i := 0; i < vType.NumMethods(); i++ {
		f := vType.Method(i)

		if !f.Exported() {
			continue
		}

		sig := f.Type().(*types.Signature)

		funcs = append(funcs, fn{
			WrappedVar: variable,
			Name:       f.Name(),
			CurrentPkg: pkg,
			TypeInfo:   f,
		})
		importer.AddImportsFrom(sig.Params())
		importer.AddImportsFrom(sig.Results())
	}
	return importer, funcs, nil
}
开发者ID:ernesto-jimenez,项目名称:gogen,代码行数:49,代码来源:generator.go

示例4: convertScope

func (c *converter) convertScope(dst *types.Scope, src *gotypes.Scope) {
	for _, name := range src.Names() {
		obj := src.Lookup(name)
		dst.Insert(c.convertObject(obj))
	}
	for i := 0; i < src.NumChildren(); i++ {
		child := src.Child(i)
		newScope := types.NewScope(dst, token.Pos(child.Pos()), token.Pos(child.End()), "")
		c.convertScope(newScope, child)
	}
}
开发者ID:tcard,项目名称:sgo,代码行数:11,代码来源:importer.go

示例5: extractTestFunctions

func extractTestFunctions(scope *types.Scope) []string {
	var tests []string
	for _, name := range scope.Names() {
		if !strings.HasPrefix(name, "Test") {
			continue
		}

		if f, ok := scope.Lookup(name).(*types.Func); ok {
			sig := f.Type().(*types.Signature)

			// basic signature checks
			if sig.Recv() != nil {
				log.Printf("Skipping %q - test function should not be a method", f.String())
				continue
			}
			if sig.Variadic() {
				log.Printf("Skipping %q - test function should not be variadic", f.String())
				continue
			}
			if sig.Results() != nil {
				log.Printf("Skipping %q - test function should not return result", f.String())
				continue
			}

			// check params
			params := sig.Params()
			if params != nil || params.Len() == 1 {
				if named, ok := params.At(0).Type().(*types.Named); ok {
					if named.Obj().Name() == "TestingT" {
						tests = append(tests, f.Name())
						continue
					}
				}
			}

			log.Printf("Skipping %q - test function should have one parameter of type gophers.TestingT", f.String())
		}
	}

	return tests
}
开发者ID:go-gophers,项目名称:gophers,代码行数:41,代码来源:import.go

示例6: analyzeCode

// analyzeCode takes the types scope and the docs and returns the import
// information and information about all the assertion functions.
func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []testFunc, error) {
	testingT := scope.Lookup("TestingT").Type().Underlying().(*types.Interface)

	importer := imports.New(*outputPkg)
	var funcs []testFunc
	// Go through all the top level functions
	for _, fdocs := range docs.Funcs {
		// Find the function
		obj := scope.Lookup(fdocs.Name)

		fn, ok := obj.(*types.Func)
		if !ok {
			continue
		}
		// Check function signatuer has at least two arguments
		sig := fn.Type().(*types.Signature)
		if sig.Params().Len() < 2 {
			continue
		}
		// Check first argument is of type testingT
		first, ok := sig.Params().At(0).Type().(*types.Named)
		if !ok {
			continue
		}
		firstType, ok := first.Underlying().(*types.Interface)
		if !ok {
			continue
		}
		if !types.Implements(firstType, testingT) {
			continue
		}

		funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
		importer.AddImportsFrom(sig.Params())
	}
	return importer, funcs, nil
}
开发者ID:rawlingsj,项目名称:gofabric8,代码行数:39,代码来源:main.go

示例7: scopeCandidates

func (c *Suggester) scopeCandidates(scope *types.Scope, pos token.Pos, b *candidateCollector) {
	seen := make(map[string]bool)
	for scope != nil {
		isPkgScope := scope.Parent() == types.Universe
		for _, name := range scope.Names() {
			if seen[name] {
				continue
			}
			obj := scope.Lookup(name)
			if !isPkgScope && obj.Pos() > pos {
				continue
			}
			seen[name] = true
			b.appendObject(obj)
		}
		scope = scope.Parent()
	}
}
开发者ID:trevordixon,项目名称:gocode,代码行数:18,代码来源:suggest.go


注:本文中的go/types.Scope类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。