source: icGREP/icgrep-devel/icgrep/re/re_range.cpp @ 6237

Last change on this file since 6237 was 6226, checked in by cameron, 9 months ago

Clean-up/refactoring step for REs

File size: 1.5 KB
RevLine 
[5764]1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
[5787]7#include <re/re_range.h>
8#include <re/re_cc.h>
9#include <re/re_name.h>
[5764]10#include <llvm/Support/Casting.h>
[6173]11#include <llvm/Support/raw_ostream.h>
[5788]12#include <llvm/Support/ErrorHandling.h>
[5764]13
14using namespace llvm;
15
16namespace re {
17
18RE * makeRange(RE * lo, RE * hi) {
19    if (isa<CC>(lo) && isa<CC>(hi)) {
[6173]20        CC * const cc_lo = cast<CC>(lo);
21        CC * const cc_hi = cast<CC>(hi);
22        if (LLVM_LIKELY((cc_lo->count() == 1) && (cc_hi->count() == 1))) {
23            const auto lo_val = cc_lo->at(0);
24            const auto hi_val = cc_hi->at(0);
25            if (LLVM_LIKELY(lo_val <= hi_val)) {
26                return makeCC(lo_val, hi_val, dyn_cast<CC>(hi)->getAlphabet());
27            }
28        }
29        std::string tmp;
30        raw_string_ostream out(tmp);
31        cc_lo->print(out);
32        out << " to ";
33        cc_hi->print(out);
34        out << " are invalid range operands";
35        report_fatal_error(out.str());
[5764]36    }
[5787]37    else if (isa<Name>(lo) && (cast<Name>(lo)->getDefinition() != nullptr)) {
38        return makeRange(cast<Name>(lo)->getDefinition(), hi);
39    }
40    else if (isa<Name>(hi) && (cast<Name>(hi)->getDefinition() != nullptr)) {
41        return makeRange(lo, cast<Name>(hi)->getDefinition());
42    }
[5764]43    else if (lo == hi) { // TODO: general check for equality, not just instance equality
44        return lo;
45    }
[6226]46    return Range::Create(lo, hi);
[5764]47}
48   
49}
Note: See TracBrowser for help on using the repository browser.