Пример #1
0
func (g *Go) complete_pkg(pkg string, cmp *content.CompletionResult) error {
	if g.imports == nil {
		g.imports = make(map[string]*types.Package)
	}

	if p, err := types.GcImport(g.imports, pkg); err != nil {
		return err
	} else {
		nn := p.Scope()
		for i := 0; i < nn.NumEntries(); i++ {
			t := nn.At(i)

			var flags content.Flags

			if n := t.Name(); n[0] != strings.ToUpper(n)[0] {
				flags = content.FLAG_ACC_PROTECTED
			} else {
				flags = content.FLAG_ACC_PUBLIC
			}
			switch t.(type) {
			case *types.Func:
				var m content.Method
				m.Flags |= flags
				m.Name.Relative = t.Name()
				sig := t.Type().Underlying().(*types.Signature)
				if sig.Recv() != nil {
					continue
				}
				par := sig.Params()
				for j := 0; j < par.Len(); j++ {
					m.Parameters = append(m.Parameters, g.pkg_var(par.At(j)))
				}
				ret := sig.Results()
				for j := 0; j < ret.Len(); j++ {
					m.Returns = append(m.Returns, g.pkg_var(ret.At(j)))
				}
				cmp.Methods = append(cmp.Methods, m)
			case *types.TypeName:
				var t2 content.Type
				t2.Flags |= flags
				t2.Name.Relative = t.Name()
				switch t.Type().Underlying().(type) {
				case *types.Interface:
					t2.Flags |= content.FLAG_TYPE_INTERFACE
				case *types.Struct:
					t2.Flags |= content.FLAG_TYPE_STRUCT
				}
				cmp.Types = append(cmp.Types, t2)
			case *types.Const, *types.Var:
				var f content.Field
				f.Name.Relative = t.Name()
				f.Type = g.pkg_type(t.Type())
				cmp.Fields = append(cmp.Fields, f)
			default:
				log4go.Warn("Unimplemented type in package completion: at: %+v, %v, %v", t, reflect.TypeOf(t), reflect.TypeOf(t.Type().Underlying()))
			}
		}
	}
	return nil
}
Пример #2
0
func (d *DWARFHelper) GetType(off dwarf.Offset) (content.Type, error) {
	var t content.Type
	r := d.df.Reader()
	r.Seek(off)
	recurse := false
	if e, err := r.Next(); err != nil {
		return t, err
	} else {
		switch e.Tag {
		case dwarf.TagVolatileType, dwarf.TagReferenceType, dwarf.TagRestrictType, dwarf.TagConstType, dwarf.TagSubroutineType:
			recurse = true
		case dwarf.TagPointerType:
			t.Flags |= content.FLAG_TYPE_POINTER
			recurse = true
		case dwarf.TagArrayType:
			recurse = true
			t.Flags |= content.FLAG_TYPE_ARRAY
		case dwarf.TagClassType, dwarf.TagTypedef, dwarf.TagBaseType, dwarf.TagEnumerationType, dwarf.TagStructType:
			if v, ok := e.Val(dwarf.AttrName).(string); ok {
				t.Name.Relative = v
			}
		case dwarf.TagUnionType:
			t.Name.Relative = "union"
		default:
			return t, errors.New(fmt.Sprintf("Don't know how to handle %+v", e))
		}
		if recurse {
			if v, ok := e.Val(dwarf.AttrType).(dwarf.Offset); ok {
				if t2, err := d.GetType(v); err != nil {
					return t, err
				} else {
					t.Specialization = append(t.Specialization, t2)
				}
			}
		}
		switch e.Tag {
		case dwarf.TagVolatileType, dwarf.TagReferenceType, dwarf.TagRestrictType, dwarf.TagConstType:
			if len(t.Specialization) == 0 {
				t.Name.Relative = "void"
			} else {
				t = t.Specialization[0]
			}
		}

		switch e.Tag {
		case dwarf.TagPointerType:
			if len(t.Specialization) == 0 {
				t.Specialization = append(t.Specialization, content.Type{Name: content.FullyQualifiedName{Relative: "void"}})
			}
		case dwarf.TagVolatileType:
			t.Flags |= content.FLAG_VOLATILE
		case dwarf.TagReferenceType:
			t.Flags |= content.FLAG_REFERENCE
		case dwarf.TagRestrictType:
			t.Flags |= content.FLAG_RESTRICT
		case dwarf.TagConstType:
			t.Flags |= content.FLAG_CONST
		case dwarf.TagSubroutineType:
			var m content.Method
			if len(t.Specialization) > 0 {
				m.Returns = append(m.Returns, content.Variable{Type: t.Specialization[0]})
			} else {
				m.Returns = append(m.Returns, content.Variable{Type: content.Type{Name: content.FullyQualifiedName{Relative: "void"}}})
			}
			t.Specialization = t.Specialization[0:0]
			t.Flags = content.FLAG_TYPE_METHOD
			for {
				if e, err := r.Next(); err != nil {
					return t, err
				} else if e == nil || e.Tag == 0 {
					break
				} else if e.Tag == dwarf.TagFormalParameter {
					var p content.Variable
					if v, ok := e.Val(dwarf.AttrType).(dwarf.Offset); ok {
						if t2, err := d.GetType(v); err != nil {
							return t, err
						} else {
							p.Type = t2
						}
					}
					if v, ok := e.Val(dwarf.AttrName).(string); ok {
						p.Name.Relative = v
					}
					m.Parameters = append(m.Parameters, p)
				}
			}
			t.Methods = append(t.Methods, m)
		}

		return t, nil
	}
}
Пример #3
0
func (td *TypeDef) ToContentType() (t *content.Type, err error) {
	if td.ct.Name.Relative != "" {
		return &td.ct, nil
	}
	t = &content.Type{}
	t.Name = td.Name()
	t.Flags = td.row.Flags.Convert()

	if ext, err := td.Extends(); err != nil && err != ErrInterface {
		return nil, err
	} else {
		t.Extends = ext
	}
	if imp, err := td.Implements(); err != nil {
		return nil, err
	} else {
		t.Implements = imp
	}
	if f, err := td.Fields(); err != nil {
		return nil, err
	} else {
		t.Fields = f
	}
	if f, err := td.Methods(); err != nil {
		return nil, err
	} else {
		t.Methods = f
	}

	idx := td.mu.Search(id_NestedClass, func(ti TableIndex) bool {
		if raw, err := ti.Data(); err == nil {
			c := raw.(*NestedClassRow)
			return c.NestedClass.Index() > td.index.Index()
		}
		return false
	})
	if idx != nil {
		ci := idx.(*ConcreteTableIndex)
		table := td.mu.Tables[idx.Table()]
		for i := idx.Index(); i < table.Rows+1; i++ {
			ci.index = i
			if raw, err := ci.Data(); err != nil {
				return nil, err
			} else {
				row := raw.(*NestedClassRow)
				if row.EnclosingClass.Index() != td.index.Index() {
					break
				} else if td2, err := TypeDefFromIndex(row.NestedClass); err != nil {
					return nil, err
				} else {
					ct := content.Type{}
					ct.Name = td2.Name()
					ct.Flags = td2.row.Flags.Convert()
					if err := check(&ct, ct.Name); err != nil {
						log4go.Fine("Skipping nested type: %s, %+v, %+v", err, ct, td2.row)
						continue
					}

					t.Types = append(t.Types, ct)
				}
			}
		}
	}
	err = content.Validate(&t)
	td.ct = *t
	return
}
Пример #4
0
func (t *Clang) CompleteAt(args *content.CompleteAtArgs, res *content.CompletionResult) error {
	origargs, _ := args.Settings().Get("compiler_flags").([]string)
	var unsaved map[string]string
	if args.Location.File.Contents != "" {
		unsaved = map[string]string{args.Location.File.Name: args.Location.File.Contents}
	}

	if tu := t.GetTranslationUnit(args.Location.File.Name, origargs, "", unsaved); tu == nil {
		return nil
	}
	cres := tu.CompleteAt(args.Location.File.Name, int(args.Location.Line), int(args.Location.Column), unsaved, 0)
	if !cres.IsValid() {
		return fmt.Errorf("CompleteResults is not valid")
	}
	defer cres.Dispose()
	for _, r := range cres.Results() {
		var (
			buf bytes.Buffer
		)
		switch r.CursorKind {
		case clang.CK_StructDecl, clang.CK_TypedefDecl:
			for _, c := range r.CompletionString.Chunks() {
				buf.WriteString(c.Text())
			}
			var tt content.Type
			tt.Flags = content.FLAG_TYPE_CLASS
			tt.Name.Absolute = buf.String()
			res.Types = append(res.Types, tt)
		case clang.CK_FunctionDecl:
			var (
				m            content.Method
				paramstarted bool
				argCount     int
			)
			for _, c := range r.CompletionString.Chunks() {
				switch k := c.Kind(); k {
				case clang.CompletionChunk_ResultType:
					var v content.Variable
					v.Name.Relative = c.Text()
					m.Returns = append(m.Returns, v)
				case clang.CompletionChunk_Placeholder:
					var v content.Variable
					v.Type.Name.Relative = c.Text()
					v.Name.Relative = fmt.Sprintf("arg%d", argCount)
					argCount++
					m.Parameters = append(m.Parameters, v)
				case clang.CompletionChunk_LeftParen:
					paramstarted = true
				case clang.CompletionChunk_RightParen, clang.CompletionChunk_Comma:
				case clang.CompletionChunk_TypedText:
					if !paramstarted {
						buf.WriteString(c.Text())
					}
				default:
					log4go.Warn("Unimplemented CompletionChunkKind: %s", k)
				}
			}
			m.Name.Relative = buf.String()
			res.Methods = append(res.Methods, m)
		default:
			log4go.Warn("Unimplemented CursorKind: %s", r.CursorKind)
		}
	}
	return nil
}