Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 95373640fdb6e5f35b24658274ebe85f7078e4c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug (initial contribution)
 */
package org.eclipse.etrice.generator.c.gen;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.etrice.core.genmodel.etricegen.ActorInstance;
import org.eclipse.etrice.core.genmodel.etricegen.InstanceBase;
import org.eclipse.etrice.core.genmodel.etricegen.InterfaceItemInstance;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.Attribute;
import org.eclipse.etrice.core.room.DataClass;
import org.eclipse.etrice.core.room.DataType;
import org.eclipse.etrice.core.room.EnumerationType;
import org.eclipse.etrice.core.room.ExternalType;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.PortClass;
import org.eclipse.etrice.core.room.PrimitiveType;
import org.eclipse.etrice.core.room.RefableType;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.generator.c.gen.CExtensions;
import org.eclipse.etrice.generator.generic.RoomExtensions;
import org.eclipse.etrice.generator.generic.TypeHelpers;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.IntegerRange;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class Initialization {
  @Inject
  private CExtensions languageExt;
  
  @Inject
  @Extension
  private RoomHelpers _roomHelpers;
  
  @Inject
  @Extension
  private RoomExtensions _roomExtensions;
  
  @Inject
  @Extension
  private TypeHelpers _typeHelpers;
  
  public CharSequence generateAttributeInit(final InstanceBase instance, final List<Attribute> attributes) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _hasElements = false;
      for(final Attribute a : attributes) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "");
        }
        ArrayList<Attribute> _arrayList = new ArrayList<Attribute>();
        List<Attribute> _union = this._roomExtensions.<Attribute>union(_arrayList, a);
        String _initAttributeArray = this.initAttributeArray(instance, _union);
        _builder.append(_initAttributeArray, "");
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  private String initAttributeArray(final InstanceBase instance, final List<Attribute> path) {
    String _xblockexpression = null;
    {
      Attribute a = IterableExtensions.<Attribute>last(path);
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("\t\t");
      _builder.append("/* ");
      String _name = a.getName();
      _builder.append(_name, "\t\t");
      {
        int _size = a.getSize();
        boolean _greaterThan = (_size > 1);
        if (_greaterThan) {
          _builder.append("[");
          int _size_1 = a.getSize();
          _builder.append(_size_1, "\t\t");
          _builder.append("]");
        }
      }
      _builder.append(" */");
      String COMMENT = _builder.toString();
      String _xifexpression = null;
      if (((a.getSize() == 0) || ((!a.getType().isRef()) && this._typeHelpers.isEnumerationOrPrimitive(a.getType().getType())))) {
        CharSequence _initAttribute = this.initAttribute(instance, path);
        _xifexpression = (_initAttribute + COMMENT);
      } else {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("{ ");
        {
          int _size_2 = a.getSize();
          IntegerRange _upTo = new IntegerRange(1, _size_2);
          boolean _hasElements = false;
          for(final Integer i : _upTo) {
            if (!_hasElements) {
              _hasElements = true;
            } else {
              _builder_1.appendImmediate(", ", "");
            }
            CharSequence _initAttribute_1 = this.initAttribute(instance, path);
            _builder_1.append(_initAttribute_1, "");
          }
        }
        _builder_1.append(" } ");
        _builder_1.append(COMMENT, "");
        _builder_1.newLineIfNotEmpty();
        _xifexpression = _builder_1.toString();
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  private CharSequence initAttribute(final InstanceBase instance, final List<Attribute> path) {
    CharSequence _xblockexpression = null;
    {
      Attribute a = IterableExtensions.<Attribute>last(path);
      RefableType _type = a.getType();
      DataType aType = _type.getType();
      RefableType _type_1 = a.getType();
      boolean _isRef = _type_1.isRef();
      if (_isRef) {
        String _xifexpression = null;
        String _defaultValueLiteral = a.getDefaultValueLiteral();
        boolean _notEquals = (!Objects.equal(_defaultValueLiteral, null));
        if (_notEquals) {
          _xifexpression = a.getDefaultValueLiteral();
        } else {
          _xifexpression = this.languageExt.nullPointer();
        }
        return _xifexpression;
      }
      CharSequence _switchResult = null;
      boolean _matched = false;
      if (aType instanceof DataClass) {
        _matched=true;
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("{");
        _builder.newLine();
        {
          List<Attribute> _allAttributes = this._roomHelpers.getAllAttributes(((DataClass) aType));
          boolean _hasElements = false;
          for(final Attribute subA : _allAttributes) {
            if (!_hasElements) {
              _hasElements = true;
            } else {
              _builder.appendImmediate(",", "\t");
            }
            _builder.append("\t");
            List<Attribute> _union = this._roomExtensions.<Attribute>union(path, subA);
            String _initAttributeArray = this.initAttributeArray(instance, _union);
            _builder.append(_initAttributeArray, "\t");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("}");
        _switchResult = _builder;
      }
      if (!_matched) {
        if (aType instanceof ExternalType) {
          _matched=true;
          String _xifexpression_1 = null;
          String _defaultValueLiteral_1 = a.getDefaultValueLiteral();
          boolean _notEquals_1 = (!Objects.equal(_defaultValueLiteral_1, null));
          if (_notEquals_1) {
            _xifexpression_1 = a.getDefaultValueLiteral();
          } else {
            _xifexpression_1 = this.languageExt.defaultValue(aType);
          }
          _switchResult = _xifexpression_1;
        }
      }
      if (!_matched) {
        if (aType instanceof PrimitiveType) {
          _matched=true;
          CharSequence _xblockexpression_1 = null;
          {
            String value = this.getPrimitiveValue(instance, path);
            CharSequence _xifexpression_1 = null;
            if ((((a.getSize() > 0) && (!this._typeHelpers.isCharacterType(((PrimitiveType)aType)))) && (!value.trim().startsWith("{")))) {
              StringConcatenation _builder = new StringConcatenation();
              _builder.append("{");
              {
                int _size = a.getSize();
                IntegerRange _upTo = new IntegerRange(1, _size);
                boolean _hasElements = false;
                for(final Integer i : _upTo) {
                  if (!_hasElements) {
                    _hasElements = true;
                  } else {
                    _builder.appendImmediate(", ", "");
                  }
                  _builder.append(value, "");
                }
              }
              _builder.append("}");
              _xifexpression_1 = _builder;
            } else {
              String _xifexpression_2 = null;
              if ((((a.getSize() > 0) && this._typeHelpers.isCharacterType(((PrimitiveType)aType))) && value.startsWith("\'"))) {
                _xifexpression_2 = (("{" + value) + "}");
              } else {
                _xifexpression_2 = value;
              }
              _xifexpression_1 = _xifexpression_2;
            }
            _xblockexpression_1 = _xifexpression_1;
          }
          _switchResult = _xblockexpression_1;
        }
      }
      if (!_matched) {
        if (aType instanceof EnumerationType) {
          _matched=true;
          CharSequence _xblockexpression_1 = null;
          {
            String value = this.getPrimitiveValue(instance, path);
            CharSequence _xifexpression_1 = null;
            if (((a.getSize() > 0) && (!value.trim().startsWith("{")))) {
              StringConcatenation _builder = new StringConcatenation();
              _builder.append("{");
              {
                int _size = a.getSize();
                IntegerRange _upTo = new IntegerRange(1, _size);
                boolean _hasElements = false;
                for(final Integer i : _upTo) {
                  if (!_hasElements) {
                    _hasElements = true;
                  } else {
                    _builder.appendImmediate(", ", "");
                  }
                  _builder.append(value, "");
                }
              }
              _builder.append("}");
              _xifexpression_1 = _builder;
            } else {
              _xifexpression_1 = value;
            }
            _xblockexpression_1 = _xifexpression_1;
          }
          _switchResult = _xblockexpression_1;
        }
      }
      _xblockexpression = _switchResult;
    }
    return _xblockexpression;
  }
  
  private String getPrimitiveValue(final InstanceBase instance, final List<Attribute> path) {
    String value = this._typeHelpers.getAttrInstanceConfigValue(path, instance);
    boolean _equals = Objects.equal(value, null);
    if (_equals) {
      String _switchResult = null;
      boolean _matched = false;
      if (instance instanceof ActorInstance) {
        _matched=true;
        ActorClass _actorClass = ((ActorInstance)instance).getActorClass();
        _switchResult = this._typeHelpers.getAttrClassConfigValue(path, _actorClass, true);
      }
      if (!_matched) {
        if (instance instanceof InterfaceItemInstance) {
          _matched=true;
          InterfaceItem _interfaceItem = ((InterfaceItemInstance)instance).getInterfaceItem();
          PortClass _portClass = this._roomHelpers.getPortClass(_interfaceItem);
          _switchResult = this._typeHelpers.getAttrClassConfigValue(path, _portClass);
        }
      }
      value = _switchResult;
    }
    boolean _equals_1 = Objects.equal(value, null);
    if (_equals_1) {
      Attribute _last = IterableExtensions.<Attribute>last(path);
      String _defaultValueLiteral = _last.getDefaultValueLiteral();
      value = _defaultValueLiteral;
    }
    Attribute _last_1 = IterableExtensions.<Attribute>last(path);
    RefableType _type = _last_1.getType();
    DataType type = _type.getType();
    String _xifexpression = null;
    if (((!Objects.equal(value, null)) && this._typeHelpers.isPrimitive(type))) {
      _xifexpression = this.languageExt.toValueLiteral(((PrimitiveType) type), value);
    } else {
      String _xifexpression_1 = null;
      if (((!Objects.equal(value, null)) && this._typeHelpers.isEnumeration(type))) {
        _xifexpression_1 = this.languageExt.toEnumLiteral(((EnumerationType) type), value);
      } else {
        _xifexpression_1 = this.languageExt.defaultValue(type);
      }
      _xifexpression = _xifexpression_1;
    }
    return _xifexpression;
  }
}

Back to the top