change the Init print methods to return strings, and implement

print in terms of that.

llvm-svn: 44276
This commit is contained in:
Chris Lattner 2007-11-22 21:05:25 +00:00
parent 1b1e96b8d7
commit 695506c046
2 changed files with 58 additions and 51 deletions

View File

@ -244,20 +244,20 @@ Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
return BI;
}
void BitsInit::print(std::ostream &OS) const {
std::string BitsInit::getAsString() const {
//if (!printInHex(OS)) return;
//if (!printAsVariable(OS)) return;
//if (!printAsUnset(OS)) return;
OS << "{ ";
std::string Result = "{ ";
for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
if (i) OS << ", ";
if (i) Result += ", ";
if (Init *Bit = getBit(e-i-1))
Bit->print(OS);
Result += Bit->getAsString();
else
OS << "*";
Result += "*";
}
OS << " }";
return Result + " }";
}
bool BitsInit::printInHex(std::ostream &OS) const {
@ -330,6 +330,10 @@ Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
return this;
}
std::string IntInit::getAsString() const {
return itostr(Value);
}
Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
BitsInit *BI = new BitsInit(Bits.size());
@ -382,13 +386,13 @@ Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
return this;
}
void ListInit::print(std::ostream &OS) const {
OS << "[";
std::string ListInit::getAsString() const {
std::string Result = "[";
for (unsigned i = 0, e = Values.size(); i != e; ++i) {
if (i) OS << ", ";
OS << *Values[i];
if (i) Result += ", ";
Result += Values[i]->getAsString();
}
OS << "]";
return Result + "]";
}
Init *BinOpInit::Fold() {
@ -464,19 +468,16 @@ Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
return Fold();
}
void BinOpInit::print(std::ostream &OS) const {
std::string BinOpInit::getAsString() const {
std::string Result;
switch (Opc) {
case CONCAT: OS << "!con"; break;
case SHL: OS << "!shl"; break;
case SRA: OS << "!sra"; break;
case SRL: OS << "!srl"; break;
case STRCONCAT: OS << "!strconcat"; break;
case CONCAT: Result = "!con"; break;
case SHL: Result = "!shl"; break;
case SRA: Result = "!sra"; break;
case SRL: Result = "!srl"; break;
case STRCONCAT: Result = "!strconcat"; break;
}
OS << "(";
LHS->print(OS);
OS << ", ";
RHS->print(OS);
OS << ")";
return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
}
Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
@ -579,6 +580,9 @@ Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
return this;
}
std::string VarBitInit::getAsString() const {
return TI->getAsString() + "{" + utostr(Bit) + "}";
}
Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
@ -586,6 +590,10 @@ Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
return this;
}
std::string VarListElementInit::getAsString() const {
return TI->getAsString() + "[" + utostr(Element) + "]";
}
Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
if (Init *I = getVariable()->resolveListElementReference(R, RV,
getElementNum()))
@ -618,8 +626,8 @@ Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
}
void DefInit::print(std::ostream &OS) const {
OS << Def->getName();
std::string DefInit::getAsString() const {
return Def->getName();
}
Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
@ -679,17 +687,17 @@ Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
}
void DagInit::print(std::ostream &OS) const {
OS << "(" << *Val;
std::string DagInit::getAsString() const {
std::string Result = "(" + Val->getAsString();
if (Args.size()) {
OS << " " << *Args[0];
if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
Result += " " + Args[0]->getAsString();
if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
for (unsigned i = 1, e = Args.size(); i != e; ++i) {
OS << ", " << *Args[i];
if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
Result += ", " + Args[i]->getAsString();
if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
}
}
OS << ")";
return Result + ")";
}

View File

@ -429,7 +429,10 @@ struct Init {
virtual bool isComplete() const { return true; }
/// print - Print out this value.
virtual void print(std::ostream &OS) const = 0;
void print(std::ostream &OS) const { OS << getAsString(); }
/// getAsString - Convert this value to a string form.
virtual std::string getAsString() const = 0;
/// dump - Debugging method that may be called through a debugger, just
/// invokes print on cerr.
@ -497,7 +500,7 @@ public:
}
virtual bool isComplete() const { return false; }
virtual void print(std::ostream &OS) const { OS << "?"; }
virtual std::string getAsString() const { return "?"; }
};
@ -514,7 +517,7 @@ public:
return Ty->convertValue(this);
}
virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
virtual std::string getAsString() const { return Value ? "1" : "0"; }
};
/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
@ -547,7 +550,7 @@ public:
if (!getBit(i)->isComplete()) return false;
return true;
}
virtual void print(std::ostream &OS) const;
virtual std::string getAsString() const;
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
@ -573,7 +576,7 @@ public:
}
virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
virtual void print(std::ostream &OS) const { OS << Value; }
virtual std::string getAsString() const;
};
@ -590,7 +593,7 @@ public:
return Ty->convertValue(this);
}
virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
virtual std::string getAsString() const { return "\"" + Value + "\""; }
};
/// CodeInit - "[{...}]" - Represent a code fragment.
@ -606,7 +609,7 @@ public:
return Ty->convertValue(this);
}
virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
virtual std::string getAsString() const { return "[{" + Value + "}]"; }
};
/// ListInit - [AL, AH, CL] - Represent a list of defs
@ -639,7 +642,7 @@ public:
///
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
virtual void print(std::ostream &OS) const;
virtual std::string getAsString() const;
};
/// BinOpInit - !op (X, Y) - Combine two inits.
@ -668,7 +671,7 @@ public:
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
virtual void print(std::ostream &OS) const;
virtual std::string getAsString() const;
};
@ -728,7 +731,7 @@ public:
///
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
virtual void print(std::ostream &OS) const { OS << VarName; }
virtual std::string getAsString() const { return VarName; }
};
@ -751,9 +754,7 @@ public:
TypedInit *getVariable() const { return TI; }
unsigned getBitNum() const { return Bit; }
virtual void print(std::ostream &OS) const {
TI->print(OS); OS << "{" << Bit << "}";
}
virtual std::string getAsString() const;
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
};
@ -786,9 +787,7 @@ public:
virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt);
virtual void print(std::ostream &OS) const {
TI->print(OS); OS << "[" << Element << "]";
}
virtual std::string getAsString() const;
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
};
@ -810,7 +809,7 @@ public:
virtual RecTy *getFieldType(const std::string &FieldName) const;
virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
virtual void print(std::ostream &OS) const;
virtual std::string getAsString() const;
};
@ -836,8 +835,8 @@ public:
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
virtual void print(std::ostream &OS) const {
Rec->print(OS); OS << "." << FieldName;
virtual std::string getAsString() const {
return Rec->getAsString() + "." + FieldName;
}
};
@ -887,7 +886,7 @@ public:
virtual Init *resolveReferences(Record &R, const RecordVal *RV);
virtual void print(std::ostream &OS) const;
virtual std::string getAsString() const;
};
//===----------------------------------------------------------------------===//