View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2008 STMicroelectronics
4    * Copyright (C) 2008 INRIA
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   * Contact: fractal@objectweb.org
21   *
22   * Author:Ali Erdem Ozcan
23   *
24   * Contributors: Matthieu Leclercq, Lionel Debroux
25   */
26  
27  package org.objectweb.fractal.cecilia.primitive.thinkMC.attributes;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.objectweb.fractal.adl.attributes.Attribute;
33  import org.objectweb.fractal.adl.attributes.Attributes;
34  import org.objectweb.fractal.adl.components.ComponentContainer;
35  import org.objectweb.fractal.api.Component;
36  import org.objectweb.fractal.cecilia.adl.AbstractInstantiationTask;
37  import org.objectweb.fractal.cecilia.adl.SourceCodeProvider;
38  import org.objectweb.fractal.cecilia.adl.attributes.AbstractAttributeVisitor;
39  import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
40  import org.objectweb.fractal.cecilia.adl.idl.ast.ArrayOf;
41  import org.objectweb.fractal.cecilia.adl.idl.ast.Field;
42  import org.objectweb.fractal.cecilia.adl.idl.ast.FieldContainer;
43  import org.objectweb.fractal.cecilia.adl.idl.ast.IDLDefinitionContainer;
44  import org.objectweb.fractal.cecilia.adl.idl.ast.Type;
45  import org.objectweb.fractal.cecilia.adl.idl.ast.ComplexType;
46  import org.objectweb.fractal.cecilia.adl.idl.ast.PointerOf;
47  import org.objectweb.fractal.cecilia.adl.idl.ast.PrimitiveType;
48  import org.objectweb.fractal.cecilia.adl.idl.util.CUtil;
49  import org.objectweb.fractal.cecilia.adl.idl.util.Util;
50  import org.objectweb.fractal.task.core.TaskException;
51  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
52  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
53  import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
54  
55  /**
56   * Visitor component that builds the instantiation of the attribute data
57   * structures of the component for <code>ThinkMC</code> dialect.
58   */
59  // TODO back port r3677:4017 from AttributeInstanceBuilder.java
60  public class AttributeInstantiationVisitor extends AbstractAttributeVisitor {
61  
62    // ---------------------------------------------------------------------------
63    // Implementation of abstract methods of AbstractAttributeVisitor
64    // ---------------------------------------------------------------------------
65  
66    @Override
67    protected Component createTask(final ComponentContainer container,
68        final Attributes attributes) throws TaskException {
69      final Map<String, Type> fieldTypes = new HashMap<String, Type>();
70      for (final Field field : ((FieldContainer) ((IDLDefinitionContainer) attributes)
71          .getIDLDefinition()).getFields()) {
72        fieldTypes.put(field.getName(), Util.getContainedType(field));
73      }
74      return taskFactoryItf.newPrimitiveTask(new AttributeDefinitionTask(
75          attributes.getAttributes(), fieldTypes), container);
76    }
77  
78    // ---------------------------------------------------------------------------
79    // Task classes
80    // ---------------------------------------------------------------------------
81  
82    /**
83     * Builds definition source code for attribute data structure for the given
84     * component node. This task provides the produced source code.
85     */
86    @TaskParameters("componentNode")
87    @ServerInterfaces(@ServerInterface(name = "source-code-provider", signature = SourceCodeProvider.class, record = "role:attributeInstantiation, id:%", parameters = "componentNode"))
88    public static class AttributeDefinitionTask extends AbstractInstantiationTask {
89  
90      protected final Map<String, Type> fieldTypes;
91      // the attributes array
92      protected final Attribute[]       fields;
93  
94      // -------------------------------------------------------------------------
95      // Task constructor
96      // -------------------------------------------------------------------------
97  
98      /**
99       * @param fields the array of fields of the attribute data structure of the
100      *            component.
101      * @param fieldTypes the attribute types.
102      */
103     public AttributeDefinitionTask(final Attribute[] fields,
104         final Map<String, Type> fieldTypes) {
105       this.fields = fields;
106       this.fieldTypes = fieldTypes;
107     }
108 
109     // -------------------------------------------------------------------------
110     // Implementation of abstract methods of AbstractInstantiationTask
111     // -------------------------------------------------------------------------
112 
113     @Override
114     protected String processSourceCode() throws Exception {
115       final CodeWriter cw = new CodeWriter();
116       cw.append("{");
117       for (final Attribute field : fields) {
118         final Type attType = fieldTypes.get(field.getName());
119         final String attTypeStr = CUtil.buildDeclarationType(attType);
120         final String valSym = field.getValue();
121 
122         // Typecast.
123         cw.append('(').append(attTypeStr).append(") ");
124 
125         // The value was created by AttributeLoader.
126         cw.append(valSym).appendln(", ");
127       }
128       cw.append("}");
129       return cw.toString();
130     }
131   }
132 }