Browse Source

Merge gcc-lua-cdecl

Peter Colberg 3 years ago
parent
commit
9c03d2911d
39 changed files with 246 additions and 455 deletions
  1. 1
    1
      gcc-lua-cdecl/LICENSE
  2. 2
    2
      gcc-lua-cdecl/Makefile
  3. 7
    1
      gcc-lua-cdecl/doc/CHANGES.html
  4. 13
    0
      gcc-lua-cdecl/doc/CHANGES.mdwn
  5. 3
    3
      gcc-lua-cdecl/doc/INSTALL.html
  6. 2
    2
      gcc-lua-cdecl/doc/INSTALL.mdwn
  7. 1
    1
      gcc-lua-cdecl/doc/README.html
  8. 1
    1
      gcc-lua-cdecl/doc/contents.mdwn
  9. 53
    89
      gcc-lua-cdecl/doc/ffi-cdecl.html
  10. 56
    174
      gcc-lua-cdecl/doc/ffi-cdecl.mdwn
  11. 1
    1
      gcc-lua-cdecl/doc/index.html
  12. 1
    1
      gcc-lua-cdecl/doc/pandoc.css
  13. 1
    1
      gcc-lua-cdecl/doc/reference.html
  14. 0
    3
      gcc-lua-cdecl/ffi-cdecl/C.c
  15. 27
    9
      gcc-lua-cdecl/ffi-cdecl/C.lua.in
  16. 0
    28
      gcc-lua-cdecl/ffi-cdecl/ffi-cdecl-luajit.h
  17. 0
    34
      gcc-lua-cdecl/ffi-cdecl/ffi-cdecl-python.h
  18. 0
    2
      gcc-lua-cdecl/ffi-cdecl/ffi-cdecl.h
  19. 33
    86
      gcc-lua-cdecl/ffi-cdecl/ffi-cdecl.lua
  20. 1
    0
      gcc-lua-cdecl/gcc/cdecl.lua
  21. 1
    1
      gcc-lua-cdecl/test/Makefile
  22. 1
    1
      gcc-lua-cdecl/test/array.c
  23. 1
    0
      gcc-lua-cdecl/test/array.lua
  24. 1
    1
      gcc-lua-cdecl/test/composite.c
  25. 1
    0
      gcc-lua-cdecl/test/composite.lua
  26. 1
    1
      gcc-lua-cdecl/test/enum.c
  27. 1
    0
      gcc-lua-cdecl/test/enum.lua
  28. 1
    1
      gcc-lua-cdecl/test/function.c
  29. 1
    0
      gcc-lua-cdecl/test/function.lua
  30. 1
    1
      gcc-lua-cdecl/test/pointer.c
  31. 1
    0
      gcc-lua-cdecl/test/pointer.lua
  32. 1
    1
      gcc-lua-cdecl/test/scalar.c
  33. 1
    0
      gcc-lua-cdecl/test/scalar.lua
  34. 1
    1
      gcc-lua-cdecl/test/variable.c
  35. 1
    0
      gcc-lua-cdecl/test/variable.lua
  36. 1
    1
      gcc-lua-cdecl/test/vector.c
  37. 1
    0
      gcc-lua-cdecl/test/vector.lua
  38. 1
    2
      mpi/C.c
  39. 25
    5
      mpi/C.lua.in

+ 1
- 1
gcc-lua-cdecl/LICENSE View File

@@ -1,4 +1,4 @@
1
-Copyright © 2013 Peter Colberg.
1
+Copyright © 2013–2015 Peter Colberg.
2 2
 
3 3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4 4
 of this software and associated documentation files (the "Software"), to deal

+ 2
- 2
gcc-lua-cdecl/Makefile View File

@@ -1,6 +1,6 @@
1 1
 #
2 2
 # C declaration composer for GCC Lua plugin.
3
-# Copyright © 2013 Peter Colberg.
3
+# Copyright © 2013–2015 Peter Colberg.
4 4
 # Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
 #
6 6
 
@@ -16,7 +16,7 @@ FILES_LUA = cdecl.lua
16 16
 FILES_DOC = index.mdwn INSTALL.mdwn README.mdwn ffi-cdecl.mdwn reference.mdwn CHANGES.mdwn
17 17
 FILES_DOC_HTML = index.html INSTALL.html README.html ffi-cdecl.html reference.html CHANGES.html pandoc.css gcc-lua-cdecl.png
18 18
 FILES_FFICDECL_LUA = ffi-cdecl.lua
19
-FILES_FFICDECL_INC = ffi-cdecl.h ffi-cdecl-luajit.h ffi-cdecl-python.h
19
+FILES_FFICDECL_INC = ffi-cdecl.h
20 20
 FILES_FFICDECL_DOC = C.c C.lua.in Makefile
21 21
 
22 22
 all: test

+ 7
- 1
gcc-lua-cdecl/doc/CHANGES.html View File

