• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / ffigen / 4783660152

24 Apr 2023 07:41AM UTC coverage: 92.363% (-7.0%) from 99.366%
4783660152

Pull #560

github

GitHub
Merge 375a93eba into fd3e1a724
Pull Request #560: Stable to 7 x

1780 of 1780 new or added lines in 41 files covered. (100.0%)

3229 of 3496 relevant lines covered (92.36%)

25.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/lib/src/code_generator/type.dart
1
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'package:ffigen/src/code_generator.dart';
6

7
import 'writer.dart';
8

9
/// Type class for return types, variable types, etc.
10
///
11
/// Implementers should extend either Type, or BindingType if the type is also a
12
/// binding, and override at least getCType and toString.
13
abstract class Type {
14
  const Type();
140✔
15

16
  /// Get all dependencies of this type and save them in [dependencies].
17
  void addDependencies(Set<Binding> dependencies) {}
34✔
18

19
  /// Get base type for any type.
20
  ///
21
  /// E.g int** has base [Type] of int.
22
  /// double[2][3] has base [Type] of double.
23
  Type get baseType => this;
30✔
24

25
  /// Get base Array type.
26
  ///
27
  /// Returns itself if it's not an Array Type.
28
  Type get baseArrayType => this;
7✔
29

30
  /// Get base typealias type.
31
  ///
32
  /// Returns itself if it's not a Typealias.
33
  Type get typealiasType => this;
25✔
34

35
  /// Returns true if the type is a [Compound] and is incomplete.
36
  bool get isIncompleteCompound => false;
29✔
37

38
  /// Returns the C type of the Type. This is the FFI compatible type that is
39
  /// passed to native code.
40
  String getCType(Writer w) => throw 'No mapping for type: $this';
×
41

42
  /// Returns the Dart type of the Type. This is the user visible type that is
43
  /// passed to Dart code.
44
  String getDartType(Writer w) => getCType(w);
46✔
45

46
  /// Returns the string representation of the Type, for debugging purposes
47
  /// only. This string should not be printed as generated code.
48
  @override
49
  String toString();
50

51
  /// Cache key used in various places to dedupe Types. By default this is just
52
  /// the hash of the Type, but in many cases this does not dedupe sufficiently.
53
  /// So Types that may be duplicated should override this to return a more
54
  /// specific key. Types that are already deduped don't need to override this.
55
  /// toString() is not a valid cache key as there may be name collisions.
56
  String cacheKey() => hashCode.toRadixString(36);
6✔
57

58
  /// Returns a string of code that creates a default value for this type. For
59
  /// example, for int types this returns the string '0'. A null return means
60
  /// that default values aren't supported for this type, eg void.
61
  String? getDefaultValue(Writer w, String nativeLib) => null;
2✔
62
}
63

64
/// Function to check if the dart and C type string are same.
65
bool sameDartAndCType(Type t, Writer w) => t.getCType(w) == t.getDartType(w);
88✔
66

67
/// Base class for all Type bindings.
68
///
69
/// Since Dart doesn't have multiple inheritance, this type exists so that we
70
/// don't have to reimplement the default methods in all the classes that want
71
/// to extend both NoLookUpBinding and Type.
72
abstract class BindingType extends NoLookUpBinding implements Type {
73
  BindingType({
30✔
74
    String? usr,
75
    String? originalName,
76
    required String name,
77
    String? dartDoc,
78
    bool isInternal = false,
79
  }) : super(
30✔
80
          usr: usr,
81
          originalName: originalName,
82
          name: name,
83
          dartDoc: dartDoc,
84
          isInternal: isInternal,
85
        );
86

87
  @override
22✔
88
  Type get baseType => this;
89

90
  @override
3✔
91
  Type get baseArrayType => this;
92

93
  @override
10✔
94
  Type get typealiasType => this;
95

96
  @override
5✔
97
  bool get isIncompleteCompound => false;
98

99
  @override
13✔
100
  String getDartType(Writer w) => getCType(w);
13✔
101

102
  @override
2✔
103
  String toString() => originalName;
2✔
104

105
  @override
2✔
106
  String cacheKey() => hashCode.toRadixString(36);
4✔
107

108
  @override
2✔
109
  String? getDefaultValue(Writer w, String nativeLib) => null;
110
}
111

112
/// Represents an unimplemented type. Used as a marker, so that declarations
113
/// having these can exclude them.
114
class UnimplementedType extends Type {
115
  String reason;
116
  UnimplementedType(this.reason);
12✔
117

118
  @override
3✔
119
  String toString() => '(Unimplemented: $reason)';
6✔
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc