1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: may 10, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module dstep.translator.Enum;
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 Enum : 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 writeEnum(spelling, (context) {
31 			foreach (cursor, parent ; cursor.declarations)
32 			{
33 				with (CXCursorKind)
34 					switch (cursor.kind)
35 					{
36 						case CXCursor_EnumConstantDecl:
37 							output.newContext();
38 							output ~= translateIdentifier(cursor.spelling);
39 							output ~= " = ";
40 							output ~= cursor.enum_.value;
41 							context.instanceVariables ~= output.currentContext.data;
42 						break;
43 						
44 						default: break;
45 					}
46 			}
47 		});
48 	}
49 
50 	@property override string spelling ()
51 	{
52 		auto name = cursor.spelling;
53 		return name.isPresent ? name : generateAnonymousName(cursor);
54 	}
55 
56 private:
57 
58 	string writeEnum (string name, void delegate (EnumData context) dg)
59 	{
60 		auto context = new EnumData;
61 		context.name = translateIdentifier(name);
62 		
63 		dg(context);
64 		
65 		return context.data;
66 	}
67 }