@@ -44,6 +44,12 @@ code > span.er { color: #ff0000; font-weight: bold; }
44 44
 <h1>Releases</h1>
45 45
 <p>This document lists the changes between stable releases.</p>
46 46
 <p>The version numbers follow <a href="http://semver.org/spec/v2.0.0.html">semantic versioning</a>.</p>
47
+<h2 id="v3.0.0">gcc‑lua‑cdecl 3.0.0 (2015-04-23)</h2>
48
+<ul>
49
+<li><p>FFI C: Format C declarations in GCC plugin script.</p>
50
+<p>The <code>ffi-cdecl</code> module no longer formats C declarations itself. The function <code>parse()</code> now returns the captured C declaration and its identifier. The conversion to a C code string with <code>cdecl.declare()</code> is performed in the GCC plugin script instead. This allows overriding the formatting for specific use cases.</p></li>
51
+<li><p>Run test suite with default make target.</p></li>
52
+</ul>
47 53
 <h2 id="v2.0.1">gcc‑lua‑cdecl 2.0.1 (2014-04-02)</h2>
48 54
 <ul>
49 55
 <li>Default to GCC plugin directory for test suite.</li>
@@ -88,7 +94,7 @@ code > span.er { color: #ff0000; font-weight: bold; }
88 94
 </ul>
89 95
 </div>
90 96
 <div class="footer">
91
-&copy; Copyright 2013–2014, Peter Colberg.
97
+&copy; Copyright 2013–2015, Peter Colberg.
92 98
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
93 99
 </div>
94 100
 </body>

+ 13
- 0
gcc-lua-cdecl/doc/CHANGES.mdwn View File

@@ -9,6 +9,19 @@ The version numbers follow [semantic versioning].
9 9
 [semantic versioning]: http://semver.org/spec/v2.0.0.html
10 10
 
11 11
 
12
+## gcc‑lua‑cdecl 3.0.0 (2015-04-23) {#v3.0.0}
13
+
14
+  * FFI C: Format C declarations in GCC plugin script.
15
+
16
+    The `ffi-cdecl` module no longer formats C declarations itself. The
17
+    function `parse()` now returns the captured C declaration and its
18
+    identifier. The conversion to a C code string with `cdecl.declare()`
19
+    is performed in the GCC plugin script instead. This allows overriding
20
+    the formatting for specific use cases.
21
+
22
+  * Run test suite with default make target.
23
+
24
+
12 25
 ## gcc‑lua‑cdecl 2.0.1 (2014-04-02) {#v2.0.1}
13 26
 
14 27
   * Default to GCC plugin directory for test suite.

+ 3
- 3
gcc-lua-cdecl/doc/INSTALL.html View File

@@ -27,9 +27,9 @@
27 27
 <p>cdecl for GCC is available from a <a href="http://git.colberg.org/gcc-lua-cdecl">git repository</a>:</p>
28 28
 <pre><code>git clone http://git.colberg.org/gcc-lua-cdecl</code></pre>
29 29
 <p>In the repository, checkout the latest release version:</p>
30
-<pre><code>git checkout 2.0.1</code></pre>
30
+<pre><code>git checkout 3.0.0</code></pre>
31 31
 <p>If you have GnuPG, verify that the tag is signed by the following key:</p>
32
-<pre><code>git tag -v 2.0.1</code></pre>
32
+<pre><code>git tag -v 3.0.0</code></pre>
33 33
 <pre><code>Primary key fingerprint: 2997 6EC2 2BD1 EBBC CC77  F3A6 183B D5ED 3527 8611
34 34
      Subkey fingerprint: 983B F9D0 F02A 6014 0CEC  529F 767E 12A8 C92A 31CB</code></pre>
35 35
 <p>The module is accompanied by a test suite:</p>
@@ -45,7 +45,7 @@
45 45
 <pre><code>make test GCCLUA=gcclua.so</code></pre>
46 46
 </div>
47 47
 <div class="footer">
48
-&copy; Copyright 2013–2014, Peter Colberg.
48
+&copy; Copyright 2013–2015, Peter Colberg.
49 49
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
50 50
 </div>
51 51
 </body>

+ 2
- 2
gcc-lua-cdecl/doc/INSTALL.mdwn View File

@@ -15,13 +15,13 @@ git clone http://git.colberg.org/gcc-lua-cdecl
15 15
 In the repository, checkout the latest release version:
16 16
 
17 17
 ~~~
18
-git checkout 2.0.1
18
+git checkout 3.0.0
19 19
 ~~~
20 20
 
21 21
 If you have GnuPG, verify that the tag is signed by the following key:
22 22
 
23 23
 ~~~
24
-git tag -v 2.0.1
24
+git tag -v 3.0.0
25 25
 ~~~
26 26
 
27 27
 ~~~

+ 1
- 1
gcc-lua-cdecl/doc/README.html View File

@@ -271,7 +271,7 @@ gcc<span class="ot">.</span>register_callback<span class="ot">(</span>gcc<span c
271 271
 <p><a href="http://packages.debian.org/cdecl">cdecl</a> converts C declarations from a human-readable phrase to C code, and vice versa.</p>
272 272
 </div>
273 273
 <div class="footer">
274
-&copy; Copyright 2013–2014, Peter Colberg.
274
+&copy; Copyright 2013–2015, Peter Colberg.
275 275
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
276 276
 </div>
277 277
 </body>

+ 1
- 1
gcc-lua-cdecl/doc/contents.mdwn View File

@@ -1,7 +1,7 @@
1 1
 ---
2 2
 title: C declaration composer for GCC
3 3
 author: Peter Colberg
4
-date: "2013–2014"
4
+date: "2013–2015"
5 5
 ---
6 6
 
7 7
 - [cdecl for GCC](index.html)

+ 53
- 89
gcc-lua-cdecl/doc/ffi-cdecl.html View File

@@ -49,7 +49,7 @@ code > span.er { color: #ff0000; font-weight: bold; }
49 49
 <li><a href="#c-capture-macros">C capture macros</a></li>
50 50
 </ul>
51 51
 </div>
52
-<p>This example illustrates how to generate foreign function interface (FFI) bindings for a C library using the Lua plugin for GCC and the <code>ffi-cdecl</code> module. The module extracts the C API of the library with the help of a C source file with capture macros and generates matching FFI C bindings.</p>
52
+<p>This example illustrates how to generate foreign function interface (FFI) bindings for a C library using the Lua plugin for GCC and the <code>ffi-cdecl</code> module. The C API of the library is extracted using a C source file that contains capture macros, and matching FFI C bindings are generated using the C declaration composer for GCC.</p>
53 53
 <p>After following the <a href="INSTALL.html">installation</a> instructions, you may run the example:</p>
54 54
 <pre><code>make -C ffi-cdecl</code></pre>
55 55
 <p>If the GCC plugin is not installed in the <a href="http://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html">GCC plugin directory</a>:</p>
@@ -70,46 +70,63 @@ ffi<span class="ot">.</span>cdef<span class="st">[[</span>
70 70
 <h2 id="usage">Usage</h2>
71 71
 <p>To generate FFI C bindings for a library, we compose a Lua script for the GCC C compiler:</p>
72 72
 <pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> gcc <span class="ot">=</span> <span class="fu">require</span><span class="ot">(</span><span class="st">&quot;gcc&quot;</span><span class="ot">)</span>
73
-<span class="kw">local</span> cdecl <span class="ot">=</span> <span class="fu">require</span><span class="ot">(</span><span class="st">&quot;ffi-cdecl&quot;</span><span class="ot">)</span>
73
+<span class="kw">local</span> cdecl <span class="ot">=</span> <span class="fu">require</span><span class="ot">(</span><span class="st">&quot;gcc.cdecl&quot;</span><span class="ot">)</span>
74
+<span class="kw">local</span> fficdecl <span class="ot">=</span> <span class="fu">require</span><span class="ot">(</span><span class="st">&quot;ffi-cdecl&quot;</span><span class="ot">)</span>
74 75
 
75 76
 <span class="co">-- Output generated assembly to /dev/null</span>
76 77
 gcc<span class="ot">.</span>set_asm_file_name<span class="ot">(</span>gcc<span class="ot">.</span>HOST_BIT_BUCKET<span class="ot">)</span></code></pre>
77 78
 <p>First, we define a function that captures C declarations in a table:</p>
78
-<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> decls <span class="ot">=</span> <span class="ot">{}</span>
79
+<pre class="sourceCode lua"><code class="sourceCode lua"><span class="co">-- Captured C declarations.</span>
80
+<span class="kw">local</span> decls <span class="ot">=</span> <span class="ot">{}</span>
81
+<span class="co">-- Type declaration identifiers.</span>
82
+<span class="kw">local</span> types <span class="ot">=</span> <span class="ot">{}</span>
79 83
 
80 84
 <span class="co">-- Parse C declaration from capture macro.</span>
81 85
 gcc<span class="ot">.</span>register_callback<span class="ot">(</span>gcc<span class="ot">.</span>PLUGIN_PRE_GENERICIZE<span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>node<span class="ot">)</span>
82
-  <span class="kw">local</span> decl <span class="ot">=</span> cdecl<span class="ot">.</span>parse<span class="ot">(</span>node<span class="ot">)</span>
83
-  <span class="kw">if</span> decl <span class="kw">then</span> <span class="fu">table.insert</span><span class="ot">(</span>decls<span class="ot">,</span> decl<span class="ot">)</span> <span class="kw">end</span>
86
+  <span class="kw">local</span> decl<span class="ot">,</span> id <span class="ot">=</span> fficdecl<span class="ot">.</span>parse<span class="ot">(</span>node<span class="ot">)</span>
87
+  <span class="kw">if</span> decl <span class="kw">then</span>
88
+    <span class="kw">if</span> decl:class<span class="ot">()</span> <span class="ot">==</span> <span class="st">&quot;type&quot;</span> <span class="kw">or</span> decl:code<span class="ot">()</span> <span class="ot">==</span> <span class="st">&quot;type_decl&quot;</span> <span class="kw">then</span>
89
+      types<span class="ot">[</span>decl<span class="ot">]</span> <span class="ot">=</span> id
90
+    <span class="kw">end</span>
91
+    <span class="fu">table.insert</span><span class="ot">(</span>decls<span class="ot">,</span> <span class="ot">{</span>decl <span class="ot">=</span> decl<span class="ot">,</span> id <span class="ot">=</span> id<span class="ot">})</span>
92
+  <span class="kw">end</span>
84 93
 <span class="kw">end</span><span class="ot">)</span></code></pre>
85 94
 <p>Second, we output FFI C bindings for the captured declarations to a Lua file:</p>
86
-<pre class="sourceCode lua"><code class="sourceCode lua"><span class="co">-- Output captured C declarations to Lua file.</span>
95
+<pre class="sourceCode lua"><code class="sourceCode lua"><span class="co">-- Formats the given declaration as a string of C code.</span>
96
+<span class="kw">local</span> <span class="kw">function</span> <span class="fu">format</span><span class="ot">(</span>decl<span class="ot">,</span> id<span class="ot">)</span>
97
+  <span class="kw">if</span> decl:class<span class="ot">()</span> <span class="ot">==</span> <span class="st">&quot;constant&quot;</span> <span class="kw">then</span>
98
+    <span class="kw">return</span> <span class="st">&quot;static const int &quot;</span> <span class="ot">..</span> id <span class="ot">..</span> <span class="st">&quot; = &quot;</span> <span class="ot">..</span> decl:value<span class="ot">()</span>
99
+  <span class="kw">end</span>
100
+  <span class="kw">return</span> cdecl<span class="ot">.</span>declare<span class="ot">(</span>decl<span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>node<span class="ot">)</span>
101
+    <span class="kw">if</span> node <span class="ot">==</span> decl <span class="kw">then</span> <span class="kw">return</span> id <span class="kw">end</span>
102
+    <span class="kw">return</span> types<span class="ot">[</span>node<span class="ot">]</span>
103
+  <span class="kw">end</span><span class="ot">)</span>
104
+<span class="kw">end</span>
105
+
106
+<span class="co">-- Output captured C declarations to Lua file.</span>
87 107
 gcc<span class="ot">.</span>register_callback<span class="ot">(</span>gcc<span class="ot">.</span>PLUGIN_FINISH_UNIT<span class="ot">,</span> <span class="kw">function</span><span class="ot">()</span>
88 108
   <span class="kw">local</span> result <span class="ot">=</span> <span class="ot">{}</span>
89
-  <span class="fu">table.sort</span><span class="ot">(</span>decls<span class="ot">)</span>
90 109
   <span class="kw">for</span> i<span class="ot">,</span> decl <span class="kw">in</span> <span class="fu">ipairs</span><span class="ot">(</span>decls<span class="ot">)</span> <span class="kw">do</span>
91
-    result<span class="ot">[</span>i<span class="ot">]</span> <span class="ot">=</span> <span class="fu">tostring</span><span class="ot">(</span>decl<span class="ot">)</span> <span class="ot">..</span> <span class="st">&quot;;</span><span class="ot">\n</span><span class="st">&quot;</span>
110
+    result<span class="ot">[</span>i<span class="ot">]</span> <span class="ot">=</span> <span class="fu">format</span><span class="ot">(</span>decl<span class="ot">.</span>decl<span class="ot">,</span> decl<span class="ot">.</span>id<span class="ot">)</span> <span class="ot">..</span> <span class="st">&quot;;</span><span class="ot">\n</span><span class="st">&quot;</span>
92 111
   <span class="kw">end</span>
93 112
   <span class="kw">local</span> f <span class="ot">=</span> <span class="fu">assert</span><span class="ot">(</span><span class="fu">io.open</span><span class="ot">(</span>arg<span class="ot">.</span>output<span class="ot">,</span> <span class="st">&quot;w&quot;</span><span class="ot">))</span>
94
-  f:<span class="fu">write</span><span class="st">[=[</span>
113
+  f:<span class="fu">write</span><span class="ot">(</span><span class="st">[=[</span>
95 114
 <span class="st">local ffi = require(&quot;ffi&quot;)</span>
96 115
 
97 116
 <span class="st">ffi.cdef[[</span>
98
-<span class="st">]=]</span>
99
-  f:<span class="fu">write</span><span class="ot">(</span><span class="fu">table.concat</span><span class="ot">(</span>result<span class="ot">))</span>
100
-  f:<span class="fu">write</span><span class="st">[=[</span>
117
+<span class="st">]=]</span><span class="ot">,</span> <span class="fu">table.concat</span><span class="ot">(</span>result<span class="ot">),</span> <span class="st">[=[</span>
101 118
 <span class="st">]]</span>
102 119
 
103 120
 <span class="st">-- Load POSIX real time extensions into global namespace.</span>
121
+<span class="st">local C = ffi.C</span>
104 122
 <span class="st">if not pcall(function() return ffi.C.clock_gettime end) then</span>
105
-<span class="st">  ffi.load(&quot;rt&quot;, true)</span>
123
+<span class="st">  C = ffi.load(&quot;rt&quot;, true)</span>
106 124
 <span class="st">end</span>
107 125
 
108
-<span class="st">return ffi.C</span>
109
-<span class="st">]=]</span>
126
+<span class="st">return C</span>
127
+<span class="st">]=]</span><span class="ot">)</span>
110 128
   f:<span class="fu">close</span><span class="ot">()</span>
111 129
 <span class="kw">end</span><span class="ot">)</span></code></pre>
112
-<p>Note we reorder the captured C declarations using <code>table.sort</code> before writing them to the Lua file, which orders type declarations in the same order as in the header file. This way C capture macros may be specified in arbitrary order, which facilitates generation of capture source files, e.g., using <a href="http://ctags.sourceforge.net/">ctags</a>.</p>
113 130
 <p>The script is executed by loading the Lua plugin for GCC as follows:</p>
114 131
 <pre><code>gcc -S C.c -fplugin=gcclua -fplugin-arg-gcclua-script=C.lua.in -fplugin-arg-gcclua-output=C.lua</code></pre>
115 132
 <p>If the GCC plugin is not installed in the <a href="http://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html">GCC plugin directory</a>:</p>
@@ -124,7 +141,6 @@ gcc<span class="ot">.</span>register_callback<span class="ot">(</span>gcc<span c
124 141
 <span class="ot">#include &lt;unistd.h&gt;</span>
125 142
 
126 143
 <span class="ot">#include &quot;ffi-cdecl.h&quot;</span>
127
-<span class="ot">#include &quot;ffi-cdecl-luajit.h&quot;</span>
128 144
 
129 145
 <span class="co">/* capture a function */</span>
130 146
 cdecl_func(getopt)
@@ -142,89 +158,37 @@ cdecl_func(mygetopt)</code></pre>
142 158
 <h2 id="c-capture-macros">C capture macros</h2>
143 159
 <p>The C header <code>ffi-cdecl.h</code> defines the following capture macros:</p>
144 160
 <dl>
145
-<dt>cdecl_typename(id)</dt>
146
-<dd><p>Declares the type with the given identifier. The macro does not output a C declaration; it is intended to be used with types that have already been defined, for example in another module of a program.</p>
147
-<p>For the <a href="http://luajit.org/ext_ffi_semantics.html#clang">FFI library of LuaJIT</a>, built-in types may be declared as follows:</p>
148
-<pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#include &quot;ffi-cdecl-luajit.h&quot;</span></code></pre>
149
-<p>For the <a href="http://cffi.readthedocs.org/en/latest/#declaring-types-and-functions">CFFI module for Python</a>, built-in types may be declared as follows:</p>
150
-<pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#include &quot;ffi-cdecl-python.h&quot;</span></code></pre>
151
-</dd>
152
-<dt>cdecl_type(id)</dt>
153
-<dd><p>Defines the type with the given identifier.</p>
154
-<p>The macro outputs a C declaration of the following form:</p>
155
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">typedef</span> &lt;type&gt; &lt;id&gt;;</code></pre>
156
-<p>If <code>id</code> is a struct, union or enum type, the members are only defined if the struct, union or enum is unnamed. For a named struct, union or enum, the members may be defined using the macro <code>cdecl_memb</code>. This allows for forward declarations, or declarations of incomplete types.</p>
157
-</dd>
158
-<dt>cdecl_typealias(id, alias)</dt>
159
-<dd><p>Defines the type with the given identifier. If the type definition refers to a type with the given alias, the alias name is substituted for the canonical name of the referred type.</p>
160
-<p>This macro may be used to substitute <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html#tag_13_47_03_01">standard integer types</a>:</p>
161
-<pre class="sourceCode c"><code class="sourceCode c">cdecl_typealias(ssize_t, intptr_t)</code></pre>
162
-<p>This outputs a C declaration that defines <code>ssize_t</code> using <code>intptr_t</code> rather than, e.g., <code>long int</code>.</p>
161
+<dt><code>cdecl_type(id)</code></dt>
162
+<dd><p>Captures the type declaration with the given identifier.</p>
163 163
 </dd>
164
-<dt>cdecl_memb(id)</dt>
165
-<dd><p>Defines the members of a struct, union or enum type with the given identifier. The type of a member of a struct or union is recursively expanded; unless the type has already been defined, or the type is a named struct, union or enum.</p>
166
-<p>The macro outputs a C declaration of any of the following forms:</p>
167
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">struct</span> &lt;id&gt; {
168
-  &lt;type&gt; &lt;member&gt;;
169
-  …
170
-};</code></pre>
171
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">union</span> &lt;id&gt; {
172
-  &lt;type&gt; &lt;member&gt;;
173
-  …
174
-};</code></pre>
175
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">enum</span> &lt;id&gt; {
176
-  &lt;member&gt; = &lt;value&gt;,
177
-  …
178
-};</code></pre>
164
+<dt><code>cdecl_memb(id)</code></dt>
165
+<dd><p>Captures the type definition of a struct, union or enum type with the given identifier.</p>
179 166
 </dd>
180
-<dt>cdecl_struct(tag)</dt>
181
-<dd><p>Defines the members of a struct with the given tag.</p>
182
-<p>The macro outputs a C declaration of of the following form:</p>
183
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">struct</span> &lt;tag&gt; {
184
-  &lt;type&gt; &lt;member&gt;;
185
-  …
186
-};</code></pre>
167
+<dt><code>cdecl_struct(tag)</code></dt>
168
+<dd><p>Captures the type definition of a struct type with the given tag.</p>
187 169
 </dd>
188
-<dt>cdecl_union(tag)</dt>
189
-<dd><p>Defines the members of a union with the given tag.</p>
190
-<p>The macro outputs a C declaration of of the following form:</p>
191
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">union</span> &lt;tag&gt; {
192
-  &lt;type&gt; &lt;member&gt;;
193
-  …
194
-};</code></pre>
170
+<dt><code>cdecl_union(tag)</code></dt>
171
+<dd><p>Captures the type definition of a union type with the given tag.</p>
195 172
 </dd>
