Convert binary conditional expressions into if statements
[vala-lang.git] / vala / valaattribute.vala
blob4144089ec4afa1cb3514de1b66107a36137c4b6d
1 /* valaattribute.vala
3 * Copyright (C) 2006-2008 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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
24 using Gee;
26 /**
27 * Represents an attribute specified in the source code.
29 public class Vala.Attribute : CodeNode {
30 /**
31 * The name of the attribute type.
33 public string name { get; set; }
35 /**
36 * Contains all specified attribute arguments.
38 public Gee.Map<string,Expression> args = new Gee.HashMap<string,Expression> (str_hash, str_equal);
40 /**
41 * Creates a new attribute.
43 * @param name attribute type name
44 * @param source_reference reference to source code
45 * @return newly created attribute
47 public Attribute (string name, SourceReference? source_reference = null) {
48 this.name = name;
49 this.source_reference = source_reference;
52 /**
53 * Adds an attribute argument.
55 * @param arg named argument
57 public void add_argument (string key, Expression value) {
58 args.set (key, value);
61 /**
62 * Returns whether this attribute has the specified named argument.
64 * @param name argument name
65 * @return true if the argument has been found, false otherwise
67 public bool has_argument (string name) {
68 return args.contains (name);
71 /**
72 * Returns the string value of the specified named argument.
74 * @param name argument name
75 * @return string value
77 public string? get_string (string name) {
78 var lit = args.get (name) as StringLiteral;
79 if (lit != null) {
80 return lit.eval ();
83 return null;
86 /**
87 * Returns the integer value of the specified named argument.
89 * @param name argument name
90 * @return integer value
92 public int get_integer (string name) {
93 var lit = args.get (name) as IntegerLiteral;
94 if (lit != null) {
95 return lit.value.to_int ();
98 return 0;
102 * Returns the double value of the specified named argument.
104 * @param name argument name
105 * @return double value
107 public double get_double (string name) {
108 var arg = args.get (name);
109 if (arg is RealLiteral) {
110 var lit = (RealLiteral) arg;
111 return lit.value.to_double ();
112 } else if (arg is IntegerLiteral) {
113 var lit = (IntegerLiteral) arg;
114 return lit.value.to_int ();
115 } else if (arg is UnaryExpression) {
116 var unary = (UnaryExpression) arg;
117 if (unary.operator == UnaryOperator.MINUS) {
118 if (unary.inner is RealLiteral) {
119 var lit = (RealLiteral) unary.inner;
120 return -lit.value.to_double ();
121 } else if (unary.inner is IntegerLiteral) {
122 var lit = (IntegerLiteral) unary.inner;
123 return -lit.value.to_int ();
128 return 0;
132 * Returns the boolean value of the specified named argument.
134 * @param name argument name
135 * @return boolean value
137 public bool get_bool (string name) {
138 var lit = args.get (name) as BooleanLiteral;
139 if (lit != null) {
140 return lit.value;
143 return false;