[cfe-commits] r59411 - in /cfe/trunk: include/clang/AST/APValue.h lib/AST/APValue.cpp

Chris Lattner sabre at nondot.org
Sat Nov 15 23:46:48 PST 2008


Author: lattner
Date: Sun Nov 16 01:46:48 2008
New Revision: 59411

URL: http://llvm.org/viewvc/llvm-project?rev=59411&view=rev
Log:
add dump and print methods, add operator<< for APValue.

Added:
    cfe/trunk/lib/AST/APValue.cpp
Modified:
    cfe/trunk/include/clang/AST/APValue.h

Modified: cfe/trunk/include/clang/AST/APValue.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/APValue.h?rev=59411&r1=59410&r2=59411&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/APValue.h (original)
+++ cfe/trunk/include/clang/AST/APValue.h Sun Nov 16 01:46:48 2008
@@ -92,6 +92,9 @@
   bool isComplexFloat() const { return Kind == ComplexFloat; }
   bool isLValue() const { return Kind == LValue; }
   
+  void print(llvm::raw_ostream &OS) const;
+  void dump() const;
+  
   APSInt &getInt() {
     assert(isInt() && "Invalid accessor");
     return *(APSInt*)(void*)Data;
@@ -204,6 +207,11 @@
   }
 };
 
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const APValue &V) {
+  V.print(OS);
+  return OS;
 }
+  
+} // end namespace clang.
 
 #endif

Added: cfe/trunk/lib/AST/APValue.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/APValue.cpp?rev=59411&view=auto

==============================================================================
--- cfe/trunk/lib/AST/APValue.cpp (added)
+++ cfe/trunk/lib/AST/APValue.cpp Sun Nov 16 01:46:48 2008
@@ -0,0 +1,97 @@
+//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements the APValue class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/APValue.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace clang;
+
+
+const APValue &APValue::operator=(const APValue &RHS) {
+  if (Kind != RHS.Kind) {
+    MakeUninit();
+    if (RHS.isInt())
+      MakeInt();
+    else if (RHS.isFloat())
+      MakeFloat();
+    else if (RHS.isComplexInt())
+      MakeComplexInt();
+    else if (RHS.isComplexFloat())
+      MakeComplexFloat();
+    else if (RHS.isLValue())
+      MakeLValue();
+  }
+  if (isInt())
+    setInt(RHS.getInt());
+  else if (isFloat())
+    setFloat(RHS.getFloat());
+  else if (isComplexInt())
+    setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
+  else if (isComplexFloat())
+    setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
+  else if (isLValue())
+    setLValue(RHS.getLValueBase(), RHS.getLValueOffset());
+  return *this;
+}
+
+void APValue::MakeUninit() {
+  if (Kind == Int)
+    ((APSInt*)(void*)Data)->~APSInt();
+  else if (Kind == Float)
+    ((APFloat*)(void*)Data)->~APFloat();
+  else if (Kind == ComplexInt)
+    ((ComplexAPSInt*)(void*)Data)->~ComplexAPSInt();
+  else if (Kind == ComplexFloat)
+    ((ComplexAPFloat*)(void*)Data)->~ComplexAPFloat();
+  else if (Kind == LValue) {
+    ((LV*)(void*)Data)->~LV();
+  }
+}
+
+void APValue::dump() const {
+  print(llvm::errs());
+  llvm::errs() << '\n';
+  llvm::errs().flush();
+}
+
+static double GetApproxValue(const llvm::APFloat &F) {
+  llvm::APFloat V = F;
+  bool ignored;
+  V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
+            &ignored);
+  return V.convertToDouble();
+}
+
+void APValue::print(llvm::raw_ostream &OS) const {
+  switch (getKind()) {
+  default: assert(0 && "Unknown APValue kind!");
+  case Uninitialized:
+    OS << "Uninitialized";
+    return;
+  case Int:
+    OS << "Int: " << getInt();
+    return;
+  case Float:
+    OS << "Float: " << GetApproxValue(getFloat());
+    return;
+  case ComplexInt:
+    OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
+    return;
+  case ComplexFloat:
+    OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
+       << ", " << GetApproxValue(getComplexFloatImag());
+  case LValue:
+    OS << "LValue: <todo>";
+    return;
+  }
+}
+





More information about the cfe-commits mailing list