196
-<dt>cdecl_enum(tag)</dt>
197
-<dd><p>Defines the members of an enumeration with the given tag.</p>
198
-<p>The macro outputs a C declaration of of the following form:</p>
199
-<pre class="sourceCode c"><code class="sourceCode c"><span class="kw">enum</span> &lt;tag&gt; {
200
-  &lt;member&gt; = &lt;value&gt;,
201
-  …
202
-};</code></pre>
173
+<dt><code>cdecl_enum(tag)</code></dt>
174
+<dd><p>Captures the type definition of an enumeration type with the given tag.</p>
203 175
 </dd>
204
-<dt>cdecl_func(id)</dt>
205
-<dd><p>Declares the function with the given identifier.</p>
206
-<p>The macro outputs a C declaration of the following form:</p>
207
-<pre class="sourceCode c"><code class="sourceCode c">&lt;rtype&gt; &lt;id&gt;(&lt;argtype&gt;, &lt;argtype&gt;, …);</code></pre>
208
-<p>The output contains an assembler label if the symbol name differs from <code>id</code>:</p>
209
-<pre class="sourceCode c"><code class="sourceCode c">&lt;rtype&gt; &lt;id&gt;(&lt;argtype&gt;, &lt;argtype&gt;, …) __asm__(<span class="st">&quot;&lt;label&gt;&quot;</span>);</code></pre>
176
+<dt><code>cdecl_func(id)</code></dt>
177
+<dd><p>Captures the function declaration with the given identifier.</p>
178
+<p>Any subexpression containing an address-of, cast, or comma operator is replaced by its right-most operand.</p>
210 179
 </dd>
