Skip to content

Commit

Permalink
Merge pull request #12 from Dicebot/fwd_structs
Browse files Browse the repository at this point in the history
Fix issue #5 : forward declaration of structures
  • Loading branch information
jacob-carlborg committed Jul 11, 2013
2 parents 115891b + b8b502a commit 8b8ceb7
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ main
tmp
.rvmrc
foo.d
*.zip
*.zip
*.swp
7 changes: 6 additions & 1 deletion clang/Cursor.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ struct Cursor
{
return clang_hashCursor(cast(CXCursor) cx);
}

bool isDefinition () const
{
return clang_isCursorDefinition(cast(CXCursor) cx) != 0;
}
}

struct ObjcCursor
Expand Down Expand Up @@ -193,4 +198,4 @@ struct EnumCursor
{
return clang_getEnumConstantDeclUnsignedValue(cx);
}
}
}
56 changes: 33 additions & 23 deletions dstep/translator/Output.d
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,27 @@ class Output

private:

void addDeclarations (string[] declarations, bool extraNewline = true)
{
void addDeclarations (string[] declarations, bool extraNewline = true)
{
auto newline = "\n";

if (extraNewline)
newline ~= "\n";

this ~= declarations.join(newline);

if (declarations.any)
this ~= "\n\n";
}
this ~= declarations.join(newline);

if (declarations.any)
this ~= "\n\n";
}
}

class StructData
{
string name;

string[] instanceVariables;

bool isFwdDeclaration;

@property string data ()
{
Expand All @@ -134,16 +136,24 @@ class StructData
if (name.isPresent)
name = ' ' ~ name;

context.put(type, name, nl, '{', nl);
if (isFwdDeclaration)
{
context.put(type, name, ";", nl);
isFwdDeclaration = true;
}
else
{
context.put(type, name, nl, '{', nl);

context.indent in {
addDeclarations(context, instanceVariables);
};

auto str = context.data.strip('\n');
context = output.newContext();
context ~= str;
context.put(nl, '}');
context.indent in {
addDeclarations(context, instanceVariables);
};

auto str = context.data.strip('\n');
context = output.newContext();
context ~= str;
context.put(nl, '}');
}

return context.data;
}
Expand All @@ -156,7 +166,7 @@ protected:
}

void addDeclarations (String context, string[] declarations)
{
{
foreach (i, e ; declarations)
{
if (i != 0)
Expand All @@ -165,12 +175,12 @@ protected:
context ~= e;
}

if (declarations.any)
if (declarations.any)
{
context ~= nl;
context ~= nl;
}
}
}
}

class EnumData : StructData
Expand All @@ -183,7 +193,7 @@ class EnumData : StructData
protected:

override void addDeclarations (String context, string[] declarations)
{
{
foreach (i, e ; declarations)
{
if (i != 0)
Expand All @@ -195,12 +205,12 @@ protected:
context ~= e;
}

if (declarations.any)
if (declarations.any)
{
context ~= nl;
context ~= nl;
}
}
}
}

class UnionData : StructData
Expand Down Expand Up @@ -436,4 +446,4 @@ private:
}

struct NewLine {}
NewLine nl;
NewLine nl;
4 changes: 3 additions & 1 deletion dstep/translator/Record.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ private:
string writeRecord (string name, void delegate (Data context) dg)
{
auto context = new Data;
static if (is(typeof(context.isFwdDeclaration) == bool))
context.isFwdDeclaration = !cursor.isDefinition;
context.name = translateIdentifier(name);

dg(context);
Expand All @@ -72,4 +74,4 @@ private:
output.newContext();
context.instanceVariables ~= translator.variable(cursor);
}
}
}
22 changes: 20 additions & 2 deletions dstep/translator/Translator.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Translator
string inputFilename;
File inputFile;
Language language;
string[string] deferredDeclarations;
}

this (string inputFilename, TranslationUnit translationUnit, const Options options = Options.init)
Expand Down Expand Up @@ -75,7 +76,10 @@ class Translator
output.classes ~= code;
break;

case CXCursor_StructDecl: output.structs ~= code; break;
case CXCursor_StructDecl:
if (cursor.isDefinition)
output.structs ~= code;
break;
case CXCursor_EnumDecl: output.enums ~= code; break;
case CXCursor_UnionDecl: output.unions ~= code; break;
case CXCursor_VarDecl: output.variables ~= code; break;
Expand All @@ -86,6 +90,7 @@ class Translator
}
}

output.structs ~= deferredDeclarations.values;
output.externDeclaration = externDeclaration();

auto data = output.toString;
Expand Down Expand Up @@ -132,7 +137,20 @@ class Translator
return typedef_(cursor, output.newContext);
break;

case CXCursor_StructDecl: return (new Record!(StructData)(cursor, parent, this)).translate; break;
case CXCursor_StructDecl:
auto code = (new Record!(StructData)(cursor, parent, this)).translate;
if (cursor.isDefinition)
{
if (cursor.spelling in deferredDeclarations)
deferredDeclarations.remove(cursor.spelling);
return code;
}
else
{
deferredDeclarations[cursor.spelling] = code;
return "";
}
break;
case CXCursor_EnumDecl: return (new Enum(cursor, parent, this)).translate; break;
case CXCursor_UnionDecl: return (new Record!(UnionData)(cursor, parent, this)).translate; break;

Expand Down
8 changes: 7 additions & 1 deletion test_files/structs.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ struct _Anonymous_0
{
int x;
int y;
}
}

struct E
{
}

struct F;
10 changes: 9 additions & 1 deletion test_files/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ typedef struct
{
int x;
int y;
} D;
} D;

struct E;

struct E
{
};

struct F;
8 changes: 8 additions & 0 deletions test_files/typedef_struct.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern (C):

alias _Foo Foo;

struct _Foo
{
int x;
}
6 changes: 6 additions & 0 deletions test_files/typedef_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
typedef struct _Foo Foo;

struct _Foo
{
int x;
};

0 comments on commit 8b8ceb7

Please sign in to comment.