2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
39 /* A pet_loc object represents a region of the input file.
40 * The "start" and "end" fields contain the offsets in the input file
41 * of the region, where end points to the first character after the region.
42 * "line" is the line number of a line inside the region.
44 * A special pet_loc_dummy instance is used to indicate that
45 * no offset information is available (yet).
56 /* A special pet_loc object that is used to indicate that
57 * no region information is available yet.
59 * This special pet_loc object cannot be changed.
60 * In particular, it is not allowed to call pet_loc_cow on this object.
62 pet_loc pet_loc_dummy
= {
70 /* Allocate a pet_loc with the given start, end and line number.
72 __isl_give pet_loc
*pet_loc_alloc(isl_ctx
*ctx
,
73 unsigned start
, unsigned end
, int line
)
77 loc
= isl_alloc_type(ctx
, struct pet_loc
);
92 /* Return a pet_loc that is equal to "loc" and that has only one reference.
94 * It is not allowed to call pet_loc_cow on pet_loc_dummy.
95 * We cannot raise an error in this case because pet_loc_dummy does
96 * not have a reference to a valid isl_ctx.
98 __isl_give pet_loc
*pet_loc_cow(__isl_take pet_loc
*loc
)
100 if (loc
== &pet_loc_dummy
)
108 return pet_loc_alloc(loc
->ctx
, loc
->start
, loc
->end
, loc
->line
);
111 /* Return an extra reference to "loc".
113 * The special pet_loc_dummy object is not reference counted.
115 __isl_give pet_loc
*pet_loc_copy(__isl_keep pet_loc
*loc
)
117 if (loc
== &pet_loc_dummy
)
127 /* Free a reference to "loc" and return NULL.
129 * The special pet_loc_dummy object is not reference counted.
131 __isl_null pet_loc
*pet_loc_free(__isl_take pet_loc
*loc
)
133 if (loc
== &pet_loc_dummy
)
140 isl_ctx_deref(loc
->ctx
);
145 /* Return the offset in the input file of the start of "loc".
147 unsigned pet_loc_get_start(__isl_keep pet_loc
*loc
)
149 return loc
? loc
->start
: 0;
152 /* Return the offset in the input file of the character after "loc".
154 unsigned pet_loc_get_end(__isl_keep pet_loc
*loc
)
156 return loc
? loc
->end
: 0;
159 /* Return the line number of a line within the "loc" region.
161 int pet_loc_get_line(__isl_keep pet_loc
*loc
)
163 return loc
? loc
->line
: -1;
166 /* Update loc->start and loc->end to include the region from "start"
169 * Since we may be modifying "loc", it should be different from
172 __isl_give pet_loc
*pet_loc_update_start_end(__isl_take pet_loc
*loc
,
173 unsigned start
, unsigned end
)
175 loc
= pet_loc_cow(loc
);
179 if (start
< loc
->start
)
187 /* Update loc->start and loc->end to include the region of "loc2".
189 * "loc" may be pet_loc_dummy, in which case we return a copy of "loc2".
190 * Similarly, if "loc2" is pet_loc_dummy, then we leave "loc" untouched.
192 __isl_give pet_loc
*pet_loc_update_start_end_from_loc(__isl_take pet_loc
*loc
,
193 __isl_keep pet_loc
*loc2
)
196 return pet_loc_free(loc
);
197 if (loc
== &pet_loc_dummy
)
198 return pet_loc_copy(loc2
);
199 if (loc2
== &pet_loc_dummy
)
201 return pet_loc_update_start_end(loc
, loc2
->start
, loc2
->end
);