2006-11-12 06:59:23 +08:00
|
|
|
//===--- Type.cpp - Type representation and manipulation ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements type-related functionality.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-20 12:02:15 +08:00
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-11-12 06:59:23 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2006-11-20 12:02:15 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2006-11-12 08:37:36 +08:00
|
|
|
#include <iostream>
|
2006-11-12 06:59:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
Type::~Type() {}
|
|
|
|
|
2006-11-12 08:37:36 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type Printing
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-11-12 06:59:23 +08:00
|
|
|
|
2006-11-12 07:56:48 +08:00
|
|
|
void TypeRef::dump() const {
|
2006-11-13 15:38:09 +08:00
|
|
|
std::string R = "foo";
|
|
|
|
getAsString(R);
|
2006-11-13 14:22:30 +08:00
|
|
|
std::cerr << R << "\n";
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 14:22:30 +08:00
|
|
|
static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
|
2006-11-12 16:50:50 +08:00
|
|
|
// Note: funkiness to ensure we get a space only between quals.
|
|
|
|
bool NonePrinted = true;
|
|
|
|
if (TypeQuals & TypeRef::Const)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "const", NonePrinted = false;
|
2006-11-12 16:50:50 +08:00
|
|
|
if (TypeQuals & TypeRef::Volatile)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" volatile"), NonePrinted = false;
|
2006-11-12 16:50:50 +08:00
|
|
|
if (TypeQuals & TypeRef::Restrict)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" restrict"), NonePrinted = false;
|
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void TypeRef::getAsString(std::string &S) const {
|
2006-11-12 07:56:48 +08:00
|
|
|
if (isNull()) {
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "NULL TYPE\n";
|
2006-11-12 07:56:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print qualifiers as appropriate.
|
2006-11-12 16:50:50 +08:00
|
|
|
if (unsigned TQ = getQualifiers()) {
|
2006-11-13 15:38:09 +08:00
|
|
|
std::string TQS;
|
|
|
|
AppendTypeQualList(TQS, TQ);
|
|
|
|
S = TQS + ' ' + S;
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
2006-11-13 15:38:09 +08:00
|
|
|
|
|
|
|
getTypePtr()->getAsString(S);
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void BuiltinType::getAsString(std::string &S) const {
|
|
|
|
if (S.empty()) {
|
|
|
|
S = Name;
|
|
|
|
} else {
|
|
|
|
// Prefix the basic type, e.g. 'int X'.
|
|
|
|
S = ' ' + S;
|
|
|
|
S = Name + S;
|
|
|
|
}
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void PointerType::getAsString(std::string &S) const {
|
|
|
|
S = '*' + S;
|
|
|
|
|
|
|
|
// Handle things like 'int (*A)[4];' correctly.
|
2006-11-19 09:31:06 +08:00
|
|
|
// FIXME: this should include vectors, but vectors use attributes I guess.
|
2006-11-13 15:38:09 +08:00
|
|
|
if (isa<ArrayType>(PointeeType.getTypePtr()))
|
|
|
|
S = '(' + S + ')';
|
|
|
|
|
|
|
|
PointeeType.getAsString(S);
|
2006-11-12 07:56:48 +08:00
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void ArrayType::getAsString(std::string &S) const {
|
2006-11-13 14:22:30 +08:00
|
|
|
S += '[';
|
2006-11-12 16:50:50 +08:00
|
|
|
|
|
|
|
if (IndexTypeQuals) {
|
2006-11-13 14:22:30 +08:00
|
|
|
AppendTypeQualList(S, IndexTypeQuals);
|
|
|
|
S += ' ';
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SizeModifier == Static)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "static";
|
2006-11-12 16:50:50 +08:00
|
|
|
else if (SizeModifier == Star)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += '*';
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 14:22:30 +08:00
|
|
|
S += ']';
|
2006-11-13 15:38:09 +08:00
|
|
|
|
|
|
|
ElementType.getAsString(S);
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
2006-11-20 12:02:15 +08:00
|
|
|
|
2006-12-02 15:52:18 +08:00
|
|
|
|
|
|
|
void FunctionTypeNoProto::getAsString(std::string &S) const {
|
|
|
|
// If needed for precedence reasons, wrap the inner part in grouping parens.
|
|
|
|
if (!S.empty())
|
|
|
|
S = "(" + S + ")";
|
|
|
|
|
|
|
|
S += "()";
|
|
|
|
getResultType().getAsString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTypeProto::getAsString(std::string &S) const {
|
|
|
|
// If needed for precedence reasons, wrap the inner part in grouping parens.
|
|
|
|
if (!S.empty())
|
|
|
|
S = "(" + S + ")";
|
|
|
|
|
|
|
|
S += "(";
|
|
|
|
std::string Tmp;
|
|
|
|
for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
|
|
|
|
if (i) S += ", ";
|
|
|
|
getArgType(i).getAsString(Tmp);
|
|
|
|
S += Tmp;
|
|
|
|
Tmp.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isVariadic()) {
|
|
|
|
if (getNumArgs())
|
|
|
|
S += ", ";
|
|
|
|
S += "...";
|
|
|
|
}
|
|
|
|
|
|
|
|
S += ")";
|
|
|
|
getResultType().getAsString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-20 12:02:15 +08:00
|
|
|
void TypeNameType::getAsString(std::string &InnerString) const {
|
2006-11-20 12:17:27 +08:00
|
|
|
if (InnerString.empty()) {
|
|
|
|
InnerString = getDecl()->getIdentifier()->getName();
|
|
|
|
} else {
|
|
|
|
// Prefix the basic type, e.g. 'typedefname X'.
|
|
|
|
InnerString = ' ' + InnerString;
|
|
|
|
InnerString = getDecl()->getIdentifier()->getName() + InnerString;
|
|
|
|
}
|
2006-11-20 12:02:15 +08:00
|
|
|
}
|