示例#1
0
func appendToTopDecls(decls map[string]*Decl, decl ast.Decl, scope *Scope) {
	foreachDecl(decl, func(data *foreachDeclStruct) {
		class := astDeclClass(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.typeValueIndex(i, 0)

			d := NewDecl2(name.Name, class, 0, typ, v, vi, scope)
			if d == nil {
				return
			}

			methodof := MethodOf(decl)
			if methodof != "" {
				decl, ok := decls[methodof]
				if ok {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, scope)
					decls[methodof] = decl
					decl.AddChild(d)
				}
			} else {
				decl, ok := decls[d.Name]
				if ok {
					decl.ExpandOrReplace(d)
				} else {
					decls[d.Name] = d
				}
			}
		}
	})
}
示例#2
0
文件: file.go 项目: kij/gocode
func (self *PackageFile) processDecl(decl ast.Decl) {
	if self.scope != self.filescope {
		if t, ok := decl.(*ast.GenDecl); ok && t.Offset > self.cursor {
			return
		}
	}
	decls := splitDecls(decl)
	for _, decl := range decls {
		names := declNames(decl)
		values := declValues(decl)

		for i, name := range names {
			var value ast.Expr = nil
			valueindex := -1
			if values != nil {
				if len(values) > 1 {
					value = values[i]
				} else {
					value = values[0]
					valueindex = i
				}
			}

			d := NewDeclFromAstDecl(name, 0, decl, value, valueindex, self.scope)
			if d == nil {
				continue
			}

			methodof := MethodOf(decl)
			if methodof != "" {
				decl, ok := self.decls[methodof]
				if ok {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, self.scope)
					self.decls[methodof] = decl
					decl.AddChild(d)
				}
			} else {
				if self.scope != self.filescope {
					// the declaration itself has a scope which follows it's definition
					// and it's false for type declarations
					if d.Class != DECL_TYPE {
						self.scope = NewScope(self.scope)
						self.topscope = self.scope
					}
					self.scope.addNamedDecl(d)
				} else {
					self.addVarDecl(d)
				}
			}
		}
	}
}
示例#3
0
func addAstDeclToPackage(pkg *Decl, decl ast.Decl, scope *Scope) {
	foreachDecl(decl, func(data *foreachDeclStruct) {
		class := astDeclClass(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.typeValueIndex(i, DECL_FOREIGN)

			d := NewDecl2(name.Name, class, DECL_FOREIGN, typ, v, vi, scope)
			if d == nil {
				return
			}

			if !name.IsExported() && d.Class != DECL_TYPE {
				return
			}

			methodof := MethodOf(data.decl)
			if methodof != "" {
				decl := pkg.FindChild(methodof)
				if decl != nil {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, scope)
					decl.AddChild(d)
					pkg.AddChild(decl)
				}
			} else {
				decl := pkg.FindChild(d.Name)
				if decl != nil {
					decl.ExpandOrReplace(d)
				} else {
					pkg.AddChild(d)
				}
			}
		}
	})
}