Release 0.7.8
[vala-lang.git] / ccode / valaccodefunction.vala
blob1dfef1b298a2d9ebca9392dd90b27c17a03bcd9e
1 /* valaccodefunction.vala
3 * Copyright (C) 2006-2009 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 declaration in the C code.
28 public class Vala.CCodeFunction : CCodeNode {
29 /**
30 * The name of this function.
32 public string name { get; set; }
34 /**
35 * The function modifiers.
37 public CCodeModifiers modifiers { get; set; }
39 /**
40 * The function return type.
42 public string return_type { get; set; }
44 /**
45 * The function body.
47 public CCodeBlock block { get; set; }
49 private List<CCodeFormalParameter> parameters = new ArrayList<CCodeFormalParameter> ();
51 public CCodeFunction (string name, string return_type = "void") {
52 this.name = name;
53 this.return_type = return_type;
56 /**
57 * Appends the specified parameter to the list of function parameters.
59 * @param param a formal parameter
61 public void add_parameter (CCodeFormalParameter param) {
62 parameters.add (param);
65 /**
66 * Returns a copy of this function.
68 * @return copied function
70 public CCodeFunction copy () {
71 var func = new CCodeFunction (name, return_type);
72 func.modifiers = modifiers;
74 /* no deep copy for lists available yet
75 * func.parameters = parameters.copy ();
77 foreach (CCodeFormalParameter param in parameters) {
78 func.parameters.add (param);
81 func.block = block;
82 return func;
85 public override void write (CCodeWriter writer) {
86 writer.write_indent (line);
87 if (CCodeModifiers.STATIC in modifiers) {
88 writer.write_string ("static ");
90 if (CCodeModifiers.INLINE in modifiers) {
91 writer.write_string ("inline ");
93 writer.write_string (return_type);
94 writer.write_string (" ");
95 writer.write_string (name);
96 writer.write_string (" (");
98 bool first = true;
99 foreach (CCodeFormalParameter param in parameters) {
100 if (!first) {
101 writer.write_string (", ");
102 } else {
103 first = false;
105 param.write (writer);
107 if (first) {
108 writer.write_string ("void");
111 writer.write_string (")");
112 if (block == null) {
113 writer.write_string (";");
114 } else {
115 block.write (writer);
116 writer.write_newline ();
118 writer.write_newline ();