Skip to content

Commit f88bbbb

Browse files
committed
Complete/fix scope for enum class values
1 parent c707bcc commit f88bbbb

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

generator/shellgenerator.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ void ShellGenerator::writeTypeInfo(QTextStream &s, const AbstractMetaType *type,
125125
s << ' ';
126126
}
127127

128+
namespace {
129+
AbstractMetaEnum* findEnumTypeOfClass(const AbstractMetaClass* implementor, const QString& enumName)
130+
{
131+
for (AbstractMetaEnum* enumType : implementor->enums()) {
132+
if (enumType->name() == enumName) {
133+
return enumType;
134+
}
135+
}
136+
return nullptr;
137+
}
138+
}
139+
128140

129141
void ShellGenerator::writeFunctionArguments(QTextStream &s,
130142
const AbstractMetaFunction *meta_function,
@@ -170,8 +182,20 @@ void ShellGenerator::writeFunctionArguments(QTextStream &s,
170182
qualifier = ((EnumTypeEntry *)arg->type()->typeEntry())->qualifier();
171183
} else if (arg->type()->typeEntry()->isFlags() && expr.indexOf("::") < 0) {
172184
qualifier = ((FlagsTypeEntry *)arg->type()->typeEntry())->originator()->qualifier();
185+
} else if (_currentScope) {
186+
int pos = expr.indexOf("::");
187+
if (pos > 0) {
188+
QString typeName = expr.left(pos);
189+
AbstractMetaEnum* enumType = findEnumTypeOfClass(_currentScope, typeName);
190+
if (enumType && enumType->typeEntry()->isEnumClass()) {
191+
// prepend original class name, otherwise the new enum type from the wrapper will be used,
192+
// which is not compatible
193+
qualifier = _currentScope->name();
194+
}
195+
}
173196
}
174-
if (!qualifier.isEmpty()) {
197+
198+
if (!qualifier.isEmpty() && !expr.startsWith("{")) {
175199
s << qualifier << "::";
176200
}
177201
}
@@ -399,6 +423,13 @@ void ShellGenerator::writeInclude(QTextStream &stream, const Include &inc)
399423
stream << endl;
400424
}
401425

426+
const AbstractMetaClass* ShellGenerator::setCurrentScope(const AbstractMetaClass* scope)
427+
{
428+
const AbstractMetaClass* previousScope = _currentScope;
429+
_currentScope = scope;
430+
return previousScope;
431+
}
432+
402433
/*!
403434
Returns true if the given function \a fun is operator>>() or
404435
operator<<() that streams from/to a Q{Data,Text}Stream, false

generator/shellgenerator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,14 @@ class ShellGenerator : public Generator
9898

9999
static void writeInclude(QTextStream &stream, const Include &inc);
100100

101+
// this scope is used in writeFunctionArguments
102+
const AbstractMetaClass* setCurrentScope(const AbstractMetaClass* scope);
103+
const AbstractMetaClass* currentScope() const { return _currentScope; }
104+
101105
protected:
102-
PriGenerator *priGenerator;
106+
PriGenerator* priGenerator{};
107+
108+
const AbstractMetaClass* _currentScope{};
103109

104110
};
105111

generator/shellheadergenerator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static bool field_lessThan(const AbstractMetaField* a, const AbstractMetaField*
100100

101101
void ShellHeaderGenerator::write(QTextStream& s, const AbstractMetaClass* meta_class)
102102
{
103+
setCurrentScope(meta_class);
103104
QString builtIn = ShellGenerator::isBuiltIn(meta_class->name()) ? "_builtin" : "";
104105
QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
105106
priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class));
@@ -447,7 +448,7 @@ void ShellHeaderGenerator::write(QTextStream& s, const AbstractMetaClass* meta_c
447448

448449
s << "#endif // " << include_block << endl;
449450

450-
451+
setCurrentScope(nullptr);
451452
}
452453

453454
void ShellHeaderGenerator::writePromoterArgs(AbstractMetaArgumentList& args, QTextStream& s)

generator/shellimplgenerator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static void writeHelperCode(QTextStream &, const AbstractMetaClass *)
7070

7171
void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
7272
{
73+
setCurrentScope(meta_class);
74+
7375
QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":"";
7476
QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
7577
priGenerator->addSource(pro_file_name, fileNameForClass(meta_class));
@@ -360,6 +362,8 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
360362
if (meta_class->qualifiedCppName().contains("Ssl")) {
361363
s << "#endif" << endl;
362364
}
365+
366+
setCurrentScope(nullptr);
363367
}
364368

365369
void ShellImplGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)

0 commit comments

Comments
 (0)