1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.objectweb.fractal.cecilia.adl.idl;
25
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.objectweb.fractal.adl.ADLException;
32 import org.objectweb.fractal.adl.Node;
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.cecilia.adl.SourceCodeProvider;
35 import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
36 import org.objectweb.fractal.cecilia.adl.idl.ast.Field;
37 import org.objectweb.fractal.cecilia.adl.idl.ast.IDLDefinition;
38 import org.objectweb.fractal.cecilia.adl.idl.ast.Import;
39 import org.objectweb.fractal.cecilia.adl.idl.ast.ImportContainer;
40 import org.objectweb.fractal.cecilia.adl.idl.ast.UnionDefinition;
41 import org.objectweb.fractal.cecilia.adl.idl.util.CUtil;
42 import org.objectweb.fractal.cecilia.adl.idl.util.Util;
43 import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
44 import org.objectweb.fractal.task.core.Executable;
45 import org.objectweb.fractal.task.core.TaskException;
46 import org.objectweb.fractal.task.core.primitive.annotations.ClientInterfaceForEach;
47 import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
48 import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
49 import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
50
51
52
53
54 public class CUnionDefinitionVisitor extends AbstractTaskFactoryUser
55 implements
56 IDLDefinitionVisitor {
57
58
59
60
61
62
63
64
65
66 public Component visit(final List<Node> path,
67 final IDLDefinition idlDefinition, final Map<Object, Object> context)
68 throws ADLException, TaskException {
69 if (idlDefinition instanceof UnionDefinition) {
70
71 final List<IDLDefinition> importedDefinitions = new ArrayList<IDLDefinition>();
72 if (idlDefinition instanceof ImportContainer) {
73 for (final Import imp : ((ImportContainer) idlDefinition).getImports()) {
74 importedDefinitions.add((IDLDefinition) Util.getImportedAST(imp));
75 }
76 }
77
78 return taskFactoryItf
79 .newPrimitiveTask(new CUnionSourceTask(
80 (UnionDefinition) idlDefinition), idlDefinition,
81 importedDefinitions);
82 } else
83 return null;
84 }
85
86
87
88
89
90
91
92
93
94 @TaskParameters({"unionDefinitionNode", "importedDefinitions"})
95 @ServerInterfaces(@ServerInterface(name = "c-union-definition-provider", signature = SourceCodeProvider.class, record = "role:cUnionDefinition, id:%", parameters = {"unionDefinitionNode"}))
96 public static class CUnionSourceTask extends AbstractUnionVisitor
97 implements
98 Executable,
99 SourceCodeProvider {
100
101 protected final UnionDefinition unionDefinition;
102
103
104
105
106
107
108 @ClientInterfaceForEach(iterable = "importedDefinitions", prefix = "imported-definition", signature = SourceCodeProvider.class, record = "role:importedDefinition, id:%", parameters = "importedDefinitions.element")
109 public final Map<IDLDefinition, SourceCodeProvider> importedDefinitionItfs = new LinkedHashMap<IDLDefinition, SourceCodeProvider>();
110
111
112
113
114
115
116
117
118
119
120
121 public CUnionSourceTask(final UnionDefinition unionDefinition) {
122 this.unionDefinition = unionDefinition;
123 }
124
125
126 protected String sourceCode;
127
128 protected CodeWriter cw;
129
130 String unionName;
131
132
133
134
135
136 public void execute() throws Exception {
137 cw = new CodeWriter("C Union builder");
138 this.unionName = unionDefinition.getName();
139 if (unionName != null) {
140
141 visit(unionDefinition);
142 }
143 sourceCode = cw.toString();
144 }
145
146
147
148
149
150 public String getSourceCode() {
151 return sourceCode;
152 }
153
154
155
156
157
158 public void enterUnion(final UnionDefinition union) throws Exception {
159 cw.append("/* Generated union :" + unionName + " */").endl();
160
161 final String unionCName = unionName.replace('.', '_');
162 cw.append("#ifndef _").append(unionCName.toUpperCase()).append("_IDL_H_")
163 .endl();
164 cw.append("#define _").append(unionCName.toUpperCase()).append("_IDL_H_")
165 .endl();
166 cw.endl();
167
168 for (final SourceCodeProvider importedDefinitionProvider : importedDefinitionItfs
169 .values()) {
170 cw.append(importedDefinitionProvider.getSourceCode());
171 }
172
173 cw.endl();
174 cw.endl();
175 cw.append("/* some cecilia typedefs for predefinite C types */").endl();
176 cw.append("#include \"cecilia_types.h\"").endl();
177 cw.endl();
178 cw.endl();
179
180 cw.append("union ").append(unionCName).append(";").endl();
181 cw.append("typedef union ").append(unionCName).append(' ').append(unionCName)
182 .append(";").endl();
183 cw.append("typedef union ").append(unionCName).append(" R").append(unionCName)
184 .append(";").endl();
185 cw.append("union ").append(unionCName).append("{").endl();
186 }
187
188 public void leaveUnion(final UnionDefinition union) throws Exception {
189 cw.append("};").endl();
190 cw.endl();
191 final String unionCName = unionName.replace('.', '_');
192 cw.append("#define ").append(unionCName.toUpperCase()).append("_POINTER \\")
193 .endl();
194 cw.append(" union ").append(unionCName).append("* ").endl();
195 cw.append("#define ").append(unionCName.toUpperCase())
196 .append("_UNION \\").endl();
197 cw.append(" union ").append(unionCName).endl();
198
199 cw.append("#endif").endl();
200 }
201
202 public void visitField(final Field field) throws Exception {
203 cw.append(CUtil.buildUnionField(field));
204 cw.append(";").endl();
205 }
206
207 }
208 }