summaryrefslogtreecommitdiff
path: root/vala/valasourcefile.vala
blob: b513f513b33b697bdbda7b81a4cdc91c8e7df211 (plain)
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
/* valasourcefile.vala
 *
 * Copyright (C) 2006-2007  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;
using Gee;

/**
 * Represents a Vala source or VAPI package file.
 */
public class Vala.SourceFile : Object {
	/**
	 * The name of this source file.
	 */
	public string! filename { get; set; }
	
	/**
	 * The header comment of this source file.
	 */
	public string comment { get; set; }
	
	/**
	 * Specifies whether this file is a VAPI package file.
	 */
	public bool pkg { get; set; }
	
	/**
	 * Specifies the dependency cycle this source file is member of. If this
	 * source file is in a cycle, all type definitions of that cycle will
	 * only be written to the C header file of the cycle head.
	 */
	public SourceFileCycle cycle { get; set; }
	
	/**
	 * Specifies whether this source file is the head of the cycle, if it is
	 * in a cycle at all.
	 */
	public bool is_cycle_head { get; set; }
	
	/**
	 * Mark used for cycle detection.
	 *
	 * 0: not yet visited
	 * 1: currently visiting
	 * 2: already visited
	 */
	public int mark { get; set; }
	
	/**
	 * The context this source file belongs to.
	 */
	public weak CodeContext context { get; set; }

	private Gee.List<NamespaceReference> using_directives = new ArrayList<NamespaceReference> ();

	private Gee.List<CodeNode> nodes = new ArrayList<CodeNode> ();
	
	private string cheader_filename = null;
	private string csource_filename = null;
	private string cinclude_filename = null;
	
	private Gee.List<string> header_external_includes = new ArrayList<string> ();
	private Gee.List<string> header_internal_includes = new ArrayList<string> ();
	private Gee.List<string> source_external_includes = new ArrayList<string> ();
	private Gee.List<string> source_internal_includes = new ArrayList<string> ();
	
	private Gee.List<weak SourceFile> header_internal_full_dependencies = new ArrayList<weak SourceFile> ();
	private Gee.List<weak SourceFile> header_internal_dependencies = new ArrayList<weak SourceFile> ();
	
	/**
	 * Creates a new source file.
	 *
	 * @param filename source file name
	 * @param pkg      true if this is a VAPI package file
	 * @return         newly created source file
	 */
	public SourceFile (construct CodeContext! context, construct string! filename, construct bool pkg = false) {
	}
	
	/**
	 * Adds a new using directive with the specified namespace.
	 *
	 * @param ns reference to namespace
	 */
	public void add_using_directive (NamespaceReference! ns) {
		using_directives.add (ns);
	}
	
	/**
	 * Returns a copy of the list of using directives.
	 *
	 * @return using directive list
	 */
	public Collection<NamespaceReference> get_using_directives () {
		return new ReadOnlyCollection<NamespaceReference> (using_directives);
	}
	
	/**
	 * Adds the specified code node to this source file.
	 *
	 * @param node a code node
	 */
	public void add_node (CodeNode! node) {
		nodes.add (node);
	}

	/**
	 * Returns a copy of the list of code nodes.
	 *
	 * @return code node list
	 */
	public Collection<CodeNode> get_nodes () {
		return new ReadOnlyCollection<CodeNode> (nodes);
	}

	public void accept (CodeVisitor! visitor) {
		visitor.visit_source_file (this);
	}

	public void accept_children (CodeVisitor! visitor) {
		foreach (NamespaceReference ns_ref in using_directives) {
			ns_ref.accept (visitor);
		}
		
		foreach (CodeNode node in nodes) {
			node.accept (visitor);
		}
	}

