Changeset 55ba7339 for src/Tests/Subrange.c
- Timestamp:
- Jun 14, 2016, 12:33:57 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- e04ef3a
- Parents:
- 4c82a3c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Tests/Subrange.c
r4c82a3c r55ba7339 1 // A small context defining the notion of an ordered type. (The standard1 // A small context defining the notion of an ordered otype. (The standard 2 2 // library should probably contain a context for this purpose.) 3 context ordered( type T) {3 context ordered(otype T) { 4 4 int ?<?(T, T), ?<=?(T, T); 5 5 }; 6 6 7 // A subrange type resembling an Ada subtype with a basetype and a range7 // A subrange otype resembling an Ada subotype with a base otype and a range 8 8 // constraint. 9 type subrange(type base_t | ordered(base_t), base_t low = 0, base_t high = 8) = base_t;9 otype subrange(otype base_t | ordered(base_t), base_t low = 0, base_t high = 8) = base_t; 10 10 11 // Note that subrange() can be applied to floating-point and pointer types, not12 // just integral types.13 // This requires a " type generator" extension to Cforall. Type generators14 // must accept type and non-type parameters, which is beyond what we discussed11 // Note that subrange() can be applied to floating-point and pointer otypes, not 12 // just integral otypes. 13 // This requires a "otype generator" extension to Cforall. Type generators 14 // must accept otype and non-otype parameters, which is beyond what we discussed 15 15 // previously. Type parameters must be usable in the declaration of 16 16 // subsequent parameters: parameter T is used to declare parameters "low" … … 22 22 subrange(int, 0, (rand() & 0xF) ) foo; 23 23 24 // What sorts of expressions can be used as arguments of type generators? Is24 // What sorts of expressions can be used as arguments of otype generators? Is 25 25 // "subrange(int, 0, rand() & 0xF)" legal? Probably. The nearest C equivalent 26 26 // to the "low" and "high" arguments is the array size in a variable-length … … 28 28 29 29 // Convenient access to subrange bounds, for instance for iteration: 30 forall ( type T, T low, T high)30 forall (otype T, T low, T high) 31 31 T lbound( subrange(T, low, high) v) { 32 32 return low; 33 33 } 34 34 35 forall ( type T, T low, T high)35 forall (otype T, T low, T high) 36 36 T hbound( subrange(T, low, high) v) { 37 37 return high; … … 41 41 unsigned lday = lbound(day_of_month); 42 42 43 // Assignment from the base type, with bounds checking. I'll ignore the issue43 // Assignment from the base otype, with bounds checking. I'll ignore the issue 44 44 // of exception handling here. Inlining allows the compiler to eliminate 45 45 // bounds checks. 46 forall ( type T | ordered(T), T low, T high)46 forall (otype T | ordered(T), T low, T high) 47 47 inline subrange(T, low, high) ?=?(subrange(T, low, high)* target, T source) { 48 48 if (low <= source && source <= high) *((T*)target) = source; … … 51 51 } 52 52 53 // Assignment between subranges with a common base type. The bounds check53 // Assignment between subranges with a common base otype. The bounds check 54 54 // compares range bounds so that the compiler can optimize checks away when the 55 55 // ranges are known to overlap. 56 forall ( type T | ordered(T), T t_low, T t_high, T s_low, T s_high)56 forall (otype T | ordered(T), T t_low, T t_high, T s_low, T s_high) 57 57 inline subrange(T, t_low, t_high) ?=?(subrange(T, t_low, t_high)* target, 58 58 subrange(T, s_low, s_high) source) {
Note: See TracChangeset
for help on using the changeset viewer.