Skip to content

Commit

Permalink
Add pragma(include) to automatically parse and include C headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Jul 16, 2013
1 parent 0627f87 commit 2837d34
Show file tree
Hide file tree
Showing 7 changed files with 6,021 additions and 3 deletions.
58 changes: 58 additions & 0 deletions src/attrib.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "hdrgen.h"
#include "utf.h"

#include "dstep.h"
#include "import.h"

/********************************* AttribDeclaration ****************************/

Expand Down Expand Up @@ -1161,6 +1163,62 @@ void PragmaDeclaration::semantic(Scope *sc)
#endif
}
}
else if (ident == Id::include)
{
if (!args || args->dim != 1)
error("string expected for library name");
else
{
Expression* e = (*args)[0];

sc = sc->startCTFE();
e = e->semantic(sc);
e = resolveProperties(sc, e);
sc = sc->endCTFE();

e = e->ctfeInterpret();
(*args)[0] = e;
if (e->op == TOKerror)
goto Lnodecl;
StringExp* se = e->toString();
if (!se)
error("string expected for library name, not '%s'", e->toChars());
else
{
char* path = (char*) mem.malloc(se->len + 1);
memcpy(path, se->string, se->len);
path[se->len] = 0;

if (global.params.verbose)
printf("library %s\n", path);

TranslationArgs args;
args.file = path;
args.outputFile = FileName::forceExt(path, global.mars_ext);
args.args = NULL;
args.argsLength = 0;
args.language = Language_C;

Translator translator;

rt_init(NULL);
int result = dstep_translate(args, &translator);
dstep_disposeTranslator(translator);
rt_term(NULL);

const char* name = FileName::name(args.outputFile);
name = FileName::removeExt(name);
Identifier* id = Lexer::idPool(name);

Import* import = new Import(loc, NULL, id, NULL, false);
import->importAll(sc);

FileName::free(name);
FileName::free(args.outputFile);
mem.free(path);
}
}
}
else if (global.params.ignoreUnsupportedPragmas)
{
if (global.params.verbose)
Expand Down
Loading

0 comments on commit 2837d34

Please sign in to comment.