girparser: Provide default constructor for classes.
[vala-lang.git] / vala / valaintegerliteral.vala
blobe16c4577ec9b9654e7c0dceea194ca07643ec99e
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 > 0x7fffffff) {
83 // value doesn't fit into signed 32-bit
84 l = 2;
85 } else if (u && n > 0xffffffff) {
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 (CodeContext.get ().profile == Profile.DOVA) {
100 if (u) {
101 type_suffix = "UL";
102 type_name = "uint64";
103 } else {
104 type_suffix = "L";
105 type_name = "int64";
107 } else if (l == 1) {
108 if (u) {
109 type_suffix = "UL";
110 type_name = "ulong";
111 } else {
112 type_suffix = "L";
113 type_name = "long";
115 } else {
116 if (u) {
117 type_suffix = "ULL";
118 type_name = "uint64";
119 } else {
120 type_suffix = "LL";
121 type_name = "int64";
125 var st = (Struct) context.analyzer.root_symbol.scope.lookup (type_name);
126 // ensure attributes are already processed in case of bootstrapping dova-core
127 st.check (context);
129 value_type = new IntegerType (st, value, type_name);
131 return !error;
134 public override void emit (CodeGenerator codegen) {
135 codegen.visit_integer_literal (this);
137 codegen.visit_expression (this);