Commit d9d367f5 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

support initializer lists support constant arrays and initializer lists

2006-05-23  Jürg Billeter  <j@bitron.ch>

	* vala/valacodevisitor.vala: support initializer lists
	* vala/valacodegenerator.vala: support constant arrays and initializer
	  lists
	* vala/valaconstant.vala: visit initializer
	* vala/valainitializerlist.vala: add accept method
	* vala/valastruct.vala: visit constants later than fields due to
	  constant initialization
	* vala/valatypereference.vala: add get_const_cname method
	* ccode/valaccodeinitializerlist.vala

svn path=/trunk/; revision=34
parent 5ec772c2
2006-05-23 Jürg Billeter <j@bitron.ch>
* vala/valacodevisitor.vala: support initializer lists
* vala/valacodegenerator.vala: support constant arrays and initializer
lists
* vala/valaconstant.vala: visit initializer
* vala/valainitializerlist.vala: add accept method
* vala/valastruct.vala: visit constants later than fields due to
constant initialization
* vala/valatypereference.vala: add get_const_cname method
* ccode/valaccodeinitializerlist.vala
2006-05-22 Jürg Billeter <j@bitron.ch>
* vala/parser.y: support abstract methods and static fields
......
/* valaccodeinitializerlist.vala
*
* Copyright (C) 2006 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 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;
namespace Vala {
public class CCodeInitializerList : CCodeExpression {
List<CCodeExpression> initializers;
public void append (CCodeExpression expr) {
initializers.append (expr);
}
public override void write (CCodeWriter writer) {
writer.write_string ("{");
bool first = true;
foreach (CCodeExpression expr in initializers) {
if (!first) {
writer.write_string (", ");
} else {
first = false;
}
if (expr != null) {
expr.write (writer);
}
}
writer.write_string ("}");
}
}
}
......@@ -311,8 +311,12 @@ namespace Vala {
public override void visit_constant (Constant c) {
if (c.symbol.parent_symbol.node is Struct) {
var t = (Struct) c.symbol.parent_symbol.node;
var cdecl = new CCodeDeclarationStatement (type_name = "const %s".printf (c.type_reference.get_cname ()));
cdecl.add_declarator (new CCodeVariableDeclarator (name = "%s_%s".printf (t.get_lower_case_cname (null), c.name)));
var cdecl = new CCodeDeclarationStatement (type_name = c.type_reference.get_const_cname ());
var arr = "";
if (c.type_reference.array) {
arr = "[]";
}
cdecl.add_declarator (new CCodeVariableDeclarator (name = "%s_%s%s".printf (t.get_lower_case_cname (null), c.name, arr), initializer = c.initializer.ccodenode));
cdecl.modifiers = CCodeModifiers.STATIC;
source_type_member_declaration.append (cdecl);
}
......@@ -476,6 +480,14 @@ namespace Vala {
}
}
public override void visit_initializer_list (InitializerList list) {
var clist = new CCodeInitializerList ();
foreach (Expression expr in list.initializers) {
clist.append (expr.ccodenode);
}
list.ccodenode = clist;
}
public override void visit_expression_statement (ExpressionStatement stmt) {
stmt.ccodenode = new CCodeExpressionStatement (expression = (CCodeExpression) stmt.expression.ccodenode);
}
......
......@@ -114,6 +114,9 @@ namespace Vala {
public virtual void visit_variable_declarator (VariableDeclarator decl) {
}
public virtual void visit_initializer_list (InitializerList list) {
}
public virtual void visit_expression_statement (ExpressionStatement stmt) {
}
......
......@@ -35,6 +35,8 @@ namespace Vala {
public override void accept (CodeVisitor visitor) {
type_reference.accept (visitor);
initializer.accept (visitor);
visitor.visit_constant (this);
}
......
......@@ -30,5 +30,13 @@ namespace Vala {
public static ref InitializerList new (List<Expression> initializers, SourceReference source) {
return (new InitializerList (initializers = initializers, source_reference = source));
}
public override void accept (CodeVisitor visitor) {
foreach (Expression expr in initializers) {
expr.accept (visitor);
}
visitor.visit_initializer_list (this);
}
}
}
......@@ -69,14 +69,14 @@ namespace Vala {
p.accept (visitor);
}
foreach (Constant c in constants) {
c.accept (visitor);
}
foreach (Field f in fields) {
f.accept (visitor);
}
foreach (Constant c in constants) {
c.accept (visitor);
}
foreach (Method m in methods) {
m.accept (visitor);
}
......
......@@ -119,6 +119,19 @@ namespace Vala {
return null;
}
}
public ref string get_const_cname () {
string ptr;
string arr;
if (!type.is_reference_type () && !is_ref) {
ptr = "";
} else if (((type.is_reference_type ()) && !is_out) || is_ref) {
ptr = "*";
} else {
ptr = "**";
}
return "const %s%s".printf (type.get_cname (), ptr);
}
public ref string get_upper_case_cname (string infix) {
return type.get_upper_case_cname (infix);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment