2012-09-05 09:37:39 +00:00
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
2014-01-07 11:08:37 +01:00
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
2012-09-05 09:37:39 +00:00
//------------------------------------------------------------------------------
# include "IncrementalParser.h"
2014-03-27 17:30:09 +01:00
2013-02-27 15:28:38 +00:00
# include "AutoSynthesizer.h"
2014-03-28 09:42:56 +01:00
# include "BackendPass.h"
2013-10-24 11:54:06 -05:00
# include "CheckEmptyTransactionTransformer.h"
2012-09-05 09:37:39 +00:00
# include "DeclCollector.h"
# include "DeclExtractor.h"
# include "DynamicLookup.h"
2013-07-14 23:35:00 +03:00
# include "NullDerefProtectionTransformer.h"
2014-02-18 11:34:18 +01:00
# include "ValueExtractionSynthesizer.h"
2013-08-01 12:27:40 +02:00
# include "TransactionPool.h"
2014-03-27 17:30:09 +01:00
# include "TransactionUnloader.h"
2012-09-05 09:37:39 +00:00
# include "ValuePrinterSynthesizer.h"
# include "cling/Interpreter/CIFactory.h"
# include "cling/Interpreter/Interpreter.h"
2012-10-02 10:30:25 +00:00
# include "cling/Interpreter/InterpreterCallbacks.h"
2012-10-02 10:34:22 +00:00
# include "cling/Interpreter/Transaction.h"
2012-09-05 09:37:39 +00:00
2014-01-15 15:40:52 +01:00
# include "clang/AST/Attr.h"
2012-09-05 09:37:39 +00:00
# include "clang/AST/ASTContext.h"
# include "clang/AST/Decl.h"
# include "clang/AST/DeclGroup.h"
2013-03-01 15:15:31 +00:00
# include "clang/AST/RecursiveASTVisitor.h"
2012-09-05 09:37:39 +00:00
# include "clang/Basic/FileManager.h"
# include "clang/CodeGen/ModuleBuilder.h"
# include "clang/Parse/Parser.h"
# include "clang/Lex/Preprocessor.h"
# include "clang/Frontend/CompilerInstance.h"
# include "clang/Serialization/ASTWriter.h"
2013-04-24 16:28:08 +00:00
# include "llvm/IR/LLVMContext.h"
# include "llvm/IR/Module.h"
2012-09-05 09:37:39 +00:00
# include "llvm/Support/CrashRecoveryContext.h"
# include "llvm/Support/MemoryBuffer.h"
# include "llvm/Support/raw_os_ostream.h"
# include <iostream>
# include <stdio.h>
# include <sstream>
using namespace clang ;
namespace cling {
IncrementalParser : : IncrementalParser ( Interpreter * interp ,
int argc , const char * const * argv ,
const char * llvmdir ) :
2013-06-24 15:55:04 +02:00
m_Interpreter ( interp ) , m_Consumer ( 0 ) {
2012-09-05 09:37:39 +00:00
2013-11-18 16:49:19 +01:00
CompilerInstance * CI = CIFactory : : createCI ( " " , argc , argv , llvmdir ) ;
2012-09-05 09:37:39 +00:00
assert ( CI & & " CompilerInstance is (null)! " ) ;
2013-11-18 16:49:19 +01:00
m_Consumer = dyn_cast < DeclCollector > ( & CI - > getSema ( ) . getASTConsumer ( ) ) ;
2012-09-05 09:37:39 +00:00
assert ( m_Consumer & & " Expected ChainedConsumer! " ) ;
m_CI . reset ( CI ) ;
if ( CI - > getFrontendOpts ( ) . ProgramAction ! = clang : : frontend : : ParseSyntaxOnly ) {
m_CodeGen . reset ( CreateLLVMCodeGen ( CI - > getDiagnostics ( ) , " cling input " ,
CI - > getCodeGenOpts ( ) ,
2013-04-24 16:28:08 +00:00
CI - > getTargetOpts ( ) ,
2012-09-05 09:37:39 +00:00
* m_Interpreter - > getLLVMContext ( )
) ) ;
}
2013-08-22 14:11:12 +02:00
initializeVirtualFile ( ) ;
2012-09-05 09:37:39 +00:00
// Add transformers to the IncrementalParser, which owns them
2013-02-27 15:28:38 +00:00
Sema * TheSema = & CI - > getSema ( ) ;
2013-06-26 20:51:05 +02:00
// Register the AST Transformers
2013-10-11 16:23:01 +02:00
m_ASTTransformers . push_back ( new AutoSynthesizer ( TheSema ) ) ;
2013-06-26 20:51:05 +02:00
m_ASTTransformers . push_back ( new EvaluateTSynthesizer ( TheSema ) ) ;
m_ASTTransformers . push_back ( new ValuePrinterSynthesizer ( TheSema , 0 ) ) ;
m_ASTTransformers . push_back ( new DeclExtractor ( TheSema ) ) ;
2014-02-18 11:34:18 +01:00
m_ASTTransformers . push_back ( new ValueExtractionSynthesizer ( TheSema ) ) ;
2013-09-11 13:31:14 +02:00
m_ASTTransformers . push_back ( new NullDerefProtectionTransformer ( TheSema ) ) ;
2014-02-27 10:37:10 +01:00
m_ASTTransformers . push_back ( new CheckEmptyTransactionTransformer ( TheSema ) ) ;
2014-03-28 09:42:56 +01:00
2014-04-17 15:24:47 +02:00
# ifdef _LIBCPP_VERSION
// libc++ relies on force_inline attributes, else symbols will be missing.
// But its passes (CallGraph and Inliner) - being module passes - have a
// quadratically increasing runtime: for each transaction they need to
// iterate over all previous transactions' functions.
// Until this is solved (for instance by feeding only the new functions
// to the CallGraph) we penalize only the use of libc++.
2014-03-28 09:42:56 +01:00
if ( m_CodeGen ) {
llvm : : Module * TheModule = m_CodeGen - > GetModule ( ) ;
// IR passes make sense if we do CodeGen.
m_IRTransformers . push_back ( new BackendPass ( TheSema , TheModule ,
CI - > getDiagnostics ( ) ,
CI - > getTargetOpts ( ) ,
CI - > getLangOpts ( ) ,
CI - > getCodeGenOpts ( ) ) ) ;
}
2014-04-17 15:24:47 +02:00
# endif
2013-03-11 13:11:15 +00:00
}
2013-06-26 20:51:05 +02:00
2014-03-04 19:02:42 +01:00
void
IncrementalParser : : Initialize ( llvm : : SmallVectorImpl < Transaction * > & result ) {
2014-03-28 14:53:34 +01:00
m_TransactionPool . reset ( new TransactionPool ( getCI ( ) - > getSema ( ) ) ) ;
2013-05-23 10:57:23 +02:00
if ( hasCodeGenerator ( ) )
2013-03-11 13:11:15 +00:00
getCodeGenerator ( ) - > Initialize ( getCI ( ) - > getASTContext ( ) ) ;
2013-05-23 11:05:02 +02:00
2013-03-26 09:59:19 +00:00
CompilationOptions CO ;
CO . DeclarationExtraction = 0 ;
CO . ValuePrinting = CompilationOptions : : VPDisabled ;
CO . CodeGeneration = hasCodeGenerator ( ) ;
2013-05-23 14:44:52 +02:00
// pull in PCHs
const std : : string & PCHFileName
= m_CI - > getInvocation ( ) . getPreprocessorOpts ( ) . ImplicitPCHInclude ;
2013-03-11 13:11:15 +00:00
if ( ! PCHFileName . empty ( ) ) {
2013-04-27 01:52:55 +02:00
Transaction * CurT = beginTransaction ( CO ) ;
2013-03-11 13:11:15 +00:00
m_CI - > createPCHExternalASTSource ( PCHFileName ,
true /*DisablePCHValidation*/ ,
true /*AllowPCHWithCompilerErrors*/ ,
0 /*DeserializationListener*/ ) ;
2013-07-06 13:04:31 +02:00
if ( Transaction * EndedT = endTransaction ( CurT ) )
2014-03-04 19:02:42 +01:00
result . push_back ( EndedT ) ;
2013-03-11 13:11:15 +00:00
}
2012-09-05 09:37:39 +00:00
2013-04-27 01:52:55 +02:00
Transaction * CurT = beginTransaction ( CO ) ;
2013-03-11 13:11:15 +00:00
Sema * TheSema = & m_CI - > getSema ( ) ;
m_Parser . reset ( new Parser ( m_CI - > getPreprocessor ( ) , * TheSema ,
2012-09-05 09:37:39 +00:00
false /*skipFuncBodies*/ ) ) ;
2013-03-11 13:11:15 +00:00
m_CI - > getPreprocessor ( ) . EnterMainSourceFile ( ) ;
2012-09-05 09:37:39 +00:00
// Initialize the parser after we have entered the main source file.
m_Parser - > Initialize ( ) ;
// Perform initialization that occurs after the parser has been initialized
// but before it parses anything. Initializes the consumers too.
2013-08-19 15:48:00 +02:00
// No - already done by m_Parser->Initialize().
// TheSema->Initialize();
2013-03-11 13:11:15 +00:00
ExternalASTSource * External = TheSema - > getASTContext ( ) . getExternalSource ( ) ;
if ( External )
External - > StartTranslationUnit ( m_Consumer ) ;
2013-04-27 01:52:55 +02:00
2014-03-23 21:08:48 +01:00
// DO NOT commit the transactions here: static initialization in these
// transactions requires gCling through local_cxa_atexit(), but that has not
// been defined yet!
2013-07-06 13:04:31 +02:00
if ( Transaction * EndedT = endTransaction ( CurT ) )
2014-03-04 19:02:42 +01:00
result . push_back ( EndedT ) ;
2014-03-23 21:08:48 +01:00
2012-09-05 09:37:39 +00:00
}
2014-04-01 16:44:59 +02:00
SourceLocation IncrementalParser : : getLastMemoryBufferEndLoc ( ) const {
const SourceManager & SM = getCI ( ) - > getSourceManager ( ) ;
SourceLocation Result = SM . getLocForStartOfFile ( m_VirtualFileID ) ;
return Result . getLocWithOffset ( m_MemoryBuffers . size ( ) + 1 ) ;
}
2012-09-05 09:37:39 +00:00
IncrementalParser : : ~ IncrementalParser ( ) {
2012-11-20 13:21:00 +00:00
if ( hasCodeGenerator ( ) ) {
getCodeGenerator ( ) - > ReleaseModule ( ) ;
}
const Transaction * T = getFirstTransaction ( ) ;
const Transaction * nextT = 0 ;
while ( T ) {
2013-09-15 13:18:49 -05:00
assert ( ( T - > getState ( ) = = Transaction : : kCommitted
| | T - > getState ( ) = = Transaction : : kRolledBackWithErrors
2013-11-27 14:28:40 +01:00
| | T - > getState ( ) = = Transaction : : kNumStates // reset from the pool
2013-06-17 12:45:18 +02:00
| | T - > getState ( ) = = Transaction : : kRolledBack )
& & " Not committed? " ) ;
2012-11-20 13:21:00 +00:00
nextT = T - > getNext ( ) ;
delete T ;
T = nextT ;
}
2013-06-26 20:51:05 +02:00
for ( size_t i = 0 ; i < m_ASTTransformers . size ( ) ; + + i )
delete m_ASTTransformers [ i ] ;
for ( size_t i = 0 ; i < m_IRTransformers . size ( ) ; + + i )
delete m_IRTransformers [ i ] ;
2012-09-05 09:37:39 +00:00
}
2012-11-14 22:06:58 +00:00
Transaction * IncrementalParser : : beginTransaction ( const CompilationOptions &
Opts ) {
2012-09-05 09:37:39 +00:00
Transaction * OldCurT = m_Consumer - > getTransaction ( ) ;
2013-07-06 13:04:31 +02:00
Transaction * NewCurT = m_TransactionPool - > takeTransaction ( ) ;
NewCurT - > setCompilationOpts ( Opts ) ;
2012-09-05 09:37:39 +00:00
// If we are in the middle of transaction and we see another begin
// transaction - it must be nested transaction.
2013-07-06 13:04:31 +02:00
if ( OldCurT & & OldCurT ! = NewCurT
2013-08-04 18:17:41 +02:00
& & ( OldCurT - > getState ( ) = = Transaction : : kCollecting
| | OldCurT - > getState ( ) = = Transaction : : kCompleted ) ) {
2013-07-06 13:04:31 +02:00
OldCurT - > addNestedTransaction ( NewCurT ) ; // takes the ownership
2012-09-05 09:37:39 +00:00
}
2013-04-24 06:55:38 +00:00
m_Consumer - > setTransaction ( NewCurT ) ;
2012-11-14 22:06:58 +00:00
return NewCurT ;
2012-09-05 09:37:39 +00:00
}
2013-07-06 13:04:31 +02:00
Transaction * IncrementalParser : : endTransaction ( Transaction * T ) {
2013-04-27 01:52:55 +02:00
assert ( T & & " Null transaction!? " ) ;
assert ( T - > getState ( ) = = Transaction : : kCollecting ) ;
2013-06-27 12:34:05 +02:00
# ifndef NDEBUG
if ( T - > hasNestedTransactions ( ) ) {
for ( Transaction : : const_nested_iterator I = T - > nested_begin ( ) ,
E = T - > nested_end ( ) ; I ! = E ; + + I )
assert ( ( * I ) - > isCompleted ( ) & & " Nested transaction not completed!? " ) ;
}
# endif
2013-04-27 01:52:55 +02:00
T - > setState ( Transaction : : kCompleted ) ;
2013-07-06 13:04:31 +02:00
// Empty transaction send it back to the pool.
if ( T - > empty ( ) ) {
2013-08-05 15:15:37 +02:00
assert ( ( ! m_Consumer - > getTransaction ( )
| | ( m_Consumer - > getTransaction ( ) = = T ) )
2013-08-04 18:18:27 +02:00
& & " Cannot release different T " ) ;
// If a nested transaction the active one should be its parent
// from now on. FIXME: Merge conditional with commitTransaction
if ( T - > isNestedTransaction ( ) )
m_Consumer - > setTransaction ( T - > getParent ( ) ) ;
else
m_Consumer - > setTransaction ( ( Transaction * ) 0 ) ;
2013-07-06 13:04:31 +02:00
m_TransactionPool - > releaseTransaction ( T ) ;
return 0 ;
}
2013-10-24 11:54:06 -05:00
transformTransactionAST ( T ) ;
if ( T - > empty ( ) ) {
m_TransactionPool - > releaseTransaction ( T ) ;
return 0 ;
}
2013-07-06 13:04:31 +02:00
2012-09-05 09:37:39 +00:00
const DiagnosticsEngine & Diags = getCI ( ) - > getSema ( ) . getDiagnostics ( ) ;
//TODO: Make the enum orable.
if ( Diags . getNumWarnings ( ) > 0 )
2013-04-27 01:52:55 +02:00
T - > setIssuedDiags ( Transaction : : kWarnings ) ;
2012-09-05 09:37:39 +00:00
if ( Diags . hasErrorOccurred ( ) | | Diags . hasFatalErrorOccurred ( ) )
2013-04-27 01:52:55 +02:00
T - > setIssuedDiags ( Transaction : : kErrors ) ;
2012-09-05 09:37:39 +00:00
2013-07-06 13:04:31 +02:00
if ( ! T - > isNestedTransaction ( ) & & T ! = getLastTransaction ( ) ) {
if ( getLastTransaction ( ) )
m_Transactions . back ( ) - > setNext ( T ) ;
m_Transactions . push_back ( T ) ;
}
2013-04-27 01:52:55 +02:00
return T ;
2012-09-05 09:37:39 +00:00
}
2013-06-25 10:51:58 +02:00
void IncrementalParser : : commitTransaction ( Transaction * T ) {
2012-11-14 22:06:58 +00:00
//Transaction* CurT = m_Consumer->getTransaction();
assert ( T - > isCompleted ( ) & & " Transaction not ended!? " ) ;
2013-04-09 15:30:16 +00:00
assert ( T - > getState ( ) ! = Transaction : : kCommitted
& & " Committing an already committed transaction. " ) ;
2013-07-06 13:04:31 +02:00
assert ( ! T - > empty ( ) & & " Transactions must not be empty; " ) ;
2012-09-05 09:37:39 +00:00
2013-06-25 09:19:15 +02:00
// If committing a nested transaction the active one should be its parent
// from now on.
2013-06-25 10:51:58 +02:00
if ( T - > isNestedTransaction ( ) )
2013-06-25 09:19:15 +02:00
m_Consumer - > setTransaction ( T - > getParent ( ) ) ;
2012-09-05 09:37:39 +00:00
// Check for errors...
2012-11-14 22:06:58 +00:00
if ( T - > getIssuedDiags ( ) = = Transaction : : kErrors ) {
rollbackTransaction ( T ) ;
2012-09-05 09:37:39 +00:00
return ;
}
2012-11-14 22:06:58 +00:00
if ( T - > hasNestedTransactions ( ) ) {
2013-07-06 13:03:21 +02:00
for ( Transaction : : const_nested_iterator I = T - > nested_begin ( ) ,
2013-04-08 14:35:35 +00:00
E = T - > nested_end ( ) ; I ! = E ; + + I )
2013-04-09 15:30:16 +00:00
if ( ( * I ) - > getState ( ) ! = Transaction : : kCommitted )
commitTransaction ( * I ) ;
2012-11-14 22:06:58 +00:00
}
2013-07-06 13:01:48 +02:00
// If there was an error coming from the transformers.
if ( T - > getIssuedDiags ( ) = = Transaction : : kErrors ) {
rollbackTransaction ( T ) ;
2013-06-25 10:51:58 +02:00
return ;
2013-07-06 13:01:48 +02:00
}
2012-09-05 09:37:39 +00:00
2013-06-13 14:53:58 +02:00
// Here we expect a template instantiation. We need to open the transaction
// that we are currently work with.
2013-07-06 13:04:31 +02:00
{
2014-04-16 11:59:36 +02:00
Transaction * prevConsumerT = m_Consumer - > getTransaction ( ) ;
m_Consumer - > setTransaction ( T ) ;
2013-07-06 13:04:31 +02:00
Transaction * nestedT = beginTransaction ( CompilationOptions ( ) ) ;
// Pull all template instantiations in that came from the consumers.
getCI ( ) - > getSema ( ) . PerformPendingInstantiations ( ) ;
if ( Transaction * T = endTransaction ( nestedT ) )
commitTransaction ( T ) ;
2014-04-16 11:59:36 +02:00
m_Consumer - > setTransaction ( prevConsumerT ) ;
2013-07-06 13:04:31 +02:00
}
2013-06-13 14:53:58 +02:00
m_Consumer - > HandleTranslationUnit ( getCI ( ) - > getASTContext ( ) ) ;
2013-04-10 15:15:49 +00:00
2013-06-25 10:51:58 +02:00
// The static initializers might run anything and can thus cause more
// decls that need to end up in a transaction. But this one is done
// with CodeGen...
2013-07-06 13:04:31 +02:00
if ( T - > getCompilationOpts ( ) . CodeGeneration & & hasCodeGenerator ( ) ) {
2014-04-16 11:59:36 +02:00
Transaction * prevConsumerT = m_Consumer - > getTransaction ( ) ;
m_Consumer - > setTransaction ( T ) ;
2013-07-06 13:04:31 +02:00
codeGenTransaction ( T ) ;
transformTransactionIR ( T ) ;
T - > setState ( Transaction : : kCommitted ) ;
2014-04-16 12:01:44 +02:00
if ( ! T - > getParent ( ) ) {
if ( m_Interpreter - > runStaticInitializersOnce ( * T )
> = Interpreter : : kExeFirstError ) {
// Roll back on error in a transformer
assert ( 0 & & " Error on inits. " ) ;
//rollbackTransaction(nestedT);
return ;
}
2013-07-06 13:04:31 +02:00
}
2014-04-16 11:59:36 +02:00
m_Consumer - > setTransaction ( prevConsumerT ) ;
2013-07-06 13:04:31 +02:00
}
2013-06-25 10:51:58 +02:00
T - > setState ( Transaction : : kCommitted ) ;
2013-04-09 15:30:16 +00:00
2013-08-02 16:20:54 +02:00
if ( InterpreterCallbacks * callbacks = m_Interpreter - > getCallbacks ( ) )
2013-06-25 10:51:58 +02:00
callbacks - > TransactionCommitted ( * T ) ;
2013-08-02 16:20:54 +02:00
2012-09-05 09:37:39 +00:00
}
2013-06-27 14:30:07 +02:00
void IncrementalParser : : markWholeTransactionAsUsed ( Transaction * T ) const {
2014-03-28 14:53:34 +01:00
ASTContext & C = m_CI - > getASTContext ( ) ;
2013-11-25 14:26:15 +01:00
for ( Transaction : : const_iterator I = T - > decls_begin ( ) , E = T - > decls_end ( ) ;
I ! = E ; + + I ) {
// Copy DCI; it might get relocated below.
Transaction : : DelayCallInfo DCI = * I ;
2013-06-25 10:51:58 +02:00
// FIXME: implement for multiple decls in a DGR.
2013-11-25 14:26:15 +01:00
assert ( DCI . m_DGR . isSingleDecl ( ) ) ;
Decl * D = DCI . m_DGR . getSingleDecl ( ) ;
2013-06-25 10:51:58 +02:00
if ( ! D - > hasAttr < clang : : UsedAttr > ( ) )
D - > addAttr ( : : new ( D - > getASTContext ( ) )
clang : : UsedAttr ( D - > getSourceRange ( ) , D - > getASTContext ( ) ,
0 /*AttributeSpellingListIndex*/ ) ) ;
}
2013-08-07 14:26:15 +02:00
for ( Transaction : : iterator I = T - > deserialized_decls_begin ( ) ,
E = T - > deserialized_decls_end ( ) ; I ! = E ; + + I ) {
// FIXME: implement for multiple decls in a DGR.
assert ( I - > m_DGR . isSingleDecl ( ) ) ;
Decl * D = I - > m_DGR . getSingleDecl ( ) ;
if ( ! D - > hasAttr < clang : : UsedAttr > ( ) )
D - > addAttr ( : : new ( C ) clang : : UsedAttr ( D - > getSourceRange ( ) , C ,
0 /*AttributeSpellingListIndex*/ ) ) ;
}
2013-06-25 10:51:58 +02:00
}
void IncrementalParser : : codeGenTransaction ( Transaction * T ) {
// codegen the transaction
assert ( T - > getCompilationOpts ( ) . CodeGeneration & & " CodeGen turned off " ) ;
2013-07-31 09:04:30 +02:00
assert ( T - > getState ( ) = = Transaction : : kCompleted & & " Must be completed " ) ;
2013-06-25 10:51:58 +02:00
assert ( hasCodeGenerator ( ) & & " No CodeGen " ) ;
T - > setModule ( getCodeGenerator ( ) - > GetModule ( ) ) ;
2013-08-04 18:39:41 +02:00
// Could trigger derserialization of decls.
Transaction * deserT = beginTransaction ( CompilationOptions ( ) ) ;
2013-11-25 14:26:15 +01:00
for ( Transaction : : const_iterator TI = T - > decls_begin ( ) , TE = T - > decls_end ( ) ;
TI ! = TE ; + + TI ) {
2013-06-25 10:51:58 +02:00
// Copy DCI; it might get relocated below.
2013-11-25 14:26:15 +01:00
Transaction : : DelayCallInfo I = * TI ;
2013-06-25 10:51:58 +02:00
if ( I . m_Call = = Transaction : : kCCIHandleTopLevelDecl )
getCodeGenerator ( ) - > HandleTopLevelDecl ( I . m_DGR ) ;
else if ( I . m_Call = = Transaction : : kCCIHandleInterestingDecl ) {
// Usually through BackendConsumer which doesn't implement
// HandleInterestingDecl() and thus calls
// ASTConsumer::HandleInterestingDecl()
getCodeGenerator ( ) - > HandleTopLevelDecl ( I . m_DGR ) ;
} else if ( I . m_Call = = Transaction : : kCCIHandleTagDeclDefinition ) {
TagDecl * TD = cast < TagDecl > ( I . m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > HandleTagDeclDefinition ( TD ) ;
}
else if ( I . m_Call = = Transaction : : kCCIHandleVTable ) {
CXXRecordDecl * CXXRD = cast < CXXRecordDecl > ( I . m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > HandleVTable ( CXXRD , /*isRequired*/ true ) ;
}
else if ( I . m_Call
= = Transaction : : kCCIHandleCXXImplicitFunctionInstantiation ) {
FunctionDecl * FD = cast < FunctionDecl > ( I . m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > HandleCXXImplicitFunctionInstantiation ( FD ) ;
}
else if ( I . m_Call
= = Transaction : : kCCIHandleCXXStaticMemberVarInstantiation ) {
VarDecl * VD = cast < VarDecl > ( I . m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > HandleCXXStaticMemberVarInstantiation ( VD ) ;
}
2014-01-29 15:48:13 +01:00
else if ( I . m_Call = = Transaction : : kCCICompleteTentativeDefinition ) {
VarDecl * VD = cast < VarDecl > ( I . m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > CompleteTentativeDefinition ( VD ) ;
}
2013-06-25 10:51:58 +02:00
else if ( I . m_Call = = Transaction : : kCCINone )
; // We use that internally as delimiter in the Transaction.
else
llvm_unreachable ( " We shouldn't have decl without call info. " ) ;
}
2013-08-26 17:59:33 +02:00
// Treat the deserialized decls differently.
2013-08-07 14:26:15 +02:00
for ( Transaction : : iterator I = T - > deserialized_decls_begin ( ) ,
E = T - > deserialized_decls_end ( ) ; I ! = E ; + + I ) {
2013-08-26 17:59:33 +02:00
2013-09-13 10:37:54 +02:00
for ( DeclGroupRef : : iterator DI = I - > m_DGR . begin ( ) , DE = I - > m_DGR . end ( ) ;
DI ! = DE ; + + DI ) {
DeclGroupRef SplitDGR ( * DI ) ;
if ( I - > m_Call = = Transaction : : kCCIHandleTopLevelDecl ) {
// FIXME: The special namespace treatment (not sending itself to
// CodeGen, but only its content - if the contained decl should be
// emitted) works around issue with the static initialization when
// having a PCH and loading a library. We don't want to generate
// code for the static that will come through the library.
//
// This will be fixed with the clang::Modules. Make sure we remember.
assert ( ! getCI ( ) - > getLangOpts ( ) . Modules & & " Please revisit! " ) ;
2013-08-26 17:59:33 +02:00
if ( NamespaceDecl * ND = dyn_cast < NamespaceDecl > ( * DI ) ) {
for ( NamespaceDecl : : decl_iterator IN = ND - > decls_begin ( ) ,
EN = ND - > decls_end ( ) ; IN ! = EN ; + + IN ) {
// Recurse over decls inside the namespace, like
// CodeGenModule::EmitNamespace() does.
2013-09-13 10:37:54 +02:00
if ( ! shouldIgnore ( * IN ) )
getCodeGenerator ( ) - > HandleTopLevelDecl ( DeclGroupRef ( * IN ) ) ;
2013-08-26 17:59:33 +02:00
}
2013-09-13 10:37:54 +02:00
} else if ( ! shouldIgnore ( * DI ) ) {
getCodeGenerator ( ) - > HandleTopLevelDecl ( SplitDGR ) ;
2013-08-26 17:59:33 +02:00
}
continue ;
2013-09-13 10:37:54 +02:00
} // HandleTopLevel
2013-08-26 17:59:33 +02:00
2013-09-13 10:37:54 +02:00
if ( shouldIgnore ( * DI ) )
continue ;
if ( I - > m_Call = = Transaction : : kCCIHandleInterestingDecl ) {
// Usually through BackendConsumer which doesn't implement
// HandleInterestingDecl() and thus calls
// ASTConsumer::HandleInterestingDecl()
getCodeGenerator ( ) - > HandleTopLevelDecl ( SplitDGR ) ;
} else if ( I - > m_Call = = Transaction : : kCCIHandleTagDeclDefinition ) {
TagDecl * TD = cast < TagDecl > ( * DI ) ;
getCodeGenerator ( ) - > HandleTagDeclDefinition ( TD ) ;
}
else if ( I - > m_Call = = Transaction : : kCCIHandleVTable ) {
CXXRecordDecl * CXXRD = cast < CXXRecordDecl > ( * DI ) ;
getCodeGenerator ( ) - > HandleVTable ( CXXRD , /*isRequired*/ true ) ;
}
else if ( I - > m_Call
= = Transaction : : kCCIHandleCXXImplicitFunctionInstantiation ) {
FunctionDecl * FD = cast < FunctionDecl > ( * DI ) ;
getCodeGenerator ( ) - > HandleCXXImplicitFunctionInstantiation ( FD ) ;
}
else if ( I - > m_Call
= = Transaction : : kCCIHandleCXXStaticMemberVarInstantiation ) {
VarDecl * VD = cast < VarDecl > ( * DI ) ;
getCodeGenerator ( ) - > HandleCXXStaticMemberVarInstantiation ( VD ) ;
}
2014-01-29 15:48:13 +01:00
else if ( I - > m_Call = = Transaction : : kCCICompleteTentativeDefinition ) {
VarDecl * VD = cast < VarDecl > ( I - > m_DGR . getSingleDecl ( ) ) ;
getCodeGenerator ( ) - > CompleteTentativeDefinition ( VD ) ;
}
2013-09-13 10:37:54 +02:00
else if ( I - > m_Call = = Transaction : : kCCINone )
; // We use that internally as delimiter in the Transaction.
else
llvm_unreachable ( " We shouldn't have decl without call info. " ) ;
} // for decls in DGR
} // for deserialized DGRs
2013-08-07 14:26:15 +02:00
2013-06-25 10:51:58 +02:00
getCodeGenerator ( ) - > HandleTranslationUnit ( getCI ( ) - > getASTContext ( ) ) ;
2014-04-16 11:54:10 +02:00
if ( ( deserT = endTransaction ( deserT ) ) )
2013-08-04 18:39:41 +02:00
commitTransaction ( deserT ) ;
2013-06-25 10:51:58 +02:00
}
2013-08-08 11:22:29 +02:00
void IncrementalParser : : transformTransactionAST ( Transaction * T ) {
2013-06-27 14:30:07 +02:00
bool success = true ;
// We are sure it's safe to pipe it through the transformers
2013-08-08 11:22:29 +02:00
// Consume late transformers init
Transaction * initT = beginTransaction ( CompilationOptions ( ) ) ;
2013-06-27 14:30:07 +02:00
for ( size_t i = 0 ; success & & i < m_ASTTransformers . size ( ) ; + + i )
success = m_ASTTransformers [ i ] - > TransformTransaction ( * T ) ;
2013-08-08 11:22:29 +02:00
if ( endTransaction ( initT ) )
commitTransaction ( initT ) ;
2013-06-27 14:30:07 +02:00
if ( ! success )
2013-07-06 13:01:48 +02:00
T - > setIssuedDiags ( Transaction : : kErrors ) ;
2013-06-25 10:51:58 +02:00
}
2014-03-24 12:02:41 +01:00
bool IncrementalParser : : transformTransactionIR ( Transaction * T ) {
2013-06-25 10:51:58 +02:00
// Transform IR
2013-06-27 14:30:07 +02:00
bool success = true ;
for ( size_t i = 0 ; success & & i < m_IRTransformers . size ( ) ; + + i )
2013-06-25 10:51:58 +02:00
success = m_IRTransformers [ i ] - > TransformTransaction ( * T ) ;
2013-06-27 14:30:07 +02:00
if ( ! success )
rollbackTransaction ( T ) ;
return success ;
2013-06-25 10:51:58 +02:00
}
2014-03-24 12:02:41 +01:00
void IncrementalParser : : rollbackTransaction ( Transaction * T ) {
assert ( T & & " Must have value " ) ;
assert ( T = = getLastTransaction ( ) & & " We always must revert the last T " ) ;
assert ( ( T - > getState ( ) ! = Transaction : : kRolledBack | |
T - > getState ( ) ! = Transaction : : kRolledBackWithErrors ) & &
" Transaction already rolled back. " ) ;
2013-12-02 10:49:37 +01:00
if ( m_Interpreter - > getOptions ( ) . ErrorOut )
return ;
2014-03-24 12:02:41 +01:00
2014-03-27 17:30:09 +01:00
TransactionUnloader U ( & getCI ( ) - > getSema ( ) , m_CodeGen . get ( ) ,
m_Interpreter - > getExecutionEngine ( ) ) ;
2012-09-05 09:37:39 +00:00
2014-03-27 17:30:09 +01:00
if ( U . RevertTransaction ( T ) )
2012-09-05 09:37:39 +00:00
T - > setState ( Transaction : : kRolledBack ) ;
else
T - > setState ( Transaction : : kRolledBackWithErrors ) ;
2014-03-24 12:02:41 +01:00
// Remove from the queue
m_Transactions . pop_back ( ) ;
//m_TransactionPool->releaseTransaction(T);
2012-09-05 09:37:39 +00:00
}
2012-11-14 22:06:58 +00:00
std : : vector < const Transaction * > IncrementalParser : : getAllTransactions ( ) {
2013-08-18 10:48:11 +02:00
std : : vector < const Transaction * > result ( m_Transactions . size ( ) ) ;
2012-11-14 22:06:58 +00:00
const cling : : Transaction * T = getFirstTransaction ( ) ;
while ( T ) {
result . push_back ( T ) ;
T = T - > getNext ( ) ;
}
return result ;
}
2012-09-05 09:37:39 +00:00
// Each input line is contained in separate memory buffer. The SourceManager
// assigns sort-of invalid FileID for each buffer, i.e there is no FileEntry
// for the MemoryBuffer's FileID. That in turn is problem because invalid
// SourceLocations are given to the diagnostics. Thus the diagnostics cannot
// order the overloads, for example
//
// Our work-around is creating a virtual file, which doesn't exist on the disk
// with enormous size (no allocation is done). That file has valid FileEntry
// and so on... We use it for generating valid SourceLocations with valid
// offsets so that it doesn't cause any troubles to the diagnostics.
//
// +---------------------+
// | Main memory buffer |
// +---------------------+
// | Virtual file SLoc |
// | address space |<-----------------+
// | ... |<------------+ |
// | ... | | |
// | ... |<----+ | |
// | ... | | | |
// +~~~~~~~~~~~~~~~~~~~~~+ | | |
// | input_line_1 | ....+.......+..--+
// +---------------------+ | |
// | input_line_2 | ....+.....--+
// +---------------------+ |
// | ... | |
// +---------------------+ |
// | input_line_N | ..--+
// +---------------------+
//
2013-08-22 14:11:12 +02:00
void IncrementalParser : : initializeVirtualFile ( ) {
2012-09-05 09:37:39 +00:00
SourceManager & SM = getCI ( ) - > getSourceManager ( ) ;
2013-03-11 13:11:15 +00:00
m_VirtualFileID = SM . getMainFileID ( ) ;
2012-09-05 09:37:39 +00:00
assert ( ! m_VirtualFileID . isInvalid ( ) & & " No VirtualFileID created? " ) ;
}
2012-11-20 03:26:52 +00:00
Transaction * IncrementalParser : : Compile ( llvm : : StringRef input ,
const CompilationOptions & Opts ) {
Transaction * CurT = beginTransaction ( Opts ) ;
EParseResult ParseRes = ParseInternal ( input ) ;
2012-09-05 09:37:39 +00:00
2012-11-20 03:26:52 +00:00
if ( ParseRes = = kSuccessWithWarnings )
CurT - > setIssuedDiags ( Transaction : : kWarnings ) ;
else if ( ParseRes = = kFailed )
CurT - > setIssuedDiags ( Transaction : : kErrors ) ;
2013-10-24 11:54:06 -05:00
if ( ( CurT = endTransaction ( CurT ) ) ) {
2013-07-06 13:04:31 +02:00
commitTransaction ( CurT ) ;
2013-06-27 14:29:44 +02:00
}
2012-11-20 03:26:52 +00:00
return CurT ;
2012-09-05 09:37:39 +00:00
}
2012-10-15 13:42:09 +00:00
Transaction * IncrementalParser : : Parse ( llvm : : StringRef input ,
const CompilationOptions & Opts ) {
2013-04-27 01:52:55 +02:00
Transaction * CurT = beginTransaction ( Opts ) ;
2012-09-05 09:37:39 +00:00
ParseInternal ( input ) ;
2013-04-27 01:52:55 +02:00
Transaction * EndedT = endTransaction ( CurT ) ;
assert ( EndedT = = CurT & & " Not ending the expected transaction. " ) ;
return EndedT ;
2012-09-05 09:37:39 +00:00
}
// Add the input to the memory buffer, parse it, and add it to the AST.
IncrementalParser : : EParseResult
IncrementalParser : : ParseInternal ( llvm : : StringRef input ) {
if ( input . empty ( ) ) return IncrementalParser : : kSuccess ;
Sema & S = getCI ( ) - > getSema ( ) ;
2013-04-10 07:09:57 +00:00
2013-04-10 07:14:37 +00:00
assert ( ! ( S . getLangOpts ( ) . Modules
& & m_Consumer - > getTransaction ( ) - > getCompilationOpts ( )
. CodeGenerationForModule )
2014-02-20 10:16:47 +01:00
& & " CodeGenerationForModule to be removed once PCMs are available! " ) ;
2013-04-10 07:09:57 +00:00
2012-09-05 09:37:39 +00:00
// Recover resources if we crash before exiting this method.
llvm : : CrashRecoveryContextCleanupRegistrar < Sema > CleanupSema ( & S ) ;
Preprocessor & PP = m_CI - > getPreprocessor ( ) ;
if ( ! PP . getCurrentLexer ( ) ) {
PP . EnterSourceFile ( m_CI - > getSourceManager ( ) . getMainFileID ( ) ,
0 , SourceLocation ( ) ) ;
}
2012-10-04 13:09:52 +00:00
assert ( PP . isIncrementalProcessingEnabled ( ) & & " Not in incremental mode!? " ) ;
2012-09-05 09:37:39 +00:00
PP . enableIncrementalProcessing ( ) ;
std : : ostringstream source_name ;
source_name < < " input_line_ " < < ( m_MemoryBuffers . size ( ) + 1 ) ;
// Create an uninitialized memory buffer, copy code in and append "\n"
size_t InputSize = input . size ( ) ; // don't include trailing 0
// MemBuffer size should *not* include terminating zero
llvm : : MemoryBuffer * MB
= llvm : : MemoryBuffer : : getNewUninitMemBuffer ( InputSize + 1 ,
source_name . str ( ) ) ;
char * MBStart = const_cast < char * > ( MB - > getBufferStart ( ) ) ;
memcpy ( MBStart , input . data ( ) , InputSize ) ;
memcpy ( MBStart + InputSize , " \n " , 2 ) ;
m_MemoryBuffers . push_back ( MB ) ;
SourceManager & SM = getCI ( ) - > getSourceManager ( ) ;
// Create SourceLocation, which will allow clang to order the overload
// candidates for example
2014-04-01 16:44:59 +02:00
SourceLocation NewLoc = getLastMemoryBufferEndLoc ( ) . getLocWithOffset ( 1 ) ;
2012-09-05 09:37:39 +00:00
// Create FileID for the current buffer
FileID FID = SM . createFileIDForMemBuffer ( m_MemoryBuffers . back ( ) ,
2013-04-24 16:28:08 +00:00
SrcMgr : : C_User ,
2012-09-05 09:37:39 +00:00
/*LoadedID*/ 0 ,
/*LoadedOffset*/ 0 , NewLoc ) ;
PP . EnterSourceFile ( FID , /*DirLookup*/ 0 , NewLoc ) ;
2013-12-03 15:31:31 +01:00
m_Consumer - > getTransaction ( ) - > setBufferFID ( FID ) ;
2012-09-05 09:37:39 +00:00
Parser : : DeclGroupPtrTy ADecl ;
while ( ! m_Parser - > ParseTopLevelDecl ( ADecl ) ) {
// If we got a null return and something *was* parsed, ignore it. This
// is due to a top-level semicolon, an action override, or a parse error
// skipping something.
if ( ADecl )
2013-09-23 15:36:32 +02:00
m_Consumer - > HandleTopLevelDecl ( ADecl . get ( ) ) ;
2012-09-05 09:37:39 +00:00
} ;
// Process any TopLevelDecls generated by #pragma weak.
for ( llvm : : SmallVector < Decl * , 2 > : : iterator I = S . WeakTopLevelDecls ( ) . begin ( ) ,
E = S . WeakTopLevelDecls ( ) . end ( ) ; I ! = E ; + + I ) {
m_Consumer - > HandleTopLevelDecl ( DeclGroupRef ( * I ) ) ;
}
DiagnosticsEngine & Diag = S . getDiagnostics ( ) ;
if ( Diag . hasErrorOccurred ( ) )
return IncrementalParser : : kFailed ;
else if ( Diag . getNumWarnings ( ) )
return IncrementalParser : : kSuccessWithWarnings ;
return IncrementalParser : : kSuccess ;
}
2013-07-06 13:02:49 +02:00
void IncrementalParser : : printTransactionStructure ( ) const {
for ( size_t i = 0 , e = m_Transactions . size ( ) ; i < e ; + + i ) {
m_Transactions [ i ] - > printStructureBrief ( ) ;
}
}
2013-09-13 10:37:54 +02:00
bool IncrementalParser : : shouldIgnore ( const Decl * D ) const {
2014-01-14 19:05:35 +01:00
// This function is called for all "deserialized" decls, where the
// "deserialized" decl either really comes from an AST file or from
// a header that's loaded to import the AST for a library with a dictionary
// (the non-PCM case).
//
2013-08-26 17:59:33 +02:00
// Functions that are inlined must be sent to CodeGen - they will not have a
// symbol in the library.
if ( const FunctionDecl * FD = dyn_cast < FunctionDecl > ( D ) ) {
2014-01-14 19:05:35 +01:00
if ( D - > isFromASTFile ( ) ) {
2013-08-26 17:59:33 +02:00
return ! FD - > hasBody ( ) ;
2014-01-14 19:05:35 +01:00
} else {
// If the decl must be emitted then it will be in the library.
// If not, we must expose it to CodeGen now because it might
// not be in the library. Does this correspond to a weak symbol
// by definition?
return ! ( FD - > isInlined ( ) | | FD - > isTemplateInstantiation ( ) ) ;
}
2013-08-26 17:59:33 +02:00
}
// Don't codegen statics coming in from a module; they are already part of
// the library.
2014-02-14 10:17:21 +01:00
// We do need to expose static variables from template instantiations.
2013-08-26 17:59:33 +02:00
if ( const VarDecl * VD = dyn_cast < VarDecl > ( D ) )
2014-02-14 10:17:21 +01:00
if ( VD - > hasGlobalStorage ( ) & & ! VD - > getType ( ) . isConstQualified ( )
& & VD - > getTemplateSpecializationKind ( ) = = TSK_Undeclared )
2013-11-21 16:44:59 +01:00
return true ;
2013-08-26 17:59:33 +02:00
return false ;
}
2012-09-05 09:37:39 +00:00
} // namespace cling