Commit 0528f240 authored by Roberto Raggi's avatar Roberto Raggi

Introduced type matchers.

parent 51809d12
......@@ -51,13 +51,11 @@
#include "CPlusPlusForwardDeclarations.h"
#include <new>
#include <cstdlib>
namespace CPlusPlus {
template <typename _Tp, int SEGMENT_SHIFT = 4>
class Array
class CPLUSPLUS_EXPORT Array
{
Array(const Array &other);
void operator =(const Array &other);
......
......@@ -49,6 +49,8 @@
#ifndef CPLUSPLUS_CPLUSPLUSFORWARDDECLARATIONS_H
#define CPLUSPLUS_CPLUSPLUSFORWARDDECLARATIONS_H
#include <cstdlib>
#ifndef CPLUSPLUS_WITHOUT_QT
# include <QtCore/qglobal.h>
......@@ -91,6 +93,7 @@ class QualifiedNameId;
class SelectorNameId;
// types
class TypeMatcher;
class FullySpecifiedType;
class TypeVisitor;
class Type;
......
......@@ -55,11 +55,9 @@
#include "Names.h"
#include "Array.h"
#include <map> // ### replace me with LiteralTable
#include <string>
using namespace CPlusPlus;
template <typename _Iterator>
static void delete_map_entries(_Iterator first, _Iterator last)
{
......@@ -639,7 +637,7 @@ Identifier *Control::findOrInsertIdentifier(const char *chars, unsigned size)
Identifier *Control::findOrInsertIdentifier(const char *chars)
{
unsigned length = std::char_traits<char>::length(chars);
unsigned length = std::strlen(chars);
return findOrInsertIdentifier(chars, length);
}
......@@ -666,7 +664,7 @@ StringLiteral *Control::findOrInsertStringLiteral(const char *chars, unsigned si
StringLiteral *Control::findOrInsertStringLiteral(const char *chars)
{
unsigned length = std::char_traits<char>::length(chars);
unsigned length = std::strlen(chars);
return findOrInsertStringLiteral(chars, length);
}
......@@ -675,7 +673,7 @@ NumericLiteral *Control::findOrInsertNumericLiteral(const char *chars, unsigned
NumericLiteral *Control::findOrInsertNumericLiteral(const char *chars)
{
unsigned length = std::char_traits<char>::length(chars);
unsigned length = std::strlen(chars);
return findOrInsertNumericLiteral(chars, length);
}
......
......@@ -48,11 +48,30 @@
#include "CoreTypes.h"
#include "TypeVisitor.h"
#include "TypeMatcher.h"
#include "Names.h"
#include <algorithm>
using namespace CPlusPlus;
bool UndefinedType::isEqualTo(const Type *other) const
{
if (other->isUndefinedType())
return true;
return false;
}
void UndefinedType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool UndefinedType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const UndefinedType *otherUndefinedTy = otherType->asUndefinedType())
return matcher->match(this, otherUndefinedTy);
return false;
}
bool VoidType::isEqualTo(const Type *other) const
{
......@@ -63,6 +82,14 @@ bool VoidType::isEqualTo(const Type *other) const
void VoidType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool VoidType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const VoidType *otherVoidTy = otherType->asVoidType())
return matcher->match(this, otherVoidTy);
return false;
}
PointerToMemberType::PointerToMemberType(Name *memberName, FullySpecifiedType elementType)
: _memberName(memberName),
_elementType(elementType)
......@@ -90,6 +117,14 @@ bool PointerToMemberType::isEqualTo(const Type *other) const
void PointerToMemberType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool PointerToMemberType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const PointerToMemberType *otherTy = otherType->asPointerToMemberType())
return matcher->match(this, otherTy);
return false;
}
PointerType::PointerType(const FullySpecifiedType &elementType)
: _elementType(elementType)
{ }
......@@ -108,6 +143,14 @@ bool PointerType::isEqualTo(const Type *other) const
void PointerType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool PointerType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const PointerType *otherTy = otherType->asPointerType())
return matcher->match(this, otherTy);
return false;
}
FullySpecifiedType PointerType::elementType() const
{ return _elementType; }
......@@ -129,6 +172,14 @@ bool ReferenceType::isEqualTo(const Type *other) const
void ReferenceType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ReferenceType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ReferenceType *otherTy = otherType->asReferenceType())
return matcher->match(this, otherTy);
return false;
}
FullySpecifiedType ReferenceType::elementType() const
{ return _elementType; }
......@@ -150,6 +201,14 @@ bool IntegerType::isEqualTo(const Type *other) const
void IntegerType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool IntegerType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const IntegerType *otherTy = otherType->asIntegerType())
return matcher->match(this, otherTy);
return false;
}
int IntegerType::kind() const
{ return _kind; }
......@@ -163,6 +222,14 @@ FloatType::~FloatType()
void FloatType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool FloatType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const FloatType *otherTy = otherType->asFloatType())
return matcher->match(this, otherTy);
return false;
}
int FloatType::kind() const
{ return _kind; }
......@@ -194,6 +261,14 @@ bool ArrayType::isEqualTo(const Type *other) const
void ArrayType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ArrayType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ArrayType *otherTy = otherType->asArrayType())
return matcher->match(this, otherTy);
return false;
}
FullySpecifiedType ArrayType::elementType() const
{ return _elementType; }
......@@ -230,4 +305,10 @@ bool NamedType::isEqualTo(const Type *other) const
void NamedType::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool NamedType::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const NamedType *otherTy = otherType->asNamedType())
return matcher->match(this, otherTy);
return false;
}
......@@ -54,7 +54,6 @@
#include "FullySpecifiedType.h"
#include <cstddef>
namespace CPlusPlus {
class CPLUSPLUS_EXPORT UndefinedType : public Type
......@@ -66,12 +65,17 @@ public:
return &t;
}
virtual bool isEqualTo(const Type *other) const
{ return this == other; }
virtual const UndefinedType *asUndefinedType() const
{ return this; }
virtual UndefinedType *asUndefinedType()
{ return this; }
virtual bool isEqualTo(const Type *other) const;
protected:
virtual void accept0(TypeVisitor *)
{ }
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
};
class CPLUSPLUS_EXPORT VoidType: public Type
......@@ -87,6 +91,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
};
class CPLUSPLUS_EXPORT IntegerType: public Type
......@@ -118,6 +123,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
int _kind;
......@@ -148,6 +154,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
int _kind;
......@@ -171,6 +178,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
FullySpecifiedType _elementType;
......@@ -195,6 +203,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
Name *_memberName;
......@@ -219,6 +228,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
FullySpecifiedType _elementType;
......@@ -243,6 +253,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
FullySpecifiedType _elementType;
......@@ -267,6 +278,7 @@ public:
protected:
virtual void accept0(TypeVisitor *visitor);
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
Name *_name;
......@@ -274,5 +286,4 @@ private:
} // end of namespace CPlusPlus
#endif // CPLUSPLUS_CORETYPES_H
......@@ -229,3 +229,10 @@ void FullySpecifiedType::copySpecifiers(const FullySpecifiedType &type)
f._isExplicit = type.f._isExplicit;
}
bool FullySpecifiedType::match(const FullySpecifiedType &otherTy, TypeMatcher *matcher) const
{
if (_flags != otherTy._flags)
return false;
return type()->matchType(otherTy.type(), matcher);
}
......@@ -119,6 +119,8 @@ public:
bool operator != (const FullySpecifiedType &other) const;
bool operator < (const FullySpecifiedType &other) const;
bool match(const FullySpecifiedType &otherTy, TypeMatcher *matcher) const;
FullySpecifiedType simplified() const;
void copySpecifiers(const FullySpecifiedType &type);
......
......@@ -53,8 +53,6 @@
#include <cctype>
#include <cassert>
using namespace std;
using namespace CPlusPlus;
Lexer::Lexer(TranslationUnit *unit)
......
......@@ -51,8 +51,6 @@
#include "CPlusPlusForwardDeclarations.h"
#include <cstring>
#include <cstdlib>
namespace CPlusPlus {
......
......@@ -51,8 +51,6 @@
#include <algorithm>
#include <iostream>
using namespace std;
using namespace CPlusPlus;
////////////////////////////////////////////////////////////////////////////////
......@@ -61,9 +59,8 @@ Literal::Literal(const char *chars, unsigned size)
{
_chars = new char[size + 1];
strncpy(_chars, chars, size);
std::strncpy(_chars, chars, size);
_chars[size] = '\0';
_size = size;
_hashCode = hashCode(_chars, _size);
......@@ -82,7 +79,7 @@ bool Literal::isEqualTo(const Literal *other) const
return false;
else if (size() != other->size())
return false;
return ! strcmp(chars(), other->chars());
return ! std::strcmp(chars(), other->chars());
}
Literal::iterator Literal::begin() const
......
......@@ -47,14 +47,11 @@
// THE SOFTWARE.
#include "MemoryPool.h"
#include <cstdlib>
#include <cstring>
#include <cassert>
using namespace CPlusPlus;
using namespace std;
MemoryPool::MemoryPool()
: _initializeAllocatedMemory(true),
_blocks(0),
......@@ -68,12 +65,12 @@ MemoryPool::~MemoryPool()
{
if (_blockCount != -1) {
for (int i = 0; i < _blockCount + 1; ++i) {
free(_blocks[i]);
std::free(_blocks[i]);
}
}
if (_blocks)
free(_blocks);
std::free(_blocks);
}
bool MemoryPool::initializeAllocatedMemory() const
......@@ -98,9 +95,9 @@ void *MemoryPool::allocate_helper(size_t size)
char *&block = _blocks[_blockCount];
if (_initializeAllocatedMemory)
block = (char *) calloc(1, BLOCK_SIZE);
block = (char *) std::calloc(1, BLOCK_SIZE);
else
block = (char *) malloc(BLOCK_SIZE);
block = (char *) std::malloc(BLOCK_SIZE);
ptr = block;
end = ptr + BLOCK_SIZE;
......@@ -117,7 +114,7 @@ void MemoryPool::rewind(const State &state)
{
if (_blockCount == state.blockCount && state.ptr < ptr) {
if (_initializeAllocatedMemory)
memset(state.ptr, '\0', ptr - state.ptr);
std::memset(state.ptr, '\0', ptr - state.ptr);
ptr = state.ptr;
}
......
......@@ -53,7 +53,6 @@
#include <cstddef>
#include <new>
namespace CPlusPlus {
class CPLUSPLUS_EXPORT MemoryPool
......
......@@ -53,7 +53,6 @@
#include "Name.h"
#include "FullySpecifiedType.h"
namespace CPlusPlus {
class CPLUSPLUS_EXPORT QualifiedNameId: public Name
......
......@@ -53,11 +53,7 @@
#include "AST.h"
#include "Literals.h"
#include "ObjectiveCTypeQualifiers.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cassert>
#include <string>
#include <cstdio> // for putchar
#define CPLUSPLUS_NO_DEBUG_RULE
......@@ -72,7 +68,13 @@ class DebugRule {
public:
DebugRule(const char *name)
: name(name)
{ std::cout << std::string(depth++, ' ') << name << std::endl; }
{
for (int i = 0; i < depth; ++i)
putchar(' ');
++depth;
printf("%s\n", name);
}
~DebugRule()
{ --depth; }
......
......@@ -50,12 +50,8 @@
#include "Symbols.h"
#include "Names.h"
#include "Literals.h"
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;
using namespace CPlusPlus;
......@@ -278,7 +274,7 @@ void Scope::rehash()
_hashSize = DefaultInitialSize;
_hash = reinterpret_cast<Symbol **>(realloc(_hash, sizeof(Symbol *) * _hashSize));
memset(_hash, 0, sizeof(Symbol *) * _hashSize);
std::memset(_hash, 0, sizeof(Symbol *) * _hashSize);
for (int index = 0; index < _symbolCount + 1; ++index) {
Symbol *symbol = _symbols[index];
......
......@@ -50,8 +50,8 @@
#include "Names.h"
#include "TypeVisitor.h"
#include "SymbolVisitor.h"
#include "TypeMatcher.h"
#include "Scope.h"
#include <cstdlib>
using namespace CPlusPlus;
......@@ -224,6 +224,14 @@ bool Function::isEqualTo(const Type *other) const
void Function::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool Function::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const Function *otherTy = otherType->asFunctionType())
return matcher->match(this, otherTy);
return false;
}
FullySpecifiedType Function::type() const
{ return FullySpecifiedType(const_cast<Function *>(this)); }
......@@ -380,6 +388,14 @@ bool Enum::isEqualTo(const Type *other) const
void Enum::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool Enum::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const Enum *otherTy = otherType->asEnumType())
return matcher->match(this, otherTy);
return false;
}
void Enum::visitSymbol0(SymbolVisitor *visitor)
{
if (visitor->visit(this)) {
......@@ -411,6 +427,14 @@ bool Namespace::isEqualTo(const Type *other) const
void Namespace::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool Namespace::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const Namespace *otherTy = otherType->asNamespaceType())
return matcher->match(this, otherTy);
return false;
}
void Namespace::visitSymbol0(SymbolVisitor *visitor)
{
if (visitor->visit(this)) {
......@@ -480,6 +504,14 @@ void ForwardClassDeclaration::visitSymbol0(SymbolVisitor *visitor)
void ForwardClassDeclaration::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ForwardClassDeclaration::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ForwardClassDeclaration *otherTy = otherType->asForwardClassDeclarationType())
return matcher->match(this, otherTy);
return false;
}
Class::Class(TranslationUnit *translationUnit, unsigned sourceLocation, Name *name)
: ScopedSymbol(translationUnit, sourceLocation, name),
_key(ClassKey),
......@@ -524,6 +556,14 @@ void Class::setTemplateParameters(TemplateParameters *templateParameters)
void Class::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool Class::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const Class *otherTy = otherType->asClassType())
return matcher->match(this, otherTy);
return false;
}
unsigned Class::baseClassCount() const
{ return _baseClasses.count(); }
......@@ -632,6 +672,14 @@ void ObjCClass::visitSymbol0(SymbolVisitor *visitor)
void ObjCClass::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ObjCClass::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ObjCClass *otherTy = otherType->asObjCClassType())
return matcher->match(this, otherTy);
return false;
}
ObjCProtocol::ObjCProtocol(TranslationUnit *translationUnit, unsigned sourceLocation, Name *name):
ScopedSymbol(translationUnit, sourceLocation, name)
{
......@@ -668,6 +716,14 @@ void ObjCProtocol::visitSymbol0(SymbolVisitor *visitor)
void ObjCProtocol::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ObjCProtocol::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ObjCProtocol *otherTy = otherType->asObjCProtocolType())
return matcher->match(this, otherTy);
return false;
}
ObjCForwardClassDeclaration::ObjCForwardClassDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation, Name *name):
Symbol(translationUnit, sourceLocation, name)
{
......@@ -699,6 +755,14 @@ void ObjCForwardClassDeclaration::visitSymbol0(SymbolVisitor *visitor)
void ObjCForwardClassDeclaration::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ObjCForwardClassDeclaration::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ObjCForwardClassDeclaration *otherTy = otherType->asObjCForwardClassDeclarationType())
return matcher->match(this, otherTy);
return false;
}
ObjCForwardProtocolDeclaration::ObjCForwardProtocolDeclaration(TranslationUnit *translationUnit, unsigned sourceLocation, Name *name):
Symbol(translationUnit, sourceLocation, name)
{
......@@ -730,6 +794,14 @@ void ObjCForwardProtocolDeclaration::visitSymbol0(SymbolVisitor *visitor)
void ObjCForwardProtocolDeclaration::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ObjCForwardProtocolDeclaration::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ObjCForwardProtocolDeclaration *otherTy = otherType->asObjCForwardProtocolDeclarationType())
return matcher->match(this, otherTy);
return false;
}
ObjCMethod::ObjCMethod(TranslationUnit *translationUnit, unsigned sourceLocation, Name *name)
: ScopedSymbol(translationUnit, sourceLocation, name),
_flags(0)
......@@ -767,6 +839,14 @@ bool ObjCMethod::isEqualTo(const Type *other) const
void ObjCMethod::accept0(TypeVisitor *visitor)
{ visitor->visit(this); }
bool ObjCMethod::matchType0(const Type *otherType, TypeMatcher *matcher) const
{
if (const ObjCMethod *otherTy = otherType->asObjCMethodType())
return matcher->match(this, otherTy);