2012-01-01 21:55:05 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-06-21 16:41:22 +02:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2012-01-01 21:55:05 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef templatesimplifierH
|
|
|
|
#define templatesimplifierH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <ctime>
|
2012-01-02 22:05:27 +01:00
|
|
|
#include <list>
|
2018-11-23 11:36:09 +01:00
|
|
|
#include <map>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <set>
|
2012-01-02 21:53:13 +01:00
|
|
|
#include <string>
|
2019-05-25 10:04:01 +02:00
|
|
|
#include <unordered_map>
|
2012-01-03 22:35:06 +01:00
|
|
|
#include <vector>
|
2012-01-01 21:55:05 +01:00
|
|
|
|
2012-05-06 10:38:55 +02:00
|
|
|
class ErrorLogger;
|
2012-01-09 20:33:11 +01:00
|
|
|
class Settings;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Token;
|
2018-09-01 11:26:10 +02:00
|
|
|
class Tokenizer;
|
2017-05-27 04:33:47 +02:00
|
|
|
class TokenList;
|
2023-04-28 08:25:52 +02:00
|
|
|
struct newInstantiation;
|
2012-01-01 21:55:05 +01:00
|
|
|
|
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/** @brief Simplify templates from the preprocessed and partially simplified code. */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB TemplateSimplifier {
|
2021-03-16 09:31:52 +01:00
|
|
|
friend class TestSimplifyTemplate;
|
|
|
|
|
2012-04-23 20:45:36 +02:00
|
|
|
public:
|
2023-03-07 12:12:31 +01:00
|
|
|
explicit TemplateSimplifier(Tokenizer &tokenizer);
|
2012-01-01 21:55:05 +01:00
|
|
|
|
2023-03-08 18:59:34 +01:00
|
|
|
std::string dump() const {
|
|
|
|
return mDump;
|
|
|
|
}
|
|
|
|
|
2012-01-02 21:37:32 +01:00
|
|
|
/**
|
|
|
|
*/
|
2018-09-01 11:26:10 +02:00
|
|
|
void checkComplicatedSyntaxErrorsInTemplates();
|
2012-01-02 21:43:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* is the token pointing at a template parameters block
|
|
|
|
* < int , 3 > => yes
|
|
|
|
* \param tok start token that must point at "<"
|
|
|
|
* \return number of parameters (invalid parameters => 0)
|
|
|
|
*/
|
|
|
|
static unsigned int templateParameters(const Token *tok);
|
2012-01-02 21:46:43 +01:00
|
|
|
|
2017-12-26 10:55:18 +01:00
|
|
|
/**
|
|
|
|
* Token and its full scopename
|
|
|
|
*/
|
2019-08-10 08:42:12 +02:00
|
|
|
class TokenAndName {
|
|
|
|
Token *mToken;
|
|
|
|
std::string mScope;
|
|
|
|
std::string mName;
|
|
|
|
std::string mFullName;
|
|
|
|
const Token *mNameToken;
|
|
|
|
const Token *mParamEnd;
|
|
|
|
unsigned int mFlags;
|
2019-01-07 06:55:22 +01:00
|
|
|
|
|
|
|
enum {
|
2019-01-27 07:46:27 +01:00
|
|
|
fIsClass = (1 << 0), // class template
|
|
|
|
fIsFunction = (1 << 1), // function template
|
|
|
|
fIsVariable = (1 << 2), // variable template
|
|
|
|
fIsAlias = (1 << 3), // alias template
|
|
|
|
fIsSpecialization = (1 << 4), // user specialized template
|
|
|
|
fIsPartialSpecialization = (1 << 5), // user partial specialized template
|
|
|
|
fIsForwardDeclaration = (1 << 6), // forward declaration
|
2019-05-23 20:53:26 +02:00
|
|
|
fIsVariadic = (1 << 7), // variadic template
|
2019-09-01 09:56:33 +02:00
|
|
|
fIsFriend = (1 << 8), // friend template
|
2019-08-10 08:42:12 +02:00
|
|
|
fFamilyMask = (fIsClass | fIsFunction | fIsVariable)
|
2019-01-07 06:55:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void isClass(bool state) {
|
|
|
|
setFlag(fIsClass, state);
|
|
|
|
}
|
|
|
|
void isFunction(bool state) {
|
|
|
|
setFlag(fIsFunction, state);
|
|
|
|
}
|
|
|
|
void isVariable(bool state) {
|
|
|
|
setFlag(fIsVariable, state);
|
|
|
|
}
|
|
|
|
void isAlias(bool state) {
|
|
|
|
setFlag(fIsAlias, state);
|
|
|
|
}
|
2019-01-27 07:46:27 +01:00
|
|
|
void isSpecialization(bool state) {
|
|
|
|
setFlag(fIsSpecialization, state);
|
|
|
|
}
|
|
|
|
void isPartialSpecialization(bool state) {
|
|
|
|
setFlag(fIsPartialSpecialization, state);
|
2019-01-07 06:55:22 +01:00
|
|
|
}
|
2019-01-18 21:12:39 +01:00
|
|
|
void isForwardDeclaration(bool state) {
|
|
|
|
setFlag(fIsForwardDeclaration, state);
|
|
|
|
}
|
2019-05-23 20:53:26 +02:00
|
|
|
void isVariadic(bool state) {
|
|
|
|
setFlag(fIsVariadic, state);
|
|
|
|
}
|
2019-09-01 09:56:33 +02:00
|
|
|
void isFriend(bool state) {
|
|
|
|
setFlag(fIsFriend, state);
|
|
|
|
}
|
2019-05-23 20:53:26 +02:00
|
|
|
|
2019-01-07 06:55:22 +01:00
|
|
|
/**
|
|
|
|
* Get specified flag state.
|
|
|
|
* @param flag flag to get state of
|
|
|
|
* @return true if flag set or false in flag not set
|
|
|
|
*/
|
|
|
|
bool getFlag(unsigned int flag) const {
|
2019-08-10 08:42:12 +02:00
|
|
|
return ((mFlags & flag) != 0);
|
2019-01-07 06:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set specified flag state.
|
|
|
|
* @param flag flag to set state
|
|
|
|
* @param state new state of flag
|
|
|
|
*/
|
|
|
|
void setFlag(unsigned int flag, bool state) {
|
2019-08-10 08:42:12 +02:00
|
|
|
mFlags = state ? mFlags | flag : mFlags & ~flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor used for instantiations.
|
2020-12-27 09:15:59 +01:00
|
|
|
* \param token template instantiation name token "name<...>"
|
|
|
|
* \param scope full qualification of template(scope)
|
2019-08-10 08:42:12 +02:00
|
|
|
*/
|
2022-07-28 22:51:45 +02:00
|
|
|
TokenAndName(Token *token, std::string scope);
|
2019-08-10 08:42:12 +02:00
|
|
|
/**
|
|
|
|
* Constructor used for declarations.
|
2020-12-27 09:15:59 +01:00
|
|
|
* \param token template declaration token "template < ... >"
|
|
|
|
* \param scope full qualification of template(scope)
|
|
|
|
* \param nameToken template name token "template < ... > class name"
|
|
|
|
* \param paramEnd template parameter end token ">"
|
2019-08-10 08:42:12 +02:00
|
|
|
*/
|
2022-07-28 22:51:45 +02:00
|
|
|
TokenAndName(Token *token, std::string scope, const Token *nameToken, const Token *paramEnd);
|
2019-08-10 08:42:12 +02:00
|
|
|
TokenAndName(const TokenAndName& other);
|
2023-03-07 12:24:01 +01:00
|
|
|
TokenAndName(TokenAndName&& other) NOEXCEPT;
|
2019-08-10 08:42:12 +02:00
|
|
|
~TokenAndName();
|
|
|
|
|
|
|
|
bool operator == (const TokenAndName & rhs) const {
|
|
|
|
return mToken == rhs.mToken && mScope == rhs.mScope && mName == rhs.mName && mFullName == rhs.mFullName &&
|
|
|
|
mNameToken == rhs.mNameToken && mParamEnd == rhs.mParamEnd && mFlags == rhs.mFlags;
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:59:34 +01:00
|
|
|
std::string dump(const std::vector<std::string>& fileNames) const;
|
|
|
|
|
2023-02-24 21:22:08 +01:00
|
|
|
// TODO: do not return non-const pointer from const object
|
2019-08-10 08:42:12 +02:00
|
|
|
Token * token() const {
|
|
|
|
return mToken;
|
|
|
|
}
|
|
|
|
void token(Token * token) {
|
|
|
|
mToken = token;
|
|
|
|
}
|
|
|
|
const std::string & scope() const {
|
|
|
|
return mScope;
|
|
|
|
}
|
|
|
|
const std::string & name() const {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
const std::string & fullName() const {
|
|
|
|
return mFullName;
|
|
|
|
}
|
2021-08-07 20:51:18 +02:00
|
|
|
const Token * nameToken() const {
|
2019-08-10 08:42:12 +02:00
|
|
|
return mNameToken;
|
|
|
|
}
|
|
|
|
const Token * paramEnd() const {
|
|
|
|
return mParamEnd;
|
|
|
|
}
|
|
|
|
void paramEnd(const Token *end) {
|
|
|
|
mParamEnd = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClass() const {
|
|
|
|
return getFlag(fIsClass);
|
|
|
|
}
|
|
|
|
bool isFunction() const {
|
|
|
|
return getFlag(fIsFunction);
|
|
|
|
}
|
|
|
|
bool isVariable() const {
|
|
|
|
return getFlag(fIsVariable);
|
|
|
|
}
|
|
|
|
bool isAlias() const {
|
|
|
|
return getFlag(fIsAlias);
|
|
|
|
}
|
|
|
|
bool isSpecialization() const {
|
|
|
|
return getFlag(fIsSpecialization);
|
|
|
|
}
|
|
|
|
bool isPartialSpecialization() const {
|
|
|
|
return getFlag(fIsPartialSpecialization);
|
|
|
|
}
|
|
|
|
bool isForwardDeclaration() const {
|
|
|
|
return getFlag(fIsForwardDeclaration);
|
|
|
|
}
|
|
|
|
bool isVariadic() const {
|
|
|
|
return getFlag(fIsVariadic);
|
2019-01-07 06:55:22 +01:00
|
|
|
}
|
2019-09-01 09:56:33 +02:00
|
|
|
bool isFriend() const {
|
|
|
|
return getFlag(fIsFriend);
|
|
|
|
}
|
2019-06-01 10:52:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get alias start token.
|
|
|
|
* template < ... > using X = foo < ... >;
|
|
|
|
* ^
|
|
|
|
* @return alias start token
|
|
|
|
*/
|
|
|
|
const Token * aliasStartToken() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get alias end token.
|
|
|
|
* template < ... > using X = foo < ... >;
|
|
|
|
* ^
|
|
|
|
* @return alias end token
|
|
|
|
*/
|
|
|
|
const Token * aliasEndToken() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is token an alias token?
|
|
|
|
* template < ... > using X = foo < ... >;
|
|
|
|
* ^
|
|
|
|
* @param tok token to check
|
|
|
|
* @return true if alias token, false if not
|
|
|
|
*/
|
|
|
|
bool isAliasToken(const Token *tok) const;
|
2019-06-07 08:22:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is declaration the same family (class, function or variable).
|
|
|
|
*
|
|
|
|
* @param decl declaration to compare to
|
|
|
|
* @return true if same family, false if different family
|
|
|
|
*/
|
|
|
|
bool isSameFamily(const TemplateSimplifier::TokenAndName &decl) const {
|
2019-08-10 08:42:12 +02:00
|
|
|
// Make sure a family flag is set and matches.
|
|
|
|
// This works because at most only one flag will be set.
|
2022-07-26 11:10:03 +02:00
|
|
|
return ((mFlags & fFamilyMask) && (decl.mFlags & fFamilyMask));
|
2019-06-07 08:22:34 +02:00
|
|
|
}
|
2017-12-26 10:55:18 +01:00
|
|
|
};
|
|
|
|
|
2019-07-07 18:33:33 +02:00
|
|
|
/**
|
|
|
|
* Find last token of a template declaration.
|
|
|
|
* @param tok start token of declaration "template" or token after "template < ... >"
|
|
|
|
* @return last token of declaration or nullptr if syntax error
|
|
|
|
*/
|
|
|
|
static Token *findTemplateDeclarationEnd(Token *tok);
|
|
|
|
static const Token *findTemplateDeclarationEnd(const Token *tok);
|
|
|
|
|
2012-01-02 22:05:27 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Match template declaration/instantiation
|
|
|
|
* @param instance template instantiation
|
|
|
|
* @param numberOfArguments number of template arguments
|
2021-04-17 21:56:13 +02:00
|
|
|
* @param variadic last template argument is variadic
|
2018-09-01 11:26:10 +02:00
|
|
|
* @param patternAfter pattern that must match the tokens after the ">"
|
|
|
|
* @return match => true
|
2012-01-02 22:05:27 +01:00
|
|
|
*/
|
2021-04-17 21:56:13 +02:00
|
|
|
static bool instantiateMatch(const Token *instance, const std::size_t numberOfArguments, bool variadic, const char patternAfter[]);
|
2012-01-02 22:05:27 +01:00
|
|
|
|
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Match template declaration/instantiation
|
|
|
|
* @param tok The ">" token e.g. before "class"
|
|
|
|
* @return -1 to bail out or positive integer to identity the position
|
|
|
|
* of the template name.
|
2012-01-02 22:05:27 +01:00
|
|
|
*/
|
2019-05-25 10:04:01 +02:00
|
|
|
int getTemplateNamePosition(const Token *tok);
|
2018-12-22 10:05:10 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-09 08:11:59 +02:00
|
|
|
* Get class template name position
|
2018-12-22 10:05:10 +01:00
|
|
|
* @param tok The ">" token e.g. before "class"
|
|
|
|
* @param namepos return offset to name
|
|
|
|
* @return true if name found, false if not
|
|
|
|
* */
|
2019-06-09 08:11:59 +02:00
|
|
|
static bool getTemplateNamePositionTemplateClass(const Token *tok, int &namepos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get function template name position
|
|
|
|
* @param tok The ">" token
|
|
|
|
* @param namepos return offset to name
|
|
|
|
* @return true if name found, false if not
|
|
|
|
* */
|
2019-01-07 06:55:22 +01:00
|
|
|
static bool getTemplateNamePositionTemplateFunction(const Token *tok, int &namepos);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-01-18 21:12:39 +01:00
|
|
|
/**
|
|
|
|
* Get variable template name position
|
|
|
|
* @param tok The ">" token
|
|
|
|
* @param namepos return offset to name
|
|
|
|
* @return true if name found, false if not
|
|
|
|
* */
|
|
|
|
static bool getTemplateNamePositionTemplateVariable(const Token *tok, int &namepos);
|
|
|
|
|
2012-01-03 22:35:06 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Simplify templates
|
|
|
|
* @param maxtime time when the simplification should be stopped
|
2012-01-03 22:35:06 +01:00
|
|
|
*/
|
2023-09-20 14:45:44 +02:00
|
|
|
void simplifyTemplates(const std::time_t maxtime);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2017-12-29 22:47:07 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Simplify constant calculations such as "1+2" => "3"
|
|
|
|
* @param tok start token
|
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
2017-12-29 22:47:07 +01:00
|
|
|
*/
|
2019-07-18 14:55:30 +02:00
|
|
|
static bool simplifyNumericCalculations(Token *tok, bool isTemplate = true);
|
2017-12-29 22:47:07 +01:00
|
|
|
|
2012-01-03 22:35:06 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Simplify constant calculations such as "1+2" => "3".
|
|
|
|
* This also performs simple cleanup of parentheses etc.
|
|
|
|
* @return true if modifications to token-list are done.
|
|
|
|
* false if no modifications are done.
|
2012-01-03 22:35:06 +01:00
|
|
|
*/
|
2023-04-06 18:46:45 +02:00
|
|
|
bool simplifyCalculations(Token* frontToken = nullptr, const Token *backToken = nullptr, bool isTemplate = true);
|
2012-01-03 22:35:06 +01:00
|
|
|
|
2019-03-03 07:40:55 +01:00
|
|
|
/** Simplify template instantiation arguments.
|
|
|
|
* @param start first token of arguments
|
|
|
|
* @param end token following last argument token
|
|
|
|
*/
|
2023-04-28 08:25:52 +02:00
|
|
|
void simplifyTemplateArgs(Token *start, const Token *end, std::vector<newInstantiation>* newInst = nullptr);
|
2019-03-03 07:40:55 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
private:
|
2012-01-03 22:49:50 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Get template declarations
|
2018-11-10 16:41:14 +01:00
|
|
|
* @return true if code has templates.
|
2012-01-03 22:49:50 +01:00
|
|
|
*/
|
2018-11-10 16:41:14 +01:00
|
|
|
bool getTemplateDeclarations();
|
2012-01-05 21:45:19 +01:00
|
|
|
|
2019-03-03 07:40:55 +01:00
|
|
|
/** Add template instantiation.
|
|
|
|
* @param token first token of instantiation
|
|
|
|
* @param scope scope of instantiation
|
|
|
|
*/
|
|
|
|
void addInstantiation(Token *token, const std::string &scope);
|
|
|
|
|
2017-12-26 22:34:39 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* Get template instantiations
|
2017-12-26 22:34:39 +01:00
|
|
|
*/
|
2018-09-01 11:26:10 +02:00
|
|
|
void getTemplateInstantiations();
|
2012-01-09 20:33:11 +01:00
|
|
|
|
2018-10-24 14:38:59 +02:00
|
|
|
/**
|
|
|
|
* Fix forward declared default argument values by copying them
|
|
|
|
* when they are not present in the declaration.
|
|
|
|
*/
|
|
|
|
void fixForwardDeclaredDefaultArgumentValues();
|
|
|
|
|
2014-11-01 22:07:24 +01:00
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* simplify template instantiations (use default argument values)
|
2014-11-01 22:07:24 +01:00
|
|
|
*/
|
2018-09-01 11:26:10 +02:00
|
|
|
void useDefaultArgumentValues();
|
|
|
|
|
2019-05-19 19:19:57 +02:00
|
|
|
/**
|
|
|
|
* simplify template instantiations (use default argument values)
|
2019-06-30 09:17:42 +02:00
|
|
|
* @param declaration template declaration or forward declaration
|
2019-05-19 19:19:57 +02:00
|
|
|
*/
|
2019-05-28 21:32:37 +02:00
|
|
|
void useDefaultArgumentValues(TokenAndName &declaration);
|
2019-05-19 19:19:57 +02:00
|
|
|
|
2018-12-29 11:19:53 +01:00
|
|
|
/**
|
|
|
|
* Try to locate a matching declaration for each user defined
|
|
|
|
* specialization.
|
|
|
|
*/
|
2019-01-27 07:46:27 +01:00
|
|
|
void getSpecializations();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to locate a matching declaration for each user defined
|
|
|
|
* partial specialization.
|
|
|
|
*/
|
|
|
|
void getPartialSpecializations();
|
2018-12-29 11:19:53 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
/**
|
|
|
|
* simplify template aliases
|
|
|
|
*/
|
|
|
|
void simplifyTemplateAliases();
|
2014-11-01 22:07:24 +01:00
|
|
|
|
2012-01-09 20:33:11 +01:00
|
|
|
/**
|
2013-02-10 07:43:09 +01:00
|
|
|
* Simplify templates : expand all instantiations for a template
|
2012-01-09 20:33:11 +01:00
|
|
|
* @todo It seems that inner templates should be instantiated recursively
|
2017-12-26 10:55:18 +01:00
|
|
|
* @param templateDeclaration template declaration
|
2017-12-30 22:14:48 +01:00
|
|
|
* @param specializations template specializations (list each template name token)
|
2016-02-12 12:05:32 +01:00
|
|
|
* @param maxtime time when the simplification will stop
|
2012-01-09 20:33:11 +01:00
|
|
|
* @param expandedtemplates all templates that has been expanded so far. The full names are stored.
|
2012-07-29 16:01:05 +02:00
|
|
|
* @return true if the template was instantiated
|
2012-01-09 20:33:11 +01:00
|
|
|
*/
|
2018-09-01 11:26:10 +02:00
|
|
|
bool simplifyTemplateInstantiations(
|
2017-12-26 10:55:18 +01:00
|
|
|
const TokenAndName &templateDeclaration,
|
2017-12-30 22:14:48 +01:00
|
|
|
const std::list<const Token *> &specializations,
|
2016-02-12 12:05:32 +01:00
|
|
|
const std::time_t maxtime,
|
2012-01-09 20:33:11 +01:00
|
|
|
std::set<std::string> &expandedtemplates);
|
|
|
|
|
2018-11-05 19:55:21 +01:00
|
|
|
/**
|
|
|
|
* Simplify templates : add namespace to template name
|
|
|
|
* @param templateDeclaration template declaration
|
|
|
|
* @param tok place to insert namespace
|
|
|
|
*/
|
|
|
|
void addNamespace(const TokenAndName &templateDeclaration, const Token *tok);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify templates : check if namespace already present
|
|
|
|
* @param templateDeclaration template declaration
|
|
|
|
* @param tok place to start looking for namespace
|
|
|
|
* @return true if namespace already present
|
|
|
|
*/
|
2019-01-07 06:55:22 +01:00
|
|
|
static bool alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok);
|
2018-11-05 19:55:21 +01:00
|
|
|
|
2018-09-01 11:26:10 +02:00
|
|
|
/**
|
|
|
|
* Expand a template. Create "expanded" class/function at end of tokenlist.
|
2018-10-26 14:20:13 +02:00
|
|
|
* @param templateDeclaration Template declaration information
|
|
|
|
* @param templateInstantiation Full name of template
|
2018-09-01 11:26:10 +02:00
|
|
|
* @param typeParametersInDeclaration The type parameters of the template
|
|
|
|
* @param newName New name of class/function.
|
2018-10-14 16:57:07 +02:00
|
|
|
* @param copy copy or expand in place
|
2018-09-01 11:26:10 +02:00
|
|
|
*/
|
|
|
|
void expandTemplate(
|
2018-10-26 14:20:13 +02:00
|
|
|
const TokenAndName &templateDeclaration,
|
|
|
|
const TokenAndName &templateInstantiation,
|
2018-09-01 11:26:10 +02:00
|
|
|
const std::vector<const Token *> &typeParametersInDeclaration,
|
|
|
|
const std::string &newName,
|
2018-10-14 16:57:07 +02:00
|
|
|
bool copy);
|
2018-09-01 11:26:10 +02:00
|
|
|
|
2017-06-08 00:16:43 +02:00
|
|
|
/**
|
|
|
|
* Replace all matching template usages 'Foo < int >' => 'Foo<int>'
|
2019-01-14 20:36:23 +01:00
|
|
|
* @param instantiation Template instantiation information.
|
2017-12-26 22:34:39 +01:00
|
|
|
* @param typeStringsUsedInTemplateInstantiation template parameters. list of token strings.
|
2017-06-08 00:16:43 +02:00
|
|
|
* @param newName The new type name
|
|
|
|
*/
|
2019-01-14 20:36:23 +01:00
|
|
|
void replaceTemplateUsage(const TokenAndName &instantiation,
|
2018-09-01 11:26:10 +02:00
|
|
|
const std::list<std::string> &typeStringsUsedInTemplateInstantiation,
|
2018-10-14 16:57:07 +02:00
|
|
|
const std::string &newName);
|
2012-08-20 18:10:32 +02:00
|
|
|
|
|
|
|
/**
|
2018-09-01 11:26:10 +02:00
|
|
|
* @brief TemplateParametersInDeclaration
|
|
|
|
* @param tok template < typename T, typename S >
|
|
|
|
* ^ tok
|
|
|
|
* @param typeParametersInDeclaration template < typename T, typename S >
|
|
|
|
* ^ [0] ^ [1]
|
2012-01-09 20:33:11 +01:00
|
|
|
*/
|
2021-03-16 09:31:52 +01:00
|
|
|
static void getTemplateParametersInDeclaration(
|
2018-09-01 11:26:10 +02:00
|
|
|
const Token * tok,
|
|
|
|
std::vector<const Token *> & typeParametersInDeclaration);
|
2012-07-29 16:01:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a specific "template < ..." template class/function
|
|
|
|
*/
|
2023-04-30 07:33:04 +02:00
|
|
|
static bool removeTemplate(Token *tok, std::map<Token*, Token*>* forwardDecls = nullptr);
|
2012-07-29 16:01:05 +02:00
|
|
|
|
2015-06-23 20:53:57 +02:00
|
|
|
/** Syntax error */
|
2021-04-25 12:52:09 +02:00
|
|
|
NORETURN static void syntaxError(const Token *tok);
|
2015-06-23 20:53:57 +02:00
|
|
|
|
2018-10-14 16:57:07 +02:00
|
|
|
static bool matchSpecialization(
|
2018-09-01 11:26:10 +02:00
|
|
|
const Token *templateDeclarationNameToken,
|
|
|
|
const Token *templateInstantiationNameToken,
|
|
|
|
const std::list<const Token *> & specializations);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as Token::eraseTokens() but tries to fix up lists with pointers to the deleted tokens.
|
|
|
|
* @param begin Tokens after this will be erased.
|
|
|
|
* @param end Tokens before this will be erased.
|
|
|
|
*/
|
2019-01-07 06:55:22 +01:00
|
|
|
static void eraseTokens(Token *begin, const Token *end);
|
2018-09-01 11:26:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete specified token without invalidating pointer to following token.
|
|
|
|
* tok will be invalidated.
|
|
|
|
* @param tok token to delete
|
|
|
|
*/
|
2018-10-14 16:57:07 +02:00
|
|
|
static void deleteToken(Token *tok);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the new token name.
|
2018-12-21 21:23:03 +01:00
|
|
|
* @param tok2 name token
|
2019-06-30 09:17:42 +02:00
|
|
|
* @param typeStringsUsedInTemplateInstantiation type strings use in template instantiation
|
2018-10-14 16:57:07 +02:00
|
|
|
* @return new token name
|
|
|
|
*/
|
|
|
|
std::string getNewName(
|
|
|
|
Token *tok2,
|
|
|
|
std::list<std::string> &typeStringsUsedInTemplateInstantiation);
|
2018-09-01 11:26:10 +02:00
|
|
|
|
2019-01-14 08:29:32 +01:00
|
|
|
void printOut(
|
|
|
|
const TokenAndName &tokenAndName,
|
|
|
|
const std::string &indent = " ") const;
|
2022-07-10 10:57:29 +02:00
|
|
|
void printOut(const std::string &text = emptyString) const;
|
2019-01-14 08:29:32 +01:00
|
|
|
|
2023-03-07 12:12:31 +01:00
|
|
|
Tokenizer &mTokenizer;
|
2018-09-01 11:26:10 +02:00
|
|
|
TokenList &mTokenList;
|
2023-03-07 12:12:31 +01:00
|
|
|
const Settings &mSettings;
|
2018-09-01 11:26:10 +02:00
|
|
|
ErrorLogger *mErrorLogger;
|
2023-08-08 11:05:02 +02:00
|
|
|
bool mChanged{};
|
2018-09-01 11:26:10 +02:00
|
|
|
|
|
|
|
std::list<TokenAndName> mTemplateDeclarations;
|
2018-11-10 16:41:14 +01:00
|
|
|
std::list<TokenAndName> mTemplateForwardDeclarations;
|
2018-11-23 11:36:09 +01:00
|
|
|
std::map<Token *, Token *> mTemplateForwardDeclarationsMap;
|
2019-01-27 07:46:27 +01:00
|
|
|
std::map<Token *, Token *> mTemplateSpecializationMap;
|
|
|
|
std::map<Token *, Token *> mTemplatePartialSpecializationMap;
|
2018-09-01 11:26:10 +02:00
|
|
|
std::list<TokenAndName> mTemplateInstantiations;
|
|
|
|
std::list<TokenAndName> mInstantiatedTemplates;
|
2018-09-14 14:16:34 +02:00
|
|
|
std::list<TokenAndName> mMemberFunctionsToDelete;
|
2018-12-21 13:51:45 +01:00
|
|
|
std::vector<TokenAndName> mExplicitInstantiationsToDelete;
|
2021-03-18 09:15:21 +01:00
|
|
|
std::vector<TokenAndName> mTypesUsedInTemplateInstantiation;
|
2019-05-25 10:04:01 +02:00
|
|
|
std::unordered_map<const Token*, int> mTemplateNamePos;
|
2023-03-08 18:59:34 +01:00
|
|
|
std::string mDump;
|
2012-01-01 21:55:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // templatesimplifierH
|