diff --git a/SWIG/Examples/test-suite/imports_b.i b/SWIG/Examples/test-suite/imports_b.i index 070dea447..afc573a39 100644 --- a/SWIG/Examples/test-suite/imports_b.i +++ b/SWIG/Examples/test-suite/imports_b.i @@ -12,6 +12,31 @@ #include "imports_b.h" %} -%import(module="imports_a") "imports_a.i" + +/* + To import, you can use either + + %import "imports_a.i" + + or + + %import(module="imports_a") "imports_a.h" + + + In the first case, imports_a.i should declare the module name using + the %module directive. + + In the second case, the file could be either a .h file, where no + %module directive will be found, or a swig interface file, where + the module option will take priority over any %module directive + inside the imported file. + +*/ + +#if 0 +%import "imports_a.i" +#else +%import(module="imports_a") "imports_a.h" +#endif %include "imports_b.h" diff --git a/SWIG/Source/CParse/parser.y b/SWIG/Source/CParse/parser.y index d5133959d..efaa6a1e7 100644 --- a/SWIG/Source/CParse/parser.y +++ b/SWIG/Source/CParse/parser.y @@ -1748,11 +1748,13 @@ include_directive: includetype options string LBRACKET { cparse_file = Swig_copy_string($3); cparse_line = 0; } interface RBRACKET { + String *mname = 0; $$ = $6; cparse_file = $1.filename; cparse_line = $1.line; if (strcmp($1.type,"include") == 0) set_nodeType($$,"include"); if (strcmp($1.type,"import") == 0) { + mname = $2 ? Getattr($2,"module") : 0; set_nodeType($$,"import"); if (import_mode) --import_mode; } @@ -1763,11 +1765,29 @@ include_directive: includetype options string LBRACKET { Node *n = firstChild($$); while (n) { if (Strcmp(nodeType(n),"module") == 0) { + if (mname) { + Setattr(n,"name", mname); + mname = 0; + } Setattr($$,"module",Getattr(n,"name")); break; } n = nextSibling(n); } + if (mname) { + /* There is no module node in the import + node, ie, you imported a .h file + directly. We are forced then to create + a new import node with a module node. + */ + Node *nint = new_node("import"); + Node *mnode = new_node("module"); + Setattr(mnode,"name", mname); + appendChild(nint,mnode); + appendChild(nint,firstChild($$)); + $$ = nint; + Setattr($$,"module",mname); + } } Setattr($$,"options",$2); }