blob: 50a6e0a50d8bf6a9de57f6c2dd3c639f0a68dfa1 [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"
Ken Dycka7305832010-01-15 12:37:54 +000015#include "clang/AST/CharUnits.h"
Chris Lattner64c34f12008-11-16 07:46:48 +000016#include "llvm/Support/raw_ostream.h"
17using namespace clang;
18
Ken Dycka7305832010-01-15 12:37:54 +000019namespace {
20 struct LV {
21 Expr* Base;
22 CharUnits Offset;
23 };
24}
25
26APValue::APValue(Expr* B) : Kind(Uninitialized) {
27 MakeLValue(); setLValue(B, CharUnits::Zero());
28}
Chris Lattner64c34f12008-11-16 07:46:48 +000029
30const APValue &APValue::operator=(const APValue &RHS) {
31 if (Kind != RHS.Kind) {
32 MakeUninit();
33 if (RHS.isInt())
34 MakeInt();
35 else if (RHS.isFloat())
36 MakeFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +000037 else if (RHS.isVector())
38 MakeVector();
Chris Lattner64c34f12008-11-16 07:46:48 +000039 else if (RHS.isComplexInt())
40 MakeComplexInt();
41 else if (RHS.isComplexFloat())
42 MakeComplexFloat();
43 else if (RHS.isLValue())
44 MakeLValue();
45 }
46 if (isInt())
47 setInt(RHS.getInt());
48 else if (isFloat())
49 setFloat(RHS.getFloat());
Nate Begeman3d309f92009-01-18 01:01:34 +000050 else if (isVector())
Douglas Gregor98300462009-09-08 19:57:33 +000051 setVector(((Vec*)(char*)RHS.Data)->Elts, RHS.getVectorLength());
Chris Lattner64c34f12008-11-16 07:46:48 +000052 else if (isComplexInt())
53 setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
54 else if (isComplexFloat())
55 setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
56 else if (isLValue())
57 setLValue(RHS.getLValueBase(), RHS.getLValueOffset());
58 return *this;
59}
60
61void APValue::MakeUninit() {
62 if (Kind == Int)
Douglas Gregor98300462009-09-08 19:57:33 +000063 ((APSInt*)(char*)Data)->~APSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +000064 else if (Kind == Float)
Douglas Gregor98300462009-09-08 19:57:33 +000065 ((APFloat*)(char*)Data)->~APFloat();
Nate Begeman3d309f92009-01-18 01:01:34 +000066 else if (Kind == Vector)
Douglas Gregor98300462009-09-08 19:57:33 +000067 ((Vec*)(char*)Data)->~Vec();
Chris Lattner64c34f12008-11-16 07:46:48 +000068 else if (Kind == ComplexInt)
Douglas Gregor98300462009-09-08 19:57:33 +000069 ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt();
Chris Lattner64c34f12008-11-16 07:46:48 +000070 else if (Kind == ComplexFloat)
Douglas Gregor98300462009-09-08 19:57:33 +000071 ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat();
Chris Lattner64c34f12008-11-16 07:46:48 +000072 else if (Kind == LValue) {
Douglas Gregor98300462009-09-08 19:57:33 +000073 ((LV*)(char*)Data)->~LV();
Chris Lattner64c34f12008-11-16 07:46:48 +000074 }
Nate Begeman3d309f92009-01-18 01:01:34 +000075 Kind = Uninitialized;
Chris Lattner64c34f12008-11-16 07:46:48 +000076}
77
78void APValue::dump() const {
79 print(llvm::errs());
80 llvm::errs() << '\n';
Chris Lattner64c34f12008-11-16 07:46:48 +000081}
82
83static double GetApproxValue(const llvm::APFloat &F) {
84 llvm::APFloat V = F;
85 bool ignored;
86 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
87 &ignored);
88 return V.convertToDouble();
89}
90
91void APValue::print(llvm::raw_ostream &OS) const {
92 switch (getKind()) {
93 default: assert(0 && "Unknown APValue kind!");
94 case Uninitialized:
95 OS << "Uninitialized";
96 return;
97 case Int:
98 OS << "Int: " << getInt();
99 return;
100 case Float:
101 OS << "Float: " << GetApproxValue(getFloat());
102 return;
Nate Begeman3d309f92009-01-18 01:01:34 +0000103 case Vector:
Nate Begeman59b5da62009-01-18 03:20:47 +0000104 OS << "Vector: " << getVectorElt(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000105 for (unsigned i = 1; i != getVectorLength(); ++i)
Nate Begeman59b5da62009-01-18 03:20:47 +0000106 OS << ", " << getVectorElt(i);
Nate Begeman3d309f92009-01-18 01:01:34 +0000107 return;
Chris Lattner64c34f12008-11-16 07:46:48 +0000108 case ComplexInt:
109 OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
110 return;
111 case ComplexFloat:
112 OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal())
113 << ", " << GetApproxValue(getComplexFloatImag());
114 case LValue:
115 OS << "LValue: <todo>";
116 return;
117 }
118}
119
Ken Dycka7305832010-01-15 12:37:54 +0000120Expr* APValue::getLValueBase() const {
121 assert(isLValue() && "Invalid accessor");
122 return ((const LV*)(const void*)Data)->Base;
123}
124
125CharUnits APValue::getLValueOffset() const {
126 assert(isLValue() && "Invalid accessor");
127 return ((const LV*)(const void*)Data)->Offset;
128}
129
130void APValue::setLValue(Expr *B, const CharUnits &O) {
131 assert(isLValue() && "Invalid accessor");
132 ((LV*)(char*)Data)->Base = B;
133 ((LV*)(char*)Data)->Offset = O;
134}
135
136void APValue::MakeLValue() {
137 assert(isUninit() && "Bad state change");
138 new ((void*)(char*)Data) LV();
139 Kind = LValue;
140}
141