chore: move compiler/chore/_xtool to _xtool
This commit is contained in:
63
_xtool/astdump/astdump.cpp
Normal file
63
_xtool/astdump/astdump.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <clang-c/Index.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
CXChildVisitResult visit(CXCursor c, CXCursor parent, CXClientData client_data);
|
||||
|
||||
void printAST(CXCursor cursor, unsigned int depth = 0) {
|
||||
CXString cursorKind = clang_getCursorKindSpelling(clang_getCursorKind(cursor));
|
||||
CXString cursorSpelling = clang_getCursorSpelling(cursor);
|
||||
|
||||
for (unsigned int i = 0; i < depth; ++i) {
|
||||
std::cout << " ";
|
||||
}
|
||||
|
||||
std::cout << clang_getCString(cursorKind) << ": "
|
||||
<< clang_getCString(cursorSpelling) << std::endl;
|
||||
|
||||
clang_disposeString(cursorKind);
|
||||
clang_disposeString(cursorSpelling);
|
||||
|
||||
CXCursor child;
|
||||
clang_visitChildren(
|
||||
cursor,
|
||||
visit,
|
||||
&depth
|
||||
);
|
||||
}
|
||||
|
||||
CXChildVisitResult visit(CXCursor c, CXCursor parent, CXClientData client_data) {
|
||||
unsigned int* depth = (unsigned int*)client_data;
|
||||
printAST(c, *depth + 1);
|
||||
return CXChildVisit_Continue;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <header_file>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
CXIndex index = clang_createIndex(0, 0);
|
||||
CXTranslationUnit unit = clang_parseTranslationUnit(
|
||||
index,
|
||||
argv[1],
|
||||
nullptr, 0,
|
||||
nullptr, 0,
|
||||
CXTranslationUnit_None
|
||||
);
|
||||
|
||||
if (unit == nullptr) {
|
||||
std::cerr << "Unable to parse translation unit. Quitting." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
CXCursor cursor = clang_getTranslationUnitCursor(unit);
|
||||
std::cout << "AST for " << argv[1] << ":" << std::endl;
|
||||
printAST(cursor);
|
||||
|
||||
clang_disposeTranslationUnit(unit);
|
||||
clang_disposeIndex(index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
_xtool/astdump/build.sh
Executable file
2
_xtool/astdump/build.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
export LLVM_DIR=$(llvm-config --prefix)
|
||||
clang -L$LLVM_DIR/lib -lclang -lc++ -I$LLVM_DIR/include astdump.cpp
|
||||
221
_xtool/castdump/castdump.go
Normal file
221
_xtool/castdump/castdump.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
Depth c.Uint
|
||||
Unit *clang.TranslationUnit
|
||||
}
|
||||
|
||||
var accessMap = map[clang.CXXAccessSpecifier]string{
|
||||
clang.CXXInvalidAccessSpecifier: "invalid",
|
||||
clang.CXXPublic: "public",
|
||||
clang.CXXProtected: "protected",
|
||||
clang.CXXPrivate: "private",
|
||||
}
|
||||
|
||||
func printIndent(depth c.Uint) {
|
||||
fmt.Print(strings.Repeat(" ", int(depth)))
|
||||
}
|
||||
|
||||
func accessToString(spec clang.CXXAccessSpecifier) string {
|
||||
if str, ok := accessMap[spec]; ok {
|
||||
return str
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func visit(cursor, parent clang.Cursor, ClientData c.Pointer) clang.ChildVisitResult {
|
||||
data := (*Data)(ClientData)
|
||||
printAST(cursor, data)
|
||||
return clang.ChildVisit_Continue
|
||||
}
|
||||
|
||||
func printType(t clang.Type, data *Data) {
|
||||
printIndent(data.Depth)
|
||||
|
||||
typeSpell := t.String()
|
||||
typeKind := t.Kind.String()
|
||||
|
||||
if t.Kind == clang.TypeInvalid {
|
||||
} else if t.Kind == clang.TypeUnexposed {
|
||||
c.Printf(c.Str("<UnexposedType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
} else if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin {
|
||||
c.Printf(c.Str("<BuiltinType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
} else if t.Kind > clang.TypeComplex {
|
||||
c.Printf(c.Str("<ComplexType|%s>: %s\n"), typeKind.CStr(), typeSpell.CStr())
|
||||
}
|
||||
|
||||
data.Depth++
|
||||
switch t.Kind {
|
||||
case clang.TypePointer:
|
||||
printType(t.PointeeType(), data)
|
||||
case clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray, clang.TypeConstantArray:
|
||||
printType(t.ArrayElementType(), data)
|
||||
case clang.TypeTypedef:
|
||||
printType(t.TypeDeclaration().TypedefDeclUnderlyingType(), data)
|
||||
case clang.TypeElaborated:
|
||||
printType(t.NamedType(), data)
|
||||
case clang.TypeFunctionProto:
|
||||
printType(t.ResultType(), data)
|
||||
for i := 0; i < int(t.NumArgTypes()); i++ {
|
||||
printType(t.ArgType(c.Uint(i)), data)
|
||||
}
|
||||
}
|
||||
data.Depth--
|
||||
|
||||
typeKind.Dispose()
|
||||
typeSpell.Dispose()
|
||||
}
|
||||
|
||||
func printLocation(cursor clang.Cursor) {
|
||||
loc := cursor.Location()
|
||||
var file clang.File
|
||||
var line, column c.Uint
|
||||
|
||||
loc.SpellingLocation(&file, &line, &column, nil)
|
||||
filename := file.FileName()
|
||||
defer filename.Dispose()
|
||||
|
||||
c.Printf(c.Str("(Loc:%s:%d:%d)\n"), filename.CStr(), line, column)
|
||||
}
|
||||
|
||||
func printAccess(cursor clang.Cursor) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s %s"), kind.CStr(), spell.CStr(), c.AllocaCStr(accessToString(cursor.CXXAccessSpecifier())))
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printMacro(cursor clang.Cursor, unit *clang.TranslationUnit) {
|
||||
kind := cursor.Kind.String()
|
||||
defer kind.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: "), kind.CStr())
|
||||
ran := cursor.Extent()
|
||||
var numTokens c.Uint
|
||||
var tokens *clang.Token
|
||||
unit.Tokenize(ran, &tokens, &numTokens)
|
||||
defer unit.DisposeTokens(tokens, numTokens)
|
||||
|
||||
tokensSlice := unsafe.Slice(tokens, int(numTokens))
|
||||
for _, tok := range tokensSlice {
|
||||
tokStr := unit.Token(tok)
|
||||
c.Printf(c.Str("%s "), tokStr.CStr())
|
||||
tokStr.Dispose()
|
||||
}
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printFunc(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
symbol := cursor.Mangling()
|
||||
defer symbol.Dispose()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s (Symbol: %s)"), kind.CStr(), spell.CStr(), symbol.CStr())
|
||||
printLocation(cursor)
|
||||
printType(cursor.Type(), data)
|
||||
}
|
||||
|
||||
func printEnumConstant(cursor clang.Cursor) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s: %s:%lld"), kind.CStr(), spell.CStr(), cursor.EnumConstantDeclValue())
|
||||
printLocation(cursor)
|
||||
}
|
||||
|
||||
func printDefault(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
defer kind.Dispose()
|
||||
defer spell.Dispose()
|
||||
|
||||
// node which has type
|
||||
if cursor.Type().Kind != clang.TypeInvalid {
|
||||
c.Printf(c.Str("%s: %s"), kind.CStr(), spell.CStr())
|
||||
printLocation(cursor)
|
||||
printType(cursor.Type(), data)
|
||||
} else {
|
||||
c.Printf(c.Str("%s: %s\n"), kind.CStr(), spell.CStr())
|
||||
}
|
||||
}
|
||||
|
||||
func printAST(cursor clang.Cursor, data *Data) {
|
||||
kind := cursor.Kind.String()
|
||||
spell := cursor.String()
|
||||
|
||||
printIndent(data.Depth)
|
||||
|
||||
switch cursor.Kind {
|
||||
case clang.CursorCXXAccessSpecifier:
|
||||
printAccess(cursor)
|
||||
case clang.CursorMacroDefinition:
|
||||
printMacro(cursor, data.Unit)
|
||||
case clang.CursorFunctionDecl, clang.CursorCXXMethod, clang.CursorConstructor, clang.CursorDestructor:
|
||||
printFunc(cursor, data)
|
||||
case clang.CursorEnumConstantDecl:
|
||||
printEnumConstant(cursor)
|
||||
default:
|
||||
printDefault(cursor, data)
|
||||
}
|
||||
|
||||
data.Depth++
|
||||
clang.VisitChildren(cursor, visit, c.Pointer(data))
|
||||
data.Depth--
|
||||
|
||||
kind.Dispose()
|
||||
spell.Dispose()
|
||||
}
|
||||
|
||||
func main() {
|
||||
if c.Argc != 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: castdump <headerFile>")
|
||||
return
|
||||
}
|
||||
|
||||
args := make([]*c.Char, 3)
|
||||
args[0] = c.Str("-x")
|
||||
args[1] = c.Str("c++")
|
||||
args[2] = c.Str("-std=c++11")
|
||||
|
||||
sourceFile := *c.Advance(c.Argv, 1)
|
||||
index := clang.CreateIndex(0, 0)
|
||||
unit := index.ParseTranslationUnit(
|
||||
sourceFile,
|
||||
unsafe.SliceData(args), 3,
|
||||
nil, 0,
|
||||
clang.DetailedPreprocessingRecord,
|
||||
)
|
||||
defer index.Dispose()
|
||||
defer unit.Dispose()
|
||||
|
||||
if unit == nil {
|
||||
println("Unable to parse translation unit. Quitting.")
|
||||
c.Exit(1)
|
||||
}
|
||||
|
||||
cursor := unit.Cursor()
|
||||
|
||||
Data := &Data{
|
||||
Depth: 0,
|
||||
Unit: unit,
|
||||
}
|
||||
printAST(cursor, Data)
|
||||
}
|
||||
60
_xtool/pydump/pydump.go
Normal file
60
_xtool/pydump/pydump.go
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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 main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/inspect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if c.Argc < 2 {
|
||||
c.Fprintf(c.Stderr, c.Str("Usage: pydump <pythonLibPath>\n"))
|
||||
return
|
||||
}
|
||||
pyLib := c.Index(c.Argv, 1)
|
||||
|
||||
py.Initialize()
|
||||
|
||||
root := cjson.Object()
|
||||
root.SetItem(c.Str("name"), cjson.String(pyLib))
|
||||
|
||||
items := cjson.Array()
|
||||
mod := py.ImportModule(pyLib)
|
||||
keys := mod.ModuleGetDict().DictKeys()
|
||||
for i, n := 0, keys.ListLen(); i < n; i++ {
|
||||
key := keys.ListItem(i)
|
||||
val := mod.GetAttr(key)
|
||||
doc := val.GetAttrString(c.Str("__doc__"))
|
||||
sym := cjson.Object()
|
||||
sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr()))
|
||||
sym.SetItem(c.Str("name"), cjson.String(key.CStr()))
|
||||
if doc != nil {
|
||||
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
|
||||
}
|
||||
if val.Callable() != 0 {
|
||||
sig := inspect.Signature(val)
|
||||
sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr()))
|
||||
}
|
||||
items.AddItem(sym)
|
||||
}
|
||||
root.SetItem(c.Str("items"), items)
|
||||
|
||||
c.Printf(c.Str("%s\n"), root.CStr())
|
||||
}
|
||||
Reference in New Issue
Block a user