valaintegerliteral.vala 2.96 KB
Newer Older
1 2
/* valaintegerliteral.vala
 *
3
 * Copyright (C) 2006-2010  Jürg Billeter
4 5 6 7
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

 * 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;

25 26 27 28 29 30 31
/**
 * Represents an integer literal in the source code.
 */
public class Vala.IntegerLiteral : Literal {
	/**
	 * The literal value.
	 */
32
	public string value { get; set; }
33

34 35
	public string type_suffix { get; set; }

36 37 38 39 40 41 42
	/**
	 * Creates a new integer literal.
	 *
	 * @param i      literal value
	 * @param source reference to source code
	 * @return       newly created integer literal
	 */
43
	public IntegerLiteral (string i, SourceReference? source = null) {
44 45
		value = i;
		source_reference = source;
46
	}
47

48
	public override void accept (CodeVisitor visitor) {
49
		visitor.visit_integer_literal (this);
50 51

		visitor.visit_expression (this);
52
	}
53

54
	public override string to_string () {
55 56
		return value;
	}
57 58 59 60 61

	public override bool is_pure () {
		return true;
	}

62
	public override bool check (CodeContext context) {
63 64 65 66 67 68
		if (checked) {
			return !error;
		}

		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);
73 74 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);
79
		}
80

81
		int64 n = int64.parse (value);
82
		if (!u && (n > int.MAX || n < int.MIN)) {
83 84
			// value doesn't fit into signed 32-bit
			l = 2;
85
		} else if (u && n > uint.MAX) {
86 87 88 89
			// value doesn't fit into unsigned 32-bit
			l = 2;
		}

90
		string type_name;
91 92
		if (l == 0) {
			if (u) {
93 94
				type_suffix = "U";
				type_name = "uint";
95
			} else {
96 97
				type_suffix = "";
				type_name = "int";
98
			}
99
		} else if (l == 1) {
100
			if (u) {
101 102
				type_suffix = "UL";
				type_name = "ulong";
103
			} else {
104 105
				type_suffix = "L";
				type_name = "long";
106
			}
107 108
		} else {
			if (u) {
109 110
				type_suffix = "ULL";
				type_name = "uint64";
111
			} else {
112 113
				type_suffix = "LL";
				type_name = "int64";
114 115
			}
		}
116

117
		var st = (Struct) context.root.scope.lookup (type_name);
Jürg Billeter's avatar
Jürg Billeter committed
118
		// ensure attributes are already processed
119
		st.check (context);
120 121

		value_type = new IntegerType (st, value, type_name);
122 123 124

		return !error;
	}
125 126 127 128 129 130

	public override void emit (CodeGenerator codegen) {
		codegen.visit_integer_literal (this);

		codegen.visit_expression (this);
	}
131
}