Skip to content

Commit

Permalink
Merge pull request #1500 from xushiwei/typesutil
Browse files Browse the repository at this point in the history
typesutil.Check: support Go/Go+ mixed project
  • Loading branch information
xushiwei committed Oct 29, 2023
2 parents 2d02f1a + 33c2b21 commit 7c44294
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 200 deletions.
137 changes: 0 additions & 137 deletions cmd/gopdecl/gopdecl.go

This file was deleted.

58 changes: 0 additions & 58 deletions x/types/load.go

This file was deleted.

41 changes: 36 additions & 5 deletions x/typesutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/token"
"github.com/goplus/gop/x/typesutil/internal/typesutil"
"github.com/qiniu/x/log"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func NewChecker(conf *types.Config, opts *Config, goInfo *types.Info, gopInfo *I
}

// Files checks the provided files as part of the checker's package.
func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) error {
func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) (err error) {
opts := p.opts
pkgTypes := opts.Types
fset := opts.Fset
Expand All @@ -107,6 +108,7 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) error {
checker := types.NewChecker(conf, fset, pkgTypes, p.goInfo)
return checker.Files(goFiles)
}
files := make([]*goast.File, 0, len(goFiles))
gofs := make(map[string]*goast.File)
gopfs := make(map[string]*ast.File)
for _, goFile := range goFiles {
Expand All @@ -117,6 +119,7 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) error {
continue
}
gofs[file] = goFile
files = append(files, goFile)
}
for _, gopFile := range gopFiles {
f := fset.File(gopFile.Pos())
Expand All @@ -130,7 +133,7 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) error {
Files: gopfs,
GoFiles: gofs,
}
_, err := cl.NewPackage(pkgTypes.Path(), pkg, &cl.Config{
_, err = cl.NewPackage(pkgTypes.Path(), pkg, &cl.Config{
Types: pkgTypes,
Fset: fset,
WorkingDir: opts.WorkingDir,
Expand All @@ -143,11 +146,39 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) error {
NoAutoGenMain: true,
NoSkipConstant: true,
})
if debugPrintErr {
if err != nil {
if err != nil {
if debugPrintErr {
log.Println("typesutil.Check err:", err)
log.SingleStack()
}
return
}
return err
if len(files) > 0 {
scope := pkgTypes.Scope()
// remove all objects defined by files
for _, f := range files {
for _, decl := range f.Decls {
switch v := decl.(type) {
case *goast.GenDecl:
for _, spec := range v.Specs {
switch v := spec.(type) {
case *goast.ValueSpec:
for _, name := range v.Names {
typesutil.ScopeDelete(scope, name.Name)
}
case *goast.TypeSpec:
typesutil.ScopeDelete(scope, v.Name.Name)
}
}
case *goast.FuncDecl:
if v.Recv == nil {
typesutil.ScopeDelete(scope, v.Name.Name)
}
}
}
}
checker := types.NewChecker(conf, fset, pkgTypes, p.goInfo)
err = checker.Files(files)
}
return
}
42 changes: 42 additions & 0 deletions x/typesutil/internal/typesutil/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package typesutil

import (
"go/token"
"go/types"
"unsafe"
)

// A Scope maintains a set of objects and links to its containing
// (parent) and contained (children) scopes. Objects may be inserted
// and looked up by name. The zero value for Scope is a ready-to-use
// empty scope.
type Scope struct {
parent *Scope
children []*Scope
number int // parent.children[number-1] is this scope; 0 if there is no parent
elems map[string]types.Object // lazily allocated
pos, end token.Pos // scope extent; may be invalid
comment string // for debugging only
isFunc bool // set if this is a function scope (internal use only)
}

// ScopeDelete deletes an object from specified scope by its name.
func ScopeDelete(s *types.Scope, name string) {
delete((*Scope)(unsafe.Pointer(s)).elems, name)
}

0 comments on commit 7c44294

Please sign in to comment.