2012-10-09 10:17:19 +00:00
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// version: $Id$
// author: Vassil Vassilev <vvasilev@cern.ch>
//------------------------------------------------------------------------------
# include "cling/Interpreter/InterpreterCallbacks.h"
2013-02-11 12:55:01 +00:00
# include "cling/Interpreter/DynamicLookupExternalSemaSource.h"
2012-10-09 10:17:19 +00:00
# include "cling/Interpreter/Interpreter.h"
# include "clang/Sema/Sema.h"
2012-10-10 13:00:17 +00:00
using namespace clang ;
2012-10-09 10:17:19 +00:00
namespace cling {
// pin the vtable here
InterpreterExternalSemaSource : : ~ InterpreterExternalSemaSource ( ) { }
2012-10-10 13:00:17 +00:00
bool InterpreterExternalSemaSource : : LookupUnqualified ( LookupResult & R ,
Scope * S ) {
2012-10-10 14:50:45 +00:00
if ( m_Callbacks )
2012-10-09 10:17:19 +00:00
return m_Callbacks - > LookupObject ( R , S ) ;
return false ;
}
2012-10-10 14:50:45 +00:00
InterpreterCallbacks : : InterpreterCallbacks ( Interpreter * interp ,
2012-10-10 13:00:17 +00:00
InterpreterExternalSemaSource * IESS )
2012-10-10 14:50:45 +00:00
: m_Interpreter ( interp ) , m_SemaExternalSource ( IESS ) {
2012-10-10 13:00:17 +00:00
if ( ! IESS )
2012-10-10 15:50:17 +00:00
m_SemaExternalSource . reset ( new InterpreterExternalSemaSource ( this ) ) ;
2012-10-14 14:20:08 +00:00
m_Interpreter - > getSema ( ) . addExternalSource ( m_SemaExternalSource . get ( ) ) ;
2012-10-09 10:17:19 +00:00
}
// pin the vtable here
2012-10-14 14:20:08 +00:00
InterpreterCallbacks : : ~ InterpreterCallbacks ( ) {
// FIXME: we have to remove the external source at destruction time. Needs
// further tweaks of the patch in clang. This will be done later once the
// patch is in clang's mainline.
}
2012-10-09 10:17:19 +00:00
2012-10-10 13:00:17 +00:00
bool InterpreterCallbacks : : LookupObject ( LookupResult & , Scope * ) {
2012-10-09 10:17:19 +00:00
return false ;
}
} // end namespace cling
2012-10-10 13:00:17 +00:00
// TODO: Make the build system in the testsuite aware how to build that class
// and extract it out there again.
2012-10-10 15:50:17 +00:00
# include "DynamicLookup.h"
2012-10-10 13:00:17 +00:00
# include "cling/Utils/AST.h"
2012-10-10 15:50:17 +00:00
2012-10-10 13:00:17 +00:00
# include "clang/Sema/Lookup.h"
2012-10-10 15:50:17 +00:00
# include "clang/Lex/Preprocessor.h"
# include "clang/Sema/Scope.h"
2012-10-10 13:00:17 +00:00
namespace cling {
namespace test {
TestProxy * Tester = 0 ;
extern " C " int printf ( const char * fmt , . . . ) ;
TestProxy : : TestProxy ( ) { }
int TestProxy : : Draw ( ) { return 12 ; }
const char * TestProxy : : getVersion ( ) { return " Interpreter.cpp " ; }
int TestProxy : : Add10 ( int num ) { return num + 10 ; }
int TestProxy : : Add ( int a , int b ) {
return a + b ;
}
void TestProxy : : PrintString ( std : : string s ) { printf ( " %s \n " , s . c_str ( ) ) ; }
bool TestProxy : : PrintArray ( int a [ ] , size_t size ) {
for ( unsigned i = 0 ; i < size ; + + i )
printf ( " %i " , a [ i ] ) ;
printf ( " %s " , " \n " ) ;
return true ;
}
void TestProxy : : PrintArray ( float a [ ] [ 5 ] , size_t size ) {
for ( unsigned i = 0 ; i < size ; + + i )
for ( unsigned j = 0 ; j < 5 ; + + j )
printf ( " %i " , ( int ) a [ i ] [ j ] ) ;
printf ( " %s " , " \n " ) ;
}
void TestProxy : : PrintArray ( int a [ ] [ 4 ] [ 5 ] , size_t size ) {
for ( unsigned i = 0 ; i < size ; + + i )
for ( unsigned j = 0 ; j < 4 ; + + j )
for ( unsigned k = 0 ; k < 5 ; + + k )
printf ( " %i " , a [ i ] [ j ] [ k ] ) ;
printf ( " %s " , " \n " ) ;
}
2012-10-10 15:50:17 +00:00
SymbolResolverCallback : : SymbolResolverCallback ( Interpreter * interp )
: InterpreterCallbacks ( interp , new DynamicIDHandler ( this ) ) , m_TesterDecl ( 0 ) {
2012-10-10 13:00:17 +00:00
m_Interpreter - > process ( " cling::test::Tester = new cling::test::TestProxy(); " ) ;
}
2012-10-10 14:37:10 +00:00
SymbolResolverCallback : : ~ SymbolResolverCallback ( ) { }
2012-10-10 13:00:17 +00:00
bool SymbolResolverCallback : : LookupObject ( LookupResult & R , Scope * S ) {
2013-02-11 13:19:55 +00:00
if ( ! DynamicIDHandler : : IsDynamicLookup ( R , S ) )
2012-10-10 14:37:10 +00:00
return false ;
2012-11-15 16:41:10 +00:00
// We should react only on empty lookup result.
if ( ! R . empty ( ) )
return false ;
2012-10-10 14:37:10 +00:00
2012-10-10 13:00:17 +00:00
// Only for demo resolve all unknown objects to cling::test::Tester
2012-10-10 14:50:45 +00:00
if ( ! m_TesterDecl ) {
2012-10-14 12:01:32 +00:00
clang : : Sema & SemaRef = m_Interpreter - > getSema ( ) ;
clang : : NamespaceDecl * NSD = utils : : Lookup : : Namespace ( & SemaRef , " cling " ) ;
NSD = utils : : Lookup : : Namespace ( & SemaRef , " test " , NSD ) ;
m_TesterDecl = utils : : Lookup : : Named ( & SemaRef , " Tester " , NSD ) ;
2012-10-10 13:00:17 +00:00
}
2012-10-10 14:50:45 +00:00
assert ( m_TesterDecl & & " Tester not found! " ) ;
R . addDecl ( m_TesterDecl ) ;
return true ;
2012-10-10 13:00:17 +00:00
}
} // end test
} // end cling