@@ -16,7 +16,7 @@ enum CppMethodKind { constructor, destructor, method }
1616
1717/// A method or constructor belonging to a C++ class.
1818class CppMethod extends AstNode with HasLocalScope {
19- final String name;
19+ final Symbol name;
2020 final String originalName;
2121 final Type returnType;
2222 final List <Parameter > parameters;
@@ -34,15 +34,16 @@ class CppMethod extends AstNode with HasLocalScope {
3434 this .kind = CppMethodKind .method,
3535 });
3636
37- bool get isConstructor => kind == CppMethodKind .constructor;
38- bool get isDestructor => kind == CppMethodKind .destructor;
37+ bool get isConstructor => kind == .constructor;
38+ bool get isDestructor => kind == .destructor;
3939
4040 @override
4141 void visit (Visitation visitation) => visitation.visitCppMethod (this );
4242
4343 @override
4444 void visitChildren (Visitor visitor) {
4545 super .visitChildren (visitor);
46+ visitor.visit (name);
4647 visitor.visit (returnType);
4748 visitor.visitAll (parameters);
4849 }
@@ -100,9 +101,12 @@ class CppClass extends BindingType with HasLocalScope {
100101
101102 final ptrVoid = '$ffiPrefix .Pointer<$ffiPrefix .Void>' ;
102103
103- final classMethods = methods
104- .where ((m) => m.kind == CppMethodKind .method)
105- .toList ();
104+ // Helper to build a comma-separated Dart parameter list.
105+ String dartParamList (Iterable <Parameter > params) =>
106+ params.map ((p) => '${p .type .getDartType (ctx )} ${p .name }' ).join (', ' );
107+
108+ final classMethods = methods.where ((m) => m.kind == .method).toList ();
109+ final constructors = methods.where ((m) => m.kind == .constructor).toList ();
106110
107111 s.write (makeDartDoc (dartDoc));
108112 s.write ('''
@@ -113,12 +117,39 @@ class $name {
113117 $name ._(this._ptr);
114118''' );
115119
120+ for (final ctor in constructors) {
121+ final glueName = ctor.name.name;
122+ final privateName = '_$glueName ' ;
123+
124+ final dartParams = dartParamList (ctor.parameters);
125+
126+ final localVars = LocalVariables (ctor.localScope);
127+ final callArgs = ctor.parameters
128+ .map (
129+ (p) => p.type.sameDartAndFfiDartType
130+ ? p.name
131+ : p.type.convertDartTypeToFfiDartType (
132+ ctx,
133+ p.name,
134+ objCRetain: false ,
135+ objCAutorelease: false ,
136+ localVariables: localVars,
137+ ),
138+ )
139+ .join (', ' );
140+
141+ s.write ('''
142+ factory $name ($dartParams ) {
143+ ${localVars .generateDeclarations ()}
144+ return $name ._($privateName ($callArgs ));
145+ }
146+ ''' );
147+ }
148+
116149 for (final method in classMethods) {
117- final glue = '_${name }_${ method .name }' ;
150+ final glue = '_${method . name .name }' ;
118151 final dartReturn = method.returnType.getDartType (ctx);
119- final dartParams = method.parameters
120- .map ((p) => '${p .type .getDartType (ctx )} ${p .name }' )
121- .join (', ' );
152+ final dartParams = dartParamList (method.parameters);
122153
123154 final callArgs = [
124155 if (! method.isStatic) '_ptr' ,
@@ -127,14 +158,14 @@ class $name {
127158
128159 final staticKeyword = method.isStatic ? 'static ' : '' ;
129160 s.write (
130- ' $staticKeyword $dartReturn ${method .name }($dartParams )'
161+ ' $staticKeyword $dartReturn ${method .originalName }($dartParams )'
131162 ' => $glue ($callArgs );\n ' ,
132163 );
133164 }
134165 s.write ('}\n ' );
135166
136167 for (final method in classMethods) {
137- final symbol = '${ name }_${ method .name }' ;
168+ final symbol = method.name.name ;
138169 final glue = '_$symbol ' ;
139170
140171 final cReturn = method.returnType.getCType (ctx);
@@ -157,12 +188,36 @@ class $name {
157188 w,
158189 nativeType: cType,
159190 dartName: glue,
160- nativeSymbolName: symbol,
191+ nativeSymbolName: Namer . cSafeName ( symbol) ,
161192 ),
162193 );
163194 s.write ('\n external $ffiReturn $glue ($ffiParams );\n\n ' );
164195 }
165196
197+ for (final ctor in constructors) {
198+ final symbol = ctor.name.name;
199+ final glue = '_$symbol ' ;
200+
201+ final paramCTypes = ctor.parameters
202+ .map ((p) => p.type.getCType (ctx))
203+ .join (', ' );
204+ final cType = '$ptrVoid Function($paramCTypes )' ;
205+
206+ final ffiParams = ctor.parameters
207+ .map ((p) => '${p .type .getFfiDartType (ctx )} ${p .name }' )
208+ .join (', ' );
209+
210+ s.write (
211+ makeNativeAnnotation (
212+ w,
213+ nativeType: cType,
214+ dartName: glue,
215+ nativeSymbolName: Namer .cSafeName (symbol),
216+ ),
217+ );
218+ s.write ('\n external $ptrVoid $glue ($ffiParams );\n\n ' );
219+ }
220+
166221 return BindingString (
167222 type: BindingStringType .cppClass,
168223 string: s.toString (),
0 commit comments