blob: 23e9e980c568d22b013a7909977b187e4386ed4a [file] [log] [blame]
Chris Lattner64c34f12008-11-16 07:46:48 +00001//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the APValue class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/APValue.h"
15#include "llvm/Support/raw_ostream.h"
16using namespace clang;
17
18
19const APValue &APValue::operator=(const APValue &RHS) {
20 if (Kind != RHS.Kind) {
21 MakeUninit();
22 if (RHS.isInt())
23 MakeInt();
24 else if (RHS.isFloat())
25 MakeFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +000026 else if (RHS.isVector())
27 MakeVector();
Chris Lattner64c34f12008-11-16 07:46:48 +000028 else if (RHS.isComplexInt())
29 MakeComplexInt();
30 else if (RHS.isComplexFloat())
31 MakeComplexFloat();
32 else if (RHS.isLValue())
33 MakeLValue();
34 }
35 if (isInt())
36 setInt(RHS.getInt());
37 else if (isFloat())
38 setFloat(RHS.getFloat());
Nate Begeman3d309f92009-01-18 01:01:34 +000039 else if (isVector())
40 setVector(((Vec*)(void*)RHS.Data)->Elts, RHS.getVectorLength());
Chris Lattner64c34f12008-11-16 07:46:48 +000041 else if (isComplexInt())
42 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
43 else if (isComplexFloat())
44 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
45 else if (isLValue())
46 setLValue(RHS.getLValueBase(), RHS.getLValueOffset());
47 return *this;
48}
49
50void APValue::MakeUninit() {
51 if (Kind == Int)
52 ((APSInt*)(void*)Data)->~APSInt();
53 else if (Kind == Float)
54 ((APFloat*)(void*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +000055 else if (Kind == Vector)
56 ((Vec*)(void*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +000057 else if (Kind == ComplexInt)
58 ((ComplexAPSInt*)(void*)Data)->~ComplexAPSInt();
59 else if (Kind == ComplexFloat)
60 ((ComplexAPFloat*)(void*)Data)->~ComplexAPFloat();
61 else if (Kind == LValue) {
62 ((LV*)(void*)Data)->~LV();
63 }
Nate Begeman3d309f92009-01-18 01:01:34 +000064 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +000065}
66
67void APValue::dump() const {
68 print(llvm::errs());
69 llvm::errs() << '\n';
70 llvm::errs().flush();
71}
72
73static double GetApproxValue(const llvm::APFloat &F) {
74 llvm::APFloat V = F;
75 bool ignored;
76 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
77 &ignored);
78 return V.convertToDouble();
79}
80
81void APValue::print(llvm::raw_ostream &OS) const {
82 switch (getKind()) {
83 default: assert(0 && "Unknown APValue kind!");
84 case Uninitialized:
85 OS << "Uninitialized";
86 return;
87 case Int:
88 OS << "Int: " << getInt();
89 return;
90 case Float:
91 OS << "Float: " << GetApproxValue(getFloat());
92 return;
Nate Begeman3d309f92009-01-18 01:01:34 +000093 case Vector:
Nate Begeman59b5da62009-01-18 03:20:47 +000094 OS << "Vector: " << getVectorElt(0);
95 for (unsigned i = 1; i != getVectorLength(); ++i)
96 OS << ", " << getVectorElt(i);
Nate Begeman3d309f92009-01-18 01:01:34 +000097 return;
Chris Lattner64c34f12008-11-16 07:46:48 +000098 case ComplexInt:
99 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
100 return;
101 case ComplexFloat:
102 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
103 << ", " << GetApproxValue(getComplexFloatImag());
104 case LValue:
105 OS << "LValue: <todo>";
106 return;
107 }
108}
109