Skip to content

Commit 2cc824b

Browse files
committed
Generate type aliases in Dart
Added dedicated DartTypeAlias template, as type aliases (typedefs) are now supported in Dart language (since Dart version 2.13). Updated other Dart tempates and resolvers to treat type aliases as a normal type, instead of skipping it through to the target type. Added/updated related smoke and functional tests. Unrelated smoke tests are updated in a separate commit. Resolves: #907 Signed-off-by: Daniel Kamkha <[email protected]>
1 parent 560bd89 commit 2cc824b

File tree

16 files changed

+143
-41
lines changed

16 files changed

+143
-41
lines changed

.github/workflows/functional-tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ jobs:
244244
- name: Install Dart SDK
245245
run: |
246246
DART_RELEASE_CHANNEL=stable
247-
DART_VERSION=2.12.0
247+
DART_VERSION=2.13.3
248248
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
249249
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
250250
- name: Build and run functional tests
@@ -276,8 +276,8 @@ jobs:
276276
uses: actions/cache@v2
277277
with:
278278
path: ~/dart_sdk
279-
key: ${{ runner.os }}-dart-2.12.0-stable
280-
restore-keys: ${{ runner.os }}-dart-2.12.0-stable
279+
key: ${{ runner.os }}-dart-2.13.3-stable
280+
restore-keys: ${{ runner.os }}-dart-2.13.3-stable
281281
- name: Install CMake
282282
uses: jwlawson/[email protected]
283283
with:
@@ -289,7 +289,7 @@ jobs:
289289
export DART_ROOT=${HOME}/dart_sdk
290290
export DART_BIN=${DART_ROOT}/bin
291291
export PATH=${PATH}:${PWD}/depot_tools:${DART_BIN}
292-
DART_VERSION=2.12.0
292+
DART_VERSION=2.13.3
293293
if [ ! -d "${DART_ROOT}/bin" ]; then
294294
sudo apt install -y python2
295295
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
@@ -364,7 +364,7 @@ jobs:
364364
- name: Install Dart SDK
365365
run: |
366366
DART_RELEASE_CHANNEL=stable
367-
DART_VERSION=2.12.0
367+
DART_VERSION=2.13.3
368368
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
369369
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
370370
- name: Build and run functional tests

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Gluecodium project Release Notes
22

33
## Unreleased
4+
### Features:
5+
* Added support for type aliases (typedefs) in Dart
46
### Bug fixes:
57
* Fixed violation of referential equality invariant for some cases of interface inheritance.
8+
### Breaking changes:
9+
* Generated Dart code now requires minimum Dart version 2.13.0.
610

711
## 10.2.1
812
Release date: 2021-11-01

functional-tests/functional/dart/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import "test/StaticIntMethods_test.dart" as StaticIntMethodsTests;
6767
import "test/StaticStringMethods_test.dart" as StaticStringMethodsTests;
6868
import "test/StructsWithConstants_test.dart" as StructsWithConstantsTests;
6969
import "test/StructsWithMethods_test.dart" as StructsWithMethodsTests;
70+
import "test/TypeAliases_test.dart" as TypeAliasesTests;
7071

