Skip to content

Commit f3097eb

Browse files
committed
[draft] Dart typealias
TODO: describe TODO: split TODO: changelog Resolves: #907 Signed-off-by: Daniel Kamkha <[email protected]>
1 parent 5835fb4 commit f3097eb

File tree

47 files changed

+432
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+432
-257
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

functional-tests/functional/dart/main.dart

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

7071
final _allTests = [
7172
BlobsTests.main,
@@ -111,7 +112,8 @@ final _allTests = [
111112
StaticIntMethodsTests.main,
112113
StaticStringMethodsTests.main,
113114
StructsWithConstantsTests.main,
114-
StructsWithMethodsTests.main
115+
StructsWithMethodsTests.main,
116+
TypeAliasesTests.main
115117
];
116118

117119
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
@@ -159,7 +159,7 @@ internal class DartGenerator : Generator {
159159
.sortedBy { ffiNameResolver.resolveName(it) }
160160

161161
val generatedFiles = dartFilteredModel.topElements.flatMap {
162-
listOfNotNull(
162+
listOf(
163163
generateDart(
164164
it, dartResolvers, dartNameResolver, listOf(importsCollector, declarationImportsCollector),
165165
exportsCollector, typeRepositoriesCollector, generatorPredicates.predicates
@@ -188,15 +188,15 @@ internal class DartGenerator : Generator {
188188
exportsCollector: MutableMap<List<String>, MutableList<DartExport>>,
189189
typeRepositoriesCollector: MutableList<LimeContainerWithInheritance>,
190190
predicates: Map<String, (Any) -> Boolean>
191-
): GeneratedFile? {
192-
val contentTemplateName = selectTemplate(rootElement) ?: return null
191+
): GeneratedFile {
192+
val contentTemplateName = selectTemplate(rootElement)
193193

194194
val packagePath = rootElement.path.head.joinToString(separator = "/")
195195
val fileName = dartNameResolver.resolveFileName(rootElement)
196196
val filePath = "$packagePath/$fileName"
197197
val relativePath = "$SRC_DIR_SUFFIX/$filePath.dart"
198198

199-
val allTypes = LimeTypeHelper.getAllTypes(rootElement).filterNot { it is LimeTypeAlias }
199+
val allTypes = LimeTypeHelper.getAllTypes(rootElement)
200200
val nonExternalTypes = allTypes.filter { it.external?.dart == null }
201201
val freeConstants = (rootElement as? LimeTypesCollection)?.constants ?: emptyList()
202202
val allSymbols =
@@ -504,7 +504,7 @@ internal class DartGenerator : Generator {
504504
is LimeEnumeration -> "dart/DartEnumeration"
505505
is LimeException -> "dart/DartException"
506506
is LimeLambda -> "dart/DartLambda"
507-
is LimeTypeAlias -> null
507+
is LimeTypeAlias -> "dart/DartTypeAlias"
508508
else -> throw GluecodiumExecutionException(
509509
"Unsupported top-level element: " +
510510
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: 2 additions & 4 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,7 +219,7 @@ 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
@@ -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 }
259+
.filterNot { it is LimeTypesCollection }
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)