summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core/src/org/genivi/commonapi/core/generator/FTypeGenerator.xtend
blob: 5af6e6ec2a864e79c3c36432a3567ff380a61599 (plain)
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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.genivi.commonapi.core.generator

import java.util.Arrays
import java.util.Collection
import java.util.LinkedList
import java.util.List
import javax.inject.Inject
import org.franca.core.franca.FArrayType
import org.franca.core.franca.FEnumerationType
import org.franca.core.franca.FEnumerator
import org.franca.core.franca.FField
import org.franca.core.franca.FMapType
import org.franca.core.franca.FModelElement
import org.franca.core.franca.FStructType
import org.franca.core.franca.FType
import org.franca.core.franca.FTypeCollection
import org.franca.core.franca.FTypeDef
import org.franca.core.franca.FUnionType

import static org.genivi.commonapi.core.generator.FTypeGenerator.*
import org.franca.core.franca.FTypeRef
import org.franca.core.franca.FBasicTypeId
import org.franca.core.franca.FInterface
import java.util.HashSet

class FTypeGenerator {
	@Inject private extension FrancaGeneratorExtensions

    def dispatch generateFTypeDeclaration(FTypeDef fTypeDef) '''
        typedef «fTypeDef.actualType.getNameReference(fTypeDef.eContainer)» «fTypeDef.name»;
    '''

    def dispatch generateFTypeDeclaration(FArrayType fArrayType) '''
        typedef std::vector<«fArrayType.elementType.getNameReference(fArrayType.eContainer)»> «fArrayType.name»;
    '''

    def dispatch generateFTypeDeclaration(FMapType fMap) '''
        typedef std::unordered_map<«fMap.generateKeyType»«fMap.generateValueType»«fMap.generateComparatorType»> «fMap.name»;
    '''

    def dispatch generateFTypeDeclaration(FStructType fStructType) '''
        struct «fStructType.name»: «fStructType.baseStructName» {
            «FOR element : fStructType.elements»
                «element.type.getNameReference(fStructType)» «element.name»;
            «ENDFOR»
            
            «fStructType.name»() = default;
            «fStructType.name»(«fStructType.allElements.map[getConstReferenceVariable(fStructType)].join(", ")»);

            virtual void readFromInputStream(CommonAPI::InputStream& inputStream);
            virtual void writeToOutputStream(CommonAPI::OutputStream& outputStream) const;
        };
    '''

    def dispatch generateFTypeDeclaration(FEnumerationType fEnumerationType) {
        fEnumerationType.generateDeclaration(fEnumerationType.name)
    }

    def generateDeclaration(FEnumerationType fEnumerationType, String name) '''
        enum class «name»: «fEnumerationType.backingType.primitiveTypeName» {
            «FOR parent : fEnumerationType.baseList SEPARATOR ',\n'»
                «FOR enumerator : parent.enumerators SEPARATOR ','»
                    «enumerator.name» = «parent.getRelativeNameReference(fEnumerationType)»::«enumerator.name»
                «ENDFOR»
            «ENDFOR»
            «IF fEnumerationType.base != null && !fEnumerationType.enumerators.empty»,«ENDIF»
            «FOR enumerator : fEnumerationType.enumerators SEPARATOR ','»
                «enumerator.name»«enumerator.generateValue»
            «ENDFOR»
        };

        // XXX Definition of a comparator still is necessary for GCC 4.4.1, topic is fixed since 4.5.1
        struct «name»Comparator;
    '''
    
    def dispatch generateFTypeDeclaration(FUnionType fUnionType) '''
        typedef CommonAPI::Variant<«fUnionType.elements.map[type.getNameReference(fUnionType)].join(", ")»>  «fUnionType.name»;        
    '''


    def dispatch generateFTypeInlineImplementation(FTypeDef fTypeDef, FModelElement parent) ''''''
    def dispatch generateFTypeInlineImplementation(FArrayType fArrayType, FModelElement parent) ''''''
    def dispatch generateFTypeInlineImplementation(FMapType fMap, FModelElement parent) ''''''

