codegen: Fix floating reference regression with Variants
[vala-gnome.git] / ccode / valaccodefunctiondeclarator.vala
blob529473d142146cb5b2a4a4fa9e0e936ea5ad12b5
1 /* valaccodefunctiondeclarator.vala
3 * Copyright (C) 2006-2007 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a function pointer declarator in the C code.
28 public class Vala.CCodeFunctionDeclarator : CCodeDeclarator {
29 /**
30 * The declarator name.
32 public string name { get; set; }
34 private List<CCodeParameter> parameters = new ArrayList<CCodeParameter> ();
36 public CCodeFunctionDeclarator (string name) {
37 this.name = name;
40 /**
41 * Appends the specified parameter to the list of function parameters.
43 * @param param a formal parameter
45 public void add_parameter (CCodeParameter param) {
46 parameters.add (param);
49 public override void write (CCodeWriter writer) {
50 write_declaration (writer);
53 public override void write_declaration (CCodeWriter writer) {
54 writer.write_string ("(*");
55 writer.write_string (name);
56 writer.write_string (") (");
58 bool has_args = (CCodeModifiers.PRINTF in modifiers || CCodeModifiers.SCANF in modifiers);
59 int i = 0;
60 int format_arg_index = -1;
61 int args_index = -1;
62 foreach (CCodeParameter param in parameters) {
63 if (i > 0) {
64 writer.write_string (", ");
66 param.write (writer);
67 if (CCodeModifiers.FORMAT_ARG in param.modifiers) {
68 format_arg_index = i;
70 if (has_args && param.ellipsis) {
71 args_index = i;
72 } else if (has_args && param.type_name == "va_list" && format_arg_index < 0) {
73 format_arg_index = i - 1;
75 i++;
78 writer.write_string (")");
80 if (CCodeModifiers.DEPRECATED in modifiers) {
81 writer.write_string (" G_GNUC_DEPRECATED");
84 if (CCodeModifiers.PRINTF in modifiers) {
85 format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
86 writer.write_string (" G_GNUC_PRINTF(%d,%d)".printf (format_arg_index, args_index + 1));
87 } else if (CCodeModifiers.SCANF in modifiers) {
88 format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
89 writer.write_string (" G_GNUC_SCANF(%d,%d)".printf (format_arg_index, args_index + 1));
90 } else if (format_arg_index >= 0) {
91 writer.write_string (" G_GNUC_FORMAT(%d)".printf (format_arg_index + 1));