Permalink
Browse files

libclang should yodawg food, and use ffi

node-libclang almost round trips itself
  • Loading branch information...
1 parent d4610dd commit e938dda4fe8662048758df7f2b830d839488aa67 @tjfontaine committed Sep 26, 2012
Showing with 339 additions and 962 deletions.
  1. +9 −2 bin/ffi-generate-node.js
  2. +0 −26 binding.gyp
  3. +109 −0 lib/cursor.js
  4. +8 −3 lib/dynamic_clang.js
  5. +54 −54 lib/generateffi.js
  6. +18 −0 lib/index.js
  7. +30 −0 lib/location.js
  8. +41 −0 lib/translationunit.js
  9. +65 −0 lib/type.js
  10. +5 −0 lib/util.js
  11. +0 −247 src/constants.cc
  12. +0 −11 src/constants.h
  13. +0 −35 src/index.cc
  14. +0 −19 src/index.h
  15. +0 −24 src/libclang.cc
  16. +0 −65 src/location.cc
  17. +0 −20 src/location.h
  18. +0 −170 src/ncursor.cc
  19. +0 −21 src/ncursor.h
  20. +0 −15 src/nstring.h
  21. +0 −94 src/translation.cc
  22. +0 −20 src/translation.h
  23. +0 −114 src/type.cc
  24. +0 −22 src/type.h
View
@@ -11,11 +11,18 @@ var argv = require('optimist')
.alias('p', 'prefix').describe('p', 'Only import functions whose name start with prefix')
.argv
-console.log(jsb.js_beautify(generate({
+var ret = generate({
filename: argv.f,
library: argv.l,
module: argv.m,
prefix: argv.p,
compiler_args: argv._,
single_file: argv.x,
-}).serialized));
+});
+
+console.log(jsb.js_beautify(ret.serialized));
+
+if (generate.unmapped) {
+ process.stderr.write("-------Unmapped-------\r\n");
+ process.stderr.write(generate.unmapped + '\r\n');
+}
View
@@ -1,26 +0,0 @@
-{
- 'targets': [ {
- 'variables': {
- 'llvm-config': 'llvm-config',
- },
- 'target_name': 'bindings',
- 'sources': [
- 'src/libclang.cc',
- 'src/index.cc',
- 'src/translation.cc',
- 'src/ncursor.cc',
- 'src/constants.cc',
- 'src/type.cc',
- 'src/location.cc'
- ],
- 'include_dirs+': [
- '<!@(<(llvm-config) --includedir)'
- ],
- 'libraries': [
- '-lclang',
- ],
- 'ldflags': [
- '-L<!@(<(llvm-config) --libdir)'
- ]
- } ]
-}
View
@@ -0,0 +1,109 @@
+var ffi = require('ffi');
+var ref = require('ref');
+
+var lib = require('./dynamic_clang').libclang;
+var CXCursor = require('./dynamic_clang').CXCursor;
+var Location = require('./location');
+
+var util = require('./util');
+
+var Cursor = function (instance) {
+ if (!(this instanceof Cursor))
+ return new Cursor(instance);
+
+ var self = this;
+
+ this._instance = instance;
+
+ Object.defineProperty(this, 'usr', {
+ get: function () {
+ return util.toString(lib.clang_getCursorUSR(self._instance));
+ }
+ });
+
+ Object.defineProperty(this, 'spelling', {
+ get: function () {
+ return util.toString(lib.clang_getCursorSpelling(self._instance));
+ }
+ });
+
+ Object.defineProperty(this, 'displayname', {
+ get: function () {
+ return util.toString(lib.clang_getCursorDisplayName(self._instance));
+ }
+ });
+
+ Object.defineProperty(this, 'kind', {
+ get: function () {
+ return lib.clang_getCursorKind(self._instance);
+ }
+ });
+
+ Object.defineProperty(this, 'type', {
+ get: function () {
+ return new Type(lib.clang_getCursorType(self._instance));
+ }
+ });
+
+ Object.defineProperty(this, 'enumType', {
+ get: function () {
+ return new Type(lib.clang_getEnumDeclIntegerType(self._instance));
+ }
+ });
+
+ Object.defineProperty(this, 'location', {
+ get: function () {
+ return new Location(lib.clang_getCursorLocation(self._instnace));
+ }
+ });
+
+ Object.defineProperty(this, 'enumValue', {
+ get: function () {
+ return lib.clang_getEnumConstantDeclValue(self._instance);
+ }
+ });
+
+ Object.defineProperty(this, 'enumUValue', {
+ get: function () {
+ return lib.clang_getEnumConstantDeclUnsignedValue(self._instance);
+ }
+ });
+};
+
+function visitor (c, p, data) {
+ var cb = data.cb;
+ var s = new Cursor(c);
+ var parent = new Cursor(p);
+ var ret;
+
+ try {
+ ret = cb.call(s, parent);
+ data.error = false;
+ } catch (e) {
+ data.error = e;
+ ret = 0; // CXChildVisit_Break
+ console.log("we have an error", e);
+ } finally {
+ return ret;
+ }
+};
+
+var visitor_ptr = ffi.Callback(ref.types.uint32, [
+ CXCursor,
+ CXCursor,
+ ref.types.Object,
+], visitor);
+
+Cursor.prototype.visitChildren = function (cb) {
+ var data = {
+ cb: cb,
+ error: undefined,
+ }
+ lib.clang_visitChildren(this._instance, visitor_ptr, data);
+ if (data.error)
+ throw data.error;
+};
+
+module.exports = Cursor;
+
+var Type = require('./type');
View
@@ -660,11 +660,16 @@ var CXTUResourceUsagePtr = exports.CXTUResourceUsagePtr = ref.refType(CXTUResour
var CXCursor = exports.CXCursor = Struct([
['uint32', 'kind'],
['int32', 'xdata'],
- ['pointer', 'data'], ]);
+ ['pointer', 'data1'],
+ ['pointer', 'data2'],
+ ['pointer', 'data3'],
+]);
var CXCursorPtr = exports.CXCursorPtr = ref.refType(CXCursor);
var CXType = exports.CXType = Struct([
['uint32', 'kind'],
- ['pointer', 'data'], ]);
+ ['pointer', 'data1'],
+ ['pointer', 'data2'],
+]);
var CXTypePtr = exports.CXTypePtr = ref.refType(CXType);
var CXCursorVisitorBlock = exports.CXCursorVisitorBlock = Struct([
[CXCursor, 'cursor'],
@@ -896,7 +901,7 @@ exports.libclang = new FFI.Library('libclang', {
clang_getNumOverloadedDecls: ['uint32', [CXCursor]],
clang_getOverloadedDecl: [CXCursor, [CXCursor, 'uint32']],
clang_getIBOutletCollectionType: [CXType, [CXCursor]],
- clang_visitChildren: ['uint32', [CXCursor, 'pointer', 'pointer']],
+ clang_visitChildren: ['uint32', [CXCursor, 'pointer', ref.types.Object]],
clang_visitChildrenWithBlock: ['uint32', [CXCursor, CXCursorVisitorBlock]],
clang_getCursorUSR: [CXString, [CXCursor]],
clang_constructUSR_ObjCClass: [CXString, ['string']],
Oops, something went wrong.

0 comments on commit e938dda

Please sign in to comment.