    def dispatch generateFTypeInlineImplementation(FStructType fStructType, FModelElement parent) '''
        bool operator==(const «fStructType.getClassNamespace(parent)»& lhs, const «fStructType.getClassNamespace(parent)»& rhs);
        inline bool operator!=(const «fStructType.getClassNamespace(parent)»& lhs, const «fStructType.getClassNamespace(parent)»& rhs) {
            return !(lhs == rhs);
        }
    '''

    def dispatch generateFTypeInlineImplementation(FEnumerationType fEnumerationType, FModelElement parent) {
        fEnumerationType.generateInlineImplementation(fEnumerationType.name, parent, parent.name)
    }

    def generateInlineImplementation(FEnumerationType fEnumerationType, String enumerationName, FModelElement parent, String parentName) '''
        inline CommonAPI::InputStream& operator>>(CommonAPI::InputStream& inputStream, «fEnumerationType.getClassNamespaceWithName(enumerationName, parent, parentName)»& enumValue) {
            return inputStream.readEnumValue<«fEnumerationType.backingType.primitiveTypeName»>(enumValue);
        }

        inline CommonAPI::OutputStream& operator<<(CommonAPI::OutputStream& outputStream, const «fEnumerationType.getClassNamespaceWithName(enumerationName, parent, parentName)»& enumValue) {
            return outputStream.writeEnumValue(static_cast<«fEnumerationType.backingType.primitiveTypeName»>(enumValue));
        }

        struct «fEnumerationType.getClassNamespaceWithName(enumerationName, parent, parentName)»Comparator {
            inline bool operator()(const «enumerationName»& lhs, const «enumerationName»& rhs) const {
                return static_cast<«fEnumerationType.backingType.primitiveTypeName»>(lhs) < static_cast<«fEnumerationType.backingType.primitiveTypeName»>(rhs);
            }
        };

        «FOR base : fEnumerationType.baseList BEFORE "\n" SEPARATOR "\n"»
            «fEnumerationType.generateInlineOperatorWithName(enumerationName, base, parent, parentName, "==")»
            «fEnumerationType.generateInlineOperatorWithName(enumerationName, base, parent, parentName, "!=")»
        «ENDFOR»
    '''

    def dispatch generateFTypeInlineImplementation(FUnionType fUnionType, FModelElement parent) ''' '''


    def dispatch generateFTypeImplementation(FTypeDef fTypeDef, FModelElement parent) ''''''
    def dispatch generateFTypeImplementation(FArrayType fArrayType, FModelElement parent) ''''''
    def dispatch generateFTypeImplementation(FMapType fMap, FModelElement parent) ''''''
    def dispatch generateFTypeImplementation(FEnumerationType fEnumerationType, FModelElement parent) ''''''

    def dispatch generateFTypeImplementation(FStructType fStructType, FModelElement parent) '''
        «fStructType.getClassNamespace(parent)»::«fStructType.name»(«fStructType.allElements.map[getConstReferenceVariable(fStructType) + "Value"].join(", ")»):
                «fStructType.base?.generateBaseConstructorCall(fStructType)»
                «FOR element : fStructType.elements SEPARATOR ','»
                    «element.name»(«element.name»Value)
                «ENDFOR»
        {
        }

        bool operator==(const «fStructType.getClassNamespace(parent)»& lhs, const «fStructType.getClassNamespace(parent)»& rhs) {
            if (&lhs == &rhs)
                return true;

            return
                «IF fStructType.base != null»
                    static_cast<«fStructType.base.getClassNamespace(parent)»>(lhs) == static_cast<«fStructType.base.getClassNamespace(parent)»>(rhs) &&
                «ENDIF»
                «FOR element : fStructType.elements SEPARATOR ' &&'»
                    lhs.«element.name» == rhs.«element.name»
                «ENDFOR»
            ;
        }

        void «fStructType.getClassNamespace(parent)»::readFromInputStream(CommonAPI::InputStream& inputStream) {
            «IF fStructType.base != null»
                «fStructType.base.getRelativeNameReference(fStructType)»::readFromInputStream(inputStream);
            «ENDIF»
            «FOR element : fStructType.elements»
                inputStream >> «element.name»;
            «ENDFOR»
        }

        void «fStructType.getClassNamespace(parent)»::writeToOutputStream(CommonAPI::OutputStream& outputStream) const {
            «IF fStructType.base != null»
                «fStructType.base.getRelativeNameReference(fStructType)»::writeToOutputStream(outputStream);
            «ENDIF»
            «FOR element : fStructType.elements»
                outputStream << «element.name»;
            «ENDFOR»
        }
    '''

