blob: 7a0bb20583f58f30c6a1e8b9d43b3b5547a7e8f3 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:ffigen/src/code_generator.dart';
import 'package:ffigen/src/header_parser/data.dart';
import 'package:logging/logging.dart';
import '../clang_bindings/clang_bindings.dart' as clang_types;
import '../data.dart' show clang;
import '../includer.dart';
import '../utils.dart';
final _logger = Logger('ffigen.header_parser.functiondecl_parser');
/// Holds temporary information regarding [Func] while parsing.
class _ParserFunc {
Func? func;
bool incompleteStructParameter = false;
bool unimplementedParameterType = false;
final _stack = Stack<_ParserFunc>();
/// Parses a function declaration.
Func? parseFunctionDeclaration(clang_types.CXCursor cursor) {
final funcUsr = cursor.usr();
final funcName = cursor.spelling();
if (shouldIncludeFunc(funcUsr, funcName)) {
_logger.fine('++++ Adding Function: ${cursor.completeStringRepr()}');
final rt = _getFunctionReturnType(cursor);
final parameters = _getParameters(cursor, funcName);
if (clang.clang_Cursor_isFunctionInlined(cursor) != 0) {
'---- Removed Function, reason: inline function: ${cursor.completeStringRepr()}');
"Skipped Function '$funcName', inline functions are not supported.");
return _stack
.func; // Returning null so that [addToBindings] function excludes this.
if (rt.isIncompleteCompound || {
'---- Removed Function, reason: Incomplete struct pass/return by value: ${cursor.completeStringRepr()}');
"Skipped Function '$funcName', Incomplete struct pass/return by value not supported.");
return _stack
.func; // Returning null so that [addToBindings] function excludes this.
if (rt.getBaseType().broadType == BroadType.Unimplemented || {
'---- Removed Function, reason: unsupported return type or parameter type: ${cursor.completeStringRepr()}');
"Skipped Function '$funcName', function has unsupported return type or parameter type.");
return _stack
.func; // Returning null so that [addToBindings] function excludes this.
} = Func(
dartDoc: getCursorDocComment(
nesting.length + commentPrefix.length,
usr: funcUsr,
name: config.functionDecl.renameUsingConfig(funcName),
originalName: funcName,
returnType: rt,
parameters: parameters,
} else if (bindingsIndex.isSeenFunc(funcUsr)) { = bindingsIndex.getSeenFunc(funcUsr);
return _stack.pop().func;
Type _getFunctionReturnType(clang_types.CXCursor cursor) {
return cursor.returnType().toCodeGenType();
List<Parameter> _getParameters(clang_types.CXCursor cursor, String funcName) {
final parameters = <Parameter>[];
final totalArgs = clang.clang_Cursor_getNumArguments(cursor);
for (var i = 0; i < totalArgs; i++) {
final paramCursor = clang.clang_Cursor_getArgument(cursor, i);
_logger.finer('===== parameter: ${paramCursor.completeStringRepr()}');
final pt = _getParameterType(paramCursor);
if (pt.isIncompleteCompound) { = true;
} else if (pt.getBaseType().broadType == BroadType.Unimplemented) {
.finer('Unimplemented type: ${pt.getBaseType().unimplementedReason}'); = true;
final pn = paramCursor.spelling();
/// If [pn] is null or empty, its set to `arg$i` by code_generator.
originalName: pn,
name: config.functionDecl.renameMemberUsingConfig(funcName, pn),
type: pt,
return parameters;
Type _getParameterType(clang_types.CXCursor cursor) {
return cursor.type().toCodeGenType();