Skip to main content
summaryrefslogtreecommitdiffstats
blob: e7f882e20b2781c758fcb3b93767c23d3bf64389 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/**
 * Copyright (c) 2011 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 		Thomas Schuetz (refactoring, adapted for other target languages)
 */
package org.eclipse.etrice.generator.generic;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.etrice.core.fsm.fSM.DetailCode;
import org.eclipse.etrice.core.genmodel.fsm.ILogger;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.Attribute;
import org.eclipse.etrice.core.room.ClassStructor;
import org.eclipse.etrice.core.room.DataClass;
import org.eclipse.etrice.core.room.DataType;
import org.eclipse.etrice.core.room.Operation;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.etrice.core.room.RefableType;
import org.eclipse.etrice.core.room.RoomClass;
import org.eclipse.etrice.core.room.VarDecl;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.generator.base.AbstractGenerator;
import org.eclipse.etrice.generator.generic.ILanguageExtension;
import org.eclipse.etrice.generator.generic.TypeHelpers;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;

/**
 * A collection of methods for generation of user code, attributes with getters and setters
 * and operations.
 */
@Singleton
@SuppressWarnings("all")
public class ProcedureHelpers {
  protected final String NEWLINE = System.getProperty("line.separator");
  
  @Inject
  @Extension
  protected RoomHelpers _roomHelpers;
  
  @Inject
  @Extension
  protected TypeHelpers _typeHelpers;
  
  @Inject
  protected ILanguageExtension languageExt;
  
  @Inject
  protected ILogger logger;
  
  /**
   * @param dc a {@link DataClass}
   * @param id 0, 1 or 2 for the corresponding user codes
   * @return the generated code
   */
  public CharSequence userCode(final DataClass dc, final int id) {
    CharSequence _switchResult = null;
    switch (id) {
      case 1:
        String _deepUserCode1 = this._roomHelpers.getDeepUserCode1(dc);
        _switchResult = this.userCode(_deepUserCode1);
        break;
      case 2:
        String _deepUserCode2 = this._roomHelpers.getDeepUserCode2(dc);
        _switchResult = this.userCode(_deepUserCode2);
        break;
      case 3:
        String _deepUserCode3 = this._roomHelpers.getDeepUserCode3(dc);
        _switchResult = this.userCode(_deepUserCode3);
        break;
    }
    return _switchResult;
  }
  
  /**
   * @param pc a {@link ProtocolClass}
   * @param id 0, 1 or 2 for the corresponding user codes
   * @return the generated code
   */
  public CharSequence userCode(final ProtocolClass pc, final int id) {
    CharSequence _switchResult = null;
    switch (id) {
      case 1:
        String _deepUserCode1 = this._roomHelpers.getDeepUserCode1(pc);
        _switchResult = this.userCode(_deepUserCode1);
        break;
      case 2:
        String _deepUserCode2 = this._roomHelpers.getDeepUserCode2(pc);
        _switchResult = this.userCode(_deepUserCode2);
        break;
      case 3:
        String _deepUserCode3 = this._roomHelpers.getDeepUserCode3(pc);
        _switchResult = this.userCode(_deepUserCode3);
        break;
    }
    return _switchResult;
  }
  
  /**
   * @param ac an {@link ActorContainerClass}
   * @param id 0, 1 or 2 for the corresponding user codes
   * @return the generated code
   */
  public CharSequence userCode(final ActorContainerClass ac, final int id, final boolean includeInherited) {
    CharSequence _xifexpression = null;
    if (includeInherited) {
      CharSequence _switchResult = null;
      switch (id) {
        case 1:
          String _deepUserCode1 = this._roomHelpers.getDeepUserCode1(ac);
          _switchResult = this.userCode(_deepUserCode1);
          break;
        case 2:
          String _deepUserCode2 = this._roomHelpers.getDeepUserCode2(ac);
          _switchResult = this.userCode(_deepUserCode2);
          break;
        case 3:
          String _deepUserCode3 = this._roomHelpers.getDeepUserCode3(ac);
          _switchResult = this.userCode(_deepUserCode3);
          break;
      }
      _xifexpression = _switchResult;
    } else {
      CharSequence _switchResult_1 = null;
      switch (id) {
        case 1:
          DetailCode _userCode1 = ac.getUserCode1();
          _switchResult_1 = this.userCode(_userCode1);
          break;
        case 2:
          DetailCode _userCode2 = ac.getUserCode2();
          _switchResult_1 = this.userCode(_userCode2);
          break;
        case 3:
          DetailCode _userCode3 = ac.getUserCode3();
          _switchResult_1 = this.userCode(_userCode3);
          break;
      }
      _xifexpression = _switchResult_1;
    }
    return _xifexpression;
  }
  