211
-<dt>cdecl_var(id)</dt>
212
-<dd><p>Declares the global variable with the given identifier.</p>
213
-<p>The macro outputs a C declaration of the following form:</p>
214
-<pre class="sourceCode c"><code class="sourceCode c">&lt;type&gt; &lt;id&gt;;</code></pre>
215
-<p>The output contains an assembler label if the symbol name differs from <code>id</code>:</p>
216
-<pre class="sourceCode c"><code class="sourceCode c">&lt;type&gt; &lt;id&gt; __asm__(<span class="st">&quot;&lt;label&gt;&quot;</span>);</code></pre>
180
+<dt><code>cdecl_var(id)</code></dt>
181
+<dd><p>Captures the variable declaration with the given identifier.</p>
182
+<p>Any subexpression containing an address-of, cast, or comma operator is replaced by its right-most operand.</p>
217 183
 </dd>
218
-<dt>cdecl_const(id)</dt>
219
-<dd><p>Declares the function or global variable, or defines the integer constant, with the given identifier. Any expression containing an address-of, cast, or comma operator as part of the expression referred to by <code>id</code> is replaced by the right-most operand.</p>
220
-<p>If <code>id</code> refers to an integer constant, the macro outputs a C declaration of the following form:</p>
221
-<pre class="sourceCode c"><code class="sourceCode c"><span class="dt">static</span> <span class="dt">const</span> <span class="dt">int</span> &lt;id&gt; = &lt;value&gt;;</code></pre>
222
-<p>Otherwise, the macro outputs a C declaration of one of the above forms.</p>
184
+<dt><code>cdecl_const(id)</code></dt>
185
+<dd><p>Captures the integer constant with the given identifier.</p>
186
+<p>Any subexpression containing an address-of, cast, or comma operator is replaced by its right-most operand.</p>
223 187
 </dd>
