diff options
author | Jürg Billeter <j@bitron.ch> | 2010-08-13 14:25:19 +0200 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2010-08-21 11:31:50 +0200 |
commit | f4f655d292b78f0b0e631507da104bc45584f634 (patch) | |
tree | 0c6c03e339dde5cbd4a81ee62b6784950024c026 /ccode | |
parent | ceb6c3877b526049ecc423ea7b12f7b545ce1f82 (diff) | |
download | vala-f4f655d292b78f0b0e631507da104bc45584f634.tar.gz |
Replace CCodeDeclarationSpace by CCodeFile
Diffstat (limited to 'ccode')
-rw-r--r-- | ccode/Makefile.am | 1 | ||||
-rw-r--r-- | ccode/valaccodefile.vala | 173 |
2 files changed, 174 insertions, 0 deletions
diff --git a/ccode/Makefile.am b/ccode/Makefile.am index 1fd4a0dfc..9d54a0954 100644 --- a/ccode/Makefile.am +++ b/ccode/Makefile.am @@ -32,6 +32,7 @@ libvalaccode_la_VALASOURCES = \ valaccodeenumvalue.vala \ valaccodeexpression.vala \ valaccodeexpressionstatement.vala \ + valaccodefile.vala \ valaccodeformalparameter.vala \ valaccodeforstatement.vala \ valaccodefragment.vala \ diff --git a/ccode/valaccodefile.vala b/ccode/valaccodefile.vala new file mode 100644 index 000000000..2276cc37d --- /dev/null +++ b/ccode/valaccodefile.vala @@ -0,0 +1,173 @@ +/* valaccodefile.vala + * + * Copyright (C) 2009-2010 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> + */ + + +public class Vala.CCodeFile { + public bool is_header { get; set; } + + Set<string> declarations = new HashSet<string> (str_hash, str_equal); + Set<string> includes = new HashSet<string> (str_hash, str_equal); + CCodeFragment comments = new CCodeFragment (); + CCodeFragment include_directives = new CCodeFragment (); + CCodeFragment type_declaration = new CCodeFragment (); + CCodeFragment type_definition = new CCodeFragment (); + CCodeFragment type_member_declaration = new CCodeFragment (); + CCodeFragment constant_declaration = new CCodeFragment (); + CCodeFragment type_member_definition = new CCodeFragment (); + + public bool add_declaration (string name) { + if (name in declarations) { + return true; + } + declarations.add (name); + return false; + } + + public void add_comment (CCodeComment comment) { + comments.append (comment); + } + + public void add_include (string filename, bool local = false) { + if (!(filename in includes)) { + include_directives.append (new CCodeIncludeDirective (filename, local)); + includes.add (filename); + } + } + + public void add_type_declaration (CCodeNode node) { + type_declaration.append (node); + } + + public void add_type_definition (CCodeNode node) { + type_definition.append (node); + } + + public void add_type_member_declaration (CCodeNode node) { + type_member_declaration.append (node); + } + + public void add_constant_declaration (CCodeNode node) { + constant_declaration.append (node); + } + + public void add_type_member_definition (CCodeNode node) { + type_member_definition.append (node); + } + + public void add_function (CCodeFunction func) { + type_member_definition.append (func); + } + + public List<string> get_symbols () { + var symbols = new ArrayList<string> (); + foreach (CCodeNode node in type_member_declaration.get_children ()) { + var func = node as CCodeFunction; + if (func != null) { + symbols.add (func.name); + } + } + return symbols; + } + + static string get_define_for_filename (string filename) { + var define = new StringBuilder ("__"); + + var i = filename; + while (i.length > 0) { + var c = i.get_char (); + if (c.isalnum () && c < 0x80) { + define.append_unichar (c.toupper ()); + } else { + define.append_c ('_'); + } + + i = i.next_char (); + } + + define.append ("__"); + + return define.str; + } + + public bool store (string filename, string? source_filename, bool write_version, bool line_directives, string? begin_decls = null, string? end_decls = null) { + var writer = new CCodeWriter (filename, source_filename); + if (!writer.open (write_version)) { + return false; + } + + if (!is_header) { + writer.line_directives = line_directives; + + comments.write (writer); + writer.write_newline (); + include_directives.write (writer); + writer.write_newline (); + type_declaration.write_combined (writer); + writer.write_newline (); + type_definition.write_combined (writer); + writer.write_newline (); + type_member_declaration.write_declaration (writer); + writer.write_newline (); + type_member_declaration.write (writer); + writer.write_newline (); + constant_declaration.write_combined (writer); + writer.write_newline (); + type_member_definition.write (writer); + writer.write_newline (); + } else { + writer.write_newline (); + + var once = new CCodeOnceSection (get_define_for_filename (writer.filename)); + once.append (new CCodeNewline ()); + once.append (include_directives); + once.append (new CCodeNewline ()); + + if (begin_decls != null) { + once.append (new CCodeIdentifier (begin_decls)); + once.append (new CCodeNewline ()); + } + + once.append (new CCodeNewline ()); + once.append (type_declaration); + once.append (new CCodeNewline ()); + once.append (type_definition); + once.append (new CCodeNewline ()); + once.append (type_member_declaration); + once.append (new CCodeNewline ()); + once.append (constant_declaration); + once.append (new CCodeNewline ()); + + if (begin_decls != null) { + once.append (new CCodeIdentifier (end_decls)); + once.append (new CCodeNewline ()); + } + + once.append (new CCodeNewline ()); + once.write (writer); + } + + writer.close (); + + return true; + } +} + |