Skip to content

Commit d164af2

Browse files
committed
Ensures all array indexes have the right type
Also fixes a typo.
1 parent c9ab4eb commit d164af2

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

symbols/arrayaccess.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from api.errmsg import warning
1515
from api.check import is_number
1616
from api.check import is_const
17+
from api.constants import SCOPE
1718

1819
from .call import SymbolCALL
1920
from .number import SymbolNUMBER as NUMBER
@@ -33,11 +34,12 @@ class SymbolARRAYACCESS(SymbolCALL):
3334
it only returns the pointer address to the element).
3435
3536
Parameters:
36-
entry will be the symboltable entry.
37+
entry will be the symbol table entry.
3738
Arglist a SymbolARGLIST instance.
3839
"""
3940
def __init__(self, entry, arglist, lineno):
4041
super(SymbolARRAYACCESS, self).__init__(entry, arglist, lineno)
42+
assert all(gl.BOUND_TYPE == x.type_.type_ for x in arglist), "Invalid type for array index"
4143

4244
@property
4345
def entry(self):
@@ -77,6 +79,9 @@ def offset(self):
7779
Otherwise, if it's not constant (e.g. A(i))
7880
returns None
7981
"""
82+
if self.scope == SCOPE.parameter:
83+
return None
84+
8085
offset = 0
8186
# Now we must typecast each argument to a u16 (POINTER) type
8287
# i is the dimension ith index, b is the bound
@@ -103,24 +108,25 @@ def make_node(cls, id_, arglist, lineno):
103108
if variable is None:
104109
return None
105110

106-
if len(variable.bounds) != len(arglist):
107-
syntax_error(lineno, "Array '%s' has %i dimensions, not %i" %
108-
(variable.name, len(variable.bounds), len(arglist)))
109-
return None
110-
111-
# Checks for array subscript range if the subscript is constant
112-
# e.g. A(1) is a constant subscript access
113-
for i, b in zip(arglist, variable.bounds):
114-
btype = gl.SYMBOL_TABLE.basic_types[gl.BOUND_TYPE]
115-
lower_bound = NUMBER(b.lower, type_=btype, lineno=lineno)
116-
i.value = BINARY.make_node('MINUS',
117-
TYPECAST.make_node(btype, i.value, lineno),
118-
lower_bound, lineno, func=lambda x, y: x - y,
119-
type_=btype)
120-
if is_number(i.value) or is_const(i.value):
121-
val = i.value.value
122-
if val < 0 or val > b.count:
123-
warning(lineno, "Array '%s' subscript out of range" % id_)
111+
if variable.scope != SCOPE.parameter:
112+
if len(variable.bounds) != len(arglist):
113+
syntax_error(lineno, "Array '%s' has %i dimensions, not %i" %
114+
(variable.name, len(variable.bounds), len(arglist)))
115+
return None
116+
117+
# Checks for array subscript range if the subscript is constant
118+
# e.g. A(1) is a constant subscript access
119+
for i, b in zip(arglist, variable.bounds):
120+
btype = gl.SYMBOL_TABLE.basic_types[gl.BOUND_TYPE]
121+
lower_bound = NUMBER(b.lower, type_=btype, lineno=lineno)
122+
i.value = BINARY.make_node('MINUS',
123+
TYPECAST.make_node(btype, i.value, lineno),
124+
lower_bound, lineno, func=lambda x, y: x - y,
125+
type_=btype)
126+
if is_number(i.value) or is_const(i.value):
127+
val = i.value.value
128+
if val < 0 or val > b.count:
129+
warning(lineno, "Array '%s' subscript out of range" % id_)
124130

125131
# Returns the variable entry and the node
126132
return cls(variable, arglist, lineno)

0 commit comments

Comments
 (0)