224 188
 </dl>
225 189
 </div>
226 190
 <div class="footer">
227
-&copy; Copyright 2013–2014, Peter Colberg.
191
+&copy; Copyright 2013–2015, Peter Colberg.
228 192
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
229 193
 </div>
230 194
 </body>

+ 56
- 174
gcc-lua-cdecl/doc/ffi-cdecl.mdwn View File

@@ -4,8 +4,9 @@ title: FFI C example
4 4
 
5 5
 This example illustrates how to generate foreign function interface (FFI)
6 6
 bindings for a C library using the Lua plugin for GCC and the `ffi-cdecl`
7
-module. The module extracts the C API of the library with the help of a C
8
-source file with capture macros and generates matching FFI C bindings.
7
+module. The C API of the library is extracted using a C source file that
8
+contains capture macros, and matching FFI C bindings are generated using
9
+the C declaration composer for GCC.
9 10
 
10 11
 After following the [installation] instructions, you may run the example:
11 12
 
@@ -47,7 +48,8 @@ GCC C compiler:
47 48
 
48 49
 ~~~ {.lua}
49 50
 local gcc = require("gcc")
50
-local cdecl = require("ffi-cdecl")
51
+local cdecl = require("gcc.cdecl")
52
+local fficdecl = require("ffi-cdecl")
51 53
 
52 54
 -- Output generated assembly to /dev/null
53 55
 gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)
@@ -56,53 +58,63 @@ gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)
56 58
 First, we define a function that captures C declarations in a table:
57 59
 
58 60
 ~~~ {.lua}
61
+-- Captured C declarations.
59 62
 local decls = {}
63
+-- Type declaration identifiers.
64
+local types = {}
60 65
 
61 66
 -- Parse C declaration from capture macro.
62 67
 gcc.register_callback(gcc.PLUGIN_PRE_GENERICIZE, function(node)
63
-  local decl = cdecl.parse(node)
64
-  if decl then table.insert(decls, decl) end
68
+  local decl, id = fficdecl.parse(node)
69
+  if decl then
70
+    if decl:class() == "type" or decl:code() == "type_decl" then
71
+      types[decl] = id
72
+    end
73
+    table.insert(decls, {decl = decl, id = id})
74
+  end
65 75
 end)
66 76
 ~~~
67 77
 
68 78
 Second, we output FFI C bindings for the captured declarations to a Lua file:
69 79
 
70 80
 ~~~ {.lua}
81
+-- Formats the given declaration as a string of C code.
82
+local function format(decl, id)
83
+  if decl:class() == "constant" then
84
+    return "static const int " .. id .. " = " .. decl:value()
85
+  end
86
+  return cdecl.declare(decl, function(node)
87
+    if node == decl then return id end
88
+    return types[node]
89
+  end)
90
+end
91
+
71 92
 -- Output captured C declarations to Lua file.
72 93
 gcc.register_callback(gcc.PLUGIN_FINISH_UNIT, function()
73 94
   local result = {}
74
-  table.sort(decls)
75 95
   for i, decl in ipairs(decls) do
76
-    result[i] = tostring(decl) .. ";\n"
96
+    result[i] = format(decl.decl, decl.id) .. ";\n"
77 97
   end
78 98
   local f = assert(io.open(arg.output, "w"))
79
-  f:write[=[
99
+  f:write([=[
80 100
 local ffi = require("ffi")
81 101
 
82 102
 ffi.cdef[[
83
-]=]
84
-  f:write(table.concat(result))
85
-  f:write[=[
103
+]=], table.concat(result), [=[
86 104
 ]]
87 105
 
88 106
 -- Load POSIX real time extensions into global namespace.
107
+local C = ffi.C
89 108
 if not pcall(function() return ffi.C.clock_gettime end) then
90
-  ffi.load("rt", true)
109
+  C = ffi.load("rt", true)
91 110
 end
92 111
 
93
-return ffi.C
94
-]=]
112
+return C
113
+]=])
95 114
   f:close()
96 115
 end)
97 116
 ~~~
98 117
 
99
-Note we reorder the captured C declarations using `table.sort` before writing
100
-them to the Lua file, which orders type declarations in the same order as in
101
-the header file. This way C capture macros may be specified in arbitrary order,
102
-which facilitates generation of capture source files, e.g., using [ctags].
103
-
104
-[ctags]: http://ctags.sourceforge.net/
105
-
106 118
 The script is executed by loading the Lua plugin for GCC as follows:
107 119
 
108 120
 ~~~
@@ -135,7 +147,6 @@ Consider the following capture file `C.c` for the POSIX C API:
135 147
 #include <unistd.h>
136 148
 
137 149
 #include "ffi-cdecl.h"
138
-#include "ffi-cdecl-luajit.h"
139 150
 
140 151
 /* capture a function */
141 152
 cdecl_func(getopt)
@@ -165,176 +176,47 @@ C capture macros
165 176
 
166 177
 The C header `ffi-cdecl.h` defines the following capture macros:
167 178
 
168
-cdecl_typename(id)
169
-
170
-  : Declares the type with the given identifier. The macro does not output
171
-    a C declaration; it is intended to be used with types that have already
172
-    been defined, for example in another module of a program.
173
-
174
-    For the [FFI library of LuaJIT], built-in types may be declared as follows:
175
-
176
-    ~~~ {.c}
177
-    #include "ffi-cdecl-luajit.h"
178
-    ~~~
179
-
180
-    [FFI library of LuaJIT]: http://luajit.org/ext_ffi_semantics.html#clang
181
-
182
-    For the [CFFI module for Python], built-in types may be declared as follows:
183
-
184
-    ~~~ {.c}
185
-    #include "ffi-cdecl-python.h"
186
-    ~~~
187
-
188
-    [CFFI module for Python]: http://cffi.readthedocs.org/en/latest/#declaring-types-and-functions
189
-
190
-cdecl_type(id)
191
-
192
-  : Defines the type with the given identifier.
193
-
194
-    The macro outputs a C declaration of the following form:
195
-
196
-    ~~~ {.c}
197
-    typedef <type> <id>;
198
-    ~~~
199
-
200
-    If `id` is a struct, union or enum type, the members are only defined
201
-    if the struct, union or enum is unnamed. For a named struct, union or
202
-    enum, the members may be defined using the macro `cdecl_memb`. This
203
-    allows for forward declarations, or declarations of incomplete types.
204
-
205
-cdecl_typealias(id, alias)
206
-
207
-  : Defines the type with the given identifier. If the type definition refers
208
-    to a type with the given alias, the alias name is substituted for the
209
-    canonical name of the referred type.
210
-
211
-    This macro may be used to substitute [standard integer types]:
212
-
213
-    ~~~ {.c}
214
-    cdecl_typealias(ssize_t, intptr_t)
215
-    ~~~
216
-
217
-    This outputs a C declaration that defines `ssize_t` using `intptr_t` rather
218
-    than, e.g., `long int`.
219
-
220
-    [standard integer types]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html#tag_13_47_03_01
221
-
222
-cdecl_memb(id)
223
-
224
-  : Defines the members of a struct, union or enum type with the given
225
-    identifier. The type of a member of a struct or union is recursively
226
-    expanded; unless the type has already been defined, or the type is a
227
-    named struct, union or enum.
228
-
229
-    The macro outputs a C declaration of any of the following forms:
230
-
231
-    ~~~ {.c}
232
-    struct <id> {
233
-      <type> <member>;
234
-      …
235
-    };
236
-    ~~~
237
-
238
-    ~~~ {.c}
239
-    union <id> {
240
-      <type> <member>;
241
-      …
242
-    };
243
-    ~~~
244
-
245
-    ~~~ {.c}
246
-    enum <id> {
247
-      <member> = <value>,
248
-      …
249
-    };
250
-    ~~~
251
-
252
-cdecl_struct(tag)
253
-
254
-  : Defines the members of a struct with the given tag.
255
-
256
-    The macro outputs a C declaration of of the following form:
257
-
258
-    ~~~ {.c}
259
-    struct <tag> {
260
-      <type> <member>;
261
-      …
262
-    };
263
-    ~~~
264
-
265
-cdecl_union(tag)
266
-
267
-  : Defines the members of a union with the given tag.
268
-
269
-    The macro outputs a C declaration of of the following form:
270
-
271
-    ~~~ {.c}
272
-    union <tag> {
273
-      <type> <member>;
274
-      …
275
-    };
276
-    ~~~
277
-
278
-cdecl_enum(tag)
279
-
280
-  : Defines the members of an enumeration with the given tag.
281
-
282
-    The macro outputs a C declaration of of the following form:
179
+`cdecl_type(id)`
283 180
 
