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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a Vala source or VAPI package file.
28 public class Vala
.SourceFile
{
30 * The name of this source file.
32 public string! filename
{ get; set construct; }
35 * The header comment of this source file.
37 public string comment
{ get; set; }
40 * Specifies whether this file is a VAPI package file.
42 public bool pkg
{ get; set; }
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; }
52 * Specifies whether this source file is the head of the cycle, if it is
55 public bool is_cycle_head
{ get; set; }
58 * Mark used for cycle detection.
61 * 1: currently visiting
64 public int mark
{ get; set; }
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
;
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) {
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
) {
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
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
);
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
214 * @param sym a symbol
215 * @param dep_type type of dependency
217 public void add_symbol_dependency (Symbol
! sym
, SourceFileDependencyType dep_type
) {
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
;
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 ());
240 } else if (sym
.node is FormalParameter
) {
241 var fp
= (FormalParameter
) sym
.node
;
242 t
= fp
.type_reference
.data_type
;
244 /* generic type parameter */
251 if (dep_type
== SourceFileDependencyType
.SOURCE
) {
252 if (t
.source_reference
.file
.pkg
) {
253 source_external_includes
.concat (t
.get_cheader_filenames ());
255 source_internal_includes
.concat (t
.get_cheader_filenames ());
260 if (t
.source_reference
.file
.pkg
) {
261 /* external package */
262 header_external_includes
.concat (t
.get_cheader_filenames ());
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
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
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
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
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
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
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
{