+2008-10-26 Jürg Billeter <j@bitron.ch>
+
+ * vala/Makefile.am:
+ * vala/valaattribute.vala:
+ * vala/valacodevisitor.vala:
+ * vala/valafield.vala:
+ * vala/valagenieparser.vala:
+ * vala/valanamedargument.vala:
+ * vala/valaparser.vala:
+ * vala/valasemanticanalyzer.vala:
+
+ Remove NamedArgument, improve attribute lookup performance
+
2008-10-26 Jürg Billeter <j@bitron.ch>
* gobject/valaccodeinvocationexpressionmodule.vala:
valamemberinitializer.vala \
valamethod.vala \
valamethodtype.vala \
- valanamedargument.vala \
valanamespace.vala \
valanullchecker.vala \
valanullliteral.vala \
/**
* Contains all specified attribute arguments.
*/
- public Gee.List<NamedArgument> args = new ArrayList<NamedArgument> ();
+ public Gee.Map<string,Expression> args = new Gee.HashMap<string,Expression> (str_hash, str_equal);
/**
* Creates a new attribute.
*
* @param arg named argument
*/
- public void add_argument (NamedArgument arg) {
- args.add (arg);
+ public void add_argument (string key, Expression value) {
+ args.set (key, value);
}
/**
* @return true if the argument has been found, false otherwise
*/
public bool has_argument (string name) {
- // FIXME: use hash table
- foreach (NamedArgument arg in args) {
- if (arg.name == name) {
- return true;
- }
- }
-
- return false;
+ return args.contains (name);
}
/**
* @return string value
*/
public string? get_string (string name) {
- // FIXME: use hash table
- foreach (NamedArgument arg in args) {
- if (arg.name == name) {
- var lit = arg.argument as StringLiteral;
- if (lit != null) {
- return lit.eval ();
- }
- }
+ var lit = args.get (name) as StringLiteral;
+ if (lit != null) {
+ return lit.eval ();
}
return null;
* @return integer value
*/
public int get_integer (string name) {
- // FIXME: use hash table
- foreach (NamedArgument arg in args) {
- if (arg.name == name) {
- var lit = arg.argument as IntegerLiteral;
- if (lit != null) {
- return lit.value.to_int ();
- }
- }
+ var lit = args.get (name) as IntegerLiteral;
+ if (lit != null) {
+ return lit.value.to_int ();
}
return 0;
* @return double value
*/
public double get_double (string name) {
- // FIXME: use hash table
- foreach (NamedArgument arg in args) {
- if (arg.name == name) {
- if (arg.argument is RealLiteral) {
- var lit = (RealLiteral) arg.argument;
- return lit.value.to_double ();
- } else if (arg.argument is IntegerLiteral) {
- var lit = (IntegerLiteral) arg.argument;
- return lit.value.to_int ();
- } else if (arg.argument is UnaryExpression) {
- var unary = (UnaryExpression) arg.argument;
- if (unary.operator == UnaryOperator.MINUS) {
- if (unary.inner is RealLiteral) {
- var lit = (RealLiteral) unary.inner;
- return -lit.value.to_double ();
- } else if (unary.inner is IntegerLiteral) {
- var lit = (IntegerLiteral) unary.inner;
- return -lit.value.to_int ();
- }
- }
+ var arg = args.get (name);
+ if (arg is RealLiteral) {
+ var lit = (RealLiteral) arg;
+ return lit.value.to_double ();
+ } else if (arg is IntegerLiteral) {
+ var lit = (IntegerLiteral) arg;
+ return lit.value.to_int ();
+ } else if (arg is UnaryExpression) {
+ var unary = (UnaryExpression) arg;
+ if (unary.operator == UnaryOperator.MINUS) {
+ if (unary.inner is RealLiteral) {
+ var lit = (RealLiteral) unary.inner;
+ return -lit.value.to_double ();
+ } else if (unary.inner is IntegerLiteral) {
+ var lit = (IntegerLiteral) unary.inner;
+ return -lit.value.to_int ();
}
}
}
* @return boolean value
*/
public bool get_bool (string name) {
- // FIXME: use hash table
- foreach (NamedArgument arg in args) {
- if (arg.name == name) {
- var lit = arg.argument as BooleanLiteral;
- if (lit != null) {
- return lit.value;
- }
- }
+ var lit = args.get (name) as BooleanLiteral;
+ if (lit != null) {
+ return lit.value;
}
return false;
public virtual void visit_destructor (Destructor d) {
}
- /**
- * Visit operation called for named arguments.
- *
- * @param n a named argument
- */
- public virtual void visit_named_argument (NamedArgument n) {
- }
-
/**
* Visit operation called for type parameters.
*
attr = new Attribute ("CCode");
attributes.append (attr);
}
- attr.add_argument (new NamedArgument ("type", new StringLiteral ("\"%s\"".printf (ctype))));
+ attr.add_argument ("type", new StringLiteral ("\"%s\"".printf (ctype)));
}
}
if (accept (TokenType.OPEN_PARENS)) {
if (current () != TokenType.CLOSE_PARENS) {
do {
- begin = get_location ();
string id = parse_identifier ();
expect (TokenType.ASSIGN);
var expr = parse_expression ();
- attr.add_argument (new NamedArgument (id, expr, get_src (begin)));
+ attr.add_argument (id, expr);
} while (accept (TokenType.COMMA));
}
expect (TokenType.CLOSE_PARENS);
+++ /dev/null
-/* valanamedargument.vala
- *
- * Copyright (C) 2006-2008 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>
- */
-
-using GLib;
-
-/**
- * Represents a named argument in the source code. A named argument may be used
- * when creating objects and attributes where no parameter list exists.
- */
-public class Vala.NamedArgument : CodeNode {
- /**
- * The name of a property.
- */
- public string name { get; set; }
-
- /**
- * The expression the property should assign.
- */
- public Expression argument { get; set; }
-
- /**
- * Creates a new named argument.
- *
- * @param name property name
- * @param arg property value expression
- * @param source reference to source code
- * @return newly created named argument
- */
- public NamedArgument (string name, Expression argument, SourceReference? source_reference = null) {
- this.name = name;
- this.argument = argument;
- this.source_reference = source_reference;
- }
-
- public override void accept (CodeVisitor visitor) {
- argument.accept (visitor);
-
- visitor.visit_named_argument (this);
- }
-}
if (accept (TokenType.OPEN_PARENS)) {
if (current () != TokenType.CLOSE_PARENS) {
do {
- begin = get_location ();
string id = parse_identifier ();
expect (TokenType.ASSIGN);
var expr = parse_expression ();
- attr.add_argument (new NamedArgument (id, expr, get_src (begin)));
+ attr.add_argument (id, expr);
} while (accept (TokenType.COMMA));
}
expect (TokenType.CLOSE_PARENS);
current_symbol = current_symbol.parent_symbol;
}
- public override void visit_named_argument (NamedArgument n) {
- }
-
public override void visit_block (Block b) {
b.owner = current_symbol.scope;
current_symbol = b;