[flang] Add position.{h,cc}.

Original-commit: flang-compiler/f18@78c9c256ba
This commit is contained in:
peter klausler 2018-01-30 11:54:19 -08:00
parent e76093ca67
commit 1651445fd3
2 changed files with 77 additions and 0 deletions

7
flang/position.cc Normal file
View file

@ -0,0 +1,7 @@
#include "position.h"
namespace Fortran {
std::ostream &operator<<(std::ostream &o, const Position &x) {
return o << "(at line " << x.lineNumber() << ", column " << x.column() << ')';
}
} // namespace Fortran

70
flang/position.h Normal file
View file

@ -0,0 +1,70 @@
#ifndef FORTRAN_POSITION_H_
#define FORTRAN_POSITION_H_
// Represents a position in a source file.
// TODO: Support multiple source files for inclusion and support contextual
// positions for macro expansions.
#include <ostream>
namespace Fortran {
class Position {
public:
constexpr Position() = default;
constexpr Position(int ln, int col)
: lineNumber_{ln}, column_{col} {}
constexpr int lineNumber() const { return lineNumber_; }
constexpr int column() const { return column_; }
constexpr bool operator<(const Position &that) const {
return lineNumber_ < that.lineNumber_ ||
(lineNumber_ == that.lineNumber_ &&
column_ < that.column_);
}
constexpr bool operator<=(const Position &that) const {
return lineNumber_ < that.lineNumber_ ||
(lineNumber_ == that.lineNumber_ &&
column_ <= that.column_);
}
constexpr bool operator==(const Position &that) const {
return lineNumber_ == that.lineNumber_ &&
column_ == that.column_;
}
constexpr bool operator!=(const Position &that) const {
return !operator==(that);
}
constexpr bool operator>(const Position &that) const {
return !operator<=(that);
}
constexpr bool operator>=(const Position &that) const {
return !operator<(that);
}
void AdvanceColumn() {
++column_;
}
void TabAdvanceColumn() {
column_ = ((column_ + 7) & -8) + 1;
}
void AdvanceLine() {
++lineNumber_;
column_ = 1;
}
private:
int lineNumber_{1};
int column_{1};
};
std::ostream &operator<<(std::ostream &, const Position &);
} // namespace Fortran
#endif // FORTRAN_POSITION_H_