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.RecordDefinition;
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 CRecordDefinitionVisitor 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 RecordDefinition) {
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 CRecordSourceTask(
80 (RecordDefinition) idlDefinition), idlDefinition,
81 importedDefinitions);
82 } else
83 return null;
84 }
85
86
87
88
89
90
91
92
93
94 @TaskParameters({"recordDefinitionNode", "importedDefinitions"})
95 @ServerInterfaces(@ServerInterface(name = "c-record-definition-provider", signature = SourceCodeProvider.class, record = "role:cRecordDefinition, id:%", parameters = {"recordDefinitionNode"}))
96 public static class CRecordSourceTask extends AbstractRecordVisitor
97 implements
98 Executable,
99 SourceCodeProvider {
100
101 protected final RecordDefinition recordDefinition;
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 CRecordSourceTask(final RecordDefinition recordDefinition) {
122 this.recordDefinition = recordDefinition;
123 }
124
125
126 protected String sourceCode;
127
128 protected CodeWriter cw;
129
130 String recordName;
131
132
133
134
135
136 public void execute() throws Exception {
137 cw = new CodeWriter("C Record builder");
138 this.recordName = recordDefinition.getName();
139 if (recordName != null) {
140
141 visit(recordDefinition);
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 enterRecord(final RecordDefinition record) throws Exception {
159 cw.append("/* Generated record :" + recordName + " */").endl();
160
161 final String struct = recordName.replace('.', '_');
162 cw.append("#ifndef _").append(struct.toUpperCase()).append("_IDL_H_")
163 .endl();
164 cw.append("#define _").append(struct.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("struct ").append(struct).append(";").endl();
181 cw.append("typedef struct ").append(struct).append(' ').append(struct)
182 .append(";").endl();
183 cw.append("typedef struct ").append(struct).append(" R").append(struct)
184 .append(";").endl();
185 cw.append("struct ").append(struct).append("{").endl();
186 }
187
188 public void leaveRecord(final RecordDefinition record) throws Exception {
189 cw.append("};").endl();
190 cw.endl();
191 final String struct = recordName.replace('.', '_');
192 cw.append("#define ").append(struct.toUpperCase()).append("_POINTER \\")
193 .endl();
194 cw.append(" struct ").append(struct).append("* ").endl();
195 cw.append("#define ").append(struct.toUpperCase())
196 .append("_STRUCTURE \\").endl();
197 cw.append(" struct ").append(struct).endl();
198
199 cw.append("#endif").endl();
200 }
201
202 public void visitField(final Field field) throws Exception {
203 cw.append(CUtil.buildRecordField(field));
204 cw.append(";").endl();
205 }
206
207 }
208 }