284
-    ~~~ {.c}
285
-    enum <tag> {
286
-      <member> = <value>,
287
-      …
288
-    };
289
-    ~~~
181
+  : Captures the type declaration with the given identifier.
290 182
 
291
-cdecl_func(id)
183
+`cdecl_memb(id)`
292 184
 
293
-  : Declares the function with the given identifier.
185
+  : Captures the type definition of a struct, union or enum type with
186
+    the given identifier.
294 187
 
295
-    The macro outputs a C declaration of the following form:
188
+`cdecl_struct(tag)`
296 189
 
297
-    ~~~ {.c}
298
-    <rtype> <id>(<argtype>, <argtype>, …);
299
-    ~~~
190
+  : Captures the type definition of a struct type with the given tag.
300 191
 
301
-    The output contains an assembler label if the symbol name differs from `id`:
192
+`cdecl_union(tag)`
302 193
 
303
-    ~~~ {.c}
304
-    <rtype> <id>(<argtype>, <argtype>, …) __asm__("<label>");
305
-    ~~~
194
+  : Captures the type definition of a union type with the given tag.
306 195
 
307
-cdecl_var(id)
196
+`cdecl_enum(tag)`
308 197
 
309
-  : Declares the global variable with the given identifier.
198
+  : Captures the type definition of an enumeration type with the given tag.
310 199
 
311
-    The macro outputs a C declaration of the following form:
200
+`cdecl_func(id)`
312 201
 
313
-    ~~~ {.c}
314
-    <type> <id>;
315
-    ~~~
202
+  : Captures the function declaration with the given identifier.
316 203
 
317
-    The output contains an assembler label if the symbol name differs from `id`:
204
+    Any subexpression containing an address-of, cast, or comma operator
205
+    is replaced by its right-most operand.
318 206
 
319
-    ~~~ {.c}
320
-    <type> <id> __asm__("<label>");
321
-    ~~~
207
+`cdecl_var(id)`
322 208
 
323
-cdecl_const(id)
209
+  : Captures the variable declaration with the given identifier.
324 210
 
325
-  : Declares the function or global variable, or defines the integer constant,
326
-    with the given identifier. Any expression containing an address-of, cast,
327
-    or comma operator as part of the expression referred to by `id` is replaced
328
-    by the right-most operand.
211
+    Any subexpression containing an address-of, cast, or comma operator
212
+    is replaced by its right-most operand.
329 213
 
330
-    If `id` refers to an integer constant, the macro outputs a C declaration of
331
-    the following form:
214
+`cdecl_const(id)`
332 215
 
333
-    ~~~ {.c}
334
-    static const int <id> = <value>;
335
-    ~~~
216
+  : Captures the integer constant with the given identifier.
336 217
 
337
-    Otherwise, the macro outputs a C declaration of one of the above forms.
218
+    Any subexpression containing an address-of, cast, or comma operator
219
+    is replaced by its right-most operand.
338 220
 
339 221
 
340 222
 [GCC plugin directory]: http://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html

+ 1
- 1
gcc-lua-cdecl/doc/index.html View File

@@ -26,7 +26,7 @@
26 26
 <p><a href="http://colberg.org/gcc-lua-cdecl/"><img src="gcc-lua-cdecl.png" title="cdecl for GCC" /></a> <a href="http://colberg.org/gcc-lua-cdecl/">cdecl for GCC</a> is a Lua module that composes C declarations from a C source file using the <a href="http://colberg.org/gcc-lua/">Lua plugin for GCC</a>. The module generates C99 function, variable and type declarations, and supports the GCC extensions for <a href="http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html">attributes</a> and <a href="http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html">vector types</a>. The module may be used to generate C library bindings for a foreign function interface, e.g., <a href="http://luajit.org/ext_ffi.html">LuaJIT FFI</a>.</p>
27 27
 </div>
28 28
 <div class="footer">
29
-&copy; Copyright 2013–2014, Peter Colberg.
29
+&copy; Copyright 2013–2015, Peter Colberg.
30 30
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
31 31
 </div>
32 32
 </body>

+ 1
- 1
gcc-lua-cdecl/doc/pandoc.css View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Stylesheet for Pandoc.
3
- * Copyright © 2013–2014 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 1
gcc-lua-cdecl/doc/reference.html View File

@@ -71,7 +71,7 @@
71 71
 </dl>
72 72
 </div>
73 73
 <div class="footer">
74
-&copy; Copyright 2013–2014, Peter Colberg.
74
+&copy; Copyright 2013–2015, Peter Colberg.
75 75
 Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
76 76
 </div>
77 77
 </body>

+ 0
- 3
gcc-lua-cdecl/ffi-cdecl/C.c View File

@@ -11,13 +11,10 @@
11 11
 #include <unistd.h>
12 12
 
13 13
 #include "ffi-cdecl.h"
14
-#include "ffi-cdecl-luajit.h"
15 14
 
16 15
 cdecl_type(clockid_t)
17 16
 cdecl_type(time_t)
18 17
 
19
-cdecl_typealias(ssize_t, intptr_t)
20
-
21 18
 cdecl_struct(timespec)
22 19
 
23 20
 cdecl_var(optarg)

+ 27
- 9
gcc-lua-cdecl/ffi-cdecl/C.lua.in View File

@@ -1,25 +1,42 @@
1 1
 local gcc = require("gcc")
2
-local cdecl = require("ffi-cdecl")
2
+local cdecl = require("gcc.cdecl")
3
+local fficdecl = require("ffi-cdecl")
3 4
 
