make "locations" a class instead of a typedef.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66895 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-03-13 16:01:53 +00:00
parent d9c9bf77d8
commit 1c8ae59dfd
7 changed files with 82 additions and 58 deletions

View File

@ -31,6 +31,10 @@ TGLexer::TGLexer(TGSourceMgr &SM) : SrcMgr(SM) {
TokStart = 0;
}
TGLoc TGLexer::getLoc() const {
return TGLoc::getFromPointer(TokStart);
}
/// ReturnError - Set the error to the specified string at the specified
/// location. This is defined to always return tgtok::Error.
@ -40,10 +44,15 @@ tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
}
void TGLexer::PrintError(LocTy Loc, const std::string &Msg) const {
void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
SrcMgr.PrintError(TGLoc::getFromPointer(Loc), Msg);
}
void TGLexer::PrintError(TGLoc Loc, const std::string &Msg) const {
SrcMgr.PrintError(Loc, Msg);
}
int TGLexer::getNextChar() {
char CurChar = *CurPtr++;
switch (CurChar) {
@ -57,11 +66,11 @@ int TGLexer::getNextChar() {
// If this is the end of an included file, pop the parent file off the
// include stack.
TGLocTy ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
if (ParentIncludeLoc != TGLocTy()) {
TGLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
if (ParentIncludeLoc != TGLoc()) {
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = ParentIncludeLoc;
CurPtr = ParentIncludeLoc.getPointer();
return getNextChar();
}
@ -239,7 +248,7 @@ bool TGLexer::LexInclude() {
}
// Save the line number and lex buffer of the includer.
CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, CurPtr);
CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, TGLoc::getFromPointer(CurPtr));
CurBuf = NewBuf;
CurPtr = CurBuf->getBufferStart();

View File

@ -23,6 +23,7 @@
namespace llvm {
class MemoryBuffer;
class TGSourceMgr;
class TGLoc;
namespace tgtok {
enum TokKind {
@ -99,10 +100,10 @@ public:
return CurIntVal;
}
typedef const char* LocTy;
LocTy getLoc() const { return TokStart; }
TGLoc getLoc() const;
void PrintError(LocTy Loc, const std::string &Msg) const;
void PrintError(const char *Loc, const std::string &Msg) const;
void PrintError(TGLoc Loc, const std::string &Msg) const;
private:
/// LexToken - Read the next token and return its code.

View File

@ -31,17 +31,17 @@ struct MultiClass {
};
struct SubClassReference {
TGParser::LocTy RefLoc;
TGLoc RefLoc;
Record *Rec;
std::vector<Init*> TemplateArgs;
SubClassReference() : RefLoc(0), Rec(0) {}
SubClassReference() : Rec(0) {}
bool isInvalid() const { return Rec == 0; }
};
} // end namespace llvm
bool TGParser::AddValue(Record *CurRec, LocTy Loc, const RecordVal &RV) {
bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
if (CurRec == 0)
CurRec = &CurMultiClass->Rec;
@ -60,7 +60,7 @@ bool TGParser::AddValue(Record *CurRec, LocTy Loc, const RecordVal &RV) {
/// SetValue -
/// Return true on error, false on success.
bool TGParser::SetValue(Record *CurRec, LocTy Loc, const std::string &ValName,
bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName,
const std::vector<unsigned> &BitList, Init *V) {
if (!V) return false;
@ -357,7 +357,7 @@ bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
if (Lex.getCode() != tgtok::less)
return false;
LocTy StartLoc = Lex.getLoc();
TGLoc StartLoc = Lex.getLoc();
Lex.Lex(); // eat the '<'
// Parse the range list.
@ -379,7 +379,7 @@ bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
if (Lex.getCode() != tgtok::l_brace)
return false;
LocTy StartLoc = Lex.getLoc();
TGLoc StartLoc = Lex.getLoc();
Lex.Lex(); // eat the '{'
// Parse the range list.
@ -464,7 +464,7 @@ RecTy *TGParser::ParseType() {
Init *TGParser::ParseIDValue(Record *CurRec) {
assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
std::string Name = Lex.getCurStrVal();
LocTy Loc = Lex.getLoc();
TGLoc Loc = Lex.getLoc();
Lex.Lex();
return ParseIDValue(CurRec, Name, Loc);
}
@ -472,7 +472,7 @@ Init *TGParser::ParseIDValue(Record *CurRec) {
/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
/// has already been read.
Init *TGParser::ParseIDValue(Record *CurRec,
const std::string &Name, LocTy NameLoc) {
const std::string &Name, TGLoc NameLoc) {
if (CurRec) {
if (const RecordVal *RV = CurRec->getValue(Name))
return new VarInit(Name, RV->getType());
@ -540,7 +540,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec) {
R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
case tgtok::Id: {
LocTy NameLoc = Lex.getLoc();
TGLoc NameLoc = Lex.getLoc();
std::string Name = Lex.getCurStrVal();
if (Lex.Lex() != tgtok::less) // consume the Id.
return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
@ -585,7 +585,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec) {
return new DefInit(NewRec);
}
case tgtok::l_brace: { // Value ::= '{' ValueList '}'
LocTy BraceLoc = Lex.getLoc();
TGLoc BraceLoc = Lex.getLoc();
Lex.Lex(); // eat the '{'
std::vector<Init*> Vals;
@ -711,7 +711,7 @@ Init *TGParser::ParseValue(Record *CurRec) {
switch (Lex.getCode()) {
default: return Result;
case tgtok::l_brace: {
LocTy CurlyLoc = Lex.getLoc();
TGLoc CurlyLoc = Lex.getLoc();
Lex.Lex(); // eat the '{'
std::vector<unsigned> Ranges = ParseRangeList();
if (Ranges.empty()) return 0;
@ -733,7 +733,7 @@ Init *TGParser::ParseValue(Record *CurRec) {
break;
}
case tgtok::l_square: {
LocTy SquareLoc = Lex.getLoc();
TGLoc SquareLoc = Lex.getLoc();
Lex.Lex(); // eat the '['
std::vector<unsigned> Ranges = ParseRangeList();
if (Ranges.empty()) return 0;
@ -849,7 +849,7 @@ std::string TGParser::ParseDeclaration(Record *CurRec,
return "";
}
LocTy IdLoc = Lex.getLoc();
TGLoc IdLoc = Lex.getLoc();
std::string DeclName = Lex.getCurStrVal();
Lex.Lex();
@ -870,7 +870,7 @@ std::string TGParser::ParseDeclaration(Record *CurRec,
// If a value is present, parse it.
if (Lex.getCode() == tgtok::equal) {
Lex.Lex();
LocTy ValLoc = Lex.getLoc();
TGLoc ValLoc = Lex.getLoc();
Init *Val = ParseValue(CurRec);
if (Val == 0 ||
SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
@ -936,7 +936,7 @@ bool TGParser::ParseBodyItem(Record *CurRec) {
if (Lex.Lex() != tgtok::Id)
return TokError("expected field identifier after let");
LocTy IdLoc = Lex.getLoc();
TGLoc IdLoc = Lex.getLoc();
std::string FieldName = Lex.getCurStrVal();
Lex.Lex(); // eat the field name.
@ -1034,7 +1034,7 @@ bool TGParser::ParseObjectBody(Record *CurRec) {
/// DefInst ::= DEF ObjectName ObjectBody
///
llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
LocTy DefLoc = Lex.getLoc();
TGLoc DefLoc = Lex.getLoc();
assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Lex.Lex(); // Eat the 'def' token.
@ -1122,7 +1122,7 @@ std::vector<LetRecord> TGParser::ParseLetList() {
return std::vector<LetRecord>();
}
std::string Name = Lex.getCurStrVal();
LocTy NameLoc = Lex.getLoc();
TGLoc NameLoc = Lex.getLoc();
Lex.Lex(); // Eat the identifier.
// Check for an optional RangeList.
@ -1174,7 +1174,7 @@ bool TGParser::ParseTopLevelLet() {
if (ParseObject())
return true;
} else { // Object ::= LETCommand '{' ObjectList '}'
LocTy BraceLoc = Lex.getLoc();
TGLoc BraceLoc = Lex.getLoc();
// Otherwise, this is a group let.
Lex.Lex(); // eat the '{'.
@ -1265,7 +1265,7 @@ bool TGParser::ParseDefm() {
if (Lex.Lex() != tgtok::Id) // eat the defm.
return TokError("expected identifier after defm");
LocTy DefmPrefixLoc = Lex.getLoc();
TGLoc DefmPrefixLoc = Lex.getLoc();
std::string DefmPrefix = Lex.getCurStrVal();
if (Lex.Lex() != tgtok::colon)
return TokError("expected ':' after defm identifier");
@ -1273,7 +1273,7 @@ bool TGParser::ParseDefm() {
// eat the colon.
Lex.Lex();
LocTy SubClassLoc = Lex.getLoc();
TGLoc SubClassLoc = Lex.getLoc();
SubClassReference Ref = ParseSubClassReference(0, true);
if (Ref.Rec == 0) return true;

View File

@ -15,6 +15,7 @@
#define TGPARSER_H
#include "TGLexer.h"
#include "TGSourceMgr.h"
#include <map>
namespace llvm {
@ -29,9 +30,9 @@ namespace llvm {
std::string Name;
std::vector<unsigned> Bits;
Init *Value;
TGLexer::LocTy Loc;
TGLoc Loc;
LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
TGLexer::LocTy L)
TGLoc L)
: Name(N), Bits(B), Value(V), Loc(L) {
}
};
@ -45,7 +46,6 @@ class TGParser {
/// current value.
MultiClass *CurMultiClass;
public:
typedef TGLexer::LocTy LocTy;
TGParser(TGSourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
@ -55,7 +55,7 @@ public:
/// routines return true on error, or false on success.
bool ParseFile();
bool Error(LocTy L, const std::string &Msg) const {
bool Error(TGLoc L, const std::string &Msg) const {
Lex.PrintError(L, Msg);
return true;
}
@ -63,8 +63,8 @@ public:
return Error(Lex.getLoc(), Msg);
}
private: // Semantic analysis methods.
bool AddValue(Record *TheRec, LocTy Loc, const RecordVal &RV);
bool SetValue(Record *TheRec, LocTy Loc, const std::string &ValName,
bool AddValue(Record *TheRec, TGLoc Loc, const RecordVal &RV);
bool SetValue(Record *TheRec, TGLoc Loc, const std::string &ValName,
const std::vector<unsigned> &BitList, Init *V);
bool AddSubClass(Record *Rec, SubClassReference &SubClass);
@ -89,7 +89,7 @@ private: // Parser methods.
SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
Init *ParseIDValue(Record *CurRec);
Init *ParseIDValue(Record *CurRec, const std::string &Name, LocTy NameLoc);
Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
Init *ParseSimpleValue(Record *CurRec);
Init *ParseValue(Record *CurRec);
std::vector<Init*> ParseValueList(Record *CurRec);

View File

@ -25,17 +25,17 @@ TGSourceMgr::~TGSourceMgr() {
/// FindBufferContainingLoc - Return the ID of the buffer containing the
/// specified location, returning -1 if not found.
int TGSourceMgr::FindBufferContainingLoc(TGLocTy Loc) const {
int TGSourceMgr::FindBufferContainingLoc(TGLoc Loc) const {
for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
if (Loc >= Buffers[i].Buffer->getBufferStart() &&
Loc < Buffers[i].Buffer->getBufferEnd())
if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Loc.getPointer() < Buffers[i].Buffer->getBufferEnd())
return i;
return -1;
}
/// FindLineNumber - Find the line number for the specified location in the
/// specified file. This is not a fast method.
unsigned TGSourceMgr::FindLineNumber(TGLocTy Loc, int BufferID) const {
unsigned TGSourceMgr::FindLineNumber(TGLoc Loc, int BufferID) const {
if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
assert(BufferID != -1 && "Invalid Location!");
@ -47,13 +47,13 @@ unsigned TGSourceMgr::FindLineNumber(TGLocTy Loc, int BufferID) const {
const char *Ptr = Buff->getBufferStart();
for (; Ptr != Loc; ++Ptr)
for (; TGLoc::getFromPointer(Ptr) != Loc; ++Ptr)
if (*Ptr == '\n') ++LineNo;
return LineNo;
}
void TGSourceMgr::PrintIncludeStack(TGLocTy IncludeLoc) const {
if (IncludeLoc == TGLocTy()) return; // Top of stack.
void TGSourceMgr::PrintIncludeStack(TGLoc IncludeLoc) const {
if (IncludeLoc == TGLoc()) return; // Top of stack.
int CurBuf = FindBufferContainingLoc(IncludeLoc);
assert(CurBuf != -1 && "Invalid or unspecified location!");
@ -66,7 +66,7 @@ void TGSourceMgr::PrintIncludeStack(TGLocTy IncludeLoc) const {
}
void TGSourceMgr::PrintError(TGLocTy ErrorLoc, const std::string &Msg) const {
void TGSourceMgr::PrintError(TGLoc ErrorLoc, const std::string &Msg) const {
raw_ostream &OS = errs();
// First thing to do: find the current buffer containing the specified
@ -83,22 +83,21 @@ void TGSourceMgr::PrintError(TGLocTy ErrorLoc, const std::string &Msg) const {
<< FindLineNumber(ErrorLoc, CurBuf) << ": ";
OS << Msg << "\n";
assert(ErrorLoc && "Location not specified!");
// Scan backward to find the start of the line.
const char *LineStart = ErrorLoc;
const char *LineStart = ErrorLoc.getPointer();
while (LineStart != CurMB->getBufferStart() &&
LineStart[-1] != '\n' && LineStart[-1] != '\r')
--LineStart;
// Get the end of the line.
const char *LineEnd = ErrorLoc;
const char *LineEnd = ErrorLoc.getPointer();
while (LineEnd != CurMB->getBufferEnd() &&
LineEnd[0] != '\n' && LineEnd[0] != '\r')
++LineEnd;
// Print out the line.
OS << std::string(LineStart, LineEnd) << "\n";
// Print out spaces before the carat.
for (const char *Pos = LineStart; Pos != ErrorLoc; ++Pos)
for (const char *Pos = LineStart; Pos != ErrorLoc.getPointer(); ++Pos)
OS << (*Pos == '\t' ? '\t' : ' ');
OS << "^\n";
}

View File

@ -20,9 +20,25 @@
namespace llvm {
class MemoryBuffer;
class TGSourceMgr;
/// FIXME: Make this a struct that is opaque.
typedef const char *TGLocTy;
class TGLoc {
const char *Ptr;
public:
TGLoc() : Ptr(0) {}
TGLoc(const TGLoc &RHS) : Ptr(RHS.Ptr) {}
bool operator==(const TGLoc &RHS) const { return RHS.Ptr == Ptr; }
bool operator!=(const TGLoc &RHS) const { return RHS.Ptr != Ptr; }
const char *getPointer() const { return Ptr; }
static TGLoc getFromPointer(const char *Ptr) {
TGLoc L;
L.Ptr = Ptr;
return L;
}
};
/// TGSourceMgr - This owns the files read by tblgen, handles include stacks,
/// and handles printing of diagnostics.
@ -33,7 +49,7 @@ class TGSourceMgr {
/// IncludeLoc - This is the location of the parent include, or null if at
/// the top level.
TGLocTy IncludeLoc;
TGLoc IncludeLoc;
};
/// Buffers - This is all of the buffers that we are reading from.
@ -55,12 +71,12 @@ public:
return Buffers[i].Buffer;
}
TGLocTy getParentIncludeLoc(unsigned i) const {
TGLoc getParentIncludeLoc(unsigned i) const {
assert(i < Buffers.size() && "Invalid Buffer ID!");
return Buffers[i].IncludeLoc;
}
unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLocTy IncludeLoc) {
unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLoc IncludeLoc) {
SrcBuffer NB;
NB.Buffer = F;
NB.IncludeLoc = IncludeLoc;
@ -70,19 +86,19 @@ public:
/// FindBufferContainingLoc - Return the ID of the buffer containing the
/// specified location, returning -1 if not found.
int FindBufferContainingLoc(TGLocTy Loc) const;
int FindBufferContainingLoc(TGLoc Loc) const;
/// FindLineNumber - Find the line number for the specified location in the
/// specified file. This is not a fast method.
unsigned FindLineNumber(TGLocTy Loc, int BufferID = -1) const;
unsigned FindLineNumber(TGLoc Loc, int BufferID = -1) const;
/// PrintError - Emit an error message about the specified location with the
/// specified string.
void PrintError(TGLocTy ErrorLoc, const std::string &Msg) const;
void PrintError(TGLoc ErrorLoc, const std::string &Msg) const;
private:
void PrintIncludeStack(TGLocTy IncludeLoc) const;
void PrintIncludeStack(TGLoc IncludeLoc) const;
};
} // end llvm namespace

View File

@ -17,7 +17,6 @@
#include "Record.h"
#include "TGParser.h"
#include "TGSourceMgr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Streams.h"
#include "llvm/System/Signals.h"
@ -124,7 +123,7 @@ static bool ParseFile(const std::string &Filename,
}
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(F, TGLocTy());
SrcMgr.AddNewSourceBuffer(F, TGLoc());
TGParser Parser(SrcMgr);