1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: may 1, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module dstep.translator.Record;
8 
9 import mambo.core._;
10 
11 import clang.c.Index;
12 import clang.Cursor;
13 import clang.Visitor;
14 import clang.Util;
15 
16 import dstep.translator.Translator;
17 import dstep.translator.Declaration;
18 import dstep.translator.Output;
19 import dstep.translator.Type;
20 
21 class Record (Data) : Declaration
22 {
23     static bool[Cursor] recordDefinitions;
24 
25     this (Cursor cursor, Cursor parent, Translator translator)
26     {
27         super(cursor, parent, translator);
28     }
29 
30     override string translate ()
31     {
32         return writeRecord(spelling, (context) {
33             foreach (cursor, parent ; cursor.declarations)
34             {
35                 with (CXCursorKind)
36                     switch (cursor.kind)
37                     {
38                         case CXCursor_FieldDecl:
39                             if (!cursor.type.isExposed && cursor.type.declaration.isValid)
40                             {
41                                 auto def = cursor.type.declaration.definition;
42                                 auto known = def in this.recordDefinitions;
43 
44                                 if (!known)
45                                 {
46                                     output.newContext();
47                                     output.currentContext.indent in {
48                                         context.instanceVariables ~= translator.translate(cursor.type.declaration);
49                                     };
50                                 }
51 
52                                 if (cursor.type.declaration.type.isEnum || !cursor.type.isAnonymous)
53                                     translateVariable(cursor, context);
54                             }
55 
56                             else
57                                 translateVariable(cursor, context);
58                         break;
59 
60                         default: break;
61                     }
62             }
63         });
64     }
65 
66 private:
67 
68     string writeRecord (string name, void delegate (Data context) dg)
69     {
70         auto context = new Data;
71         
72         if (cursor.isDefinition)
73             this.recordDefinitions[cursor] = true;
74         else
75             context.isFwdDeclaration = true;
76 
77         context.name = translateIdentifier(name);
78 
79         dg(context);
80 
81         return context.data;
82     }
83 
84     void translateVariable (Cursor cursor, Data context)
85     {
86         output.newContext();
87         context.instanceVariables ~= translator.variable(cursor);
88     }
89 }