girparser: Fix parameter index calculation
[vala-lang.git] / vala / valacodecontext.vala
blob2c9e32694cff2fd4e2e5cff7c751228094e7a808
1 /* valacodecontext.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 * The root of the code tree.
28 public class Vala.CodeContext {
29 /**
30 * Enable run-time checks for programming errors.
32 public bool assert { get; set; }
34 /**
35 * Enable additional run-time checks such as type checks.
37 public bool checking { get; set; }
39 /**
40 * Do not warn when using deprecated features.
42 public bool deprecated { get; set; }
44 /**
45 * Do not warn when using experimental features.
47 public bool experimental { get; set; }
49 /**
50 * Enable experimental enhancements for non-null types.
52 public bool experimental_non_null { get; set; }
54 /**
55 * Enable transformation of D-Bus member names in dynamic client support.
57 public bool dbus_transformation { get; set; }
59 /**
60 * Output C code, don't compile to object code.
62 public bool ccode_only { get; set; }
64 /**
65 * Output C header file.
67 public string? header_filename { get; set; }
69 /**
70 * Output internal C header file.
72 public string? internal_header_filename { get; set; }
74 public bool use_header { get; set; }
76 /**
77 * Base directory used for header_filename in the VAPIs.
79 public string? includedir { get; set; }
81 /**
82 * Output symbols file.
84 public string? symbols_filename { get; set; }
86 /**
87 * Compile but do not link.
89 public bool compile_only { get; set; }
91 /**
92 * Output filename.
94 public string output { get; set; }
96 /**
97 * Base source directory.
99 public string basedir { get; set; }
102 * Code output directory.
104 public string directory { get; set; }
107 * Produce debug information.
109 public bool debug { get; set; }
112 * Optimization level.
114 public int optlevel { get; set; }
117 * Enable multithreading support.
119 public bool thread { get; set; }
122 * Enable memory profiler.
124 public bool mem_profiler { get; set; }
127 * Specifies the optional module initialization method.
129 public Method module_init_method { get; set; }
132 * Keep temporary files produced by the compiler.
134 public bool save_temps { get; set; }
136 public Profile profile { get; set; }
139 * Target major version number of glib for code generation.
141 public int target_glib_major { get; set; }
144 * Target minor version number of glib for code generation.
146 public int target_glib_minor { get; set; }
148 public bool verbose_mode { get; set; }
150 public bool version_header { get; set; }
153 * Returns true if the target version of glib is greater than or
154 * equal to the specified version.
156 public bool require_glib_version (int major, int minor) {
157 return (target_glib_major > major) || (target_glib_major == major && target_glib_minor >= minor);
160 public bool save_csources {
161 get { return save_temps; }
164 public Report report { get; set; default = new Report ();}
166 public Method? entry_point { get; set; }
168 public string entry_point_name { get; set; }
170 private List<SourceFile> source_files = new ArrayList<SourceFile> ();
171 private List<string> c_source_files = new ArrayList<string> ();
172 private Namespace _root = new Namespace (null);
174 private List<string> packages = new ArrayList<string> (str_equal);
176 private Set<string> defines = new HashSet<string> (str_hash, str_equal);
178 static StaticPrivate context_stack_key = StaticPrivate ();
181 * The root namespace of the symbol tree.
183 * @return root namespace
185 public Namespace root {
186 get { return _root; }
190 * The selected code generator.
192 public CodeGenerator codegen { get; set; default = new CodeGenerator (); }
194 public CodeContext () {
198 * Return the topmost context from the context stack.
200 public static CodeContext get () {
201 List<CodeContext>* context_stack = context_stack_key.get ();
203 return context_stack->get (context_stack->size - 1);
207 * Push the specified context to the context stack.
209 public static void push (CodeContext context) {
210 ArrayList<CodeContext>* context_stack = context_stack_key.get ();
211 if (context_stack == null) {
212 context_stack = new ArrayList<CodeContext> ();
213 context_stack_key.set (context_stack, null);
216 context_stack->add (context);
220 * Remove the topmost context from the context stack.
222 public static void pop () {
223 List<CodeContext>* context_stack = context_stack_key.get ();
225 context_stack->remove_at (context_stack->size - 1);
229 * Returns a copy of the list of source files.
231 * @return list of source files
233 public List<SourceFile> get_source_files () {
234 return source_files;
238 * Returns a copy of the list of C source files.
240 * @return list of C source files
242 public List<string> get_c_source_files () {
243 return c_source_files;
247 * Adds the specified file to the list of source files.
249 * @param file a source file
251 public void add_source_file (SourceFile file) {
252 source_files.add (file);
256 * Adds the specified file to the list of C source files.
258 * @param file a C source file
260 public void add_c_source_file (string file) {
261 c_source_files.add (file);
265 * Returns a copy of the list of used packages.
267 * @return list of used packages
269 public List<string> get_packages () {
270 return packages;
274 * Returns whether the specified package is being used.
276 * @param pkg a package name
277 * @return true if the specified package is being used
279 public bool has_package (string pkg) {
280 return packages.contains (pkg);
284 * Adds the specified package to the list of used packages.
286 * @param pkg a package name
288 public void add_package (string pkg) {
289 packages.add (pkg);
293 * Visits the complete code tree file by file.
295 * @param visitor the visitor to be called when traversing
297 public void accept (CodeVisitor visitor) {
298 root.accept (visitor);
300 foreach (SourceFile file in source_files) {
301 file.accept (visitor);
305 public void add_define (string define) {
306 defines.add (define);
309 public bool is_defined (string define) {
310 return (define in defines);
313 public string? get_package_path (string pkg, string[] directories) {
314 var path = get_file_path (pkg + ".vapi", "vala/vapi", directories);
316 if (path == null) {
317 /* last chance: try the package compiled-in vapi dir */
318 var filename = Path.build_filename (Config.PACKAGE_DATADIR, "vapi", pkg + ".vapi");
319 if (FileUtils.test (filename, FileTest.EXISTS)) {
320 path = filename;
324 return path;
327 public string? get_gir_path (string gir, string[] directories) {
328 return get_file_path (gir + ".gir", "gir-1.0", directories);
331 string? get_file_path (string basename, string data_dir, string[] directories) {
332 string filename = null;
334 if (directories != null) {
335 foreach (string dir in directories) {
336 filename = Path.build_filename (dir, basename);
337 if (FileUtils.test (filename, FileTest.EXISTS)) {
338 return filename;
343 foreach (string dir in Environment.get_system_data_dirs ()) {
344 filename = Path.build_filename (dir, data_dir, basename);
345 if (FileUtils.test (filename, FileTest.EXISTS)) {
346 return filename;
350 return null;