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 	this (Cursor cursor, Cursor parent, Translator translator)
24 	{
25 		super(cursor, parent, translator);
26 	}
27 
28 	override string translate ()
29 	{
30 		return writeRecord(spelling, (context) {
31 			foreach (cursor, parent ; cursor.declarations)
32 			{
33 				with (CXCursorKind)
34 					switch (cursor.kind)
35 					{
36 						case CXCursor_FieldDecl:
37 							if (!cursor.type.isExposed && cursor.type.declaration.isValid)
38 							{
39 								output.newContext();
40 								output.currentContext.indent in {
41 									context.instanceVariables ~= translator.translate(cursor.type.declaration);
42 								};
43 
44 								if (cursor.type.declaration.type.isEnum || !cursor.type.isAnonymous)
45 									translateVariable(cursor, context);
46 							}
47 
48 							else
49 								translateVariable(cursor, context);
50 						break;
51 
52 						default: break;
53 					}
54 			}
55 		});
56 	}
57 
58 private:
59 
60 	string writeRecord (string name, void delegate (Data context) dg)
61 	{
62 		auto context = new Data;
63 		static if (is(typeof(context.isFwdDeclaration) == bool))
64 			context.isFwdDeclaration = !cursor.isDefinition;
65 		context.name = translateIdentifier(name);
66 
67 		dg(context);
68 
69 		return context.data;
70 	}
71 
72 	void translateVariable (Cursor cursor, Data context)
73 	{
74 		output.newContext();
75 		context.instanceVariables ~= translator.variable(cursor);
76 	}
77 }