1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
6 <title>Hackers' Guide to Vala</title>
8 <!-- If you edit this document, list yourself as an author or
9 contributor, and put an entry in the revision history. -->
13 <firstname>Rodney</firstname>
14 <surname>Lorrimar</surname>
15 <contrib>Document Author</contrib>
19 <firstname>Jürg</firstname>
20 <surname>Billeter</surname>
21 <contrib>Vala Author</contrib>
25 <firstname>Raffaele</firstname>
26 <surname>Sandrini</surname>
27 <contrib>Vala Author</contrib>
31 <firstname>Philip</firstname>
32 <surname>van Hoof</surname>
33 <contrib>Contribution of the building Vala section.</contrib>
37 <firstname>Rodney</firstname>
38 <surname>Lorrimar</surname>
39 <email>rodney@rodney.id.au</email>
43 <date>23 Apr 2008</date>
47 <revnumber>0.11</revnumber>
48 <date>23 Apr 2008</date>
49 <revremark>Pre draft</revremark>
55 The goal of this document is to provide a single point of
56 information for developers interested in improving Vala.
60 It is hoped that that this document will encourage more Vala users
61 to contribute to Vala by finding/fixing bugs, writing
62 documentation, writing test-cases, and implementing new features.
66 In the opinion of this document's author, a quality Vala 1.0 is an
67 important part of the future of the GNOME Platform, because it
68 will simplify the task of creating and maintaining excellent
69 language-neutral libraries, which are necessary for the next
70 generation of applications.
74 The Vala code is fresh, and easy to read. The variable and class
75 names are descriptive, and one often has a general feel of what
76 the code is supposed to do, so the sparse comments are generally
77 not a problem. However, because it is a compiler, Vala is
78 inevitably long, and its call stack deep. This document should
79 provide a high-level view of how Vala is put together.
83 Eventually, most of this document will be merged into the source
84 code and generated from there. However the necessary tool has not
88 <sect1><title>Environment Issues</title>
90 <sect2><title>Getting the source</title>
92 The Vala source code is available from GNOME svn.
96 <ulink url="http://svn.gnome.org/viewcvs/vala/trunk/">http://svn.gnome.org/svn/vala/trunk</ulink>
99 <sect3><title>Git</title>
102 Git allows developers without SVN access to maintain their own
103 branches and simplifies the task of submitting patches back to the
108 It allows all users to participate in development on more equal
109 terms with the developers who have SVN access.
114 url="http://www.flavio.castelli.name/howto_use_git_with_svn">these
115 instructions</ulink> to get your own git tree from the svn.
121 <sect2><title>Getting this document</title>
124 For the time being, until this document is of suitable quality to
125 be released, it will live in a separate git tree. Anyone
126 interested can make edits on the <ulink
127 url="http://repo.or.cz/mob.html">mob branch</ulink>.
132 <ulink url="http://repo.or.cz/w/vala-hackers-guide.git">git://repo.or.cz/vala-hackers-guide.git</ulink>
136 <sect2><title>Compiling from SVN</title>
138 <sect3><title>Prerequisites</title>
141 Install packages like a C compiler, glib-2.0. Older versions (0.20
142 and older) require flex and bison. TODO: complete this list
146 <sect3><title>Step One</title>
148 Grab yourself a vala to c compiled release of vala. Vala is
149 self-hosting so it needs another vala compiler to compile
150 itself. Since vala compiles to C code, you can use a tarball
151 release of vala to compile the generated C code of such a release
152 into a working Vala compiler, that will compile the Vala compiler
153 which you might have checked out from the repository.
156 <ulink url="http://live.gnome.org/Vala/Release">http://live.gnome.org/Vala/Release</ulink>
159 <table><title>Required release versions</title>
162 <row><entry>SVN Version</entry><entry>Release required to build</entry></row>
165 <row><entry>1207 and later</entry><entry>0.20</entry></row>
166 <row><entry>xxxx</entry><entry>0.17</entry></row>
173 <sect3><title>Step Two</title>
175 Compiling the release which you just grabbed:
178 tar jxvf vala-x.y.z.tar.bz2
180 ./configure --prefix=/opt/vala-release
181 make && sudo make install
187 <sect3><title>Step Three</title>
189 Compiling the newest Vala from the repository:
194 svn co svn co svn+ssh://[user]@svn.gnome.org/svn/vala/trunk vala
196 export VALAC=/opt/vala-release/bin/vala
197 ./autogen.sh --prefix=/opt/vala
198 make && sudo make install
203 Maybe you want to compile the new Vala with itself. Then you
204 simply go over it again:
208 export VALAC=/opt/vala/bin/vala
209 ./autogen.sh --prefix=/opt/vala
210 make && sudo make install
215 <sect3><title>Out-of-tree builds</title>
218 The build works, but <filename>*.[ch]</filename> files are left
219 behind in the source directory.
224 <sect2><title>Setting up your editor - GNU Emacs</title>
226 The csharp-mode available from fixme does a good-enough job of
231 "Intellisense" using the csharp Semantic Wisent parser partially
236 This document's author intends to extend csharp-mode to support
237 Vala and to create a new Semantic parser for Vala, though no
238 significant work has been done yet.
242 Here is something you can put in .emacs which will make editing
243 Vala in emacs easier.
247 ;; C# mode (used for vala!)
248 (require 'csharp-mode)
250 (defcustom vala-mode-hook nil
251 "*Hook called by `vala-mode'."
259 ; (kill-all-local-variables)
260 ; (c-initialize-cc-mode t)
262 (setq mode-name "Vala")
263 (run-hooks 'c-mode-common-hook)
264 (c-set-style "linux")
265 (setq indent-tabs-mode t)
266 (setq c-basic-offset 4)
268 ; auto stuff isn't good yet
269 (c-toggle-auto-newline -1)
270 (c-toggle-hungry-state -1)
271 (run-hooks 'vala-mode-hook))
273 (add-to-list 'auto-mode-alist '("\\.vala\\'" . vala-mode))
275 ;; If you have CEDET or Semantic loaded, uncomment this line
276 ;(semantic-load-enable-code-helpers)
281 <sect2><title>Coding Style</title>
284 The coding style used in Vala itself seems to be a variation of
285 the GTK+ coding style.
289 <listitem><para>Tabs rather than spaces.</para></listitem>
290 <listitem><para>Tab width unspecified, but 4 works
291 well.</para></listitem>
292 <listitem><para>Hanging braces.</para></listitem>
293 <listitem><para>Cuddled else.</para></listitem>
294 <listitem><para>Braces not necessary for single-line
295 blocks.</para></listitem>
296 <listitem><para>Variable and method identifiers in lowercase,
297 words seperated by underscores.</para></listitem>
298 <listitem><para>Type identifiers in CamelCase.</para></listitem>
299 <listitem><para>Enum members and constants in ALL_CAPS, words
300 seperated by underscores.</para></listitem>
301 <listitem><para>C-style /* comments. */</para></listitem>
302 <listitem><para>Hungarian notation not used.</para></listitem>
303 <listitem><para>Variables often declared without type
304 (i.e. "var").</para></listitem>
305 <listitem><para>No line-length limit.</para></listitem>
306 <listitem><para>No function-length limit.</para></listitem>
307 <listitem><para>Space between method name and parameters' opening
308 parenthesis.</para></listitem>
309 <listitem><para>Property "get", "set", "default" declaration all
310 on one line, seperated by semicolons, if default implementations
311 are used.</para></listitem>
312 <listitem><para>If properties have implementations, then "get {",
313 "set {" open new lines.</para></listitem>
314 <listitem><para>Attributes on their own line.</para></listitem>
315 <listitem><para>JavaDoc-style commenting on types, methods,
316 variables.</para></listitem>
317 <listitem><para>Header at top of file contains:
321 * Copyright (C) 20yy-20yy Copyright Holder <email@address>
326 * Programmer Name <programmer@email>
328 </programlisting></para></listitem>
332 <sect2><title>Files</title>
334 Vala source files are named in the GTK+ style, i.e. all lowercase,
335 which no separators between words, in the format
336 namespaceclassname.vala. For example, the filename for
337 Vala.FormalParameter is valaformalparameter.vala.
341 Classes can be split across files, and this is often done for
342 classes with a lot of methods, or very long methods.
346 For the Vala compiler and library there is only one namespace, and
347 it is called "Vala". Don't put "using Vala;"; instead qualify the
348 name of types you declare. For example "class Vala.FormalParameter
353 <sect2><title>Website, Mailing List, Bug Tracker, IRC</title>
356 <listitem><para>http://www.valaproject.org</para></listitem>
357 <listitem><para><email>vala-list@gnome.org</email></para></listitem>
358 <listitem><para>GNOME Bugzilla - http://bugs.gnome.org</para></listitem>
359 <listitem><para>#vala on GIMPnet</para></listitem>
363 <sect2><title>Project Maintainers</title>
365 The principle authors and project maintainers are Jürg Billeter
366 and Raffaele Sandrini.
370 <sect2><title>License</title>
372 Vala compiler is licensed under LGPL 2.1, so that proprietary
373 programs compiled with Vala can be distributed under different
374 licenses and possibly without source code.
379 <sect1 id="ch-valac"><title>The Vala Compiler</title>
381 I suppose the best place to start is <command>valac</command>, the
382 tool which Vala programmers know the best.
385 <sect2><title>Vala in a Nutshell</title>
387 The Vala compiler <command>valac</command> is a small shell around
388 libvala which handles command-line arguments, locates the sources
389 and libraries which are required, and drives the compilation
393 <figure><title>How <command>valac</command> is linked</title>
394 <graphic fileref="figures/valac-link.png"/>
398 All the important work such as parsing, error checking/reporting,
399 code generation, calling <command>gcc</command>, is done in libvala.
403 The code for <command>valac</command> can be found in
404 <filename>compiler/valacompiler.vala</filename>.
407 <sect3><title>Command-line Options</title>
409 These are handled in the normal way by the Vala binding to
410 <classname>GLib.OptionContext</classname>. Most of the instance
411 variables in <classname>Vala.Compiler</classname> are referenced in
412 the options array. It's not very interesting.
416 <sect3><title>The Compilation Procedure and Vala.CodeContext</title>
418 Vala.Compiler plugs together the classes of libvala in a big
419 pipeline. This modular design means that different Vala tools can
420 share their code. Most modules have impressive-sounding names like
421 AlgorithmicFrobnicator.
424 <procedure><title>valac Pipeline</title>
425 <step><para>Initialize context with command-line
426 options.</para></step>
427 <step><para>Plug in a code generator.</para></step>
428 <step><para>Add packages from command-line and the non-optional
430 <step><para>Add sources, Vala, VAPI, and C from command-line.</para></step>
431 <step><para>Parse everything.</para></step>
432 <step><para>Process attributes.</para></step>
433 <step><para>Resolve symbols.</para></step>
434 <step><para>Setup a DBus Binding Provider.</para></step>
435 <step><para>Setup the Semantic Analyzer.</para></step>
436 <step><para>Plug in the DBus Binding Provider to the Semantic Analyzer.</para></step>
437 <step><para>Do the Semantic Analysis</para></step>
438 <step><para>Build Config.</para></step>
439 <step><para>Check that null variables are treated
440 properly.</para></step>
441 <step><para>Analyse code with the memory manager. ???</para></step>
442 <step><para>Use the code generator to emit code.</para></step>
443 <step><para>Write out VAPI and GIDL files, if a library is being
444 compiled.</para></step>
445 <step><para>Compile the generated C code.</para></step>
449 The individual steps will be explained later, but first
450 <classname>Vala.CodeContext</classname>, the data structure which
451 holds everything together. It stores the compile options which were
452 specified on the command line, and a list of source files to
453 compile. There is only one <classname>CodeContext</classname>
454 instantiated and its reference is passed around a lot, so
455 effectively it's a global variable.
459 Vala.CodeContext is the root of the code tree, because
460 it contains the root Namespace, which holds references to all
461 parsed code nodes. In addition to the code tree, the context
462 contains a reference to a code generator object. This object
463 walks the code tree and generates code.
467 Vala.CodeContext contains an important method called accept,
468 which initiates a depth-first traversal of the code tree. This
469 method, and the CodeVisitor pattern will be discussed later.
473 ignore_node method contains the logic for the "Conditional"
478 For every one of the 77 (or so) code node types, there is a
479 create_code_node_type method which constructs the code node and
480 links it with the code generator. The parser will use these
481 methods to create code nodes as it parses them, which know how to
485 <figure><title>Data diagram</title>
486 <graphic fileref="figures/valac-data.png"/>
490 The Vala code tree is an abstract syntax tree (AST) built by parsing
491 the Vala sources. For example, if you see a class called
492 <classname>Vala.Destructor</classname> which inherits
493 <classname>Vala.Symbol</classname>, then it is a part of the
494 AST. Data structures for the AST and the parser which builds it are
495 in the <filename>vala</filename> directory.
499 Vala also uses a tree to represent the C code that will be
500 output. Data structures for the C code tree are in the
501 <filename>ccode</filename> directory.
505 The machinery which transforms the Vala AST into a C code tree is in
506 the <filename>gobject</filename> directory. The reason it's there is
507 because Vala outputs "GObject C" rather than plain C. GObject C is
508 arranged in a particular object-oriented way, uses reference counted
509 memory management, signals, data-structures, and links with glib.
513 Vala is split upon these lines, most probably to break the system
514 into conceptually-related, understandable chunks. However, with
515 suitable modifications, the different modules could be replaced.
516 Conceivably, Vala could produce non-GObject C code. More
517 realistically, Vala could produce intermediate code as a <ulink
518 url="http://gcc.gnu.org/frontends.html">GCC frontend</ulink>.
523 <sect2><title>Parser</title>
525 The parser reads Vala and VAPI code and outputs an AST. In the eyes
526 of the parser, Vala and VAPI are the same thing. The difference to
527 us is that VAPI files never have method definitions, but the parser
528 will read pretty much everything as long as it seems syntactically
529 correct. Most errors are caught later by the Semantic Analyzer.
533 Before 0.3.1, Vala's parser was the classic flex scanner and Bison
534 LALR parser combination. But as of <ulink
535 url="http://svn.gnome.org/viewvc/vala?view=revision&revision=1194">SVN
536 1194</ulink>, the parser is a hand-crafted recursive descent
537 parser. The parser is in <filename>vala/valaparser.vala</filename>
538 and its lexer is in <filename>vala/valascanner.vala</filename>.
542 The entry point of the parser is
543 <code>Vala.Parser.parse()</code>. This method is called by
544 <code>Vala.Compiler.run()</code>. <classname>Vala.Parser</classname>
545 is an implementation of <classname>Vala.CodeVisitor</classname> for
549 <sect3><title>Visitors and Ping Pong</title>
552 <classname>CodeVisitor</classname> is an abstract base class which
553 provides 75 empty virtual methods for each kind of code node. A
554 class which inherits <classname>CodeVisitor</classname> is supposed
555 to do some kind of processing of the code tree. Here are all the
556 <classname>CodeVisitor</classname> classes in Vala:
560 public class Vala.CodeVisitor : Object
561 public class Vala.AttributeProcessor : CodeVisitor
562 public class Vala.CFGBuilder : CodeVisitor
563 public class Vala.CodeGenerator : CodeVisitor
564 public class Vala.GIdlParser : CodeVisitor
565 public class Vala.GIdlWriter : CodeVisitor
566 public class Vala.InterfaceWriter : CodeVisitor
567 public class Vala.MemoryManager : CodeVisitor
568 public class Vala.NullChecker : CodeVisitor
569 public class Vala.Parser : CodeVisitor
570 public class Vala.SemanticAnalyzer : CodeVisitor
571 public class Vala.SymbolResolver : CodeVisitor
575 <classname>CodeVisitor</classname> works closely with the different
576 <classname>Vala.CodeNode</classname> classes to traverse the code
577 tree. Here are all the code node types in Vala:
581 public abstract class Vala.CodeNode : Object
582 public interface Vala.Statement : CodeNode
583 public class Vala.BreakStatement : CodeNode, Statement
584 public class Vala.ContinueStatement : CodeNode, Statement
585 public class Vala.DeclarationStatement : CodeNode, Statement
586 public class Vala.DeleteStatement : CodeNode, Statement
587 public class Vala.DoStatement : CodeNode, Statement
588 public class Vala.EmptyStatement : CodeNode, Statement
589 public class Vala.ExpressionStatement : CodeNode, Statement
590 public class Vala.ForStatement : CodeNode, Statement
591 public class Vala.IfStatement : CodeNode, Statement
592 public class Vala.LockStatement : CodeNode, Statement
593 public class Vala.ReturnStatement : CodeNode, Statement
594 public class Vala.SwitchStatement : CodeNode, Statement
595 public class Vala.ThrowStatement : CodeNode, Statement
596 public class Vala.TryStatement : CodeNode, Statement
597 public class Vala.WhileStatement : CodeNode, Statement
599 public abstract class Vala.DataType : CodeNode
600 public abstract class Vala.ReferenceType : DataType
601 public class Vala.ArrayType : ReferenceType
602 public class Vala.ClassType : ReferenceType
603 public class Vala.ErrorType : ReferenceType
604 public class Vala.InterfaceType : ReferenceType
605 public class Vala.NullType : ReferenceType
607 public class Vala.DelegateType : DataType
608 public class Vala.InvalidType : DataType
609 public class Vala.MethodType : DataType
610 public class Vala.PointerType : DataType
611 public class Vala.SignalType : DataType
612 public class Vala.TypeParameterType : DataType
613 public class Vala.UnresolvedType : DataType
614 public class Vala.ValueType : DataType
615 public class Vala.VoidType : DataType
617 public abstract class Vala.Symbol : CodeNode
618 public abstract class Vala.Typesymbol : Symbol
620 public class Vala.Block : Symbol, Statement
621 public class Vala.Constructor : Symbol
622 public class Vala.Destructor : Symbol
623 public class Vala.EnumValue : Symbol
624 public class Vala.ErrorCode : Symbol
625 public class Vala.FormalParameter : Symbol
626 public class Vala.Member : Symbol
627 public class Vala.Namespace : Symbol
628 public class Vala.TypeParameter : Symbol
629 public class Vala.VariableDeclarator : Symbol
631 public abstract class Vala.Expression : CodeNode
632 public abstract class Vala.Literal : Expression
633 public class Vala.BooleanLiteral : Literal
634 public class Vala.CharacterLiteral : Literal
635 public class Vala.IntegerLiteral : Literal
636 public class Vala.NullLiteral : Literal
637 public class Vala.RealLiteral : Literal
638 public class Vala.StringLiteral : Literal
640 public class Vala.AddressofExpression : Expression
641 public class Vala.ArrayCreationExpression : Expression
642 public class Vala.Assignment : Expression
643 public class Vala.BaseAccess : Expression
644 public class Vala.BinaryExpression : Expression
645 public class Vala.CastExpression : Expression
646 public class Vala.ConditionalExpression : Expression
647 public class Vala.ElementAccess : Expression
648 public class Vala.InitializerList : Expression
649 public class Vala.InvocationExpression : Expression
650 public class Vala.LambdaExpression : Expression
651 public class Vala.MemberAccess : Expression
652 public class Vala.ObjectCreationExpression : Expression
653 public class Vala.ParenthesizedExpression : Expression
654 public class Vala.PointerIndirection : Expression
655 public class Vala.PostfixExpression : Expression
656 public class Vala.ReferenceTransferExpression : Expression
657 public class Vala.SizeofExpression : Expression
658 public class Vala.Tuple : Expression
659 public class Vala.TypeCheck : Expression
660 public class Vala.TypeofExpression : Expression
661 public class Vala.UnaryExpression : Expression
663 public class Vala.LocalVariableDeclaration : CodeNode
664 public class Vala.CatchClause : CodeNode
665 public class Vala.Attribute : CodeNode
666 public class Vala.PropertyAccessor : CodeNode
667 public class Vala.MemberInitializer : CodeNode
668 public class Vala.SwitchLabel : CodeNode
669 public class Vala.UnresolvedSymbol : CodeNode
670 public class Vala.NamedArgument : CodeNode
671 public class Vala.NamespaceReference : CodeNode
675 All <classname>CodeNode</classname>s except the root have a non-null
676 parent CodeNode. Some specializations of
677 <classname>CodeNode</classname> may have children. The type and
678 number of children are declared in the specialized class.
682 The two important methods in a <classname>CodeNode</classname> are
683 <function>accept</function> and
684 <function>accept_children</function>. <function>accept()</function>
685 lets the node declare to the <classname>CodeVisitor</classname> what
686 it is, so the <classname>CodeVisitor</classname> can act on it. For
687 example, <code>Vala.Struct.accept()</code>
691 public override void accept (CodeVisitor visitor) {
692 visitor.visit_struct (this); /* I am a struct! */
697 <function>accept_children()</function> lets the node accept all of
698 its children so they can declare themselves to the
699 <classname>CodeVisitor</classname>. This is the recursive part of
700 the traversal. For example, a <classname>Struct</classname> code
701 node has a list of base types, type parameters, fields, constants,
702 and methods. <code>Vala.Struct.accept_children()</code> accepts all
707 public override void accept_children (CodeVisitor visitor) {
708 foreach (DataType type in base_types) {
709 type.accept (visitor);
712 foreach (TypeParameter p in type_parameters) {
716 foreach (Field f in fields) {
720 foreach (Constant c in constants) {
724 foreach (Method m in methods) {
731 As you can see, the <classname>CodeVisitor</classname> is repeatedly
732 asked to visit different code nodes. It can do whatever analysis is
733 necessary and then traverse deeper into the code tree. This is what
734 a hypothetical implementation of
735 <code>XmlGenerator.visit_struct()</code> might look like:
739 public override void visit_struct (Struct st) {
740 /* Do some processing of this struct. */
741 stdout.printf ("<vala:struct name=\"%s\">\n", st.name);
743 /* recurse through struct's children */
744 st.accept_children (this);
746 /* Do some more processing of the struct, now that its
747 * children have been accepted. */
748 stdout.printf ("</vala:struct>\n");
753 The <function>visit_</function> methods of a
754 <function>CodeVisitor</function> needn't call
755 <code>CodeNode.accept_children()</code>, if it isn't necessary to
756 traverse the whole depth of the code tree. It also isn't necessary
757 to write <function>visit_</function> methods for every kind of code
758 node, because empty implementations are already provided in
759 <classname>CodeVisitor</classname>.
763 What does this have to do with ping pong? Well, the flow of control
764 bounces between the <classname>CodeVisitor</classname> and the
765 <classname>CodeNode</classname>s: <function>accept</function>,
766 <function>visit</function>, <function>accept</function>,
767 <function>visit</function>, ... When you navigate the code you will
768 probably find yourself bouncing between different classes.
773 <sect3><title>Back to the Parser</title>
776 <classname>Vala.Parser</classname> is a highly specialized
777 <classname>CodeVisitor</classname> - the only type of code node it
778 visits is a <classname>Vala.SourceFile</classname>. However the
779 <classname>Parser</classname> calls back to the context and uses it
780 to create code nodes (mentioned before), then adds these code nodes
781 into the context's root code node.
785 <sect3><title>Error Handling</title>
788 I don't want to spoil your fun too much by going into the details of
789 the parser, other than that every <function>parse_</function>
791 <classname>ParseError</classname>. <classname>ParseError</classname>
792 is caught when parsing a block or the declarations of a namespace,
793 class, struct, or interface. Fixme.
797 <sect3><title>Grammar of Vala</title>
800 This grammar is hand-generated from
801 <classname>Vala.Parser</classname>. Sometimes the structure of this
802 grammar diverges slightly from the code, for example optional
803 non-terminal symbols. However the non-terminal symbol names usually
804 match a <function>parse_</function> method in
805 <classname>Vala.Parser</classname>.
810 using_directive* root_declarations
813 "using" symbol ("," symbol)* ";"
815 // from parse_symbol_name
817 identifier ("." identifier)*
819 // from parse_identifier, skip_identifier, get_last_string
823 // corresponds to parse_declarations
824 root_declarations ::=
825 namespace_declarations
827 // corresponds to parse_declarations
828 other_declarations ::=
829 namespace_declarations
831 | struct_declarations
832 | interface_declarations
834 // corresponds to parse_declarations
836 "{" other_declarations "}"
838 namespace_declarations ::=
841 // from parse_namespace_member
843 namespace_declaration
845 | interface_declaration
848 | error_domain_declaration // ???
849 | delegate_declaration
852 | constant_declaration
854 // from parse_class_member
859 | delegate_declaration
863 | constant_declaration
864 | property_declaration
865 | constructor_declaration
866 | destructor_declaration
868 // from parse_declaration and parse_namespace_declaration
869 namespace_declaration ::=
870 attribute* "namespace" symbol_name declarations
872 // from parse_declaration and parse_class_declaration
873 class_declaration ::=
874 [access_modifier] type_declaration_modifiers
875 "class" symbol_name [type_parameter_list] ":" type ("," type)*
878 // from parse_access_modifier
884 // from parse_type_declaration_modifiers
885 type_declaration_modifiers ::=
886 ("abstract" | "static")*
888 // from parse_member_declaration_modifiers
889 member_declaration_modifiers ::=
890 ("abstract" | "class" | "inline" | "override" | "static" | "virtual")*
892 // from parse_type_parameter_list
893 type_parameter_list ::=
894 "<" identifier ("," identifier)* ">"
896 // fixme: complete the grammar, check it
904 <sect2><title>Attribute Processing</title>
907 <classname>Vala.Attribute</classname>s are code tree nodes and have
908 a name and a possibly empty list of key-value arguments. Some types
909 of code tree nodes have as children a list of
910 <classname>Attributes</classname>. The attribute processor's purpose
911 is to interpret the attributes which were parsed into the code tree.
915 Later in the compilation, the results of attribute processing will
916 be used, for example the <code>CCode cname</code> attribute affects
917 what function names are used in emitted C code.
921 All attributes except for <code>Conditional</code> are handled from
922 <classname>Vala.AttributeProcessor</classname>. I don't know where
923 and how conditional is handled, but there is a function
924 <function>ignore_node()</function> in
925 <classname>Vala.CodeContext</classname>.
929 <classname>Vala.AttributeProcessor</classname> is a
930 <classname>CodeVisitor</classname> which simply calls the
931 <function>process_attributes()</function> method on every namespace,
932 class, struct, interface, enum, method, constructor, parameter,
933 property, delegate, constant, field, and signal that it visits.
937 Inside the <function>process_attributes()</function> method of each
938 of these objects, a series of string comparisons will be made to
939 parse the attributes. If the attribute is called "CCode", then the
940 <function>process_ccode_attributes()</function> function will be
941 called to parse the key-value pairs supplied.
945 fixme: mention <function>Vala.Parser.set_attributes()</function>
948 <variablelist><title>Attributes Recognized by Vala</title>
950 <term><classname>Vala.Namespace</classname></term>
951 <listitem><itemizedlist>
952 <listitem><para>CCode</para></listitem>
953 </itemizedlist></listitem>
956 <term><classname>Vala.Class</classname></term>
957 <listitem><itemizedlist>
958 <listitem><para>CCode</para></listitem>
959 <listitem><para>ErrorBase</para></listitem>
960 </itemizedlist></listitem>
963 <term><classname>Vala.Struct</classname></term>
964 <listitem><itemizedlist>
965 <listitem><para>CCode</para></listitem>
966 <listitem><para>SimpleType</para></listitem>
967 <listitem><para>IntegerType</para></listitem>
968 <listitem><para>FloatingType</para></listitem>
969 </itemizedlist></listitem>
972 <term><classname>Vala.Interface</classname></term>
973 <listitem><itemizedlist>
974 <listitem><para>CCode</para></listitem>
976 <para>DBusInterface</para>
978 <listitem><para>DBusGProxy</para></listitem>
981 </itemizedlist></listitem>
984 <term><classname>Vala.Enum</classname></term>
985 <listitem><itemizedlist>
986 <listitem><para>CCode</para></listitem>
987 <listitem><para>Flags</para></listitem>
988 </itemizedlist></listitem>
991 <term><classname>Vala.Method</classname></term>
992 <listitem><itemizedlist>
993 <listitem><para>CCode</para></listitem>
994 <listitem><para>ReturnsModifiedPointer</para></listitem>
995 <listitem><para>FloatingReference</para></listitem>
996 <listitem><para>NoArrayLength</para></listitem>
997 <listitem><para>PrintfFormat</para></listitem>
998 <listitem><para>Import</para></listitem>
999 </itemizedlist></listitem>
1002 <term><classname>Vala.CreationMethod</classname></term>
1004 <para><emphasis>Same as <classname>Vala.Method</classname> - this class
1005 inherits Method</emphasis></para>
1009 <term><classname>Vala.FormalParameter</classname></term>
1010 <listitem><itemizedlist>
1011 <listitem><para>CCode</para></listitem>
1012 </itemizedlist></listitem>
1015 <term><classname>Vala.Property</classname></term>
1016 <listitem><itemizedlist>
1017 <listitem><para>Notify</para></listitem>
1018 <listitem><para>NoAccessorMethod</para></listitem>
1020 <para>Description</para>
1022 <listitem><para>nick</para></listitem>
1023 <listitem><para>blurb</para></listitem>
1026 </itemizedlist></listitem>
1029 <term><classname>Vala.Delegate</classname></term>
1030 <listitem><itemizedlist>
1031 <listitem><para>CCode</para></listitem>
1032 <listitem><para>NoArrayLength</para></listitem>
1033 </itemizedlist></listitem>
1036 <term><classname>Vala.Constant</classname></term>
1037 <listitem><itemizedlist>
1038 <listitem><para>CCode</para></listitem>
1039 </itemizedlist></listitem>
1042 <term><classname>Vala.Field</classname></term>
1043 <listitem><itemizedlist>
1044 <listitem><para>CCode</para></listitem>
1045 <listitem><para>NoArrayLength</para></listitem>
1046 </itemizedlist></listitem>
1049 <term><classname>Vala.Signal</classname></term>
1050 <listitem><itemizedlist>
1051 <listitem><para>HasEmitter</para></listitem>
1052 </itemizedlist></listitem>
1058 <sect2><title>Symbol Resolution</title>
1061 <classname>Vala.SymbolResolver</classname> is a
1062 <classname>CodeVisitor</classname> that exchanges
1063 <classname>Vala.UnresolvedType</classname>s in the parse tree with
1064 <classname>Vala.DataType</classname>s and links
1065 <classname>Vala.NamespaceReference</classname>s with the correct
1066 namespace symbol. Additionally, it checks base types for classes so
1067 that classes don't inherit from multiple classes or themselves, and
1068 likewise it checks that interfaces don't need to implement
1072 <sect3><title>Data Types</title>
1075 Every expression has a static type. This is computed by the semantic
1076 analyser. <classname>Vala.DataType</classname> is called a "type
1077 reference" because it contains a reference to a
1078 <classname>Vala.Typesymbol</classname> (a class, interface, etc) as
1079 well as information about the expression's type e.g. if it can be
1080 null, if it's an out parameter.
1083 <para>Fixme: expand this section</para>
1086 <sect3><title>Symbols</title>
1089 A <classname>Vala.Symbol</classname> is a specialization of
1090 <classname>Vala.CodeNode</classname>. All symbols except for the
1091 root symbol are contained within another's scope. Types have scope
1092 and variables have scope. For types and variables, scope determines
1093 their accessibility, subject to access modifiers. For variables,
1094 scope also determines their lifetime. As the code tree is traversed,
1095 <classname>SymbolResolver</classname> keeps track of the current
1096 scope. For example, when a class is visited,
1097 <varname>current_scope</varname> is set to that class's scope.
1101 When the parser parses a type, e.g. in the statement
1102 <code>Gtk.Window main_window;</code>, the type
1103 <classname>Gtk.Window</classname> is initially a
1104 <classname>Vala.UnresolvedType</classname>. In
1105 <function>visit_data_type()</function>, the
1106 <classname>UnresolvedType</classname> code node asks its parent to
1107 replace it with a new <classname>Vala.DataType</classname> created
1108 with <function>resolve_type()</function>.
1112 <classname>UnresolvedType</classname>s have
1113 <classname>UnresolvedSymbol</classname>s. <function>resolve_type()</function>
1114 uses <function>resolve_symbol()</function> to find the
1115 <classname>Typesymbol</classname> referred to, and then wraps it in
1116 a new <classname>DataType</classname> object.
1120 <function>resolve_symbol</function> is a recursive method which looks
1121 up an unresolved symbol's name in the current scope and returns the
1122 corresponding <classname>Typesymbol</classname>. The base case is
1123 when the <classname>UnresolvedSymbol</classname> has no qualifiers,
1124 e.g. <classname>Window</classname>. The recursive case is when the
1125 symbol looks like <classname>Gtk.Window</classname> or
1126 <varname>Gtk.Orientation.HORIZONTAL</varname>. In <function>Vala.Parser.parse_symbol_name()</function>, the symbol is built inside-out, so <code>Gtk.Orientation.HORIZONTAL</code> is parsed as
1131 (UnresolvedSymbol(null, "Gtk"),
1136 This is inside-out because <code>Orientation</code> is the parent
1137 scope of <code>HORIZONTAL</code>, but <code>Orientation</code> is the
1138 child node of <code>HORIZONTAL</code>.
1142 In the base case, the symbol's name is looked up in
1143 <varname>current_scope</varname>. If the symbol is not found
1144 there, then the scope of all imported namespaces is searched. If
1145 more than one imported namespace contains the symbol, an "ambiguous
1146 reference" error will be reported.
1150 In the recursive case, <function>resolve_symbol()</function> is
1151 called on the child node to give a parent scope, in which the symbol
1156 One last function of <classname>SymbolResolver</classname> is in
1157 <function>visit_variable_declarator()</function> - to mark a
1158 variable type reference as "nullable" if the variable's type is a
1159 class, interface, array or error (reference type). This is used
1160 later by <classname>Vala.NullChecker</classname>.
1167 <sect2><title>Semantic Analyzer</title>
1172 <sect2 id="ch-codegen"><title>C Code Generation</title>
1177 <sect2><title>C Code Compilation and Linking</title>
1183 <sect1 id="ch-vapi"><title>Vala Bindings - VAPI</title>
1186 The bindings are located in the vapi directories.
1189 <sect2><title><code>CCode</code> Attribute Reference</title>
1192 <code>CCode</code> attributes influence the C code which is
1193 generated by Vala. They have the form
1196 [CCode (argument0 = value0, argument1 = value1, ...)]
1201 Usually these attributes are not required because Vala chooses
1202 normal values. However, they exist for the cases when Vala's chosen
1203 values don't fit the API.
1208 I'm not sure how much of this will apply once we have GObject
1213 <table><title>CCode Attributes</title>
1215 <colspec colname='name'/>
1216 <colspec colname='applies'/>
1217 <colspec colname='type'/>
1218 <colspec colname='ex'/>
1219 <spanspec spanname="desc" namest="name" nameend="ex"/>
1223 <entry>Applies to</entry>
1225 <entry>Example</entry>
1228 <entry spanname="desc">Description</entry>
1233 <entry>array_length_pos</entry>
1234 <entry>constructor, delegate, method, parameter</entry>
1235 <entry>string</entry>
1239 <entry spanname="desc">
1244 <entry>cheader_filename</entry>
1245 <entry>class, constant, constructor, delegate, enum, field, interface, method, namespace, struct</entry>
1246 <entry>string - comma-separated list of headers</entry>
1247 <entry>"glib.h"</entry>
1250 <entry spanname="desc">
1251 The header file(s) which should be <code>#include</code>d in
1252 the emitted C code, so that this symbol is usable. If more
1253 than one header file is needed, separate them by commas.
1257 <entry>cname</entry>
1258 <entry>class, constant, constructor, delegate, enum, field, method, struct</entry>
1259 <entry>string</entry>
1260 <entry>"gboolean"</entry>
1263 <entry spanname="desc">
1264 The name that this symbol will take when translated into C
1265 code. If this attribute is not specified, the symbol will
1266 get a name with the normal vala translation rules.
1270 <entry>const_cname</entry>
1271 <entry>class, struct</entry>
1272 <entry>string</entry>
1276 <entry spanname="desc">
1281 <entry>copy_function </entry>
1282 <entry>class</entry>
1283 <entry>string</entry>
1287 <entry spanname="desc">
1292 <entry>cprefix</entry>
1293 <entry>class, enum, namespace, struct</entry>
1294 <entry>string</entry>
1298 <entry spanname="desc">
1303 <entry>default_value</entry>
1304 <entry>struct</entry>
1305 <entry>string - C value expression</entry>
1306 <entry>"FALSE"</entry>
1309 <entry spanname="desc">
1310 A C expression representing this type's default value. This
1311 is needed because ...
1315 <entry>delegate_target_pos</entry>
1316 <entry>constructor, delegate, method, parameter</entry>
1317 <entry>string</entry>
1321 <entry spanname="desc">
1326 <entry>free_function</entry>
1327 <entry>class</entry>
1328 <entry>string</entry>
1332 <entry spanname="desc">
1337 <entry>get_value_function</entry>
1338 <entry>class, struct</entry>
1339 <entry>string - function name</entry>
1340 <entry>"g_value_get_boolean"</entry>
1343 <entry spanname="desc">
1345 <emphasis>type_cname</emphasis>get_value_function (const GValue *value);
1348 A function which will return an object when passed a
1349 GValue. This is used ...
1353 <entry>has_type_id</entry>
1354 <entry>class, enum</entry>
1355 <entry>string</entry>
1359 <entry spanname="desc">
1364 <entry>instance_pos</entry>
1365 <entry>constructor, delegate, method</entry>
1366 <entry>string</entry>
1370 <entry spanname="desc">
1375 <entry>lower_case_cprefix</entry>
1376 <entry>class, enum, interface, namespace</entry>
1377 <entry>string</entry>
1381 <entry spanname="desc">
1386 <entry>marshaller_type_name</entry>
1387 <entry>class, struct</entry>
1388 <entry>string</entry>
1389 <entry>"BOOLEAN"</entry>
1392 <entry spanname="desc">
1397 <entry>ref_function</entry>
1398 <entry>class</entry>
1399 <entry>string</entry>
1403 <entry spanname="desc">
1408 <entry>sentinel</entry>
1409 <entry>constructor, method</entry>
1410 <entry>string</entry>
1414 <entry spanname="desc">
1419 <entry>set_value_function</entry>
1420 <entry>class, struct</entry>
1421 <entry>string - function name</entry>
1422 <entry>"g_value_set_boolean"</entry>
1425 <entry spanname="desc">
1427 void set_value_function (GValue *value, <emphasis>type_cname</emphasis> v);
1430 A function that will set a GValue with an object of this
1431 type. This is used ...
1436 <entry>parameter</entry>
1437 <entry>string</entry>
1441 <entry spanname="desc">
1446 <entry>type_cname</entry>
1447 <entry>interface</entry>
1448 <entry>string</entry>
1452 <entry spanname="desc">
1457 <entry>type_id</entry>
1458 <entry>class, enum, struct</entry>
1459 <entry>string - </entry>
1460 <entry>"G_TYPE_BOOLEAN"</entry>
1463 <entry spanname="desc">
1464 The GObject type system type that this object is registered
1465 with. If <code>type_id</code> is not specified, Vala uses a
1466 type ID based on the type's name.
1470 <entry>unref_function</entry>
1471 <entry>class</entry>
1472 <entry>string</entry>
1476 <entry spanname="desc">
1481 <entry>vfunc_name</entry>
1482 <entry>constructor, method</entry>
1483 <entry>string</entry>
1487 <entry spanname="desc">
1498 <sect1 id="ch-libgee"><title>libgee Internal</title>
1501 What is the difference between external and internal libgee?
1505 <sect1><title>Other Tools</title>
1507 <sect2 id="ch-vapigen"><title>vapigen</title>
1511 <sect2 id="ch-gen-project"><title>gen-project</title>
1515 <sect2 id="ch-gen-introspect"><title>vala-gen-introspect</title>
1520 <sect1 id="ch-testing"><title>Testing</title>
1523 A goal for Vala 1.0 is to have every tests for every documented
1524 feature of the language.
1528 <sect1 id="ch-docs"><title>Documentation</title>
1531 XML sources for the Vala Reference are in the
1532 <filename>doc/vala</filename> directory. You can rebuild the docs by
1533 cd'ing into <filename>doc/vala</filename> and typing
1534 <userinput>make</userinput>. Currently the conversion to HTML is
1535 done using xsltproc and a simple stylesheet.
1539 Docbook XML sources for the Hackers' Guide are in
1540 <filename>doc/hackers</filename>. HTML documentation is built by
1541 default. This document can be rebuilt by running
1542 <userinput>make</userinput> in <filename>doc/hackers</filename>.
1546 Generated binding documentation - fixme.
1550 libvala documentation - fixme.
1555 url="http://live.gnome.org/Vala">http://live.gnome.org/Vala</ulink>
1556 - The Wiki is open to anyone who would like to make a quality
1561 <sect1><title>Build System</title>
1564 Vala is built using the standard GNU Autotools. The built
1565 executables are actually stored in <filename>.libs</filename>
1566 directories and wrapped by scripts. Therefore to debug, follow
1567 these instructions fixme.
1571 <command>./configure</command> uses the
1572 <varname>AC_PATH_PROG</varname> macro to choose the
1573 <command>valac</command> which is on your path, or one specified in
1574 the <varname>VALAC</varname> environment variable. Therefore, to
1575 build Vala with your own <command>valac</command>, type this, for
1579 VALAC=$HOME/dev/vala-0.20/compiler/valac ./configure --prefix=$HOME/prefix
1583 <sect2><title>Out-of-tree build</title>
1586 An out-of-tree build does not properly work yet. Out-of-tree
1587 builds have the advantage that your source tree is not cluttered
1588 with built files. Suppose you have vala checked out in
1589 <filename>~/dev/valatrunk</filename>.
1593 rodney@solaria:~/dev % svn co http://svn.gnome.org/svn/vala/trunk valatrunk
1594 rodney@solaria:~/dev % ls valatrunk
1595 fixme fixme fixme fixme
1596 aclocal.m4 config.log gobject-introspection README
1597 AUTHORS config.status INSTALL stamp-h1
1598 autogen.sh config.sub install-sh tests
1599 autom4te.cache configure libtool vala
1600 ccode configure.ac ltmain.sh vala-1.0.pc
1601 ChangeLog COPYING MAINTAINERS vala-1.0.pc.in
1602 compile depcomp Makefile vapi
1603 compiler doc Makefile.am vapigen
1604 config.guess gee Makefile.in ylwrap
1605 config.h gen-project missing
1606 config.h.in gobject NEWS
1607 fixme fixme fixme fixme
1608 rodney@solaria:~/dev % mkdir buildvala
1609 rodney@solaria:~/dev % cd buildvala
1610 rodney@solaria:~/dev/buildvala % ../valatrunk/autogen.sh --prefix=$HOME/dev/prefix
1611 rodney@solaria:~/dev/buildvala % make
1615 All <filename>Makefile</filename>s, etc, generated by configure
1616 will be put in the <filename>buildvala</filename> directory and
1617 you can run make directly from there.
1622 <sect1><title>Directory Index</title>
1624 <informaltable frame='none'>
1627 <row><entry><filename>gen-project</filename></entry>
1628 <entry><xref linkend="ch-gen-project"/></entry></row>
1629 <row><entry><filename>gen-project/licenses</filename></entry>
1630 <entry><xref linkend="ch-gen-project"/></entry></row>
1631 <row><entry><filename>vapi</filename></entry>
1632 <entry><xref linkend="ch-vapi"/></entry></row>
1633 <row><entry><filename>vapi/packages</filename></entry>
1634 <entry><xref linkend="ch-vapi"/></entry></row>
1635 <row><entry><filename>gee</filename></entry>
1636 <entry><xref linkend="ch-libgee"/></entry></row>
1637 <row><entry><filename>gobject</filename></entry>
1638 <entry><xref linkend="ch-codegen"/></entry></row>
1639 <row><entry><filename>doc</filename></entry>
1640 <entry><xref linkend="ch-docs"/></entry></row>
1641 <row><entry><filename>doc/hackers</filename></entry>
1642 <entry><xref linkend="ch-docs"/></entry></row>
1643 <row><entry><filename>doc/vala</filename></entry>
1644 <entry><xref linkend="ch-docs"/></entry></row>
1645 <row><entry><filename>vapigen</filename></entry>
1646 <entry><xref linkend="ch-vapigen"/></entry></row>
1647 <row><entry><filename>vapigen/vala-gen-introspect</filename></entry>
1648 <entry><xref linkend="ch-gen-introspect"/></entry></row>
1649 <row><entry><filename>compiler</filename></entry>
1650 <entry><xref linkend="ch-valac"/></entry></row>
1651 <row><entry><filename>vala</filename></entry>
1652 <entry><xref linkend="ch-valac"/></entry></row>
1653 <row><entry><filename>gobject-introspection</filename></entry>
1654 <entry><xref linkend=""/></entry></row>
1655 <row><entry><filename>ccode</filename></entry>
1656 <entry><xref linkend="ch-codegen"/></entry></row>
1657 <row><entry><filename>tests</filename></entry>
1658 <entry><xref linkend="ch-testing"/></entry></row>
1665 <sect1><title>Index</title>
1667 Should automatically generate this from Docbook.
1671 <sect1><title>GNU Free Documentation License</title>
1683 public class Vala.CCodeTypeDefinition : CCodeNode
1684 public class Vala.CCodeMacroReplacement : CCodeNode
1685 public class Vala.CCodeStruct : CCodeNode
1686 public class Vala.CCodeFragment : CCodeNode
1687 public class Vala.CCodeFormalParameter : CCodeNode
1688 public class Vala.CCodeFunction : CCodeNode
1689 public class Vala.CCodeNewline : CCodeNode
1690 public class Vala.CCodeEnumValue : CCodeNode
1691 public abstract class Vala.CCodeDeclarator : CCodeNode
1692 public class Vala.CCodeComment : CCodeNode
1693 public abstract class Vala.CCodeExpression : CCodeNode
1694 public class Vala.CCodeLineDirective : CCodeNode
1695 public abstract class Vala.CCodeStatement : CCodeNode
1696 public class Vala.CCodeEnum : CCodeNode
1697 public class Vala.CCodeIncludeDirective : CCodeNode