1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/* valasourcefile.vala
*
* Copyright (C) 2006-2009 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
/**
* Represents a Vala source or VAPI package file.
*/
public class Vala.SourceFile {
/**
* The name of this source file.
*/
public string filename { get; private set; }
public string? relative_filename {
set {
if (Path.DIR_SEPARATOR != '/') {
// don't use backslashes internally,
// to avoid problems in #line / #include directives
string[] components = value.split ("\\");
_relative_filename = string.joinv ("/", components);
} else {
_relative_filename = value;
}
}
}
private string _package_name;
public string? package_name {
get {
if (file_type != SourceFileType.PACKAGE) {
return null;
}
if (_package_name == null) {
_package_name = Path.get_basename (filename[0:filename.last_index_of_char ('.')]);
}
return _package_name;
}
set {
_package_name = value;
}
}
private string? _installed_version = null;
private bool _version_requested = false;
/**
* The installed package version or null
*/
public string? installed_version {
get {
if (_version_requested) {
return _installed_version;
}
_version_requested = true;
if (package_name != null) {
_installed_version = context.pkg_config_modversion (package_name);
}
return _installed_version;
}
set {
_version_requested = value != null;
_installed_version = value;
}
}
/**
* Specifies whether this file is a VAPI package file.
*/
public SourceFileType file_type { get; set; }
/**
* Specifies whether this file came from the command line directly.
*/
public bool from_commandline { get; set; }
/**
* GIR Namespace for this source file, if it's a VAPI package
*/
public string gir_namespace { get; set; }
/**
* GIR Namespace version for this source file, if it's a VAPI package
*/
public string gir_version { get; set; }
public bool gir_ambiguous { get; set; }
/**
* The context this source file belongs to.
*/
public weak CodeContext context { get; set; }
public string? content {
get { return this._content; }
set {
this._content = value;
this.source_array = null;
}
}
/**
* If the file has been used (ie: if anything in the file has
* been emitted into C code as a definition or declaration).
*/
public bool used { get; set; }
private ArrayList<Comment> comments = new ArrayList<Comment> ();
public List<UsingDirective> current_using_directives { get; set; default = new ArrayList<UsingDirective> (); }
private List<CodeNode> nodes = new ArrayList<CodeNode> ();
string? _relative_filename;
private string csource_filename = null;
private string cinclude_filename = null;
private ArrayList<string> source_array = null;
private MappedFile mapped_file = null;
private string? _content = null;
/**
* Creates a new source file.
*
* @param filename source file name
* @return newly created source file
*/
public SourceFile (CodeContext context, SourceFileType type, string filename, string? content = null, bool cmdline = false) {
this.context = context;
this.file_type = type;
this.filename = filename;
this.content = content;
this.from_commandline = cmdline;
}
/**
* Adds a header comment to this source file.
*/
public void add_comment (Comment comment) {
comments.add (comment);
}
/**
* Returns the list of header comments.
*
* @return list of comments
*/
public unowned List<Comment> get_comments () {
return comments;
}
/**
* Adds a new using directive with the specified namespace.
*
* @param ns reference to namespace
*/
public void add_using_directive (UsingDirective ns) {
// do not modify current_using_directives, it should be considered immutable
// for correct symbol resolving
var old_using_directives = current_using_directives;
current_using_directives = new ArrayList<UsingDirective> ();
foreach (var using_directive in old_using_directives) {
current_using_directives.add (using_directive);
}
current_using_directives.add (ns);
}
/**
* Adds the specified code node to this source file.
*
* @param node a code node
*/
public void add_node (CodeNode node) {
nodes.add (node);
}
public void remove_node (CodeNode node) {
nodes.remove (node);
}
/**
* Returns the list of code nodes.
*
* @return code node list
*/
public unowned List<CodeNode> get_nodes () {
return nodes;
}
public void accept (CodeVisitor visitor) {
visitor.visit_source_file (this);
}
public void accept_children (CodeVisitor visitor) {
foreach (CodeNode node in nodes) {
node.accept (visitor);
}
}
private string get_subdir () {
if (context.basedir == null) {
return "";
}
// filename and basedir are already canonicalized
if (filename.has_prefix (context.basedir + "/")) {
var basename = Path.get_basename (filename);
var subdir = filename.substring (context.basedir.length, filename.length - context.basedir.length - basename.length);
while (subdir[0] == '/') {
subdir = subdir.substring (1);
}
return subdir;
}
return "";
}
private string get_destination_directory () {
if (context.directory == null) {
return get_subdir ();
}
return Path.build_path ("/", context.directory, get_subdir ());
}
private string get_basename () {
int dot = filename.last_index_of_char ('.');
return Path.get_basename (filename.substring (0, dot));
}
public string get_relative_filename () {
if (_relative_filename != null) {
return _relative_filename;
} else {
return Path.get_basename (filename);
}
}
/**
* Returns the filename to use when generating C source files.
*
* @return generated C source filename
*/
public string get_csource_filename () {
if (csource_filename == null) {
if (context.run_output) {
csource_filename = context.output + ".c";
} else if (context.ccode_only || context.save_csources) {
csource_filename = Path.build_path ("/", get_destination_directory (), get_basename () + ".c");
} else {
// temporary file
csource_filename = Path.build_path ("/", get_destination_directory (), get_basename () + ".vala.c");
}
}
return csource_filename;
}
/**
* Returns the filename to use when including the generated C header
* file.
*
* @return C header filename to include
*/
public string get_cinclude_filename () {
if (cinclude_filename == null) {
if (context.header_filename != null) {
cinclude_filename = Path.get_basename (context.header_filename);
if (context.includedir != null) {
cinclude_filename = Path.build_path ("/", context.includedir, cinclude_filename);
}
} else {
cinclude_filename = Path.build_path ("/", get_subdir (), get_basename () + ".h");
}
}
return cinclude_filename;
}
/**
* Returns the requested line from this file, loading it if needed.
*
* @param lineno 1-based line number
* @return the specified source line
*/
public string? get_source_line (int lineno) {
if (source_array == null) {
if (content != null) {
read_source_lines (content);
} else {
read_source_file ();
}
}
if (lineno < 1 || lineno > source_array.size) {
return null;
}
return source_array.get (lineno - 1);
}
/**
* Parses the input file into ::source_array.
*/
private void read_source_file () {
string cont;
try {
FileUtils.get_contents (filename, out cont);
} catch (FileError fe) {
return;
}
read_source_lines (cont);
}
private void read_source_lines (string cont)
{
source_array = new ArrayList<string> ();
string[] lines = cont.split ("\n", 0);
int idx;
for (idx = 0; lines[idx] != null; ++idx) {
source_array.add (lines[idx]);
}
}
public char* get_mapped_contents () {
if (content != null) {
return (char*) content;
}
if (mapped_file == null) {
try {
mapped_file = new MappedFile (filename, false);
} catch (FileError e) {
Report.error (null, "Unable to map file `%s': %s", filename, e.message);
return null;
}
}
return mapped_file.get_contents ();
}
public size_t get_mapped_length () {
if (content != null) {
return content.length;
}
if (mapped_file == null) {
return 0;
}
return mapped_file.get_length ();
}
public bool check (CodeContext context) {
foreach (CodeNode node in nodes) {
node.check (context);
}
return true;
}
}
public enum Vala.SourceFileType {
NONE,
SOURCE,
PACKAGE,
FAST
}
|