2ba21e9579979180104b09dc0f44dc8079d457cc
[roobuilder] / src / ccode / valaccodefunctiondeclarator.vala
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 public class Vala.CCodeFunctionDeclarator : CCodeDeclarator {
29         private List<CCodeParameter> parameters = new ArrayList<CCodeParameter> ();
30
31         public CCodeFunctionDeclarator (string name) {
32                 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         public void add_parameter (CCodeParameter param) {
41                 parameters.add (param);
42         }
43
44         public override void write (CCodeWriter writer) {
45                 write_declaration (writer);
46         }
47
48         public override void write_declaration (CCodeWriter writer) {
49                 writer.write_string ("(*");
50                 writer.write_string (name);
51                 writer.write_string (") (");
52
53                 bool has_args = (CCodeModifiers.PRINTF in modifiers || CCodeModifiers.SCANF in modifiers);
54                 int i = 0;
55                 int format_arg_index = -1;
56                 int args_index = -1;
57                 foreach (CCodeParameter param in parameters) {
58                         if (i > 0) {
59                                 writer.write_string (", ");
60                         }
61                         param.write (writer);
62                         if (CCodeModifiers.FORMAT_ARG in param.modifiers) {
63                                 format_arg_index = i;
64                         }
65                         if (has_args && param.ellipsis) {
66                                 args_index = i;
67                         } else if (has_args && param.type_name == "va_list" && format_arg_index < 0) {
68                                 format_arg_index = i - 1;
69                         }
70                         i++;
71                 }
72                 if (i == 0) {
73                         writer.write_string ("void");
74                 }
75
76                 writer.write_string (")");
77
78                 if (CCodeModifiers.DEPRECATED in modifiers) {
79                         writer.write_string (GNUC_DEPRECATED);
80                 }
81
82                 if (CCodeModifiers.PRINTF in modifiers) {
83                         format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
84                         writer.write_string (GNUC_PRINTF.printf (format_arg_index, args_index + 1));
85                 } else if (CCodeModifiers.SCANF in modifiers) {
86                         format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
87                         writer.write_string (GNUC_SCANF.printf (format_arg_index, args_index + 1));
88                 } else if (format_arg_index >= 0) {
89                         writer.write_string (GNUC_FORMAT.printf (format_arg_index + 1));
90                 }
91         }
92 }