Files
libreoffice/basic/source/comp/codegen.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

586 lines
19 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 15:18:56 +00:00
#include <basic/sberrors.hxx>
#include <basic/sbx.hxx>
#include <basic/sbmeth.hxx>
#include <basic/sbmod.hxx>
#include <image.hxx>
#include <codegen.hxx>
#include <parser.hxx>
#include <sbintern.hxx>
#include <cstddef>
#include <limits>
2011-01-29 17:12:46 +00:00
#include <algorithm>
#include <string_view>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/script/ModuleType.hpp>
2000-09-18 15:18:56 +00:00
2010-11-19 22:21:03 +01:00
// nInc is the increment size of the buffers
2000-09-18 15:18:56 +00:00
SbiCodeGen::SbiCodeGen(SbModule& r, SbiParser* p)
: pParser(p)
, rMod(r)
, nLine(0)
, nCol(0)
, nForLevel(0)
, bStmnt(false)
2000-09-18 15:18:56 +00:00
{
}
sal_uInt32 SbiCodeGen::GetPC() const
2000-09-18 15:18:56 +00:00
{
return aCode.GetSize();
}
2010-11-19 22:21:03 +01:00
// memorize the statement
2000-09-18 15:18:56 +00:00
void SbiCodeGen::Statement()
{
if( pParser->IsCodeCompleting() )
return;
bStmnt = true;
2000-09-18 15:18:56 +00:00
nLine = pParser->GetLine();
nCol = pParser->GetCol1();
2010-11-19 22:21:03 +01:00
// #29955 Store the information of the for-loop-layer
// in the upper Byte of the column
2000-09-18 15:18:56 +00:00
nCol = (nCol & 0xff) + 0x100 * nForLevel;
}
2010-11-19 22:21:03 +01:00
// Mark the beginning of a statement
2000-09-18 15:18:56 +00:00
void SbiCodeGen::GenStmnt()
{
if( pParser->IsCodeCompleting() )
return;
2000-09-18 15:18:56 +00:00
if( bStmnt )
{
bStmnt = false;
Gen( SbiOpcode::STMNT_, nLine, nCol );
2000-09-18 15:18:56 +00:00
}
}
2010-11-19 22:21:03 +01:00
// The Gen-Routines return the offset of the 1. operand,
// so that jumps can sink their backchain there.
2000-09-18 15:18:56 +00:00
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode )
2000-09-18 15:18:56 +00:00
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP0_START || eOpcode > SbiOpcode::SbOP0_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE1" );
2000-09-18 15:18:56 +00:00
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
2000-09-18 15:18:56 +00:00
return GetPC();
}
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd )
2000-09-18 15:18:56 +00:00
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP1_START || eOpcode > SbiOpcode::SbOP1_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE2" );
2000-09-18 15:18:56 +00:00
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
sal_uInt32 n = GetPC();
2000-09-18 15:18:56 +00:00
aCode += nOpnd;
return n;
}
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd1, sal_uInt32 nOpnd2 )
2000-09-18 15:18:56 +00:00
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP2_START || eOpcode > SbiOpcode::SbOP2_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE3" );
2000-09-18 15:18:56 +00:00
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
sal_uInt32 n = GetPC();
2000-09-18 15:18:56 +00:00
aCode += nOpnd1;
aCode += nOpnd2;
return n;
}
2010-11-19 22:21:03 +01:00
// Storing of the created image in the module
2000-09-18 15:18:56 +00:00
void SbiCodeGen::Save()
{
if( pParser->IsCodeCompleting() )
return;
std::unique_ptr<SbiImage> p(new SbiImage);
2000-09-18 15:18:56 +00:00
rMod.StartDefinitions();
2010-11-19 22:21:03 +01:00
// OPTION BASE-Value:
2000-09-18 15:18:56 +00:00
p->nDimBase = pParser->nBase;
2010-11-19 22:21:03 +01:00
// OPTION take over the EXPLICIT-Flag
2000-09-18 15:18:56 +00:00
if( pParser->bExplicit )
p->SetFlag( SbiImageFlags::EXPLICIT );
int nIfaceCount = 0;
if( rMod.mnType == css::script::ModuleType::CLASS )
{
rMod.bIsProxyModule = true;
p->SetFlag( SbiImageFlags::CLASSMODULE );
2012-01-15 16:15:08 +01:00
GetSbData()->pClassFac->AddClassModule( &rMod );
nIfaceCount = pParser->aIfaceVector.size();
if( !rMod.pClassData )
rMod.pClassData.reset(new SbClassData);
if( nIfaceCount )
{
for( int i = 0 ; i < nIfaceCount ; i++ )
{
const OUString& rIfaceName = pParser->aIfaceVector[i];
SbxVariable* pIfaceVar = new SbxVariable( SbxVARIANT );
pIfaceVar->SetName( rIfaceName );
SbxArray* pIfaces = rMod.pClassData->mxIfaces.get();
pIfaces->Insert( pIfaceVar, pIfaces->Count() );
}
}
rMod.pClassData->maRequiredTypes = pParser->aRequiredTypes;
}
else
{
2012-01-15 16:15:08 +01:00
GetSbData()->pClassFac->RemoveClassModule( &rMod );
// Only a ClassModule can revert to Normal
if ( rMod.mnType == css::script::ModuleType::CLASS )
{
rMod.mnType = css::script::ModuleType::NORMAL;
}
rMod.bIsProxyModule = false;
}
2000-09-18 15:18:56 +00:00
// GlobalCode-Flag
if( pParser->HasGlobalCode() )
{
p->SetFlag( SbiImageFlags::INITCODE );
}
// The entry points:
2000-09-18 15:18:56 +00:00
for( SbiSymDef* pDef = pParser->aPublics.First(); pDef;
pDef = pParser->aPublics.Next() )
2000-09-18 15:18:56 +00:00
{
SbiProcDef* pProc = pDef->GetProcDef();
if( pProc && pProc->IsDefined() )
2000-09-18 15:18:56 +00:00
{
OUString aProcName = pProc->GetName();
OUStringBuffer aIfaceProcName;
OUString aIfaceName;
sal_uInt16 nPassCount = 1;
if( nIfaceCount )
{
int nPropPrefixFound = aProcName.indexOf("Property ");
OUString aPureProcName = aProcName;
OUString aPropPrefix;
if( nPropPrefixFound == 0 )
{
aPropPrefix = aProcName.copy( 0, 13 ); // 13 == Len( "Property ?et " )
aPureProcName = aProcName.copy( 13 );
}
for( int i = 0 ; i < nIfaceCount ; i++ )
{
const OUString& rIfaceName = pParser->aIfaceVector[i];
int nFound = aPureProcName.indexOf( rIfaceName );
if( nFound == 0 && aPureProcName[rIfaceName.getLength()] == '_' )
{
if( nPropPrefixFound == 0 )
{
aIfaceProcName.append(aPropPrefix);
}
aIfaceProcName.append(aPureProcName.subView(rIfaceName.getLength() + 1) );
aIfaceName = rIfaceName;
nPassCount = 2;
break;
}
}
}
SbMethod* pMeth = nullptr;
for( sal_uInt16 nPass = 0 ; nPass < nPassCount ; nPass++ )
{
if( nPass == 1 )
{
aProcName = aIfaceProcName.toString();
}
PropertyMode ePropMode = pProc->getPropertyMode();
if( ePropMode != PropertyMode::NONE )
{
SbxDataType ePropType = SbxEMPTY;
switch( ePropMode )
{
case PropertyMode::Get:
ePropType = pProc->GetType();
break;
case PropertyMode::Let:
{
// type == type of first parameter
ePropType = SbxVARIANT; // Default
SbiSymPool* pPool = &pProc->GetParams();
if( pPool->GetSize() > 1 )
{
SbiSymDef* pPar = pPool->Get( 1 );
if( pPar )
{
ePropType = pPar->GetType();
}
}
break;
}
case PropertyMode::Set:
ePropType = SbxOBJECT;
break;
default:
OSL_FAIL("Illegal PropertyMode");
break;
}
OUString aPropName = pProc->GetPropName();
if( nPass == 1 )
{
aPropName = aPropName.copy( aIfaceName.getLength() + 1 );
}
2011-03-21 09:32:19 +00:00
rMod.GetProcedureProperty( aPropName, ePropType );
}
if( nPass == 1 )
{
2011-03-21 09:32:19 +00:00
rMod.GetIfaceMapperMethod( aProcName, pMeth );
}
else
{
pMeth = rMod.GetMethod( aProcName, pProc->GetType() );
if( !pProc->IsPublic() )
{
pMeth->SetFlag( SbxFlagBits::Private );
}
2010-08-06 09:35:51 +02:00
// Declare? -> Hidden
if( !pProc->GetLib().isEmpty())
{
pMeth->SetFlag( SbxFlagBits::Hidden );
}
pMeth->nStart = pProc->GetAddr();
pMeth->nLine1 = pProc->GetLine1();
pMeth->nLine2 = pProc->GetLine2();
2010-11-19 22:21:03 +01:00
// The parameter:
SbxInfo* pInfo = pMeth->GetInfo();
OUString aHelpFile, aComment;
sal_uInt32 nHelpId = 0;
if( pInfo )
{
2010-11-19 22:21:03 +01:00
// Rescue the additional data
aHelpFile = pInfo->GetHelpFile();
aComment = pInfo->GetComment();
nHelpId = pInfo->GetHelpId();
}
2010-11-19 22:21:03 +01:00
// And reestablish the parameter list
pInfo = new SbxInfo( aHelpFile, nHelpId );
pInfo->SetComment( aComment );
SbiSymPool* pPool = &pProc->GetParams();
2010-11-19 22:21:03 +01:00
// The first element is always the value of the function!
for( sal_uInt16 i = 1; i < pPool->GetSize(); i++ )
{
SbiSymDef* pPar = pPool->Get( i );
SbxDataType t = pPar->GetType();
if( !pPar->IsByVal() )
{
t = static_cast<SbxDataType>( t | SbxBYREF );
}
if( pPar->GetDims() )
{
t = static_cast<SbxDataType>( t | SbxARRAY );
}
2010-11-19 22:21:03 +01:00
// #33677 hand-over an Optional-Info
SbxFlagBits nFlags = SbxFlagBits::Read;
if( pPar->IsOptional() )
{
nFlags |= SbxFlagBits::Optional;
}
pInfo->AddParam( pPar->GetName(), t, nFlags );
sal_uInt32 nUserData = 0;
sal_uInt16 nDefaultId = pPar->GetDefaultId();
if( nDefaultId )
{
nUserData |= nDefaultId;
}
if( pPar->IsParamArray() )
{
nUserData |= PARAM_INFO_PARAMARRAY;
}
if( pPar->IsWithBrackets() )
{
nUserData |= PARAM_INFO_WITHBRACKETS;
}
SbxParamInfo* pParam = nullptr;
if( nUserData )
{
pParam = const_cast<SbxParamInfo*>(pInfo->GetParam( i ));
}
if( pParam )
{
pParam->nUserData = nUserData;
}
}
pMeth->SetInfo( pInfo );
}
} // for( iPass...
2000-09-18 15:18:56 +00:00
}
}
if (aCode.GetErrCode())
{
pParser->Error(aCode.GetErrCode(), aCode.GetErrMessage());
}
2010-11-19 22:21:03 +01:00
// The code
p->AddCode(aCode.GetBuffer());
2000-09-18 15:18:56 +00:00
2010-11-19 22:21:03 +01:00
// The global StringPool. 0 is not occupied.
2000-09-18 15:18:56 +00:00
SbiStringPool* pPool = &pParser->aGblStrings;
sal_uInt16 nSize = pPool->GetSize();
2000-09-18 15:18:56 +00:00
p->MakeStrings( nSize );
sal_uInt32 i;
2000-09-18 15:18:56 +00:00
for( i = 1; i <= nSize; i++ )
{
2000-09-18 15:18:56 +00:00
p->AddString( pPool->Find( i ) );
}
2010-11-19 22:21:03 +01:00
// Insert types
sal_uInt32 nCount = pParser->rTypeArray->Count();
2000-09-18 15:18:56 +00:00
for (i = 0; i < nCount; i++)
{
p->AddType(static_cast<SbxObject *>(pParser->rTypeArray->Get(i)));
}
// Insert enum objects
nCount = pParser->rEnumArray->Count();
for (i = 0; i < nCount; i++)
{
p->AddEnum(static_cast<SbxObject *>(pParser->rEnumArray->Get(i)));
}
2000-09-18 15:18:56 +00:00
if( !p->IsError() )
{
rMod.pImage = std::move(p);
}
2000-09-18 15:18:56 +00:00
rMod.EndDefinitions();
}
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
namespace {
template < class T >
class PCodeVisitor
{
public:
2006-11-03 14:11:10 +00:00
virtual ~PCodeVisitor();
virtual void start( const sal_uInt8* pStart ) = 0;
2006-11-03 14:11:10 +00:00
virtual void processOpCode0( SbiOpcode eOp ) = 0;
virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) = 0;
virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) = 0;
virtual bool processParams() = 0;
};
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
}
2006-11-03 14:11:10 +00:00
template <class T> PCodeVisitor< T >::~PCodeVisitor()
{}
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
namespace {
template <class T>
class PCodeBufferWalker
{
private:
T m_nBytes;
const sal_uInt8* m_pCode;
static T readParam( sal_uInt8 const *& pCode )
{
T nOp1=0;
for ( std::size_t i=0; i<sizeof( T ); ++i )
nOp1 |= *pCode++ << ( i * 8);
return nOp1;
}
public:
PCodeBufferWalker( const sal_uInt8* pCode, T nBytes ): m_nBytes( nBytes ), m_pCode( pCode )
{
}
void visitBuffer( PCodeVisitor< T >& visitor )
{
const sal_uInt8* pCode = m_pCode;
if ( !pCode )
return;
const sal_uInt8* pEnd = pCode + m_nBytes;
visitor.start( m_pCode );
T nOp1 = 0, nOp2 = 0;
for( ; pCode < pEnd; )
{
SbiOpcode eOp = static_cast<SbiOpcode>(*pCode++);
if ( eOp <= SbiOpcode::SbOP0_END )
visitor.processOpCode0( eOp );
else if( eOp >= SbiOpcode::SbOP1_START && eOp <= SbiOpcode::SbOP1_END )
{
if ( visitor.processParams() )
nOp1 = readParam( pCode );
else
pCode += sizeof( T );
visitor.processOpCode1( eOp, nOp1 );
}
else if( eOp >= SbiOpcode::SbOP2_START && eOp <= SbiOpcode::SbOP2_END )
{
if ( visitor.processParams() )
{
nOp1 = readParam( pCode );
nOp2 = readParam( pCode );
}
else
pCode += ( sizeof( T ) * 2 );
visitor.processOpCode2( eOp, nOp1, nOp2 );
}
}
}
};
template < class T, class S >
class OffSetAccumulator : public PCodeVisitor< T >
{
T m_nNumOp0;
T m_nNumSingleParams;
T m_nNumDoubleParams;
public:
OffSetAccumulator() : m_nNumOp0(0), m_nNumSingleParams(0), m_nNumDoubleParams(0){}
virtual void start( const sal_uInt8* /*pStart*/ ) override {}
virtual void processOpCode0( SbiOpcode /*eOp*/ ) override { ++m_nNumOp0; }
virtual void processOpCode1( SbiOpcode /*eOp*/, T /*nOp1*/ ) override { ++m_nNumSingleParams; }
virtual void processOpCode2( SbiOpcode /*eOp*/, T /*nOp1*/, T /*nOp2*/ ) override { ++m_nNumDoubleParams; }
S offset()
{
typedef decltype(T(1) + S(1)) larger_t; // type capable to hold both value ranges of T and S
T result = 0 ;
static const S max = std::numeric_limits< S >::max();
result = m_nNumOp0 + ( ( sizeof(S) + 1 ) * m_nNumSingleParams ) + ( (( sizeof(S) * 2 )+ 1 ) * m_nNumDoubleParams );
return std::min<larger_t>(max, result);
}
virtual bool processParams() override { return false; }
};
template < class T, class S >
class BufferTransformer : public PCodeVisitor< T >
{
const sal_uInt8* m_pStart;
SbiBuffer m_ConvertedBuf;
public:
BufferTransformer():m_pStart(nullptr) {}
virtual void start( const sal_uInt8* pStart ) override { m_pStart = pStart; }
virtual void processOpCode0( SbiOpcode eOp ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
}
virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
switch( eOp )
{
case SbiOpcode::JUMP_:
case SbiOpcode::JUMPT_:
case SbiOpcode::JUMPF_:
case SbiOpcode::GOSUB_:
case SbiOpcode::CASEIS_:
case SbiOpcode::RETURN_:
case SbiOpcode::ERRHDL_:
case SbiOpcode::TESTFOR_:
2006-11-03 14:11:10 +00:00
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
break;
case SbiOpcode::RESUME_:
if ( nOp1 > 1 )
2006-11-03 14:11:10 +00:00
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
break;
default:
break;
}
m_ConvertedBuf += static_cast<S>(nOp1);
}
virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
if ( eOp == SbiOpcode::CASEIS_ && nOp1 )
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
m_ConvertedBuf += static_cast<S>(nOp1);
m_ConvertedBuf += static_cast<S>(nOp2);
}
virtual bool processParams() override { return true; }
// yeuch, careful here, you can only call
// GetBuffer on the returned SbiBuffer once, also
// you (as the caller) get to own the memory
SbiBuffer& buffer()
{
return m_ConvertedBuf;
}
static S convertBufferOffSet( const sal_uInt8* pStart, T nOp1 )
{
PCodeBufferWalker< T > aBuff( pStart, nOp1);
OffSetAccumulator< T, S > aVisitor;
aBuff.visitBuffer( aVisitor );
return aVisitor.offset();
}
};
Extend loplugin:external to warn about classes ...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend loplugin:external to warn about enums". Cases where free functions were moved into an unnamed namespace along with a class, to not break ADL, are in: filter/source/svg/svgexport.cxx sc/source/filter/excel/xelink.cxx sc/source/filter/excel/xilink.cxx svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx All other free functions mentioning moved classes appear to be harmless and not give rise to (silent, even) ADL breakage. (One remaining TODO in compilerplugins/clang/external.cxx is that derived classes are not covered by computeAffectedTypes, even though they could also be affected by ADL-breakage--- but don't seem to be in any acutal case across the code base.) For friend declarations using elaborate type specifiers, like class C1 {}; class C2 { friend class C1; }; * If C2 (but not C1) is moved into an unnamed namespace, the friend declaration must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.") * If C1 (but not C2) is moved into an unnamed namespace, the friend declaration must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882> "elaborated-type-specifier friend not looked up in unnamed namespace". Apart from that, to keep changes simple and mostly mechanical (which should help avoid regressions), out-of-line definitions of class members have been left in the enclosing (named) namespace. But explicit specializations of class templates had to be moved into the unnamed namespace to appease <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of template from unnamed namespace using unqualified-id in enclosing namespace". Also, accompanying declarations (of e.g. typedefs or static variables) that could arguably be moved into the unnamed namespace too have been left alone. And in some cases, mention of affected types in blacklists in other loplugins needed to be adapted. And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is not moved into an unnamed namespace (because it is declared in sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler doesn’t give this warning for types defined in the main .C file, as those are unlikely to have multiple definitions." (<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The warned-about classes also don't have multiple definitions in the given test, so disable the warning when including the .cxx. Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4 Reviewed-on: https://gerrit.libreoffice.org/83239 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
}
sal_uInt32
SbiCodeGen::calcNewOffSet( sal_uInt8 const * pCode, sal_uInt16 nOffset )
{
return BufferTransformer< sal_uInt16, sal_uInt32 >::convertBufferOffSet( pCode, nOffset );
}
sal_uInt16
SbiCodeGen::calcLegacyOffSet( sal_uInt8 const * pCode, sal_uInt32 nOffset )
{
return BufferTransformer< sal_uInt32, sal_uInt16 >::convertBufferOffSet( pCode, nOffset );
}
template <class T, class S>
void
PCodeBuffConvertor<T,S>::convert()
{
PCodeBufferWalker< T > aBuf( m_pStart, m_nSize );
BufferTransformer< T, S > aTrnsfrmer;
aBuf.visitBuffer( aTrnsfrmer );
// TODO: handle buffer errors
m_aCnvtdBuf = aTrnsfrmer.buffer().GetBuffer();
}
template class PCodeBuffConvertor< sal_uInt16, sal_uInt32 >;
template class PCodeBuffConvertor< sal_uInt32, sal_uInt16 >;
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */