cl handleTypeDecl: typeBackground
This commit is contained in:
50
cl/import.go
50
cl/import.go
@@ -197,14 +197,7 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case token.TYPE:
|
case token.TYPE:
|
||||||
if len(decl.Specs) == 1 {
|
handleTypeDecl(p.prog, p.goTyps, decl)
|
||||||
if hasTypec(decl.Doc) {
|
|
||||||
inPkgName := decl.Specs[0].(*ast.TypeSpec).Name.Name
|
|
||||||
if obj := p.goTyps.Scope().Lookup(inPkgName); obj != nil {
|
|
||||||
p.prog.Type(obj.Type(), llssa.InC)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -553,7 +546,7 @@ func (p *context) initPyModule() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParsePkgSyntax: check llgo:type C
|
// ParsePkgSyntax parses AST of a package to check llgo:type in type declaration.
|
||||||
func ParsePkgSyntax(prog llssa.Program, pkg *types.Package, files []*ast.File) {
|
func ParsePkgSyntax(prog llssa.Program, pkg *types.Package, files []*ast.File) {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
for _, decl := range file.Decls {
|
for _, decl := range file.Decls {
|
||||||
@@ -561,35 +554,50 @@ func ParsePkgSyntax(prog llssa.Program, pkg *types.Package, files []*ast.File) {
|
|||||||
case *ast.GenDecl:
|
case *ast.GenDecl:
|
||||||
switch decl.Tok {
|
switch decl.Tok {
|
||||||
case token.TYPE:
|
case token.TYPE:
|
||||||
|
handleTypeDecl(prog, pkg, decl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTypeDecl(prog llssa.Program, pkg *types.Package, decl *ast.GenDecl) {
|
||||||
if len(decl.Specs) == 1 {
|
if len(decl.Specs) == 1 {
|
||||||
if hasTypec(decl.Doc) {
|
if bg := typeBackground(decl.Doc); bg != "" {
|
||||||
inPkgName := decl.Specs[0].(*ast.TypeSpec).Name.Name
|
inPkgName := decl.Specs[0].(*ast.TypeSpec).Name.Name
|
||||||
if obj := pkg.Scope().Lookup(inPkgName); obj != nil {
|
if obj := pkg.Scope().Lookup(inPkgName); obj != nil {
|
||||||
prog.Type(obj.Type(), llssa.InC)
|
prog.Type(obj.Type(), toBackground(bg))
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
llgotypec = "//llgo:type C"
|
llgotype = "//llgo:type"
|
||||||
llgotypec2 = "// llgo:type C"
|
llgotype2 = "// llgo:type"
|
||||||
)
|
)
|
||||||
|
|
||||||
func hasTypec(doc *ast.CommentGroup) bool {
|
func typeBackground(doc *ast.CommentGroup) (bg string) {
|
||||||
if doc != nil {
|
if doc != nil {
|
||||||
if n := len(doc.List); n > 0 {
|
if n := len(doc.List); n > 0 {
|
||||||
line := doc.List[n-1].Text
|
line := doc.List[n-1].Text
|
||||||
if strings.HasPrefix(line, llgotypec) || strings.HasPrefix(line, llgotypec2) {
|
if strings.HasPrefix(line, llgotype) {
|
||||||
return true
|
return strings.TrimSpace(line[len(llgotype):])
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(line, llgotype2) {
|
||||||
|
return strings.TrimSpace(line[len(llgotype2):])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func toBackground(bg string) llssa.Background {
|
||||||
|
switch bg {
|
||||||
|
case "C":
|
||||||
|
return llssa.InC
|
||||||
|
}
|
||||||
|
return llssa.InGo
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -247,7 +247,6 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
built[pkg.PkgPath] = none{}
|
built[pkg.PkgPath] = none{}
|
||||||
//parsePkg(ctx, aPkg, verbose)
|
|
||||||
switch kind, param := cl.PkgKindOf(pkg.Types); kind {
|
switch kind, param := cl.PkgKindOf(pkg.Types); kind {
|
||||||
case cl.PkgDeclOnly:
|
case cl.PkgDeclOnly:
|
||||||
// skip packages that only contain declarations
|
// skip packages that only contain declarations
|
||||||
@@ -586,21 +585,6 @@ func checkFlag(arg string, i *int, verbose *bool, swflags map[string]bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
rootDir string
|
|
||||||
)
|
|
||||||
|
|
||||||
func llgoRoot() string {
|
|
||||||
if rootDir == "" {
|
|
||||||
root := os.Getenv("LLGOROOT")
|
|
||||||
if root == "" {
|
|
||||||
panic("todo: LLGOROOT not set")
|
|
||||||
}
|
|
||||||
rootDir, _ = filepath.Abs(root)
|
|
||||||
}
|
|
||||||
return rootDir
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendLinkFiles(args []string, file string) []string {
|
func appendLinkFiles(args []string, file string) []string {
|
||||||
if isSingleLinkFile(file) {
|
if isSingleLinkFile(file) {
|
||||||
return append(args, file)
|
return append(args, file)
|
||||||
|
|||||||
Reference in New Issue
Block a user