Commit 2b532c73 authored by hjk's avatar hjk
Browse files

CPlusPlus: Make (sub-)languague selection more generic



Change-Id: I4e2df6992b446adec662ab07671acd41715e41fd
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 0a600e04
......@@ -347,7 +347,8 @@ FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType
std::swap(_declaratorId, declaratorId);
bool isAuto = false;
if (translationUnit()->cxx0xEnabled())
const bool cxx11Enabled = translationUnit()->languageFeatures().cxx11Enabled;
if (cxx11Enabled)
isAuto = type.isAuto();
for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
......@@ -369,8 +370,7 @@ FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType
}
if (!type->isFunctionType()) {
ExpressionTy initializer = this->expression(ast->initializer);
if (translationUnit()->cxx0xEnabled() && isAuto) {
if (cxx11Enabled && isAuto) {
type = initializer;
type.setAuto(true);
}
......@@ -1249,7 +1249,7 @@ bool Bind::visit(ForeachStatementAST *ast)
DeclaratorIdAST *declaratorId = 0;
type = this->declarator(ast->declarator, type, &declaratorId);
const StringLiteral *initializer = 0;
if (type.isAuto() && translationUnit()->cxx0xEnabled()) {
if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) {
ExpressionTy exprType = this->expression(ast->expression);
ArrayType* arrayType = 0;
......@@ -1299,7 +1299,7 @@ bool Bind::visit(RangeBasedForStatementAST *ast)
DeclaratorIdAST *declaratorId = 0;
type = this->declarator(ast->declarator, type, &declaratorId);
const StringLiteral *initializer = 0;
if (type.isAuto() && translationUnit()->cxx0xEnabled()) {
if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) {
ExpressionTy exprType = this->expression(ast->expression);
ArrayType* arrayType = 0;
......@@ -2722,7 +2722,7 @@ bool Bind::visit(SimpleSpecifierAST *ast)
break;
case T_AUTO:
if (!translationUnit()->cxx0xEnabled()) {
if (!translationUnit()->languageFeatures().cxx11Enabled) {
if (_type.isAuto())
translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
}
......
......@@ -21,9 +21,10 @@
#include "Lexer.h"
#include "Token.h"
using namespace CPlusPlus;
namespace CPlusPlus {
static inline int classify2(const char *s, bool, bool) {
static inline int classify2(const char *s, LanguageFeatures)
{
if (s[0] == 'd') {
if (s[1] == 'o') {
return T_DO;
......@@ -37,7 +38,8 @@ static inline int classify2(const char *s, bool, bool) {
return T_IDENTIFIER;
}
static inline int classify3(const char *s, bool q, bool) {
static inline int classify3(const char *s, LanguageFeatures features)
{
if (s[0] == 'a') {
if (s[1] == 's') {
if (s[2] == 'm') {
......@@ -73,7 +75,7 @@ static inline int classify3(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtMocRunEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'D') {
return T_Q_D;
......@@ -86,7 +88,8 @@ static inline int classify3(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify4(const char *s, bool q, bool) {
static inline int classify4(const char *s, LanguageFeatures features)
{
if (s[0] == 'a') {
if (s[1] == 'u') {
if (s[2] == 't') {
......@@ -136,7 +139,7 @@ static inline int classify4(const char *s, bool q, bool) {
}
}
}
else if (q && s[1] == 'm') {
else if (features.qtKeywordsEnabled && s[1] == 'm') {
if (s[2] == 'i') {
if (s[3] == 't') {
return T_EMIT;
......@@ -187,7 +190,7 @@ static inline int classify4(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'S') {
else if (features.qtEnabled && s[0] == 'S') {
if (s[1] == 'L') {
if (s[2] == 'O') {
if (s[3] == 'T') {
......@@ -199,7 +202,8 @@ static inline int classify4(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify5(const char *s, bool q, bool) {
static inline int classify5(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'a') {
......@@ -281,7 +285,7 @@ static inline int classify5(const char *s, bool q, bool) {
}
}
}
else if (q) {
else if (features.qtKeywordsEnabled) {
if (s[1] == 'l') {
if (s[2] == 'o') {
if (s[3] == 't') {
......@@ -338,7 +342,8 @@ static inline int classify5(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify6(const char *s, bool q, bool) {
static inline int classify6(const char *s, LanguageFeatures features)
{
if (s[0] == 'd') {
if (s[1] == 'e') {
if (s[2] == 'l') {
......@@ -508,7 +513,7 @@ static inline int classify6(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'S') {
else if (features.qtKeywordsEnabled && s[0] == 'S') {
if (s[1] == 'I') {
if (s[2] == 'G') {
if (s[3] == 'N') {
......@@ -521,7 +526,7 @@ static inline int classify6(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtKeywordsEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'S') {
if (s[3] == 'L') {
......@@ -546,7 +551,8 @@ static inline int classify6(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify7(const char *s, bool q, bool x) {
static inline int classify7(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'a') {
......@@ -573,7 +579,7 @@ static inline int classify7(const char *s, bool q, bool x) {
}
}
}
else if (x && s[0] == 'a') {
else if (features.cxx11Enabled && s[0] == 'a') {
if (s[1] == 'l') {
if (s[2] == 'i') {
if (s[3] == 'g') {
......@@ -623,7 +629,7 @@ static inline int classify7(const char *s, bool q, bool x) {
}
}
}
else if (x && s[0] == 'n') {
else if (features.cxx11Enabled && s[0] == 'n') {
if (s[1] == 'u') {
if (s[2] == 'l') {
if (s[3] == 'l') {
......@@ -653,7 +659,7 @@ static inline int classify7(const char *s, bool q, bool x) {
}
}
}
else if (q && s[0] == 'f') {
else if (features.qtKeywordsEnabled && s[0] == 'f') {
if (s[1] == 'o') {
if (s[2] == 'r') {
if (s[3] == 'e') {
......@@ -668,7 +674,7 @@ static inline int classify7(const char *s, bool q, bool x) {
}
}
}
else if (q && s[0] == 's') {
else if (features.qtEnabled && s[0] == 's') {
if (s[1] == 'i') {
if (s[2] == 'g') {
if (s[3] == 'n') {
......@@ -728,7 +734,7 @@ static inline int classify7(const char *s, bool q, bool x) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'S') {
if (s[3] == 'L') {
......@@ -768,7 +774,8 @@ static inline int classify7(const char *s, bool q, bool x) {
return T_IDENTIFIER;
}
static inline int classify8(const char *s, bool q, bool x) {
static inline int classify8(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'i') {
......@@ -814,7 +821,7 @@ static inline int classify8(const char *s, bool q, bool x) {
}
}
}
} else if (x && s[1] == 'h') {
} else if (features.cxx11Enabled && s[1] == 'h') {
if (s[2] == 'a') {
if (s[3] == 'r') {
if (s[4] == '1') {
......@@ -838,7 +845,7 @@ static inline int classify8(const char *s, bool q, bool x) {
}
}
}
else if (x && s[0] == 'd') {
else if (features.cxx11Enabled && s[0] == 'd') {
if (s[1] == 'e') {
if (s[2] == 'c') {
if (s[3] == 'l') {
......@@ -872,7 +879,7 @@ static inline int classify8(const char *s, bool q, bool x) {
}
}
}
else if (x && s[0] == 'n') {
else if (features.cxx11Enabled && s[0] == 'n') {
if (s[1] == 'o') {
if (s[2] == 'e') {
if (s[3] == 'x') {
......@@ -989,7 +996,7 @@ static inline int classify8(const char *s, bool q, bool x) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'G') {
if (s[3] == 'A') {
......@@ -1035,7 +1042,8 @@ static inline int classify8(const char *s, bool q, bool x) {
return T_IDENTIFIER;
}
static inline int classify9(const char *s, bool q, bool x) {
static inline int classify9(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'c') {
......@@ -1055,7 +1063,7 @@ static inline int classify9(const char *s, bool q, bool x) {
}
}
}
else if (x && s[0] == 'c') {
else if (features.cxx11Enabled && s[0] == 'c') {
if (s[1] == 'o') {
if (s[2] == 'n') {
if (s[3] == 's') {
......@@ -1112,7 +1120,7 @@ static inline int classify9(const char *s, bool q, bool x) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'S') {
if (s[3] == 'I') {
......@@ -1148,7 +1156,8 @@ static inline int classify9(const char *s, bool q, bool x) {
return T_IDENTIFIER;
}
static inline int classify10(const char *s, bool q, bool) {
static inline int classify10(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'i') {
......@@ -1242,7 +1251,7 @@ static inline int classify10(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'O') {
if (s[3] == 'V') {
......@@ -1283,7 +1292,8 @@ static inline int classify10(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify11(const char *s, bool q, bool) {
static inline int classify11(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'a') {
......@@ -1330,7 +1340,7 @@ static inline int classify11(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'I') {
if (s[3] == 'N') {
......@@ -1356,7 +1366,8 @@ static inline int classify11(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify12(const char *s, bool q, bool) {
static inline int classify12(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'v') {
......@@ -1382,7 +1393,7 @@ static inline int classify12(const char *s, bool q, bool) {
}
}
}
else if (q && s[0] == 'Q') {
else if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'I') {
if (s[3] == 'N') {
......@@ -1435,7 +1446,8 @@ static inline int classify12(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify13(const char *s, bool, bool x) {
static inline int classify13(const char *s, LanguageFeatures features)
{
if (s[0] == '_') {
if (s[1] == '_') {
if (s[2] == 'a') {
......@@ -1462,7 +1474,7 @@ static inline int classify13(const char *s, bool, bool x) {
}
}
}
} else if (x && s[0] == 's') {
} else if (features.cxx11Enabled && s[0] == 's') {
if (s[1] == 't') {
if (s[2] == 'a') {
if (s[3] == 't') {
......@@ -1492,7 +1504,8 @@ static inline int classify13(const char *s, bool, bool x) {
return T_IDENTIFIER;
}
static inline int classify16(const char *s, bool, bool) {
static inline int classify16(const char *s, LanguageFeatures)
{
if (s[0] == 'r') {
if (s[1] == 'e') {
if (s[2] == 'i') {
......@@ -1529,8 +1542,9 @@ static inline int classify16(const char *s, bool, bool) {
return T_IDENTIFIER;
}
static inline int classify14(const char *s, bool q, bool) {
if (q && s[0] == 'Q') {
static inline int classify14(const char *s, LanguageFeatures features)
{
if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'P') {
if (s[3] == 'R') {
......@@ -1562,8 +1576,9 @@ static inline int classify14(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify18(const char *s, bool q, bool) {
if (q && s[0] == 'Q') {
static inline int classify18(const char *s, LanguageFeatures features)
{
if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'P') {
if (s[3] == 'R') {
......@@ -1603,8 +1618,9 @@ static inline int classify18(const char *s, bool q, bool) {
return T_IDENTIFIER;
}
static inline int classify19(const char *s, bool q, bool) {
if (q && s[0] == 'Q') {
static inline int classify19(const char *s, LanguageFeatures features)
{
if (features.qtEnabled && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'D') {
if (s[3] == 'E') {
......@@ -1647,24 +1663,24 @@ static inline int classify19(const char *s, bool q, bool) {
}
int Lexer::classify(const char *s, int n, bool q, bool x) {
int Lexer::classify(const char *s, int n, LanguageFeatures features) {
switch (n) {
case 2: return classify2(s, q, x);
case 3: return classify3(s, q, x);
case 4: return classify4(s, q, x);
case 5: return classify5(s, q, x);
case 6: return classify6(s, q, x);
case 7: return classify7(s, q, x);
case 8: return classify8(s, q, x);
case 9: return classify9(s, q, x);
case 10: return classify10(s, q, x);
case 11: return classify11(s, q, x);
case 12: return classify12(s, q, x);
case 13: return classify13(s, q, x);
case 14: return classify14(s, q, x);
case 16: return classify16(s, q, x);
case 18: return classify18(s, q, x);
case 19: return classify19(s, q, x);
case 2: return classify2(s, features);
case 3: return classify3(s, features);
case 4: return classify4(s, features);
case 5: return classify5(s, features);
case 6: return classify6(s, features);
case 7: return classify7(s, features);
case 8: return classify8(s, features);
case 9: return classify9(s, features);
case 10: return classify10(s, features);
case 11: return classify11(s, features);
case 12: return classify12(s, features);
case 13: return classify13(s, features);
case 14: return classify14(s, features);
case 16: return classify16(s, features);
case 18: return classify18(s, features);
case 19: return classify19(s, features);
default: return T_IDENTIFIER;
} // switch
}
......@@ -1807,3 +1823,4 @@ int Lexer::classifyOperator(const char *s, int n) {
}
} // namespace CPlusPlus
......@@ -81,24 +81,6 @@ int Lexer::state() const
void Lexer::setState(int state)
{ _state = state; }
bool Lexer::qtMocRunEnabled() const
{ return f._qtMocRunEnabled; }
void Lexer::setQtMocRunEnabled(bool onoff)
{ f._qtMocRunEnabled = onoff; }
bool Lexer::cxx0xEnabled() const
{ return f._cxx0xEnabled; }
void Lexer::setCxxOxEnabled(bool onoff)
{ f._cxx0xEnabled = onoff; }
bool Lexer::objCEnabled() const
{ return f._objCEnabled; }
void Lexer::setObjCEnabled(bool onoff)
{ f._objCEnabled = onoff; }
bool Lexer::isIncremental() const
{ return f._isIncremental; }
......@@ -557,7 +539,7 @@ void Lexer::scan_helper(Token *tok)
break;
default: {
if (f._objCEnabled) {
if (_languageFeatures.objCEnabled) {
if (ch == '@' && _yychar >= 'a' && _yychar <= 'z') {
const char *yytext = _currentChar;
......@@ -780,7 +762,7 @@ void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
yyinp();
int yylen = _currentChar - yytext;
if (f._scanKeywords)
tok->f.kind = classify(yytext, yylen, f._qtMocRunEnabled, f._cxx0xEnabled);
tok->f.kind = classify(yytext, yylen, _languageFeatures);
else
tok->f.kind = T_IDENTIFIER;
......
......@@ -45,15 +45,6 @@ public:
Control *control() const { return _control; }
TranslationUnit *translationUnit() const;
bool qtMocRunEnabled() const;
void setQtMocRunEnabled(bool onoff);
bool cxx0xEnabled() const;
void setCxxOxEnabled(bool onoff);
bool objCEnabled() const;
void setObjCEnabled(bool onoff);
void scan(Token *tok);
inline void operator()(Token *tok)
......@@ -82,10 +73,13 @@ public:
bool isIncremental() const;
void setIncremental(bool isIncremental);
LanguageFeatures languageFeatures() const { return _languageFeatures; }
void setLanguageFeatures(LanguageFeatures features) { _languageFeatures = features; }
private:
void scan_helper(Token *tok);
void setSource(const char *firstChar, const char *lastChar);
static int classify(const char *string, int length, bool q, bool cxx0x);
static int classify(const char *string, int length, LanguageFeatures features);
static int classifyObjCAtKeyword(const char *s, int n);
static int classifyOperator(const char *string, int length);
......@@ -111,9 +105,6 @@ private:
unsigned _scanCommentTokens: 1;
unsigned _scanKeywords: 1;
unsigned _scanAngleStringLiteralTokens: 1;
unsigned _qtMocRunEnabled: 1;
unsigned _cxx0xEnabled: 1;
unsigned _objCEnabled: 1;
};
TranslationUnit *_translationUnit;
......@@ -129,6 +120,8 @@ private:
Flags f;
};
unsigned _currentLine;
LanguageFeatures _languageFeatures;
};
} // namespace CPlusPlus
......
This diff is collapsed.
......@@ -36,15 +36,6 @@ public:
Parser(TranslationUnit *translationUnit);
~Parser();
bool qtMocRunEnabled() const;
void setQtMocRunEnabled(bool onoff);
bool cxx0xEnabled() const;
void setCxxOxEnabled(bool onoff);
bool objCEnabled() const;
void setObjCEnabled(bool onoff);
bool parseTranslationUnit(TranslationUnitAST *&node);
public:
......@@ -315,11 +306,9 @@ private:
TranslationUnit *_translationUnit;
Control *_control;
MemoryPool *_pool;
LanguageFeatures _languageFeatures;
unsigned _tokenIndex;
bool _templateArguments: 1;
bool _qtMocRunEnabled: 1;
bool _cxx0xEnabled: 1;
bool _objCEnabled: 1;
bool _inFunctionBody: 1;
bool _inObjCImplementationContext: 1;
bool _inExpressionStatement: 1;
......
......@@ -373,6 +373,22 @@ public:
};
};
struct LanguageFeatures
{
LanguageFeatures() : flags(0) {}
union {
unsigned int flags;
struct {
unsigned int qtEnabled : 1; // If Qt is used.
unsigned int qtMocRunEnabled : 1;
unsigned int qtKeywordsEnabled : 1; // If Qt is used but QT_NO_KEYWORDS defined
unsigned int cxx11Enabled : 1;
unsigned int objCEnabled : 1;
};
};
};
} // namespace CPlusPlus
......
......@@ -63,24 +63,6 @@ TranslationUnit::~TranslationUnit()
delete _pool;
}
bool TranslationUnit::qtMocRunEnabled() const
{ return f._qtMocRunEnabled; }
void TranslationUnit::setQtMocRunEnabled(bool onoff)
{ f._qtMocRunEnabled = onoff; }
bool TranslationUnit::cxx0xEnabled() const
{ return f._cxx0xEnabled; }