clang.h 10.2 KB
Newer Older
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#pragma once

#include <boost/shared_ptr.hpp>

#include <clang/Frontend/ASTUnit.h>
#include <clang/AST/Type.h>


#include "typeinfoimp.h"


namespace kdlib {


class ClangASTSession;
typedef boost::shared_ptr<ClangASTSession>  ClangASTSessionPtr;

ussrhero's avatar
ussrhero committed
18
class TypeInfoProviderClang;
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
19 20 21 22 23 24 25 26 27 28


class ClangASTSession : public boost::enable_shared_from_this<ClangASTSession>
{
public:

    static ClangASTSessionPtr getASTSession(std::unique_ptr<clang::ASTUnit>&  astUnit) {
        return ClangASTSessionPtr( new ClangASTSession(astUnit) );
    }

ussrhero's avatar
ussrhero committed
29
    //TypeInfoPtr getTypeInfo(const std::wstring& name);
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    clang::ASTContext&  getASTContext() {
          return m_astUnit->getASTContext();
    }

    size_t getPtrSize() ;


private:

    ClangASTSession(std::unique_ptr<clang::ASTUnit>& astUnit) 
    {
        astUnit.swap(m_astUnit);
    }

    std::unique_ptr<clang::ASTUnit>  m_astUnit;

};


class TypeFieldClangField : public TypeField
{
public:

    static TypeFieldPtr getField(ClangASTSessionPtr& astSession, clang::RecordDecl* structDecl, clang::FieldDecl* fieldDecl, MEMOFFSET_32 startOffset);
    static TypeFieldPtr getStaticField(ClangASTSessionPtr& astSession, clang::RecordDecl* structDecl, clang::VarDecl* varDecl);

private:

    TypeFieldClangField(const std::wstring  &name) :
        TypeField(name)
        {}

    virtual TypeInfoPtr getTypeInfo();

    ClangASTSessionPtr  m_astSession;
    clang::DeclaratorDecl*  m_fieldDecl;
    clang::RecordDecl*  m_recordDecl;
};


class TypeInfoClangStruct : public TypeInfoFields
{
public:
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
74 75

    TypeInfoClangStruct(const std::wstring & name, ClangASTSessionPtr& session, clang::RecordDecl*  decl);
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
76 77 78 79 80 81 82 83 84 85 86 87

protected:

    virtual bool isUserDefined() {
        return true;
    }

    virtual std::wstring str() {
        TypeInfoPtr  selfPtr = shared_from_this();
        return printStructType(selfPtr);
    }

88 89 90 91 92 93 94 95 96 97
    virtual size_t getBaseClassesCount();

    virtual TypeInfoPtr getBaseClass(const std::wstring& className);
    
    virtual TypeInfoPtr getBaseClass(size_t index);

    virtual MEMOFFSET_REL getBaseClassOffset(const std::wstring &name);

    virtual MEMOFFSET_REL getBaseClassOffset(size_t index);

SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
98 99 100 101 102 103
    virtual size_t getSize();

protected:

    virtual void getFields();

104 105 106
    void getRecursiveFields(clang::RecordDecl* recordDecl, MEMOFFSET_32 startOffset);

    void getFieldFromBaseClasses(clang::RecordDecl* recordDecl, MEMOFFSET_32 startOffset);
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
107 108 109

    ClangASTSessionPtr  m_astSession;

110
    clang::RecordDecl*  m_decl;
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
111 112
};

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
113 114 115 116 117 118 119 120 121 122 123 124
class TypeInfoClangStructNoDef : public TypeInfoImp
{
public:

    TypeInfoClangStructNoDef(const std::wstring& name, ClangASTSessionPtr& session, clang::RecordDecl*  decl) :
        m_name(name),
        m_astSession(session),
        m_decl(decl)
        {}

protected:

ussrhero's avatar
ussrhero committed
125
    bool isUserDefined() override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
126 127 128
        return true;
    }

ussrhero's avatar
ussrhero committed
129
    std::wstring getName() override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
130 131 132
        return  m_name;
    }

ussrhero's avatar
ussrhero committed
133
    std::wstring str() override;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
134

ussrhero's avatar
ussrhero committed
135 136 137
    size_t getSize() override {
        throw TypeException(L"not complete declaration");
    }
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
138

ussrhero's avatar
ussrhero committed
139
    TypeInfoPtr getElement( const std::wstring &name ) override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