	/**
	 * Returns the filename to use when generating C header files.
	 *
	 * @return generated C header filename
	 */
	public string! get_cheader_filename () {
		if (cheader_filename == null) {
			var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
			basename = Path.get_basename (basename);
			if (context.directory != null && context.directory != "") {
				cheader_filename = "%s/%s.h".printf (context.directory, basename);
			} else {
				cheader_filename = "%s.h".printf (basename);
			}
		}
		return cheader_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) {
			var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
			basename = Path.get_basename (basename);
			if (context.directory != null && context.directory != "") {
				csource_filename = "%s/%s.c".printf (context.directory, basename);
			} else {
				csource_filename = "%s.c".printf (basename);
			}
		}
		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) {
			var basename = filename.ndup ((uint) (filename.len () - ".vala".len ()));
			basename = Path.get_basename (basename);
			if (context.library != null) {
				cinclude_filename = "%s/%s.h".printf (context.library, basename);
			} else {
				cinclude_filename = "%s.h".printf (basename);
			}
		}
		return cinclude_filename;
	}
	
	/**
	 * Adds the specified symbol to the list of symbols code in this source
	 * file depends on.
	 *
	 * @param sym      a symbol
	 * @param dep_type type of dependency
	 */
	public void add_symbol_dependency (Symbol! sym, SourceFileDependencyType dep_type) {
		if (pkg) {
			return;
		}

		Symbol s;
		
		if (sym is DataType ||
		    sym is Method ||
		    sym is Field ||
		    sym is Property ||
		    sym is Constant) {
			s = sym;
		} else if (sym is FormalParameter) {
			var fp = (FormalParameter) sym;
			s = fp.type_reference.data_type;
			if (s == null) {
				/* generic type parameter */
				return;
			}
		} else {
			return;
		}
		
		if (dep_type == SourceFileDependencyType.SOURCE) {
			if (s.source_reference.file.pkg) {
				foreach (string fn in s.get_cheader_filenames ()) {
					source_external_includes.add (fn);
				}
			} else {
				foreach (string fn in s.get_cheader_filenames ()) {
					source_internal_includes.add (fn);
				}
			}
			return;
		}

		if (s.source_reference.file.pkg) {
			/* external package */
			foreach (string fn in s.get_cheader_filenames ()) {
				header_external_includes.add (fn);
			}
			return;
		}
		
		if (dep_type == SourceFileDependencyType.HEADER_FULL || (s is DataType && !((DataType)s).is_reference_type ())) {
			foreach (string fn in s.get_cheader_filenames ()) {
				header_internal_includes.add (fn);
			}
			header_internal_full_dependencies.add (s.source_reference.file);
		}

		header_internal_dependencies.add (s.source_reference.file);
	}

	/**
	 * Returns the list of external includes the generated C header file
	 * requires.
	 *
	 * @return external include list for C header file
	 */
	public Collection<string> get_header_external_includes () {
		return new ReadOnlyCollection<string> (header_external_includes);
	}
	
	/**
	 * Adds the specified filename to the list of package-internal includes
	 * the generated C header file requires.
	 *
	 * @param include internal include for C header file
	 */
	public void add_header_internal_include (string! include) {
		/* skip includes to self */
		if (include != get_cinclude_filename ()) {
			header_internal_includes.add (include);
		}
	}
	
	/**
	 * Returns the list of package-internal includes the generated C header
	 * file requires.
	 *
	 * @return internal include list for C header file
	 */
	public Collection<string> get_header_internal_includes () {
		return new ReadOnlyCollection<string> (header_internal_includes);
	}
	
	/**
	 * Returns the list of external includes the generated C source file
	 * requires.
	 *
	 * @return include list for C source file
	 */
	public Collection<string> get_source_external_includes () {
		return new ReadOnlyCollection<string> (source_external_includes);
	}
	
	/**
	 * Returns the list of package-internal includes the generated C source
	 * file requires.
	 *
	 * @return include list for C source file
	 */
	public Collection<string> get_source_internal_includes () {
		return new ReadOnlyCollection<string> (source_internal_includes);
	}
	
	/**
	 * Returns the list of source files the generated C header file requires
	 * definitely.
	 *
	 * @return definite source file dependencies
	 */
	public Collection<weak SourceFile> get_header_internal_full_dependencies () {
		return new ReadOnlyCollection<weak SourceFile> (header_internal_full_dependencies);
	}
	
	/**
	 * Returns the list of source files the generated C header file loosely
	 * depends on.
	 *
	 * @return loose source file dependencies
	 */
	public Collection<weak SourceFile> get_header_internal_dependencies () {
		return new ReadOnlyCollection<weak SourceFile> (header_internal_dependencies);
	}
}

public enum Vala.SourceFileDependencyType {
	HEADER_FULL,
	HEADER_SHALLOW,
	SOURCE
}