XRM 81: Detect overlapping declarations of arrays.

https://svn.lrde.epita.fr/svn/xrm/trunk Index: ChangeLog from SIGOURE Benoit <sigoure.benoit@lrde.epita.fr> Detect overlapping declarations of arrays. Consider the following case: x[1][1] : [0..2] init 2; x[1][1,2] : [0..3] init 3; Here, we're declaring the array `x' in two parts. As we can see, the second part overlaps with the first. Moreover the, overlapping part has a different definition in both declarations. This is now caught and reported as an error. * src/str/array-decl-desugar.str: Detect overlapping declarations. * doc/user-guide.txt: Bring up to date. doc/user-guide.txt | 2 - src/str/array-decl-desugar.str | 48 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) Index: src/str/array-decl-desugar.str --- src/str/array-decl-desugar.str (revision 80) +++ src/str/array-decl-desugar.str (working copy) @@ -33,13 +33,57 @@ */ <fetch-idf> array-access => idf ; <desugar-array-access> array-access => aa-list - ; rules(DeclaredIdentifier: Identifier(idf) -> aa-list) - ; desugared-array-access-list-to-identifier-list(|idf) + ; where(check-decl-overlaps-previous-decl(|idf, aa-list) => old-aa-list) + ; <conc>(old-aa-list, aa-list) => new-aa-list + ; rules(DeclaredIdentifier: Identifier(idf) -> new-aa-list) + ; <desugared-array-access-list-to-identifier-list(|idf)> aa-list ; map({var-name: ?var-name ; <build-dec(|var-name)> data }) + /** @internal + ** Check whether the current array declaration overlaps with a previous + ** array declaration. The dimensions declared in the current decl are + ** in aa-list. We fetch previously declared dimensions (if any) using the + ** DR DeclaredIdentifier. If one of the dimensions in aa-list is the same + ** as one of those previously declared, the current declaration overlaps + ** with a previous one (=> error!). + ** + ** @return the list of previously declared dimensions (if any) or an empty + ** list otherwise. + */ + check-decl-overlaps-previous-decl(|idf, aa-list) = + if <DeclaredIdentifier> Identifier(idf) => old-aa-list then + /* Here old-aa-list contains the dimensions already defined for `idf'. */ + if !old-aa-list => [_|_] then // if the list is not empty ... + /* ... then we already had a definition for that idf, check that + * both definitions don't overlap. */ + !old-aa-list + /* ok now we must check if one of the dimensions in aa-list is + * also present in the current term (which is list of dimensions + * already defined). If one of them is present in both lists, the + * current definition overlaps a former definition. */ + ; map({old-dim: + ?old-dim // for each old-dim in old-aa-list... + /* ... check whether old-dim is somewhere in aa-list */ + ; <map(try(?old-dim; error-overlapping-dim(|idf, aa-list)))> aa-list + }) + ; !old-aa-list + else // return an empty list + ![] + end + else // ditto. + ![] + end + + error-overlapping-dim(|idf, aa-list) = + err-msg(|["Invalid declaration of array `", idf, "'. This array is ", + "declared in multiple parts which overlap."]) + ; debug(!"Overlapping dimension: ") + ; <debug(!"Dimensions already declared for that array: ")> aa-list + ; <xtc-exit> 5 + /** Builders. */ rules Index: doc/user-guide.txt --- doc/user-guide.txt (revision 80) +++ doc/user-guide.txt (working copy) @@ -101,7 +101,7 @@ - 2: error with meta-vars (eg: undefined meta-var, redefined meta-var) - 3: arithmetic error when evaluating code (eg: division/modulo by 0) - 4: invalid call to a builtin (eg: rand(1,2,3)) - - 5: invalid array access (eg: subscript is not a positive integer) + - 5: errors related with arrays (eg: subscript is not a positive integer) - 6: invalid call to a parameterized formula (eg: not enough arguments) - 42: internal compiler error (please send a bug report) - 51: not yet implemented
participants (1)
-
SIGOURE Benoit