remove obsolete ref modifier and callback keyword
[vala-lang.git] / vala / valasourcefile.vala
blob8fed003f7932c7949e203e658d0727e823634aae
1 /* valasourcefile.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 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 Vala source or VAPI package file.
28 public class Vala.SourceFile {
29 /**
30 * The name of this source file.
32 public string! filename { get; set construct; }
34 /**
35 * The header comment of this source file.
37 public string comment { get; set; }
39 /**
40 * Specifies whether this file is a VAPI package file.
42 public bool pkg { get; set; }
44 /**
45 * Specifies the dependency cycle this source file is member of. If this
46 * source file is in a cycle, all type definitions of that cycle will
47 * only be written to the C header file of the cycle head.
49 public SourceFileCycle cycle { get; set; }
51 /**
52 * Specifies whether this source file is the head of the cycle, if it is
53 * in a cycle at all.
55 public bool is_cycle_head { get; set; }
57 /**
58 * Mark used for cycle detection.
60 * 0: not yet visited
61 * 1: currently visiting
62 * 2: already visited
64 public int mark { get; set; }
66 /**
67 * The context this source file belongs to.
69 public weak CodeContext context { get; set; }
71 private List<NamespaceReference> using_directives;
73 private Namespace global_namespace;
74 private List<Namespace> namespaces;
76 private string cheader_filename = null;
77 private string csource_filename = null;
78 private string cinclude_filename = null;
80 private List<string> header_external_includes;
81 private List<string> header_internal_includes;
82 private List<string> source_external_includes;
83 private List<string> source_internal_includes;
85 private List<weak SourceFile> header_internal_full_dependencies;
86 private List<weak SourceFile> header_internal_dependencies;
88 /**
89 * Creates a new source file.
91 * @param filename source file name
92 * @param pkg true if this is a VAPI package file
93 * @return newly created source file
95 public SourceFile (CodeContext! _context, string! _filename, bool _pkg = false) {
96 context = _context;
97 filename = _filename;
98 pkg = _pkg;
101 construct {
102 global_namespace = new Namespace (null, new SourceReference (this));
106 * Adds a new using directive with the specified namespace.
108 * @param ns reference to namespace
110 public void add_using_directive (NamespaceReference! ns) {
111 using_directives.append (ns);
115 * Returns a copy of the list of using directives.
117 * @return using directive list
119 public List<weak NamespaceReference> get_using_directives () {
120 return using_directives.copy ();
124 * Adds the specified namespace to this source file.
126 * @param ns a namespace
128 public void add_namespace (Namespace! ns) {
129 namespaces.append (ns);
133 * Returns the implicitly declared root namespace of this source file.
135 * @return root namespace
137 public Namespace! get_global_namespace () {
138 return global_namespace;
142 * Returns a copy of the list of namespaces.
144 * @return namespace list
146 public List<weak Namespace> get_namespaces () {
147 return namespaces.copy ();
150 public void accept (CodeVisitor! visitor) {
151 visitor.visit_source_file (this);
154 public void accept_children (CodeVisitor! visitor) {
155 foreach (NamespaceReference ns_ref in using_directives) {
156 ns_ref.accept (visitor);
159 global_namespace.accept (visitor);
161 foreach (Namespace ns in namespaces) {
162 ns.accept (visitor);
167 * Returns the filename to use when generating C header files.
169 * @return generated C header filename
171 public string! get_cheader_filename () {
172 if (cheader_filename == null) {
173 var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
174 cheader_filename = "%s.h".printf (basename);
176 return cheader_filename;
180 * Returns the filename to use when generating C source files.
182 * @return generated C source filename
184 public string! get_csource_filename () {
185 if (csource_filename == null) {
186 var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
187 csource_filename = "%s.c".printf (basename);
189 return csource_filename;
193 * Returns the filename to use when including the generated C header
194 * file.
196 * @return C header filename to include
198 public string! get_cinclude_filename () {
199 if (cinclude_filename == null) {
200 var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
201 if (context.library != null) {
202 cinclude_filename = "%s/%s.h".printf (context.library, basename);
203 } else {
204 cinclude_filename = "%s.h".printf (basename);
207 return cinclude_filename;
211 * Adds the specified symbol to the list of symbols code in this source
212 * file depends on.
214 * @param sym a symbol
215 * @param dep_type type of dependency
217 public void add_symbol_dependency (Symbol! sym, SourceFileDependencyType dep_type) {
218 DataType t;
220 if (sym.node is DataType) {
221 t = (DataType) sym.node;
222 } else if (sym.node is Method || sym.node is Field) {
223 if (sym.parent_symbol.node is DataType) {
224 t = (DataType) sym.parent_symbol.node;
225 } else {
226 return;
228 } else if (sym.node is Property) {
229 t = (DataType) sym.parent_symbol.node;
230 } else if (sym.node is Constant) {
231 if (sym.parent_symbol.node is DataType) {
232 t = (DataType) sym.parent_symbol.node;
233 } else if (sym.parent_symbol.node is Namespace) {
234 var ns = (Namespace) sym.parent_symbol.node;
235 source_internal_includes.concat (ns.get_cheader_filenames ());
236 return;
237 } else {
238 return;
240 } else if (sym.node is FormalParameter) {
241 var fp = (FormalParameter) sym.node;
242 t = fp.type_reference.data_type;
243 if (t == null) {
244 /* generic type parameter */
245 return;
247 } else {
248 return;
251 if (dep_type == SourceFileDependencyType.SOURCE) {
252 if (t.source_reference.file.pkg) {
253 source_external_includes.concat (t.get_cheader_filenames ());
254 } else {
255 source_internal_includes.concat (t.get_cheader_filenames ());
257 return;
260 if (t.source_reference.file.pkg) {
261 /* external package */
262 header_external_includes.concat (t.get_cheader_filenames ());
263 return;
266 if (dep_type == SourceFileDependencyType.HEADER_FULL || !t.is_reference_type ()) {
267 header_internal_includes.concat (t.get_cheader_filenames ());
268 header_internal_full_dependencies.append (t.source_reference.file);
271 header_internal_dependencies.append (t.source_reference.file);
275 * Returns the list of external includes the generated C header file
276 * requires.
278 * @return external include list for C header file
280 public weak List<string> get_header_external_includes () {
281 return header_external_includes;
285 * Adds the specified filename to the list of package-internal includes
286 * the generated C header file requires.
288 * @param include internal include for C header file
290 public void add_header_internal_include (string! include) {
291 header_internal_includes.append (include);
295 * Returns the list of package-internal includes the generated C header
296 * file requires.
298 * @return internal include list for C header file
300 public weak List<string> get_header_internal_includes () {
301 return header_internal_includes;
305 * Returns the list of external includes the generated C source file
306 * requires.
308 * @return include list for C source file
310 public weak List<string> get_source_external_includes () {
311 return source_external_includes;
315 * Returns the list of package-internal includes the generated C source
316 * file requires.
318 * @return include list for C source file
320 public weak List<string> get_source_internal_includes () {
321 return source_internal_includes;
325 * Returns the list of source files the generated C header file requires
326 * definitely.
328 * @return definite source file dependencies
330 public weak List<SourceFile> get_header_internal_full_dependencies () {
331 return header_internal_full_dependencies;
335 * Returns the list of source files the generated C header file loosely
336 * depends on.
338 * @return loose source file dependencies
340 public weak List<SourceFile> get_header_internal_dependencies () {
341 return header_internal_dependencies;
345 public enum Vala.SourceFileDependencyType {
346 HEADER_FULL,
347 HEADER_SHALLOW,
348 SOURCE