7172
final _allTests = [
7273
BlobsTests.main,
@@ -113,7 +114,8 @@ final _allTests = [
113114
StaticIntMethodsTests.main,
114115
StaticStringMethodsTests.main,
115116
StructsWithConstantsTests.main,
116-
StructsWithMethodsTests.main
117+
StructsWithMethodsTests.main,
118+
TypeAliasesTests.main
117119
];
118120

119121
String _getLibraryPath(String nativeLibraryName) {

functional-tests/functional/dart/pubspec.yaml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: FunctionalDartTests
22
environment:
3-
sdk: '>=2.12.0 <3.0.0'
3+
sdk: '>=2.13.0 <3.0.0'
44
dependencies:
55
test:
66
functional:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// Copyright (C) 2016-2021 HERE Europe B.V.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// SPDX-License-Identifier: Apache-2.0
17+
// License-Filename: LICENSE
18+
//
19+
// -------------------------------------------------------------------------------------------------
20+
21+
import "package:test/test.dart";
22+
import "package:functional/test.dart";
23+
import "../test_suite.dart";
24+
25+
final _testSuite = TestSuite("Type Aliases");
26+
27+
void main() {
28+
_testSuite.test("Type alias to struct", () {
29+
final result = StaticTypedefExampleStructTypedef("nonsense");
30+
31+
expect(result is StaticTypedefExampleStruct, isTrue);
32+
expect(result.exampleString, "nonsense");
33+
});
34+
_testSuite.test("Type alias used by a function", () {
35+
final result = StaticTypedef.returnIntTypedef(2);
36+
37+
expect(result is int, isTrue);
38+
expect(result, 3);
39+
});
40+
_testSuite.test("Type alias points to a type alias", () {
41+
final result = StaticTypedef.returnNestedIntTypedef(4);
42+
43+
expect(result is int, isTrue);
44+
expect(result, 5);
45+
});
46+
_testSuite.test("Type alias from type collection", () {
47+
final result = StaticTypedef.returnTypedefPointFromTypeCollection(PointTypedef(1.0, 3.0));
48+
49+
expect(result is Point, isTrue);
50+
expect(result.x, 1.0);
51+
expect(result.y, 3.0);
52+
});
53+
}

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartGenerator.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ internal class DartGenerator : Generator {
165165
.sortedBy { ffiNameResolver.resolveName(it) }
166166

167167
val generatedFiles = dartFilteredModel.topElements.flatMap {
168-
listOfNotNull(
168+
listOf(
169169
generateDart(
170170
it, dartResolvers, dartNameResolver, listOf(importsCollector, declarationImportsCollector),
171171
exportsCollector, typeRepositoriesCollector, generatorPredicates.predicates, descendantInterfaces
@@ -195,15 +195,15 @@ internal class DartGenerator : Generator {
195195
typeRepositoriesCollector: MutableList<LimeContainerWithInheritance>,
196196
predicates: Map<String, (Any) -> Boolean>,
197197
descendantInterfaces: Map<String, List<LimeInterface>>
198-
): GeneratedFile? {
199-
val contentTemplateName = selectTemplate(rootElement) ?: return null
198+
): GeneratedFile {
199+
val contentTemplateName = selectTemplate(rootElement)
200200

201201
val packagePath = rootElement.path.head.joinToString(separator = "/")
202202
val fileName = dartNameResolver.resolveFileName(rootElement)
203203
val filePath = "$packagePath/$fileName"
204204
val relativePath = "$SRC_DIR_SUFFIX/$filePath.dart"
205205

206-
val allTypes = LimeTypeHelper.getAllTypes(rootElement).filterNot { it is LimeTypeAlias }
206+
val allTypes = LimeTypeHelper.getAllTypes(rootElement)
207207
val nonExternalTypes = allTypes.filter { it.external?.dart == null }
208208
val freeConstants = (rootElement as? LimeTypesCollection)?.constants ?: emptyList()
209209
val allSymbols =
@@ -512,7 +512,7 @@ internal class DartGenerator : Generator {
512512
is LimeEnumeration -> "dart/DartEnumeration"
513513
is LimeException -> "dart/DartException"
514514
is LimeLambda -> "dart/DartLambda"
515-
is LimeTypeAlias -> null
515+
is LimeTypeAlias -> "dart/DartTypeAlias"
516516
else -> throw GluecodiumExecutionException(
517517
"Unsupported top-level element: " +
518518
limeElement::class.java.name

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartImportResolver.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ internal class DartImportResolver(
5454
}
5555

5656
private fun resolveTypeImports(limeType: LimeType): List<DartImport> =
57-
when (val actualType = limeType.actualType) {
58-
is LimeBasicType -> resolveBasicTypeImports(actualType)
59-
is LimeGenericType -> resolveGenericTypeImports(actualType)
57+
when (limeType) {
58+
is LimeBasicType -> resolveBasicTypeImports(limeType)
59+
is LimeGenericType -> resolveGenericTypeImports(limeType)
6060
else -> listOfNotNull(
61-
createImport(actualType),
62-
resolveExternalImport(actualType, IMPORT_PATH_NAME, useAlias = true)
61+
createImport(limeType),
62+
resolveExternalImport(limeType, IMPORT_PATH_NAME, useAlias = true)
6363
)
6464
}
6565

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartImportsCollector.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import com.here.gluecodium.model.lime.LimeClass
2525
import com.here.gluecodium.model.lime.LimeContainerWithInheritance
2626

2727
internal class DartImportsCollector(importsResolver: ImportsResolver<DartImport>) :
28-
GenericImportsCollector<DartImport>(importsResolver, collectTypeRefImports = true, parentTypeFilter = { true }) {
28+
GenericImportsCollector<DartImport>(
29+
importsResolver,
30+
collectTypeRefImports = true,
31+
parentTypeFilter = { true },
32+
collectTypeAliasImports = true
33+
) {
2934

3035
override fun collectParentTypeRefs(limeContainer: LimeContainerWithInheritance) =
3136
when (limeContainer) {

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartNameResolver.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import com.here.gluecodium.model.lime.LimeReturnType
4444
import com.here.gluecodium.model.lime.LimeSet
4545
import com.here.gluecodium.model.lime.LimeStruct
4646
import com.here.gluecodium.model.lime.LimeType
47-
import com.here.gluecodium.model.lime.LimeTypeAlias
4847
import com.here.gluecodium.model.lime.LimeTypeRef
4948
import com.here.gluecodium.model.lime.LimeTypesCollection
5049
import com.here.gluecodium.model.lime.LimeValue
@@ -80,7 +79,6 @@ internal class DartNameResolver(
8079
is LimeValue -> resolveValue(element)
8180
is LimeGenericType -> resolveGenericType(element)
8281
is LimeTypeRef -> resolveTypeRefName(element)
83-
is LimeTypeAlias -> resolveName(element.typeRef)
8482
is LimeType -> resolveTypeName(element)
8583
is LimeNamedElement -> getPlatformName(element)
8684
else ->
@@ -221,11 +219,11 @@ internal class DartNameResolver(
221219

222220
private fun resolveTypeRefName(limeTypeRef: LimeTypeRef, ignoreDuplicates: Boolean = false): String {
223221
val typeName = resolveName(limeTypeRef.type)
224-
val importPath = limeTypeRef.type.actualType.external?.dart?.get(IMPORT_PATH_NAME)
222+
val importPath = limeTypeRef.type.external?.dart?.get(IMPORT_PATH_NAME)
225223
val alias = when {
226224
importPath != null -> computeAlias(importPath)
227225
ignoreDuplicates -> null
228-
duplicateNames.contains(typeName) -> limeTypeRef.type.actualType.path.head.joinToString("_")
226+
duplicateNames.contains(typeName) -> limeTypeRef.type.path.head.joinToString("_")
229227
else -> null
230228
}
231229
val suffix = if (limeTypeRef.isNullable) "?" else ""
@@ -258,7 +256,7 @@ internal class DartNameResolver(
258256
private fun buildDuplicateNames() =
259257
limeReferenceMap.values
260258
.filterIsInstance<LimeType>()
261-
.filterNot { it is LimeTypesCollection || it is LimeTypeAlias || it is LimeGenericType || it is LimeBasicType }
259+
.filterNot { it is LimeTypesCollection || it is LimeGenericType || it is LimeBasicType }
262260
.filter { it.external?.dart == null }
263261
.groupBy { resolveTypeName(it) }
264262
.filterValues { it.size > 1 }

gluecodium/src/main/resources/templates/dart/DartClass.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ abstract class {{resolveName}}{{!!
4848
{{/ifPredicate}}
4949
}
5050

51+
{{#typeAliases}}
52+
{{>dart/DartTypeAlias}}
53+
{{/typeAliases}}
5154
{{#enumerations}}
5255
{{>dart/DartEnumeration}}
5356
{{/enumerations}}

gluecodium/src/main/resources/templates/dart/DartInterface.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ abstract class {{resolveName}}{{!!
6464
{{/ifPredicate}}
6565
}
6666

67+
{{#typeAliases}}
68+
{{>dart/DartTypeAlias}}
69+
{{/typeAliases}}
6770
{{#enumerations}}
6871
{{>dart/DartEnumeration}}
6972
{{/enumerations}}

gluecodium/src/main/resources/templates/dart/DartPubspec.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
!}}
2121
name: {{libraryName}}
2222
environment:
23-
sdk: '>=2.12.0 <3.0.0'
23+
sdk: '>=2.13.0 <3.0.0'
2424
dependencies:
2525
ffi:
2626
intl:

gluecodium/src/main/resources/templates/dart/DartStruct.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class {{resolveName}}{{#if external.dart.converter}}Internal{{/if}} {
7070
}
7171
{{/unlessPredicate}}
7272

73+
{{#typeAliases}}
74+
{{>dart/DartTypeAlias}}
75+
{{/typeAliases}}
7376
{{#enumerations}}
7477
{{>dart/DartEnumeration}}
7578
{{/enumerations}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{!!
2+
!
3+
! Copyright (C) 2016-2021 HERE Europe B.V.
4+
!
5+
! Licensed under the Apache License, Version 2.0 (the "License");
6+
! you may not use this file except in compliance with the License.
7+
! You may obtain a copy of the License at
8+
!
9+
! http://www.apache.org/licenses/LICENSE-2.0
10+
!
11+
! Unless required by applicable law or agreed to in writing, software
12+
! distributed under the License is distributed on an "AS IS" BASIS,
13+
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
! See the License for the specific language governing permissions and
15+
! limitations under the License.
16+
!
17+
! SPDX-License-Identifier: Apache-2.0
18+
! License-Filename: LICENSE
19+
!
20+
!}}
21+
{{>dart/DartDocumentation}}{{>dart/DartAttributes}}
22+
typedef {{resolveName visibility}}{{resolveName}} = {{resolveName typeRef}};

gluecodium/src/main/resources/templates/dart/DartTypes.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
! License-Filename: LICENSE
1919
!
2020
!}}
21+
{{#typeAliases}}
22+
{{>dart/DartTypeAlias}}
23+
{{/typeAliases}}
2124
{{#enumerations}}
2225
{{>dart/DartEnumeration}}
2326
{{/enumerations}}

0 commit comments

Comments
 (0)