3 * Copyright (C) 2006-2010 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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a node in the symbol tree.
28 public abstract class Vala
.Symbol
: CodeNode
{
30 * The parent of this symbol.
32 public weak Symbol? parent_symbol
{
43 * The scope this symbol opens.
45 public weak Scope owner
{
51 _scope
.parent_scope
= value
;
58 public string? gir_name
{
60 return _gir_name
== null ? name
: _gir_name
;
70 public string? name
{ get; set; }
73 * Specifies whether this symbol is active.
75 * Symbols may become inactive when they only apply to a part of a
76 * scope. This is used for local variables not declared at the beginning
77 * of the block to determine which variables need to be freed before
80 public bool active
{ get; set; default = true; }
83 * Specifies whether this symbol has been deprecated.
85 public bool deprecated
{ get; set; default = false; }
88 * Specifies what version this symbol has been deprecated since.
90 public string? deprecated_since
{ get; set; default = null; }
93 * Specifies the replacement if this symbol has been deprecated.
95 public string? replacement
{ get; set; default = null; }
98 * Specifies whether this symbol has been accessed.
100 public bool used
{ get; set; }
103 * Specifies the accessibility of this symbol. Public accessibility
104 * doesn't limit access. Default accessibility limits access to this
105 * program or library. Private accessibility limits access to instances
106 * of the contained type.
108 public SymbolAccessibility access
{ get; set; }
110 public Comment? comment
{ get; set; }
112 private List
<string> cheader_filenames
;
115 * Specifies whether this method explicitly hides a member of a base
118 public bool hides
{ get; set; }
121 * Check if this symbol is just internal API (and therefore doesn't need
122 * to be listed in header files for instance) by traversing parent symbols
123 * and checking their accessibility.
125 public bool is_internal_symbol () {
126 if (!external
&& external_package
) {
127 // non-external symbols in VAPI files are internal symbols
131 for (Symbol sym
= this
; null != sym
; sym
= sym
.parent_symbol
) {
132 if (sym
.access
== SymbolAccessibility
.PRIVATE
133 || sym
.access
== SymbolAccessibility
.INTERNAL
) {
141 public bool is_private_symbol () {
142 if (!external
&& external_package
) {
143 // non-external symbols in VAPI files are private symbols
147 for (Symbol sym
= this
; null != sym
; sym
= sym
.parent_symbol
) {
148 if (sym
.access
== SymbolAccessibility
.PRIVATE
) {
157 get { return _scope
; }
161 * Specifies whether the implementation is external, for example in
162 * a separate C source file or in an external library.
164 public bool external
{ get; set; }
167 * Specifies whether the implementation is in an external library.
169 public bool external_package
{
171 return source_type
== SourceFileType
.PACKAGE
;
176 * Gets the SourceFileType of the source file that this symbol
177 * came from, or SourceFileType.NONE.
179 public SourceFileType source_type
{
181 if (source_reference
!= null) {
182 return source_reference
.file
.file_type
;
184 return SourceFileType
.NONE
;
189 private weak Scope _owner
;
190 private Scope _scope
;
191 private string? _gir_name
= null;
193 public Symbol (string? name
, SourceReference? source_reference
, Comment? comment
= null) {
195 this
.source_reference
= source_reference
;
196 this
.comment
= comment
;
197 _scope
= new
Scope (this
);
201 * Returns the fully expanded GIR name of this symbol
203 * @return full GIR name
205 public string get_full_gir_name () {
206 if (parent_symbol
== null) {
211 return parent_symbol
.get_full_gir_name ();
214 if (parent_symbol
.get_full_gir_name () == null) {
218 if (name
.has_prefix (".")) {
219 return "%s%s".printf (parent_symbol
.get_full_gir_name (), gir_name
);
221 return "%s.%s".printf (parent_symbol
.get_full_gir_name (), gir_name
);
226 * Returns the fully expanded name of this symbol for use in
227 * human-readable messages.
231 public string get_full_name () {
232 if (parent_symbol
== null) {
237 return parent_symbol
.get_full_name ();
240 if (parent_symbol
.get_full_name () == null) {
244 if (name
.has_prefix (".")) {
245 return "%s%s".printf (parent_symbol
.get_full_name (), name
);
247 return "%s.%s".printf (parent_symbol
.get_full_name (), name
);
252 * Returns the camel case string to be prepended to the name of members
253 * of this symbol when used in C code.
255 * @return the camel case prefix to be used in C code
257 public virtual string get_cprefix () {
266 * Returns the C name of this symbol in lower case. Words are
267 * separated by underscores. The lower case C name of the parent symbol
268 * is prefix of the result, if there is one.
270 * @param infix a string to be placed between namespace and data type
272 * @return the lower case name to be used in C code
274 public virtual string?
get_lower_case_cname (string? infix
= null) {
279 * Returns the string to be prefixed to members of this symbol in
280 * lower case when used in C code.
282 * @return the lower case prefix to be used in C code
284 public virtual string get_lower_case_cprefix () {
288 static List
<string> _empty_string_list
;
291 * Returns a list of C header filenames users of this symbol must
294 * @return list of C header filenames for this symbol
296 public virtual List
<string> get_cheader_filenames () {
297 if (cheader_filenames
== null || cheader_filenames
.size
== 0) {
298 // parent_symbol can be null on incremental parsing
299 if (parent_symbol
!= null) {
300 /* default to header filenames of the namespace */
301 var parent_header_filenames
= parent_symbol
.get_cheader_filenames ();
302 if (parent_header_filenames
.size
> 0) {
303 return parent_header_filenames
;
307 if (source_reference
!= null && !external_package
) {
308 // don't add default include directives for VAPI files
309 add_cheader_filename (source_reference
.file
.get_cinclude_filename ());
311 if (_empty_string_list
== null) {
312 _empty_string_list
= new ArrayList
<string> ();
314 return _empty_string_list
;
317 return cheader_filenames
;
321 * Converts a string from CamelCase to lower_case.
323 * @param camel_case a string in camel case
324 * @return the specified string converted to lower case
326 public static string camel_case_to_lower_case (string camel_case
) {
327 if ("_" in camel_case
) {
328 // do not insert additional underscores if input is not real camel case
329 return camel_case
.down ();
332 var result_builder
= new
StringBuilder ("");
334 weak string i
= camel_case
;
337 while (i
.length
> 0) {
338 unichar c
= i
.get_char ();
339 if (c
.isupper () && !first
) {
340 /* current character is upper case and
341 * we're not at the beginning */
342 weak string t
= i
.prev_char ();
343 bool prev_upper
= t
.get_char ().isupper ();
345 bool next_upper
= t
.get_char ().isupper ();
346 if (!prev_upper
|| (i
.length
>= 2 && !next_upper
)) {
347 /* previous character wasn't upper case or
348 * next character isn't upper case*/
349 long len
= result_builder
.str
.length
;
350 if (len
!= 1 && result_builder
.str
.offset (len
- 2).get_char () != '_') {
351 /* we're not creating 1 character words */
352 result_builder
.append_c ('_');
357 result_builder
.append_unichar (c
.tolower ());
363 return result_builder
.str
;
367 * Converts a string from lower_case to CamelCase.
369 * @param lower_case a string in lower case
370 * @return the specified string converted to camel case
372 public static string lower_case_to_camel_case (string lower_case
) {
373 var result_builder
= new
StringBuilder ("");
375 weak string i
= lower_case
;
377 bool last_underscore
= true;
378 while (i
.length
> 0) {
379 unichar c
= i
.get_char ();
381 last_underscore
= true;
382 } else if (c
.isupper ()) {
383 // original string is not lower_case, don't apply transformation
385 } else if (last_underscore
) {
386 result_builder
.append_unichar (c
.toupper ());
387 last_underscore
= false;
389 result_builder
.append_unichar (c
);
395 return result_builder
.str
;
398 // get the top scope from where this symbol is still accessible
399 public Scope?
get_top_accessible_scope (bool is_internal
= false) {
400 if (access
== SymbolAccessibility
.PRIVATE
) {
401 // private symbols are accessible within the scope where the symbol has been declared
405 if (access
== SymbolAccessibility
.INTERNAL
) {
409 if (parent_symbol
== null) {
410 // this is the root symbol
412 // only accessible within the same library
421 // if this is a public symbol, it's equally accessible as the parent symbol
422 return parent_symbol
.get_top_accessible_scope (is_internal
);
425 public virtual bool is_instance_member () {
426 bool instance
= true;
428 var f
= (Field
) this
;
429 instance
= (f
.binding
== MemberBinding
.INSTANCE
);
430 } else if (this is Method
) {
431 var m
= (Method
) this
;
432 if (!(m is CreationMethod
)) {
433 instance
= (m
.binding
== MemberBinding
.INSTANCE
);
435 } else if (this is Property
) {
436 var prop
= (Property
) this
;
437 instance
= (prop
.binding
== MemberBinding
.INSTANCE
);
438 } else if (this is EnumValue
) {
440 } else if (this is ErrorCode
) {
447 public virtual bool is_class_member () {
450 var f
= (Field
) this
;
451 isclass
= (f
.binding
== MemberBinding
.CLASS
);
452 } else if (this is Method
) {
453 var m
= (Method
) this
;
454 if (!(m is CreationMethod
)) {
455 isclass
= (m
.binding
== MemberBinding
.CLASS
);
457 } else if (this is Property
) {
458 var prop
= (Property
) this
;
459 isclass
= (prop
.binding
== MemberBinding
.CLASS
);
460 } else if (this is EnumValue
) {
462 } else if (this is ErrorCode
) {
470 * Process a [Deprecated] attribute
472 public virtual void process_deprecated_attribute (Attribute attr
) {
473 if (attr
.name
!= "Deprecated") {
479 if (attr
.has_argument ("since")) {
480 deprecated_since
= attr
.get_string ("since");
482 if (attr
.has_argument ("replacement")) {
483 replacement
= attr
.get_string ("replacement");
488 * Check to see if the symbol has been deprecated, and emit a warning
491 public bool check_deprecated (SourceReference? source_ref
= null) {
493 if (!CodeContext
.get ().deprecated
) {
494 Report
.warning (source_ref
, "%s %s%s".printf (get_full_name (), (deprecated_since
== null) ?
"is deprecated" : "has been deprecated since %s".printf (deprecated_since
), (replacement
== null) ?
"" : ". Use %s".printf (replacement
)));
503 * Sets the C header filename of this namespace to the specified
506 * @param cheader_filename header filename
508 public void set_cheader_filename (string cheader_filename
) {
509 cheader_filenames
= new ArrayList
<string> ();
510 cheader_filenames
.add (cheader_filename
);
514 * Adds a filename to the list of C header filenames users of this data
517 * @param filename a C header filename
519 public void add_cheader_filename (string filename
) {
520 if (cheader_filenames
== null) {
521 cheader_filenames
= new ArrayList
<string> ();
523 cheader_filenames
.add (filename
);
526 public Symbol?
get_hidden_member () {
529 if (parent_symbol is Class
) {
530 var cl
= ((Class
) parent_symbol
).base_class
;
532 sym
= cl
.scope
.lookup (name
);
533 if (sym
!= null && sym
.access
!= SymbolAccessibility
.PRIVATE
) {
538 } else if (parent_symbol is Struct
) {
539 var st
= ((Struct
) parent_symbol
).base_struct
;
541 sym
= st
.scope
.lookup (name
);
542 if (sym
!= null && sym
.access
!= SymbolAccessibility
.PRIVATE
) {
552 // check whether this symbol is at least as accessible as the specified symbol
553 public bool is_accessible (Symbol sym
) {
554 Scope sym_scope
= sym
.get_top_accessible_scope ();
555 Scope this_scope
= this
.get_top_accessible_scope ();
556 if ((sym_scope
== null && this_scope
!= null)
557 || (sym_scope
!= null && !sym_scope
.is_subscope_of (this_scope
))) {
564 public virtual void add_namespace (Namespace ns
) {
565 Report
.error (ns
.source_reference
, "unexpected declaration");
568 public virtual void add_class (Class cl
) {
569 Report
.error (cl
.source_reference
, "unexpected declaration");
572 public virtual void add_interface (Interface iface
) {
573 Report
.error (iface
.source_reference
, "unexpected declaration");
576 public virtual void add_struct (Struct st
) {
577 Report
.error (st
.source_reference
, "unexpected declaration");
580 public virtual void add_enum (Enum en
) {
581 Report
.error (en
.source_reference
, "unexpected declaration");
584 public virtual void add_error_domain (ErrorDomain edomain
) {
585 Report
.error (edomain
.source_reference
, "unexpected declaration");
588 public virtual void add_delegate (Delegate d
) {
589 Report
.error (d
.source_reference
, "unexpected declaration");
592 public virtual void add_constant (Constant constant
) {
593 Report
.error (constant
.source_reference
, "unexpected declaration");
596 public virtual void add_field (Field f
) {
597 Report
.error (f
.source_reference
, "unexpected declaration");
600 public virtual void add_method (Method m
) {
601 Report
.error (m
.source_reference
, "unexpected declaration");
604 public virtual void add_property (Property prop
) {
605 Report
.error (prop
.source_reference
, "unexpected declaration");
608 public virtual void add_signal (Signal sig
) {
609 Report
.error (sig
.source_reference
, "unexpected declaration");
612 public virtual void add_constructor (Constructor c
) {
613 Report
.error (c
.source_reference
, "unexpected declaration");
616 public virtual void add_destructor (Destructor d
) {
617 Report
.error (d
.source_reference
, "unexpected declaration");
621 public enum Vala
.SymbolAccessibility
{
628 public enum Vala
.MemberBinding
{