140 141 142
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
143
    TypeInfoPtr getElement( size_t index ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
144 145 146
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
147
    MEMOFFSET_REL getElementOffset( const std::wstring &name ) override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
148 149 150
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
151
    MEMOFFSET_REL getElementOffset( size_t index ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
152 153 154
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
155
    std::wstring getElementName( size_t index ) override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
156 157 158
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
159
    size_t getElementIndex( const std::wstring &name )  override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
160 161 162
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
163
    size_t getElementCount() override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
164 165 166
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
167
    MEMOFFSET_64 getElementVa( const std::wstring &name ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
168 169 170
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
171
    MEMOFFSET_64 getElementVa( size_t index ) override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
172 173 174
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
175
    bool isStaticMember( const std::wstring &name ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
176 177 178
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
179
    bool isStaticMember( size_t index ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
180 181 182
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
183
    bool isVirtualMember( const std::wstring &name ) override {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
184 185 186
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
187
    bool isVirtualMember( size_t index ) override  {
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
188 189 190
        throw TypeException(L"not complete declaration");
    }

ussrhero's avatar
ussrhero committed
191 192 193 194
    bool isIncomplete() override {
        return true;
    }

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
195 196 197 198 199 200 201 202 203 204
protected:

    std::wstring  m_name;

    ClangASTSessionPtr  m_astSession;

    clang::RecordDecl*  m_decl;

};

SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
205 206 207 208 209

class TypeInfoClangPointer : public TypeInfoPointer
{
public:

210
    TypeInfoClangPointer( ClangASTSessionPtr& session, const clang::PointerType* ptrType) :
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
211 212 213 214 215
        TypeInfoPointer( TypeInfoClangPointer::getDerefType(session, ptrType), session->getPtrSize() )
        {}

protected:

216
    static TypeInfoPtr  getDerefType( ClangASTSessionPtr& session, const clang::PointerType* refType);
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
217 218 219 220 221 222
};


class TypeInfoClangArray: public TypeInfoArray
{
public:
223
    TypeInfoClangArray(ClangASTSessionPtr& session, const clang::ArrayType* arrayType) :
224
        TypeInfoArray(TypeInfoClangArray::getDerefType(session, arrayType), getElementCount(arrayType))
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
225 226 227 228
        {}

protected:

229
    static TypeInfoPtr getDerefType( ClangASTSessionPtr& session, const clang::ArrayType* arrayType);
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
230

231
    size_t getElementCount(const clang::ArrayType* arrayType);
232

SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
233 234
};

ussrhero's avatar
ussrhero committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

class TypeInfoIncompleteClangArray : public TypeInfoClangArray
{
public:
    TypeInfoIncompleteClangArray(ClangASTSessionPtr& session, const clang::ArrayType* arrayType) :
        TypeInfoClangArray(session, arrayType)
    {}

protected:

    bool isIncomplete() override
    {
        return true;
    }

    size_t getSize() override
    {
        throw TypeException(L"incomplete array");
    }

};

SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
257 258 259
class TypeInfoClangRef : public TypeInfoImp
{
public:
260
    TypeInfoClangRef(ClangASTSessionPtr& session, const clang::ReferenceType* refType) :
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        m_astSession(session),
        m_refType(refType)
        {}

protected:

    virtual TypeInfoPtr deref();

    virtual std::wstring getName() {
        return deref()->getName() + L"&";
    }

    virtual std::wstring str() {
        return L"ref to " + deref()->getName();
    }

protected:


    ClangASTSessionPtr  m_astSession;

282
    const clang::ReferenceType*  m_refType;
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
283 284
};

ussrhero's avatar
ussrhero committed
285 286 287 288 289 290 291
class TypeInfoClangFuncPrototype : public TypeInfoFunctionPrototype
{
public:
    TypeInfoClangFuncPrototype(ClangASTSessionPtr& session, const clang::FunctionProtoType* functionProtoType);
};

class TypeInfoClangFunc : public TypeInfoClangFuncPrototype
292 293
{

294
public:
295

ussrhero's avatar
ussrhero committed
296 297 298 299 300 301 302 303
    TypeInfoClangFunc(ClangASTSessionPtr& session, clang::FunctionDecl*  funcDecl);

private:

    TypeInfoPtr getClassParent() override
    {
        return m_classParent;
    }
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
304

ussrhero's avatar
ussrhero committed
305
    TypeInfoPtr  m_classParent;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
};


class TypeFieldClangEnumField : public TypeField
{
public:

    static TypeFieldPtr getField(ClangASTSessionPtr& astSession, clang::EnumConstantDecl*  EnumDecl);

private:

    TypeFieldClangEnumField(const std::wstring  &name) :
        TypeField(name)
        {}

    virtual TypeInfoPtr getTypeInfo();

    NumVariant getValue() const;

    clang::EnumConstantDecl*   m_decl;
  
    ClangASTSessionPtr  m_astSession;
};


class TypeInfoClangEnum : public TypeInfoFields
{
public:

     TypeInfoClangEnum(ClangASTSessionPtr& session, clang::EnumDecl* decl) :
336
        TypeInfoFields(strToWStr(decl->getNameAsString())),
337 338 339 340 341 342 343 344
        m_astSession(session),
        m_decl(decl)
     {}

protected:
    
    virtual bool isEnum() {
        return true;
345 346 347
    }

    virtual std::wstring str() {
348 349
        TypeInfoPtr  selfPtr = shared_from_this();
        return printEnumType(selfPtr);
350
    }
351 352 353 354 355 356 357 358

protected:

    virtual void getFields();

    ClangASTSessionPtr  m_astSession;

    clang::EnumDecl*  m_decl;
359
};
SND\ussrhero_cp's avatar
 
SND\ussrhero_cp committed
360

361

ussrhero's avatar
ussrhero committed
362

ussrhero's avatar
ussrhero committed
363
class TypeInfoProviderClangEnum  : public TypeInfoEnumerator {
ussrhero's avatar
ussrhero committed
364 365

public:
ussrhero's avatar
ussrhero committed
366
    
ussrhero's avatar
ussrhero committed
367 368
    virtual TypeInfoPtr Next();

ussrhero's avatar
ussrhero committed
369
    TypeInfoProviderClangEnum(const std::wstring& mask, const boost::shared_ptr<TypeInfoProviderClang>& clangProvider );
ussrhero's avatar
ussrhero committed
370

ussrhero's avatar
ussrhero committed
371 372 373

private:

ussrhero's avatar
ussrhero committed
374 375 376
    size_t   m_index;

    std::vector<TypeInfoPtr>  m_typeList;
ussrhero's avatar
ussrhero committed
377 378 379
};


ussrhero's avatar
ussrhero committed
380
class TypeInfoProviderClang : public TypeInfoProvider, public boost::enable_shared_from_this<TypeInfoProviderClang>
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
381
{
ussrhero's avatar
ussrhero committed
382 383 384

    friend TypeInfoProviderClangEnum;

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
385 386
public:

ussrhero's avatar
ussrhero committed
387
    TypeInfoProviderClang( const std::string&  sourceCode, const std::string&  compileOptions);
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
388 389 390 391 392

private:

    virtual TypeInfoPtr getTypeByName(const std::wstring& name);

ussrhero's avatar
ussrhero committed
393 394
    virtual TypeInfoEnumeratorPtr getTypeEnumerator(const std::wstring& mask);

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
395 396 397 398
private:

    ClangASTSessionPtr  m_astSession;

ussrhero's avatar
ussrhero committed
399
    std::map< std::string, TypeInfoPtr>  m_typeCache;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
400 401 402
};


ussrhero's avatar
ussrhero committed
403 404
class SymbolEnumeratorClang;

ussrhero's avatar
ussrhero committed
405 406
using SymbolList = std::vector<std::pair<std::string, clang::FunctionDecl*> >;

ussrhero's avatar
ussrhero committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
class SymbolProviderClang : public SymbolProvider, public boost::enable_shared_from_this< SymbolProviderClang>
{

public:

    friend SymbolEnumeratorClang;

    SymbolProviderClang(const std::string&  sourceCode, const std::string&  compileOptions);

private:

    SymbolEnumeratorPtr getSymbolEnumerator(const std::wstring& mask = L"") override;

    ClangASTSessionPtr  m_astSession;

ussrhero's avatar
ussrhero committed
422
    SymbolList  m_symbols;
ussrhero's avatar
ussrhero committed
423 424 425 426
};


class SymbolEnumeratorClang : public SymbolEnumerator
427 428 429 430
{

public:

ussrhero's avatar
ussrhero committed
431
    SymbolEnumeratorClang(const std::string& mask, const boost::shared_ptr<SymbolProviderClang>& clangProvider) :
ussrhero's avatar
ussrhero committed
432
        m_symbolProvider(clangProvider),
ussrhero's avatar
ussrhero committed
433 434
        m_index(-1),
        m_mask(mask)
ussrhero's avatar
ussrhero committed
435
    {}
436 437 438

private:

ussrhero's avatar
ussrhero committed
439 440 441 442
    virtual bool Next() override;
    virtual std::wstring getName() override;
    virtual MEMOFFSET_64 getOffset() override;
    virtual TypeInfoPtr getType() override;
443 444 445

private:

ussrhero's avatar
ussrhero committed
446
    size_t   m_index;
447

ussrhero's avatar
ussrhero committed
448 449 450
    std::string  m_mask;

    boost::shared_ptr<SymbolProviderClang> m_symbolProvider;
451
};
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
452

ussrhero's avatar
ussrhero committed
453
}