vala: Allow read-only properties
[vala-gnome.git] / vala / valasourcefile.vala
blobb056c012661bf211a9ae5d08c5c58dc92bde61eb
1 /* valasourcefile.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 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; }
34 public string? relative_filename {
35 set {
36 this._relative_filename = value;
40 private string _package_name;
42 public string? package_name {
43 get {
44 if (file_type != SourceFileType.PACKAGE) {
45 return null;
48 if (_package_name == null) {
49 _package_name = Path.get_basename (filename[0:filename.last_index_of_char ('.')]);
52 return _package_name;
54 set {
55 _package_name = value;
59 private string? _installed_version = null;
60 private bool _version_requested = false;
62 /**
63 * The installed package version or null
65 public string? installed_version {
66 get {
67 if (_version_requested) {
68 return _installed_version;
71 _version_requested = true;
73 if (_package_name != null) {
74 _installed_version = context.pkg_config_modversion (package_name);
77 return _installed_version;
79 set {
80 _version_requested = value != null;
81 _installed_version = value;
86 /**
87 * Specifies whether this file is a VAPI package file.
89 public SourceFileType file_type { get; set; }
91 /**
92 * Specifies whether this file came from the command line directly.
94 public bool from_commandline { get; set; }
96 /**
97 * GIR Namespace for this source file, if it's a VAPI package
100 public string gir_namespace { get; set; }
103 * GIR Namespace version for this source file, if it's a VAPI package
106 public string gir_version { get; set; }
109 * The context this source file belongs to.
111 public weak CodeContext context { get; set; }
113 public string? content {
114 get { return this._content; }
115 set {
116 this._content = value;
117 this.source_array = null;
122 * If the file has been used (ie: if anything in the file has
123 * been emitted into C code as a definition or declaration).
125 public bool used { get; set; }
128 * Whether this source-file was explicitly passed on the commandline.
130 public bool explicit { get; set; }
132 private ArrayList<Comment> comments = new ArrayList<Comment> ();
134 public List<UsingDirective> current_using_directives { get; set; default = new ArrayList<UsingDirective> (); }
136 private List<CodeNode> nodes = new ArrayList<CodeNode> ();
138 string? _relative_filename;
140 private string csource_filename = null;
141 private string cinclude_filename = null;
143 private ArrayList<string> source_array = null;
145 private MappedFile mapped_file = null;
147 private string? _content = null;
150 * Creates a new source file.
152 * @param filename source file name
153 * @return newly created source file
155 public SourceFile (CodeContext context, SourceFileType type, string filename, string? content = null, bool cmdline = false) {
156 this.context = context;
157 this.file_type = type;
158 this.filename = filename;
159 this.content = content;
160 this.from_commandline = cmdline;
164 * Adds a header comment to this source file.
166 public void add_comment (Comment comment) {
167 comments.add (comment);
171 * Returns a copy of the list of header comments.
173 * @return list of comments
175 public List<Comment> get_comments () {
176 return comments;
180 * Adds a new using directive with the specified namespace.
182 * @param ns reference to namespace
184 public void add_using_directive (UsingDirective ns) {
185 // do not modify current_using_directives, it should be considered immutable
186 // for correct symbol resolving
187 var old_using_directives = current_using_directives;
188 current_using_directives = new ArrayList<UsingDirective> ();
189 foreach (var using_directive in old_using_directives) {
190 current_using_directives.add (using_directive);
192 current_using_directives.add (ns);
196 * Adds the specified code node to this source file.
198 * @param node a code node
200 public void add_node (CodeNode node) {
201 nodes.add (node);
204 public void remove_node (CodeNode node) {
205 nodes.remove (node);
209 * Returns a copy of the list of code nodes.
211 * @return code node list
213 public List<CodeNode> get_nodes () {
214 return nodes;
217 public void accept (CodeVisitor visitor) {
218 visitor.visit_source_file (this);
221 public void accept_children (CodeVisitor visitor) {
222 foreach (CodeNode node in nodes) {
223 node.accept (visitor);
227 private string get_subdir () {
228 if (context.basedir == null) {
229 return "";
232 // filename and basedir are already canonicalized
233 if (filename.has_prefix (context.basedir + "/")) {
234 var basename = Path.get_basename (filename);
235 var subdir = filename.substring (context.basedir.length, filename.length - context.basedir.length - basename.length);
236 while (subdir[0] == '/') {
237 subdir = subdir.substring (1);
239 return subdir;
241 return "";
244 private string get_destination_directory () {
245 if (context.directory == null) {
246 return get_subdir ();
248 return Path.build_path ("/", context.directory, get_subdir ());
251 private string get_basename () {
252 int dot = filename.last_index_of_char ('.');
253 return Path.get_basename (filename.substring (0, dot));
256 public string get_relative_filename () {
257 if (_relative_filename != null) {
258 return _relative_filename;
259 } else {
260 return Path.get_basename (filename);
265 * Returns the filename to use when generating C source files.
267 * @return generated C source filename
269 public string get_csource_filename () {
270 if (csource_filename == null) {
271 if (context.run_output) {
272 csource_filename = context.output + ".c";
273 } else if (context.ccode_only || context.save_csources) {
274 csource_filename = Path.build_path ("/", get_destination_directory (), get_basename () + ".c");
275 } else {
276 // temporary file
277 csource_filename = Path.build_path ("/", get_destination_directory (), get_basename () + ".vala.c");
280 return csource_filename;
284 * Returns the filename to use when including the generated C header
285 * file.
287 * @return C header filename to include
289 public string get_cinclude_filename () {
290 if (cinclude_filename == null) {
291 if (context.header_filename != null) {
292 cinclude_filename = Path.get_basename (context.header_filename);
293 if (context.includedir != null) {
294 cinclude_filename = Path.build_path ("/", context.includedir, cinclude_filename);
296 } else {
297 cinclude_filename = Path.build_path ("/", get_subdir (), get_basename () + ".h");
300 return cinclude_filename;
304 * Returns the requested line from this file, loading it if needed.
306 * @param lineno 1-based line number
307 * @return the specified source line
309 public string? get_source_line (int lineno) {
310 if (source_array == null) {
311 if (content != null) {
312 read_source_lines (content);
313 } else {
314 read_source_file ();
317 if (lineno < 1 || lineno > source_array.size) {
318 return null;
320 return source_array.get (lineno - 1);
324 * Parses the input file into ::source_array.
326 private void read_source_file () {
327 string cont;
328 try {
329 FileUtils.get_contents (filename, out cont);
330 } catch (FileError fe) {
331 return;
333 read_source_lines (cont);
336 private void read_source_lines (string cont)
338 source_array = new ArrayList<string> ();
339 string[] lines = cont.split ("\n", 0);
340 int idx;
341 for (idx = 0; lines[idx] != null; ++idx) {
342 source_array.add (lines[idx]);
346 public char* get_mapped_contents () {
347 if (content != null) {
348 return (char*) content;
351 if (mapped_file == null) {
352 try {
353 mapped_file = new MappedFile (filename, false);
354 } catch (FileError e) {
355 Report.error (null, "Unable to map file `%s': %s".printf (filename, e.message));
356 return null;
360 return mapped_file.get_contents ();
363 public size_t get_mapped_length () {
364 if (content != null) {
365 return content.length;
368 return mapped_file.get_length ();
371 public bool check (CodeContext context) {
372 foreach (CodeNode node in nodes) {
373 node.check (context);
375 return true;
379 public enum Vala.SourceFileType {
380 NONE,
381 SOURCE,
382 PACKAGE,
383 FAST