Skip to content

Commit 170cb33

Browse files
WalterBrightdlang-bot
authored andcommitted
ImportC: add documentation for __import
1 parent e0f9fec commit 170cb33

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

spec/importc.dd

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ $(H2 $(LNAME2 extensions, Extensions))
242242
$(P Any declarations in scope can be accessed, not just
243243
declarations that lexically precede a reference.)
244244

245+
245246
$(H3 $(LNAME2 ctfe, Compile Time Function Execution))
246247

247248
$(P Evaluating constant expressions includes executing functions in the
248249
same manner as D's CTFE can.)
249250

251+
250252
$(H3 $(LNAME2 inlining, Function Inlining))
251253

252254
$(P Functions for which the function body is present can
@@ -283,6 +285,77 @@ _Static_assert(sizeof(A) == 1, "A should be size 1");
283285
$(P Arrays can have `register` storage class, and may be enregistered by the compiler. C11 6.3.2.1-3)
284286

285287

288+
$(H3 $(LNAME2 __import, Import Declarations))
289+
290+
$(P Modules can be imported with a $(I CImportDeclaration):)
291+
292+
$(GRAMMAR
293+
$(GNAME CImportDeclaration):
294+
$(D __import) $(GLINK2 module, ImportList) $(D ;)
295+
)
296+
297+
$(P Imports enable ImportC code to directly access D declarations and functions
298+
without the necessity of creating a $(TT .h) file representing those declarations.
299+
The tedium and brittleness of keeping the $(TT .h) file up-to-date with the D
300+
declarations is eliminated.
301+
D functions are available to be inlined.
302+
)
303+
304+
$(P Imports also enable ImportC code to directly import other C files without
305+
needing to create a .h file for them, either.
306+
Imported C functions become available to be inlined.
307+
)
308+
309+
$(P The $(I ImportList) works the same as it does for D.)
310+
311+
$(P The ordering of $(I CImportDeclaration)s has no significance.)
312+
313+
$(P An ImportC file can be imported, the name of the C file to be
314+
imported is derived from the module name.)
315+
316+
$(P All the global symbols in the ImportC file become available to the
317+
importing module.)
318+
319+
$(P If a name is referred to in the importing file is not found,
320+
the global symbols in each imported file are searched for the name.
321+
If it is found in exactly one module, that becomes the resolution of the
322+
name. If it is found in multiple modules, it is an error.)
323+
324+
$(NOTE Since ImportC has no scope resolution operator, only global symbols
325+
can be found, and a qualification cannot be added to specifiy which module
326+
a symbols is in.)
327+
328+
$(P Preprocessor symbols in the imported module are not available to the
329+
importing module, and preprocessing symbols in the importing file are not
330+
available to the imported module.)
331+
332+
$(P A D module can be imported, in the same manner as that
333+
of a $(GLINK2 module, ImportDeclaration).)
334+
335+
$(P Imports can be circular.)
336+
337+
$(CCODE
338+
__import core.stdc.stdarg; // get D declaration of va_list
339+
__import mycode; // import mycode.c
340+
341+
int foo()
342+
{
343+
va_list x; // picks up va_list from core.stdc.stdarg
344+
return 1 + A; // returns 4
345+
})
346+
347+
$(P $(TT mycode.c) looks like:)
348+
349+
$(CCODE
350+
enum E { A = 3; }
351+
)
352+
353+
$(BEST_PRACTICE Avoid using preprocessor $(TT #define)s like $(TT #define A 3).
354+
Use the enum form shown in the above example.
355+
Prefer $(TT const) declarations over $(TT #define)s.
356+
Rewrite function-style preprocessor macros as inline functions.
357+
)
358+
286359
$(H2 $(LNAME2 gnu-clang-extensions, Gnu and Clang Extensions))
287360

288361
$(P `gcc` and `clang` are presumed to have the same behavior w.r.t. extensions,

0 commit comments

Comments
 (0)