Files
swift-mirror/include/swift/Basic/SourceLoc.h

81 lines
2.7 KiB
C++

//===- SourceLoc.h - Source Locations and Ranges ----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines types used to reason about source locations and ranges.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SOURCELOC_H
#define SWIFT_SOURCELOC_H
#include "swift/AST/LLVM.h"
#include "llvm/Support/SMLoc.h"
namespace llvm {
class SourceMgr;
}
namespace swift {
/// SourceLoc in swift is just an SMLoc. We define it as a different type
/// (instead of as a typedef) just to remove the "getFromPointer" methods and
/// enforce purity in the Swift codebase.
class SourceLoc {
public:
llvm::SMLoc Value;
SourceLoc() {}
explicit SourceLoc(llvm::SMLoc Value) : Value(Value) {}
bool isValid() const { return Value.isValid(); }
bool isInvalid() const { return !Value.isValid(); }
bool operator==(const SourceLoc &RHS) const { return RHS.Value == Value; }
bool operator!=(const SourceLoc &RHS) const { return RHS.Value != Value; }
/// getAdvanced - Return a source location advanced a specified number of
/// characters.
SourceLoc getAdvancedLoc(int NumCharacters) const {
assert(isValid() && "Can't advance an invalid location");
return SourceLoc(llvm::SMLoc::getFromPointer(Value.getPointer() +
NumCharacters));
}
void print(raw_ostream &OS, const llvm::SourceMgr &SM) const;
void dump(const llvm::SourceMgr &SM) const;
};
/// SourceRange in swift is a pair of locations. However, note that the end
/// location is the start of the last token in the range, not the last character
/// in the range. This is unlike SMRange, so we use a distinct type to make
/// sure that proper conversions happen where important.
class SourceRange {
public:
SourceLoc Start, End;
SourceRange() {}
SourceRange(SourceLoc Loc) : Start(Loc), End(Loc) { }
SourceRange(SourceLoc Start, SourceLoc End) : Start(Start), End(End) {
assert(Start.isValid() == End.isValid() &&
"Start and end should either both be valid or both be invalid!");
}
bool isValid() const { return Start.isValid(); }
bool isInvalid() const { return Start.isInvalid(); }
void print(raw_ostream &OS, const llvm::SourceMgr &SM) const;
void dump(const llvm::SourceMgr &SM) const;
};
} // end namespace swift
#endif