gstreamer-1.0: Update to 1.13.1+
[vala-gnome.git] / vala / valaintegerliteral.vala
blob3a1d6734c906c0660cf7fdc298bf7b0e4e15d903
1 /* valaintegerliteral.vala
3 * Copyright (C) 2006-2010 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;
25 /**
26 * Represents an integer literal in the source code.
28 public class Vala.IntegerLiteral : Literal {
29 /**
30 * The literal value.
32 public string value { get; set; }
34 public string type_suffix { get; set; }
36 /**
37 * Creates a new integer literal.
39 * @param i literal value
40 * @param source reference to source code
41 * @return newly created integer literal
43 public IntegerLiteral (string i, SourceReference? source = null) {
44 value = i;
45 source_reference = source;
48 public override void accept (CodeVisitor visitor) {
49 visitor.visit_integer_literal (this);
51 visitor.visit_expression (this);
54 public override string to_string () {
55 return value;
58 public override bool is_pure () {
59 return true;
62 public override bool check (CodeContext context) {
63 if (checked) {
64 return !error;
67 checked = true;
69 int l = 0;
70 while (value.has_suffix ("l") || value.has_suffix ("L")) {
71 l++;
72 value = value.substring (0, value.length - 1);
75 bool u = false;
76 if (value.has_suffix ("u") || value.has_suffix ("U")) {
77 u = true;
78 value = value.substring (0, value.length - 1);
81 int64 n = int64.parse (value);
82 if (!u && (n > int.MAX || n < int.MIN)) {
83 // value doesn't fit into signed 32-bit
84 l = 2;
85 } else if (u && n > uint.MAX) {
86 // value doesn't fit into unsigned 32-bit
87 l = 2;
90 string type_name;
91 if (l == 0) {
92 if (u) {
93 type_suffix = "U";
94 type_name = "uint";
95 } else {
96 type_suffix = "";
97 type_name = "int";
99 } else if (l == 1) {
100 if (u) {
101 type_suffix = "UL";
102 type_name = "ulong";
103 } else {
104 type_suffix = "L";
105 type_name = "long";
107 } else {
108 if (u) {
109 type_suffix = "ULL";
110 type_name = "uint64";
111 } else {
112 type_suffix = "LL";
113 type_name = "int64";
117 var st = (Struct) context.root.scope.lookup (type_name);
118 // ensure attributes are already processed
119 st.check (context);
121 value_type = new IntegerType (st, value, type_name);
123 return !error;
126 public override void emit (CodeGenerator codegen) {
127 codegen.visit_integer_literal (this);
129 codegen.visit_expression (this);