Line data Source code
1 : /* valaccodefunctiondeclarator.vala
2 : *
3 : * Copyright (C) 2006-2007 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents a function pointer declarator in the C code.
27 : */
28 4584 : public class Vala.CCodeFunctionDeclarator : CCodeDeclarator {
29 4376 : private List<CCodeParameter> parameters = new ArrayList<CCodeParameter> ();
30 :
31 6564 : public CCodeFunctionDeclarator (string name) {
32 2188 : this.name = name;
33 : }
34 :
35 : /**
36 : * Appends the specified parameter to the list of function parameters.
37 : *
38 : * @param param a formal parameter
39 : */
40 4531 : public void add_parameter (CCodeParameter param) {
41 4531 : parameters.add (param);
42 : }
43 :
44 0 : public override void write (CCodeWriter writer) {
45 0 : write_declaration (writer);
46 : }
47 :
48 617 : public override void write_declaration (CCodeWriter writer) {
49 617 : writer.write_string ("(*");
50 617 : writer.write_string (name);
51 617 : writer.write_string (") (");
52 :
53 617 : bool has_args = (CCodeModifiers.PRINTF in modifiers || CCodeModifiers.SCANF in modifiers);
54 617 : int i = 0;
55 617 : int format_arg_index = -1;
56 617 : int args_index = -1;
57 3211 : foreach (CCodeParameter param in parameters) {
58 1297 : if (i > 0) {
59 693 : writer.write_string (", ");
60 : }
61 1297 : param.write (writer);
62 1297 : if (CCodeModifiers.FORMAT_ARG in param.modifiers) {
63 2 : format_arg_index = i;
64 : }
65 1297 : if (has_args && param.ellipsis) {
66 : args_index = i;
67 1297 : } else if (has_args && param.type_name == "va_list" && format_arg_index < 0) {
68 2 : format_arg_index = i - 1;
69 : }
70 1297 : i++;
71 : }
72 617 : if (i == 0) {
73 13 : writer.write_string ("void");
74 : }
75 :
76 617 : writer.write_string (")");
77 :
78 617 : if (CCodeModifiers.DEPRECATED in modifiers) {
79 3 : writer.write_string (GNUC_DEPRECATED);
80 : }
81 :
82 617 : if (CCodeModifiers.PRINTF in modifiers) {
83 2 : format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
84 2 : writer.write_string (GNUC_PRINTF.printf (format_arg_index, args_index + 1));
85 615 : } else if (CCodeModifiers.SCANF in modifiers) {
86 1 : format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
87 1 : writer.write_string (GNUC_SCANF.printf (format_arg_index, args_index + 1));
88 614 : } else if (format_arg_index >= 0) {
89 1 : writer.write_string (GNUC_FORMAT.printf (format_arg_index + 1));
90 : }
91 : }
92 : }
|