Permalink
Browse files

newclay/lib-clay: safety assertions for Vectors, Arrays, and Coordina…

…teRanges
  • Loading branch information...
1 parent bf24617 commit 250bdb24e22a42ceeaa6a4bcfc57cd43d6176275 @jckarter jckarter committed Jun 17, 2011
View
7 newclay/lib-clay/prelude/arrays/arrays.clay
@@ -40,11 +40,14 @@ overload end(x:Array['T, 'n]) inline = pointerCast(ContiguousCoordinate['T], &x)
// indexing
//
-overload index(forward a:Array['T, 'n], i:'I) inline | Integer?('I)
- = forward cIndex('T, a, i);
+overload index(forward a:Array['T, 'n], i:'I) inline | Integer?('I) {
+ assert(-> i >= 'I(0) and UInt(i) < UInt('n), Array['T, 'n], " index out of bounds");
+ return forward cIndex('T, a, i);
+}
overload index(rvalue a:Array['T, 'n], i:'I) inline --> returned:'T
| Integer?('I) and not DestroyDoesNothing?('T)
{
+ assert(-> i >= 'I(0) and UInt(i) < UInt('n), Array['T, 'n], " index out of bounds");
ref ra = rvalueToRef(a);
returned <-- refToRvalue(cIndex('T, ra, i));
var n = 0u;
View
25 newclay/lib-clay/unsafe/coordinates/coordinates.clay
@@ -99,12 +99,18 @@ overload begin(i:CoordinateRange['C]) inline = i.begin;
overload end(i:CoordinateRange['C]) inline = i.end;
overload hasFront?(i:CoordinateRange['C]) inline = i.begin < i.end;
-overload front(i:CoordinateRange['C]) inline = forward ..i.begin^;
+overload front(i:CoordinateRange['C]) inline {
+ assert(-> i.begin < i.end, "accessing front() of invalid ", CoordinateRange['C]);
+ return forward ..i.begin^;
+}
overload incFront(ref i:CoordinateRange['C]) inline { inc(i.begin); }
overload back(i:CoordinateRange['C]) inline
| BidirectionalCoordinate?('C)
- = forward ..(var preend = i.end; dec(preend); preend)^;
+{
+ assert(-> i.begin < i.end, "accessing back() of invalid ", CoordinateRange['C]);
+ return forward ..(var preend = i.end; dec(preend); preend)^;
+}
overload incBack(ref i:CoordinateRange['C]) inline
| BidirectionalCoordinate?('C)
{ dec(i.end); }
@@ -130,12 +136,19 @@ overload iterator(x:'S) | CoordinateSequence?('S)
overload size(x:'S) | CoordinateSequence?('S) and defined?(-> end(x) - begin(x))
= UInt(end(x) - begin(x));
-overload index(x:'S, n)
- | CoordinateSequence?('S) and defined?(-> begin(x) + n)
- = forward ..dereference(begin(x) + n);
+overload index(x:'S, n:'I)
+ | CoordinateSequence?('S) and Integer?('n) and defined?(-> begin(x) + n)
+{
+ assert(-> n >= 'I(0) and UInt(n) < size(x), 'S, " index out of range");
+ return forward ..dereference(begin(x) + n);
+}
-overload front(x:'S) | CoordinateSequence?('S) = forward ..dereference(begin(x));
+overload front(x:'S) | CoordinateSequence?('S) {
+ assert(-> size(x) > 0u, "accessing front() of empty ", 'S);
+ return forward ..dereference(begin(x));
+}
overload back(x:'S) | CoordinateSequence?('S) {
+ assert(-> size(x) > 0u, "accessing back() of empty ", 'S);
var c = end(x);
dec(c);
return forward ..c^;
View
6 newclay/lib-clay/vectors/vectors.clay
@@ -85,10 +85,14 @@ overload copy(a:Vector['T]) inline = Vector['T](a);
overload size(v:Vector['T]) inline = v.size;
overload index(/*not rvalue*/ forward v:Vector['T], i:'I) inline
| Integer?('I)
- = forward v.data[i];
+{
+ assert(-> i >= 'I(0) and UInt(i) < v.size, Vector['T], " index out of bounds");
+ return forward v.data[i];
+}
overload index(rvalue v:Vector['T], i:'I) inline {
var w = v;
+ assert(-> i >= 'I(0) and UInt(i) < w.size, Vector['T], " index out of bounds");
return w.data[i];
}

0 comments on commit 250bdb2

Please sign in to comment.