4 5
 -- Output generated assembly to /dev/null
5 6
 gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)
6 7
 
8
+-- Captured C declarations.
7 9
 local decls = {}
10
+-- Type declaration identifiers.
11
+local types = {}
8 12
 
9 13
 -- Parse C declaration from capture macro.
10 14
 gcc.register_callback(gcc.PLUGIN_PRE_GENERICIZE, function(node)
11
-  local decl = cdecl.parse(node, function(id)
12
-    return "mylib_" .. id
13
-  end)
14
-  if decl then table.insert(decls, decl) end
15
+  local decl, id = fficdecl.parse(node)
16
+  if decl then
17
+    if decl:class() == "type" or decl:code() == "type_decl" then
18
+      types[decl] = id
19
+    end
20
+    table.insert(decls, {decl = decl, id = id})
21
+  end
15 22
 end)
16 23
 
24
+-- Formats the given declaration as a string of C code.
25
+local function format(decl, id)
26
+  if decl:class() == "constant" then
27
+    return "static const int " .. id .. " = " .. decl:value()
28
+  end
29
+  return cdecl.declare(decl, function(node)
30
+    if node == decl then return id end
31
+    return types[node]
32
+  end)
33
+end
34
+
17 35
 -- Output captured C declarations to Lua file.
18 36
 gcc.register_callback(gcc.PLUGIN_FINISH_UNIT, function()
19 37
   local result = {}
20
-  table.sort(decls)
21 38
   for i, decl in ipairs(decls) do
22
-    result[i] = tostring(decl) .. ";\n"
39
+    result[i] = format(decl.decl, decl.id) .. ";\n"
23 40
   end
24 41
   local f = assert(io.open(arg.output, "w"))
25 42
   f:write([=[
@@ -30,11 +47,12 @@ ffi.cdef[[
30 47
 ]]
31 48
 
32 49
 -- Load POSIX real time extensions into global namespace.
50
+local C = ffi.C
33 51
 if not pcall(function() return ffi.C.clock_gettime end) then
34
-  ffi.load("rt", true)
52
+  C = ffi.load("rt", true)
35 53
 end
36 54
 
37
-return ffi.C
55
+return C
38 56
 ]=])
39 57
   f:close()
40 58
 end)

+ 0
- 28
gcc-lua-cdecl/ffi-cdecl/ffi-cdecl-luajit.h View File

@@ -1,28 +0,0 @@
1
-#ifndef FFI_CDECL_LUAJIT_H
2
-#define FFI_CDECL_LUAJIT_H
3
-
4
-/* http://luajit.org/ext_ffi_semantics.html#clang */
5
-
6
-#include <stddef.h>
7
-cdecl_typename(ptrdiff_t)
8
-cdecl_typename(size_t)
9
-cdecl_typename(wchar_t)
10
-
11
-#include <stdint.h>
12
-cdecl_typename(int8_t)
13
-cdecl_typename(int16_t)
14
-cdecl_typename(int32_t)
15
-cdecl_typename(int64_t)
16
-cdecl_typename(uint8_t)
17
-cdecl_typename(uint16_t)
18
-cdecl_typename(uint32_t)
19
-cdecl_typename(uint64_t)
20
-cdecl_typename(intptr_t)
21
-cdecl_typename(uintptr_t)
22
-
23
-#if __STDC_VERSION__ >= 199901L
24
-#include <stdbool.h>
25
-cdecl_typename(bool)
26
-#endif
27
-
28
-#endif

+ 0
- 34
gcc-lua-cdecl/ffi-cdecl/ffi-cdecl-python.h View File

@@ -1,34 +0,0 @@
1
-#ifndef FFI_CDECL_PYTHON_H
2
-#define FFI_CDECL_PYTHON_H
3
-
4
-/* http://cffi.readthedocs.org/en/latest/#declaring-types-and-functions */
5
-
6
-#include <stddef.h>
7
-cdecl_typename(ptrdiff_t)
8
-cdecl_typename(size_t)
9
-cdecl_typename(wchar_t)
10
-
11
-#include <stdint.h>
12
-cdecl_typename(int8_t)
13
-cdecl_typename(int16_t)
14
-cdecl_typename(int32_t)
15
-cdecl_typename(int64_t)
16
-cdecl_typename(uint8_t)
17
-cdecl_typename(uint16_t)
18
-cdecl_typename(uint32_t)
19
-cdecl_typename(uint64_t)
20
-cdecl_typename(intptr_t)
21
-cdecl_typename(uintptr_t)
22
-
23
-#if __STDC_VERSION__ >= 199901L
24
-#include <stdbool.h>
25
-cdecl_typename(bool)
26
-#endif
27
-
28
-#include <sys/types.h>
29
-cdecl_typename(ssize_t)
30
-
31
-#include <stdio.h>
32
-cdecl_typename(FILE)
33
-
34
-#endif

+ 0
- 2
gcc-lua-cdecl/ffi-cdecl/ffi-cdecl.h View File

@@ -1,9 +1,7 @@
1 1
 #ifndef FFI_CDECL_H
2 2
 #define FFI_CDECL_H
3 3
 
4
-#define cdecl_typename(id)              void cdecl_typename__ ## id(id *unused) {}
5 4
 #define cdecl_type(id)                  void cdecl_type__ ## id(id *unused) {}
6
-#define cdecl_typealias(id, alias)      void cdecl_typealias__ ## id(id *unused1, alias *unused2) {}
7 5
 #define cdecl_memb(id)                  void cdecl_memb__ ## id(id *unused) {}
8 6
 #define cdecl_struct(tag)               void cdecl_struct__ ## tag(struct tag *unused) {}
9 7
 #define cdecl_union(tag)                void cdecl_union__ ## tag(union tag *unused) {}

+ 33
- 86
gcc-lua-cdecl/ffi-cdecl/ffi-cdecl.lua View File

@@ -1,15 +1,13 @@
1 1
 ------------------------------------------------------------------------------
2 2
 -- Generate C declarations for use with a foreign function interface (FFI).
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 ------------------------------------------------------------------------------
5 6
 
6
-local cdecl = require("gcc.cdecl")
7
-
8 7
 local _M = {}
9 8
 
10 9
 -- Cache library functions
11
-local select, setmetatable = select, setmetatable
10
+local select = select
12 11
 
13 12
 -- The C capture macros are implemented using C function definitions.
14 13
 -- Some macros parse the function body to evaluate an expression, which
@@ -19,103 +17,44 @@ local select, setmetatable = select, setmetatable
19 17
 -- PLUGIN_FINISH_UNIT event, but not at the PLUGIN_PRE_GENERICIZE event.
20 18
 --
21 19
 -- Each macro function parses a C declaration from a function definition
22
-local macro = {}
23
-
24
-local rank = setmetatable({}, {__mode = "k"})
20
+-- at the PLUGIN_PRE_GENERICIZE event for that definition and returns the
21
+-- captured C declaration and its identifier. The C declaration may be
22
+-- formatted using gcc.cdecl.declare() at the PLUGIN_FINISH_UNIT event.
23
+local macro = {
24
+  type = function(decl)
25
+    return decl:args():type():type():name()
26
+  end,
27
+
28
+  memb = function(decl, id)
29
+    return decl:args():type():type():main_variant()
30
+  end,
31
+
32
+  expr = function(decl, id)
33
+    local result = decl:body():body():args()
34
+    while true do
35
+      if result:class() == "declaration" then
36
+        break
37
+      elseif result:class() == "constant" then
38
+        break
39
+      end
40
+      result = select(-1, result:operand())
41
+    end
42
+    return result
43
+  end,
44
+}
25 45
 