    def dispatch generateFTypeImplementation(FUnionType fUnionType, FModelElement parent) ''' '''

    def hasImplementation(FType fType) {
        return (fType instanceof FStructType);
    }

    def generateRequiredTypeIncludes(FInterface fInterface) '''
        «FOR requiredTypeHeaderFile : fInterface.requiredTypeHeaderFiles»
            #include <«requiredTypeHeaderFile»>
        «ENDFOR»
    '''

    def private getRequiredTypeHeaderFiles(FInterface fInterface) {
        val headerSet = new HashSet<String>

        fInterface.attributes.forEach[type.derived?.addRequiredHeaders(headerSet)]
        fInterface.methods.forEach[
            inArgs.forEach[type.derived?.addRequiredHeaders(headerSet)]
            outArgs.forEach[type.derived?.addRequiredHeaders(headerSet)]
        ]
        fInterface.broadcasts.forEach[outArgs.forEach[type.derived?.addRequiredHeaders(headerSet)]]

        headerSet.remove(fInterface.headerPath)

        return headerSet
    }

    def addRequiredHeaders(FType fType, Collection<String> requiredHeaders) {
        requiredHeaders.add(fType.FTypeCollection.headerPath)
        fType.addFTypeRequiredHeaders(requiredHeaders)
    }

    def private getFTypeCollection(FType fType) {
        fType.eContainer as FTypeCollection
    }

    def private dispatch void addFTypeRequiredHeaders(FTypeDef fTypeDef, Collection<String> requiredHeaders) {
        requiredHeaders.add(fTypeDef.actualType.requiredHeaderPath)
    }
    def private dispatch void addFTypeRequiredHeaders(FArrayType fArrayType, Collection<String> requiredHeaders) {
        requiredHeaders.addAll('vector', fArrayType.elementType.requiredHeaderPath)
    }
    def private dispatch void addFTypeRequiredHeaders(FMapType fMapType, Collection<String> requiredHeaders) {
        requiredHeaders.addAll('unordered_map', fMapType.keyType.requiredHeaderPath, fMapType.valueType.requiredHeaderPath)
    }
    def private dispatch void addFTypeRequiredHeaders(FStructType fStructType, Collection<String> requiredHeaders) {
        if (fStructType.base != null)
            requiredHeaders.add(fStructType.base.FTypeCollection.headerPath)
        else
            requiredHeaders.addAll('CommonAPI/InputStream.h', 'CommonAPI/OutputStream.h', 'CommonAPI/SerializableStruct.h')
        fStructType.elements.forEach[requiredHeaders.add(type.requiredHeaderPath)]
    }
    def private dispatch void addFTypeRequiredHeaders(FEnumerationType fEnumerationType, Collection<String> requiredHeaders) {
        if (fEnumerationType.base != null)
            requiredHeaders.add(fEnumerationType.base.FTypeCollection.headerPath)
        requiredHeaders.addAll('cstdint', 'CommonAPI/InputStream.h', 'CommonAPI/OutputStream.h')
    }
    def private dispatch void addFTypeRequiredHeaders(FUnionType fUnionType, Collection<String> requiredHeaders) {
        if (fUnionType.base != null)
            requiredHeaders.add(fUnionType.base.FTypeCollection.headerPath)
        else
            requiredHeaders.add('CommonAPI/SerializableVariant.h')
        fUnionType.elements.forEach[requiredHeaders.add(type.requiredHeaderPath)]
        requiredHeaders.addAll('cstdint', 'memory')
    }
    
    def private getRequiredHeaderPath(FTypeRef fTypeRef) {
        if (fTypeRef.derived != null)
            return fTypeRef.derived.FTypeCollection.headerPath
        return fTypeRef.predefined.requiredHeaderPath
    }

