- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSwiftIntTypes.py
129 lines (87 loc) · 3.57 KB
/
SwiftIntTypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# ===--- SwiftIntTypes.py ----------------------------*- coding: utf-8 -*-===//
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
# Bit counts for all int types
_all_integer_type_bitwidths= [8, 16, 32, 64]
# Number of bits in the biggest int type
int_max_bits=max(_all_integer_type_bitwidths)
defint_max(bits, signed):
bits=bits-1ifsignedelsebits
bits=max(bits, 0)
return (1<<bits) -1
defint_min(bits, signed):
return (-1*int_max(bits, signed) -1) ifsignedelse0
classSwiftIntegerType(object):
def__init__(self, is_word, bits, is_signed):
self.is_word=is_word
self.bits=bits
self.is_signed=is_signed
ifis_word:
self.possible_bitwidths= [32, 64]
else:
self.possible_bitwidths= [bits]
self.min=int_min(bits, is_signed)
self.max=int_max(bits, is_signed)
# Derived properties
self.stdlib_name= \
(''ifis_signedelse'U') + \
'Int'+ \
(''ifis_wordelsestr(bits))
self.builtin_name='Int'+str(bits)
defget_opposite_signedness(self):
returnSwiftIntegerType(self.is_word, self.bits, notself.is_signed)
def__eq__(self, other):
returnself.is_word==other.is_wordand \
self.bits==other.bitsand \
self.is_signed==other.is_signed
def__ne__(self, other):
returnnotself.__eq__(other)
defall_integer_types(word_bits):
forbitwidthin_all_integer_type_bitwidths:
foris_signedin [False, True]:
yieldSwiftIntegerType(
is_word=False, bits=bitwidth,
is_signed=is_signed)
foris_signedin [False, True]:
yieldSwiftIntegerType(
is_word=True, bits=word_bits,
is_signed=is_signed)
# 'truncatingBitPattern' initializer is defined if the conversion is truncating
# on any platform that Swift supports.
defshould_define_truncating_bit_pattern_init(src_ty, dst_ty):
# Don't define a truncating conversion between a type and itself.
ifsrc_ty==dst_ty:
returnFalse
# Conversion to opposite signedness is never truncating.
ifsrc_ty==dst_ty.get_opposite_signedness():
returnFalse
forsrc_ty_bitsinsrc_ty.possible_bitwidths:
fordst_ty_bitsindst_ty.possible_bitwidths:
ifsrc_ty_bits>dst_ty_bits:
returnTrue
returnFalse
defall_integer_type_names():
return [self_ty.stdlib_nameforself_tyinall_integer_types(0)]
defall_real_number_type_names():
# FIXME , 'Float80' Revert until I figure out a test failure # Float80
# for i386 & x86_64
return ['Float', 'Double']
defall_numeric_type_names():
returnall_integer_type_names() +all_real_number_type_names()
defnumeric_type_names_macintosh_only():
return ['Float80']
# Swift_Programming_Language/Expressions.html
defall_integer_binary_operator_names():
return ['%', '<<', '>>', '&*', '&', '&+', '&-', '|', '^']
defall_integer_or_real_binary_operator_names():
return ['*', '/', '+', '-', '..<', '...']
defall_integer_assignment_operator_names():
return ['%=', '<<=', '>>=', '&=', '^=', '|=']
defall_integer_or_real_assignment_operator_names():
return ['=', '*=', '/=', '+=', '-=']