26
-local function comp(a, b)
27
-  return rank[a] < rank[b]
28
-end
46
+macro.struct = macro.memb
47
+macro.union = macro.memb
48
+macro.enum = macro.memb
29 49
 
30 50
 -- Parse C declaration from capture macro.
31
-function _M.parse(node, f)
51
+function _M.parse(node)
32 52
   local name = node:name()
33 53
   if not name then return end
34 54
   local op, id = name:value():match("^cdecl_(.-)__(.+)")
35 55
   if not op then return end
36
-  if f and op ~= "typename" then id = f(id) or id end
37
-  local output, uid = macro[op](node, id)
38
-  if not output then return end
39
-  local decl = setmetatable({}, {__tostring = output, __lt = comp})
40
-  rank[decl] = uid or node:uid()
41
-  return decl
42
-end
43
-
44
-local typename = {}
45
-
46
-function macro.typename(decl, id)
47
-  local result = decl:args():type():type():name()
48
-  typename[result] = id
49
-end
50
-
51
-function macro.type(decl, id)
52
-  local result = decl:args():type():type():name()
53
-  typename[result] = id
54
-  local output = function()
55
-    return cdecl.declare(result, function(node)
56
-      return typename[node]
57
-    end)
58
-  end
59
-  return output, result:uid()
60
-end
61
-
62
-function macro.typealias(decl, id)
63
-  local result = decl:args():type():type():name()
64
-  typename[result] = id
65
-  local alias = decl:args():chain():type():type():name()
66
-  local output = function()
67
-    return cdecl.declare(result, function(node)
68
-      if node == alias:type():canonical():name() then
69
-        return typename[alias] or alias:name():value()
70
-      end
71
-      return typename[node]
72
-    end)
73
-  end
74
-  return output, result:uid()
75
-end
76
-
77
-function macro.memb(decl, id)
78
-  local result = decl:args():type():type():main_variant()
79
-  typename[result] = id
80
-  local output = function()
81
-    return cdecl.declare(result, function(node)
82
-      return typename[node]
83
-    end)
84
-  end
85
-  if result:code() == "enumeral_type" or not result:fields() then
86
-    return output, result:stub_decl():uid()
87
-  end
88
-  return output, result:fields():uid()
89
-end
90
-
91
-macro.struct = macro.memb
92
-macro.union  = macro.memb
93
-macro.enum   = macro.memb
94
-
95
-function macro.expr(decl, id)
96
-  local result = decl:body():body():args()
97
-  while true do
98
-    if result:class() == "declaration" then
99
-      return function()
100
-        return cdecl.declare(result, function(node)
101
-          if node == result then return id end
102
-          return typename[node]
103
-        end)
104
-      end
105
-    elseif result:class() == "constant" then
106
-      return function()
107
-        return "static const int " .. id .. " = " .. result:value()
108
-      end
109
-    end
110
-    result = select(-1, result:operand())
111
-  end
56
+  local decl = macro[op](node)
57
+  return decl, id
112 58
 end
113 59
 
114 60
 return _M

+ 1
- 0
gcc-lua-cdecl/gcc/cdecl.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/Makefile View File

@@ -1,6 +1,6 @@
1 1
 #
2 2
 # C declaration composer for the GCC Lua plugin.
3
-# Copyright © 2013 Peter Colberg.
3
+# Copyright © 2013–2015 Peter Colberg.
4 4
 # Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
 #
6 6
 

+ 1
- 1
gcc-lua-cdecl/test/array.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/array.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/composite.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/composite.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/enum.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/enum.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/function.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/function.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/pointer.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/pointer.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/scalar.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/scalar.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/variable.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/variable.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 1
gcc-lua-cdecl/test/vector.c View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * C declaration composer for the GCC Lua plugin.
3
- * Copyright © 2013 Peter Colberg.
3
+ * Copyright © 2013–2015 Peter Colberg.
4 4
  * Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
  */
6 6
 

+ 1
- 0
gcc-lua-cdecl/test/vector.lua View File

@@ -1,6 +1,6 @@
1 1
 --
2 2
 -- C declaration composer for the GCC Lua plugin.
3
+-- Copyright © 2013–2015 Peter Colberg.
3 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
4 5
 --
5 6
 

+ 1
- 2
mpi/C.c View File

@@ -10,10 +10,9 @@
10 10
 #include <mpi.h>
11 11
 
12 12
 #include "ffi-cdecl.h"
13
-#include "ffi-cdecl-luajit.h"
14 13
 
15 14
 /* C opaque types */
16
-cdecl_typealias(MPI_Aint, ptrdiff_t)
15
+cdecl_type(MPI_Aint)
17 16
 #if MPI_VERSION >= 3
18 17
 cdecl_type(MPI_Count)
19 18
 #endif

+ 25
- 5
mpi/C.lua.in View File

@@ -4,25 +4,45 @@
4 4
 -- Distributed under the MIT license. (See accompanying file LICENSE.)
5 5
 ------------------------------------------------------------------------------
6 6
 
7
-local gcc   = require("gcc")
8
-local cdecl = require("ffi-cdecl")
7
+local gcc = require("gcc")
8
+local cdecl = require("gcc.cdecl")
9
+local fficdecl = require("ffi-cdecl")
9 10
 
10 11
 -- Output generated assembly to /dev/null
11 12
 gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)
12 13
 
14
+-- Captured C declarations.
13 15
 local decls = {}
16
+-- Type declaration identifiers.
17
+local types = {}
14 18
 
15 19
 -- Parse C declaration from capture macro.
16 20
 gcc.register_callback(gcc.PLUGIN_PRE_GENERICIZE, function(node)
17
-  local decl = cdecl.parse(node)
18
-  if decl then table.insert(decls, decl) end
21
+  local decl, id = fficdecl.parse(node)
22
+  if decl then
23
+    if decl:class() == "type" or decl:code() == "type_decl" then
24
+      types[decl] = id
25
+    end
26
+    table.insert(decls, {decl = decl, id = id})
27
+  end
19 28
 end)
20 29
 
30
+-- Formats the given declaration as a string of C code.
31
+local function format(decl, id)
32
+  if decl:class() == "constant" then
33
+    return "static const int " .. id .. " = " .. decl:value()
34
+  end
35
+  return cdecl.declare(decl, function(node)
36
+    if node == decl then return id end
37
+    return types[node]
38
+  end)
39
+end
40
+
21 41
 -- Output captured C declarations to Lua file.
22 42
 gcc.register_callback(gcc.PLUGIN_FINISH_UNIT, function()
23 43
   local result = {}
24 44
   for i, decl in ipairs(decls) do
25
-    result[i] = tostring(decl) .. ";\n"
45
+    result[i] = format(decl.decl, decl.id) .. ";\n"
26 46
   end
27 47
   local f = assert(io.open(arg.output, "w"))
28 48
   f:write([=[

Loading…
Cancel
Save