    def private getRequiredHeaderPath(FBasicTypeId fBasicTypeId) {
        switch fBasicTypeId {
            case FBasicTypeId::STRING : 'string'
            case FBasicTypeId::BYTE_BUFFER : 'CommonAPI/ByteBuffer.h'
            default : 'cstdint'
        }
    }

    def private getClassNamespace(FModelElement child, FModelElement parent) {
        child.getClassNamespaceWithName(child.name, parent, parent.name)
    }
    
    def private getClassNamespaceWithName(FModelElement child, String name, FModelElement parent, String parentName) {
        var reference = name
        if (parent != null && parent != child)
            reference = parentName + '::' + reference
        return reference
    }

    def private generateBaseConstructorCall(FStructType parent, FStructType source) {
        var call = parent.getRelativeNameReference(source)
        call = call + "(" + parent.allElements.map[name + "Value"].join(", ") + ")"
        if (!source.elements.empty)
            call = call + ","
        return call
    }


    def private isFEnumerationType(FMapType fMap) {
        fMap.keyType.derived instanceof FEnumerationType
    }
    def private generateKeyType(FMapType fMap) '''«fMap.keyType.getNameReference(fMap.eContainer)»'''
    def private generateValueType(FMapType fMap) ''', «fMap.valueType.getNameReference(fMap.eContainer)»'''
    def private generateComparatorType(FMapType fMap) '''«IF fMap.isFEnumerationType», «fMap.generateKeyType»Comparator«ENDIF»'''

    def private getBaseStructName(FStructType fStructType) {
        if (fStructType.base != null)
            return fStructType.base.getRelativeNameReference(fStructType)
        return "CommonAPI::SerializableStruct"
    }

    def private getConstReferenceVariable(FField destination, FModelElement source) {
        "const " + destination.type.getNameReference(source) + "& " + destination.name
    }

    def private List<FField> getAllElements(FStructType fStructType) {
        if (fStructType.base == null)
            return new LinkedList(fStructType.elements)

        val elements = fStructType.base.allElements
        elements.addAll(fStructType.elements)
        return elements
    }

    def private generateInlineOperatorWithName(FEnumerationType fEnumerationType, String enumerationName, FEnumerationType base, FModelElement parent, String parentName, String operator) '''
        inline bool operator«operator»(const «fEnumerationType.getClassNamespaceWithName(enumerationName, parent, parentName)»& lhs, const «base.getRelativeNameReference(fEnumerationType)»& rhs) {
            return static_cast<«fEnumerationType.backingType.primitiveTypeName»>(lhs) «operator» static_cast<«fEnumerationType.backingType.primitiveTypeName»>(rhs);
        }
        inline bool operator«operator»(const «base.getRelativeNameReference(fEnumerationType)»& lhs, const «fEnumerationType.getClassNamespaceWithName(enumerationName, parent, parentName)»& rhs) {
            return static_cast<«fEnumerationType.backingType.primitiveTypeName»>(lhs) «operator» static_cast<«fEnumerationType.backingType.primitiveTypeName»>(rhs);
        }
    '''

    def private getBaseList(FEnumerationType fEnumerationType) {
        val baseList = new LinkedList<FEnumerationType>
        var currentBase = fEnumerationType.base
        
        while (currentBase != null) {
            baseList.add(0, currentBase)
            currentBase = currentBase.base
        }

        return baseList
    }

    def private generateValue(FEnumerator fEnumerator) {
        val parsedValue = tryParseInteger(fEnumerator.value)
        if (parsedValue != null)
            return ' = ' + parsedValue
        return ''
    }

    private static List<Integer> integerRadixList = Arrays::asList(10, 16, 2, 8)

    def private tryParseInteger(String string) {
        if (!string.nullOrEmpty) {
            try {
                return Integer::decode(string)
            } catch (NumberFormatException e1) {
                for (radix : integerRadixList)
                    try {
                        return new Integer(Integer::parseInt(string, radix.intValue))
                    } catch (NumberFormatException e2) { }
            }
        }
        return null
    }
}