  /**
   * @param dc some {@link DetailCode}
   * @return a string containing the expanded code surrounded by
   * 		comments (no tag replacement will happen)
   */
  public CharSequence userCode(final DetailCode dc) {
    String _detailCode = this._roomHelpers.getDetailCode(dc);
    return this.userCode(_detailCode);
  }
  
  private CharSequence userCode(final String code) {
    StringConcatenation _builder = new StringConcatenation();
    {
      if (((!Objects.equal(code, null)) && (!code.isEmpty()))) {
        _builder.append("/*--------------------- begin user code ---------------------*/");
        _builder.newLine();
        _builder.append(code, "");
        _builder.newLineIfNotEmpty();
        _builder.append("/*--------------------- end user code ---------------------*/");
        _builder.newLine();
      }
    }
    return _builder;
  }
  
  /**
   * @param attributes a list of {@link Attribute}s
   * @return code declaring the attributes
   */
  public CharSequence attributes(final List<Attribute> attributes) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/*--------------------- attributes ---------------------*/");
    _builder.newLine();
    {
      for(final Attribute it : attributes) {
        CharSequence _attributeDeclaration = this.attributeDeclaration(it);
        _builder.append(_attributeDeclaration, "");
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  /**
   * @param attribute an {@link Attribute}
   * @return the code declaring the attribute
   */
  public CharSequence attributeDeclaration(final Attribute attribute) {
    StringConcatenation _builder = new StringConcatenation();
    String _accessLevelPublic = this.languageExt.accessLevelPublic();
    _builder.append(_accessLevelPublic, "");
    _builder.append(" ");
    String _declarationString = this.declarationString(attribute);
    _builder.append(_declarationString, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  /**
   * @param attribute an {@link Attribute}
   * @return the code for an array initializer
   */
  public String arrayInitializer(final Attribute att) {
    String _xifexpression = null;
    String _defaultValueLiteral = att.getDefaultValueLiteral();
    boolean _notEquals = (!Objects.equal(_defaultValueLiteral, null));
    if (_notEquals) {
      _xifexpression = att.getDefaultValueLiteral();
    } else {
      RefableType _type = att.getType();
      DataType _type_1 = _type.getType();
      _xifexpression = this.languageExt.defaultValue(_type_1);
    }
    final String dflt = _xifexpression;
    boolean _startsWith = dflt.startsWith("{");
    if (_startsWith) {
      String[] _split = dflt.split(",");
      int _size = ((List<String>)Conversions.doWrapArray(_split)).size();
      int _size_1 = att.getSize();
      boolean _notEquals_1 = (_size != _size_1);
      if (_notEquals_1) {
        String _name = att.getName();
        String _plus = ("WARNING: array size determined by initializer differs from attribute size (" + _name);
        String _plus_1 = (_plus + "[");
        int _size_2 = att.getSize();
        String _plus_2 = (_plus_1 + Integer.valueOf(_size_2));
        String _plus_3 = (_plus_2 + "] <-> ");
        String _plus_4 = (_plus_3 + dflt);
        String _plus_5 = (_plus_4 + ")");
        this.logger.logInfo(_plus_5);
      }
      return dflt;
    }
    String result = "{";
    int i = 0;
    while ((i < att.getSize())) {
      {
        result = (result + dflt);
        i = (i + 1);
        int _size_3 = att.getSize();
        boolean _lessThan = (i < _size_3);
        if (_lessThan) {
          result = (result + ", ");
        }
      }
    }
    return (result + "}");
  }
  
  /**
   * @param EnumTest a list of {@link Attribute}s
   * @param classname the name of the defining class
   * @return code declaring setters and getters for the attributes
   */
  public CharSequence attributeSettersGettersDeclaration(final List<Attribute> attributes, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* --------------------- attribute setters and getters */");
    _builder.newLine();
    {
      for(final Attribute attribute : attributes) {
        CharSequence _setterHeader = this.setterHeader(attribute, classname);
        _builder.append(_setterHeader, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        CharSequence _terHeader = this.getterHeader(attribute, classname);
        _builder.append(_terHeader, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  /**
   * @param EnumTest a list of {@link Attribute}s
   * @param classname the name of the defining class
   * @return code defining setters and getters for the attributes
   */
  public CharSequence attributeSettersGettersImplementation(final List<Attribute> attributes, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* --------------------- attribute setters and getters */");
    _builder.newLine();
    {
      for(final Attribute attribute : attributes) {
        CharSequence _setterHeader = this.setterHeader(attribute, classname);
        _builder.append(_setterHeader, "");
        _builder.append(" {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t ");
        String _memberAccess = this.languageExt.memberAccess();
        _builder.append(_memberAccess, "\t ");
        String _name = attribute.getName();
        _builder.append(_name, "\t ");
        _builder.append(" = ");
        String _name_1 = attribute.getName();
        _builder.append(_name_1, "\t ");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        CharSequence _terHeader = this.getterHeader(attribute, classname);
        _builder.append(_terHeader, "");
        _builder.append(" {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("return ");
        String _memberAccess_1 = this.languageExt.memberAccess();
        _builder.append(_memberAccess_1, "\t");
        String _name_2 = attribute.getName();
        _builder.append(_name_2, "\t");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
      }
    }
    return _builder;
  }
  
  /**
   * @param attribute an {@link Attribute}
   * @param classname the name of the defining class
   * @return code for the attribute setter declaration
   */
  protected CharSequence setterHeader(final Attribute attribute, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    String _accessLevelPublic = this.languageExt.accessLevelPublic();
    _builder.append(_accessLevelPublic, "");
    _builder.append("void set");
    String _name = attribute.getName();
    String _firstUpper = StringExtensions.toFirstUpper(_name);
    _builder.append(_firstUpper, "");
    _builder.append("(");
    String _selfPointer = this.languageExt.selfPointer(classname, true);
    _builder.append(_selfPointer, "");
    String _argList = this.argList(Collections.<Attribute>unmodifiableList(CollectionLiterals.<Attribute>newArrayList(attribute)));
    _builder.append(_argList, "");
    _builder.append(")");
    return _builder;
  }
  
  /**
   * @param attribute an {@link Attribute}
   * @param classname the name of the defining class
   * @return code for the attribute getter declaration
   */
  protected CharSequence getterHeader(final Attribute attribute, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    String _accessLevelPublic = this.languageExt.accessLevelPublic();
    _builder.append(_accessLevelPublic, "");
    String _signatureString = this.signatureString(attribute);
    _builder.append(_signatureString, "");
    _builder.append(" get");
    String _name = attribute.getName();
    String _firstUpper = StringExtensions.toFirstUpper(_name);
    _builder.append(_firstUpper, "");
    _builder.append("(");
    String _selfPointer = this.languageExt.selfPointer(classname, false);
    _builder.append(_selfPointer, "");
    _builder.append(")");
    return _builder;
  }
  
  /**
   * @param attributes a list of {@link Attribute}s
   * @return an argument list for the attributes
   */
  public String argList(final Iterable<Attribute> attributes) {
    final Function1<Attribute, String> _function = (Attribute it) -> {
      String _signatureString = this.signatureString(it);
      String _plus = (_signatureString + " ");
      String _name = it.getName();
      return (_plus + _name);
    };
    Iterable<String> _map = IterableExtensions.<Attribute, String>map(attributes, _function);
    return IterableExtensions.join(_map, ", ");
  }
  
  /**
   * @param EnumTest an iterable of {@link Attribute}s representing a path
   * @param classname the name of the defining class
   * @return the invocation code for the call of a setter
   */
  public CharSequence invokeGetters(final Iterable<Attribute> path, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _hasElements = false;
      for(final Attribute a : path) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(".", "");
        }
        String _name = a.getName();
        CharSequence _invokeGetter = this.invokeGetter(_name, classname);
        _builder.append(_invokeGetter, "");
      }
    }
    return _builder;
  }
  
  /**
   * @param typeName the type name of the attribute
   * @param name the name of the attribute
   * @param classname the name of the type defining the getter
   * @return code defining the attribute getter
   */
  public CharSequence getterImplementation(final String typeName, final String name, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    String _accessLevelPublic = this.languageExt.accessLevelPublic();
    _builder.append(_accessLevelPublic, "");
    _builder.append(typeName, "");
    _builder.append(" get");
    String _firstUpper = StringExtensions.toFirstUpper(name);
    _builder.append(_firstUpper, "");
    _builder.append(" (");
    String _selfPointer = this.languageExt.selfPointer(classname, false);
    _builder.append(_selfPointer, "");
    _builder.append("){");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("return ");
    String _memberAccess = this.languageExt.memberAccess();
    _builder.append(_memberAccess, "\t");
    _builder.append(name, "\t");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  /**
   * @param name the name of the attribute
   * @param classname the name of the type defining the getter
   * @return code defining the getter call
   */
  public CharSequence invokeGetter(final String name, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("get");
    String _firstUpper = StringExtensions.toFirstUpper(name);
    _builder.append(_firstUpper, "");
    _builder.append("(");
    String _selfPointer = this.languageExt.selfPointer(classname, true);
    _builder.append(_selfPointer, "");
    _builder.append(")");
    return _builder;
  }
  
  /**
   * @param name the name of the attribute
   * @param classname the name of the type defining the getter
   * @param value the value to be assigned
   * @return code defining the setter call
   */
  public CharSequence invokeSetter(final String name, final String classname, final String value) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("set");
    String _firstUpper = StringExtensions.toFirstUpper(name);
    _builder.append(_firstUpper, "");
    _builder.append("(");
    String _selfPointer = this.languageExt.selfPointer(classname, true);
    _builder.append(_selfPointer, "");
    _builder.append(value, "");
    _builder.append(")");
    return _builder;
  }
  
  /**
   * @param operations a list of {@link Operation}s
   * @param classname the name of the type defining the getter
   * @return code declaring the operations
   */
  public CharSequence operationsDeclaration(final List<? extends Operation> operations, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/*--------------------- operations ---------------------*/");
    _builder.newLine();
    {
      for(final Operation operation : operations) {
        CharSequence _operationSignature = this.operationSignature(operation, classname, true);
        _builder.append(_operationSignature, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  /**
   * @param operations a list of {@link Operation}s
   * @param classname the name of the type defining the getter
   * @return code defining the operations
   */
  public CharSequence operationsImplementation(final List<? extends Operation> operations, final String classname) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/*--------------------- operations ---------------------*/");
    _builder.newLine();
    {
      for(final Operation operation : operations) {
        CharSequence _operationSignature = this.operationSignature(operation, classname, false);
        _builder.append(_operationSignature, "");
        _builder.append(" {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        AbstractGenerator _instance = AbstractGenerator.getInstance();
        DetailCode _detailCode = operation.getDetailCode();
        String _translatedCode = _instance.getTranslatedCode(_detailCode);
        _builder.append(_translatedCode, "\t");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
      }
    }
    return _builder;
  }
  
  public CharSequence asBlock(final CharSequence str) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append(str, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  /**
   * invoke user structor, if (inherited) present - <b>C only</b>
   * 
   * @param cls {@link ActorClass} or {@link DataClass}
   * @param args self pointer to instance
   */
  public String invokeUserStructor(final RoomClass cls, final String args, final boolean ctor) {
    boolean _usesInheritance = this.languageExt.usesInheritance();
    boolean _not = (!_usesInheritance);
    List<ClassStructor> _structors = this.getStructors(cls, _not);
    final Function1<ClassStructor, Boolean> _function = (ClassStructor it) -> {
      boolean _isConstructor = it.isConstructor();
      return Boolean.valueOf((_isConstructor == ctor));
    };
    boolean _exists = IterableExtensions.<ClassStructor>exists(_structors, _function);
    if (_exists) {
      StringConcatenation _builder = new StringConcatenation();
      String _name = cls.getName();
      String _xifexpression = null;
      if (ctor) {
        _xifexpression = "ctor";
      } else {
        _xifexpression = "dtor";
      }
      String _memberInDeclaration = this.languageExt.memberInDeclaration(_name, _xifexpression);
      _builder.append(_memberInDeclaration, "");
      _builder.append("(");
      _builder.append(args, "");
      _builder.append(");");
      return _builder.toString();
    }
    return "";
  }
  
  /**
   * declaration of user constructor + destructor, if (inherited) present - <b>C only</b>
   * 
   * @param cls {@link ActorClass} or {@link DataClass}
   */
  public String userStructorsDeclaration(final RoomClass cls) {
    String _xblockexpression = null;
    {
      String _name = cls.getName();
      final String namePrefix = this.languageExt.operationScope(_name, true);
      final ArrayList<String> declBlock = CollectionLiterals.<String>newArrayList();
      declBlock.add("/*--------------------- user constructor/destructor ---------------------*/");
      boolean _usesInheritance = this.languageExt.usesInheritance();
      boolean _not = (!_usesInheritance);
      List<ClassStructor> _structors = this.getStructors(cls, _not);
      final Function1<ClassStructor, Boolean> _function = (ClassStructor it) -> {
        return Boolean.valueOf(it.isConstructor());
      };
      boolean _exists = IterableExtensions.<ClassStructor>exists(_structors, _function);
      if (_exists) {
        String _name_1 = cls.getName();
        CharSequence _functionSignature = this.functionSignature(_name_1, (namePrefix + "ctor"), "void", "");
        String _plus = (_functionSignature + ";");
        declBlock.add(_plus);
      }
      boolean _usesInheritance_1 = this.languageExt.usesInheritance();
      boolean _not_1 = (!_usesInheritance_1);
      List<ClassStructor> _structors_1 = this.getStructors(cls, _not_1);
      final Function1<ClassStructor, Boolean> _function_1 = (ClassStructor it) -> {
        boolean _isConstructor = it.isConstructor();
        return Boolean.valueOf((!_isConstructor));
      };
      boolean _exists_1 = IterableExtensions.<ClassStructor>exists(_structors_1, _function_1);
      if (_exists_1) {
        String _name_2 = cls.getName();
        CharSequence _functionSignature_1 = this.functionSignature(_name_2, (namePrefix + "dtor"), "void", "");
        String _plus_1 = (_functionSignature_1 + ";");
        declBlock.add(_plus_1);
      }
      _xblockexpression = IterableExtensions.join(declBlock, this.NEWLINE);
    }
    return _xblockexpression;
  }
  
  /**
   * implementation of user constructor + destructor, if (inherited) present - <b>C only</b>
   * 
   * @param cls {@link ActorClass} or {@link DataClass}
   */
  public String userStructorsImplementation(final RoomClass cls) {
    String _xblockexpression = null;
    {
      final ArrayList<String> declBlock = CollectionLiterals.<String>newArrayList();
      declBlock.add("/*--------------------- user constructor/destructor ---------------------*/");
      String _userStuctorImplementation = this.userStuctorImplementation(cls, true);
      declBlock.add(_userStuctorImplementation);
      String _userStuctorImplementation_1 = this.userStuctorImplementation(cls, false);
      declBlock.add(_userStuctorImplementation_1);
      Iterable<String> _filterNull = IterableExtensions.<String>filterNull(declBlock);
      _xblockexpression = IterableExtensions.join(_filterNull, this.NEWLINE);
    }
    return _xblockexpression;
  }
  
  protected String userStuctorImplementation(final RoomClass cls, final boolean ctor) {
    String _xblockexpression = null;
    {
      String _name = cls.getName();
      final String namePrefix = this.languageExt.operationScope(_name, false);
      boolean _usesInheritance = this.languageExt.usesInheritance();
      boolean _not = (!_usesInheritance);
      List<ClassStructor> _structors = this.getStructors(cls, _not);
      final Function1<ClassStructor, Boolean> _function = (ClassStructor it) -> {
        boolean _isConstructor = it.isConstructor();
        return Boolean.valueOf((_isConstructor == ctor));
      };
      boolean _exists = IterableExtensions.<ClassStructor>exists(_structors, _function);
      boolean _not_1 = (!_exists);
      if (_not_1) {
        return null;
      }
      StringConcatenation _builder = new StringConcatenation();
      String _name_1 = cls.getName();
      String _xifexpression = null;
      if (ctor) {
        _xifexpression = "ctor";
      } else {
        _xifexpression = "dtor";
      }
      String _plus = (namePrefix + _xifexpression);
      CharSequence _functionSignature = this.functionSignature(_name_1, _plus, "void", "");
      _builder.append(_functionSignature, "");
      _builder.append("{");
      _builder.newLineIfNotEmpty();
      _builder.append("\t");
      String _userStructorBody = this.userStructorBody(cls, ctor);
      _builder.append(_userStructorBody, "\t");
      _builder.newLineIfNotEmpty();
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  /**
   * implementation of user structor, if (inherited) present
   */
  public String userStructorBody(final RoomClass cls, final boolean ctor) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* user defined ");
    {
      if (ctor) {
        _builder.append("con");
      } else {
        _builder.append("de");
      }
    }
    _builder.append("structor body */");
    final String comment = _builder.toString();
    boolean _usesInheritance = this.languageExt.usesInheritance();
    boolean _not = (!_usesInheritance);
    List<ClassStructor> _structors = this.getStructors(cls, _not);
    final Function1<ClassStructor, Boolean> _function = (ClassStructor it) -> {
      boolean _isConstructor = it.isConstructor();
      return Boolean.valueOf((_isConstructor == ctor));
    };
    final Iterable<ClassStructor> implementedStructors = IterableExtensions.<ClassStructor>filter(_structors, _function);
    final Function1<ClassStructor, DetailCode> _function_1 = (ClassStructor it) -> {
      return it.getDetailCode();
    };
    Iterable<DetailCode> _map = IterableExtensions.<ClassStructor, DetailCode>map(implementedStructors, _function_1);
    final Function1<DetailCode, String> _function_2 = (DetailCode it) -> {
      AbstractGenerator _instance = AbstractGenerator.getInstance();
      return _instance.getTranslatedCode(it);
    };
    final Iterable<String> translatedCodes = IterableExtensions.<DetailCode, String>map(_map, _function_2);
    final Function1<String, CharSequence> _function_3 = (String it) -> {
      CharSequence _xifexpression = null;
      int _size = IterableExtensions.size(translatedCodes);
      boolean _greaterThan = (_size > 1);
      if (_greaterThan) {
        _xifexpression = this.asBlock(it);
      } else {
        _xifexpression = it;
      }
      return _xifexpression;
    };
    Iterable<CharSequence> _map_1 = IterableExtensions.<String, CharSequence>map(translatedCodes, _function_3);
    String _join = IterableExtensions.join(_map_1);
    return ((comment + this.NEWLINE) + _join);
  }
  
  protected List<ClassStructor> getStructors(final RoomClass cls, final boolean inherited) {
    List<ClassStructor> _switchResult = null;
    final RoomClass it = cls;
    boolean _matched = false;
    if (it instanceof ActorClass) {
      if ((!inherited)) {
        _matched=true;
        _switchResult = ((ActorClass)it).getStructors();
      }
    }
    if (!_matched) {
      if (it instanceof DataClass) {
        if ((!inherited)) {
          _matched=true;
          _switchResult = ((DataClass)it).getStructors();
        }
      }
    }
    if (!_matched) {
      if (it instanceof ActorClass) {
        if (inherited) {
          _matched=true;
          _switchResult = this._roomHelpers.getAllStructors(((ActorClass)it));
        }
      }
    }
    if (!_matched) {
      if (it instanceof DataClass) {
        if (inherited) {
          _matched=true;
          _switchResult = this._roomHelpers.getAllStructors(((DataClass)it));
        }
      }
    }
    return _switchResult;
  }
  
  /**
   * @param operation an {@link Operation}
   * @return the operation signature (with special care for
   * 		constructor and destructor
   */
  protected CharSequence operationSignature(final Operation operation, final String classname, final boolean isDeclaration) {
    CharSequence _xblockexpression = null;
    {
      StringConcatenation _builder = new StringConcatenation();
      {
        EList<VarDecl> _arguments = operation.getArguments();
        boolean _hasElements = false;
        for(final VarDecl argument : _arguments) {
          if (!_hasElements) {
            _hasElements = true;
          } else {
            _builder.appendImmediate(", ", "");
          }
          RefableType _refType = argument.getRefType();
          String _signatureString = this.signatureString(_refType);
          _builder.append(_signatureString, "");
          _builder.append(" ");
          String _name = argument.getName();
          _builder.append(_name, "");
        }
      }
      final String arguments = _builder.toString();
      RefableType _returnType = operation.getReturnType();
      final String returnType = this.signatureString(_returnType);
      String _operationScope = this.languageExt.operationScope(classname, isDeclaration);
      String _name_1 = operation.getName();
      String _plus = (_operationScope + _name_1);
      _xblockexpression = this.functionSignature(classname, _plus, returnType, arguments);
    }
    return _xblockexpression;
  }
  
  /**
   * @param type a {@link RefableType}
   * @return a string for the type (also for pointers)
   */
  public String signatureString(final RefableType type) {
    String _switchResult = null;
    final RefableType it = type;
    boolean _matched = false;
    if (Objects.equal(it, null)) {
      _matched=true;
      _switchResult = "void";
    }
    if (!_matched) {
      boolean _isRef = it.isRef();
      if (_isRef) {
        _matched=true;
        DataType _type = type.getType();
        String _typeName = this._typeHelpers.typeName(_type);
        String _pointerLiteral = this.languageExt.pointerLiteral();
        _switchResult = (_typeName + _pointerLiteral);
      }
    }
    if (!_matched) {
      DataType _type_1 = type.getType();
      _switchResult = this._typeHelpers.typeName(_type_1);
    }
    return _switchResult;
  }
  
  public String signatureString(final Attribute attribute) {
    String _switchResult = null;
    final Attribute it = attribute;
    boolean _matched = false;
    int _size = it.getSize();
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
      _matched=true;
      RefableType _type = it.getType();
      DataType _type_1 = _type.getType();
      String _typeName = this._typeHelpers.typeName(_type_1);
      int _size_1 = it.getSize();
      RefableType _type_2 = it.getType();
      boolean _isRef = _type_2.isRef();
      _switchResult = this.languageExt.arrayType(_typeName, _size_1, _isRef);
    }
    if (!_matched) {
      RefableType _type_3 = it.getType();
      _switchResult = this.signatureString(_type_3);
    }
    return _switchResult;
  }
  
  /**
   * @param attribute a {@link Attribute}
   * @return a string for <code>type name</code>
   */
  public String declarationString(final Attribute attribute) {
    String _switchResult = null;
    final Attribute it = attribute;
    boolean _matched = false;
    int _size = it.getSize();
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
      _matched=true;
      RefableType _type = it.getType();
      DataType _type_1 = _type.getType();
      String _typeName = this._typeHelpers.typeName(_type_1);
      int _size_1 = it.getSize();
      RefableType _type_2 = it.getType();
      boolean _isRef = _type_2.isRef();
      String _name = it.getName();
      _switchResult = this.languageExt.arrayDeclaration(_typeName, _size_1, _isRef, _name);
    }
    if (!_matched) {
      RefableType _type_3 = it.getType();
      String _signatureString = this.signatureString(_type_3);
      String _plus = (_signatureString + " ");
      String _name_1 = it.getName();
      _switchResult = (_plus + _name_1);
    }
    return _switchResult;
  }
  
  protected CharSequence functionSignature(final String className, final String fullFctName, final String returnType, final String arguments) {
    StringConcatenation _builder = new StringConcatenation();
    String _accessLevelPublic = this.languageExt.accessLevelPublic();
    _builder.append(_accessLevelPublic, "");
    _builder.append(" ");
    _builder.append(returnType, "");
    _builder.append(" ");
    _builder.append(fullFctName, "");
    _builder.append("(");
    boolean _isEmpty = arguments.isEmpty();
    boolean _not = (!_isEmpty);
    String _selfPointer = this.languageExt.selfPointer(className, _not);
    _builder.append(_selfPointer, "");
    _builder.append(arguments, "");
    _builder.append(")");
    return _builder;
  }
}

Back to the top