Skip to content

Commit 6fb192c

Browse files
committed
Backported cloop feature: ability to create stubs for new methods
1 parent 90fdb97 commit 6fb192c

File tree

7 files changed

+134
-74
lines changed

7 files changed

+134
-74
lines changed

extern/cloop/src/cloop/Action.cpp

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,39 +94,84 @@ void DefAction::generate(const ActionParametersBlock& apb, unsigned ident)
9494
switch(apb.language)
9595
{
9696
case LANGUAGE_C:
97-
if (!apb.statusName.empty())
97+
if (!apb.method->statusName.empty())
9898
{
9999
identify(apb, ident);
100100
fprintf(apb.out, "CLOOP_setVersionError(%s, \"%s%s\", cloopVTable->version, %d);\n",
101-
apb.statusName.c_str(), apb.prefix.c_str(),
101+
apb.method->statusName.c_str(), apb.prefix.c_str(),
102102
apb.interface->name.c_str(), apb.method->version);
103103
}
104104
break;
105105

106106
case LANGUAGE_CPP:
107-
if (!apb.statusName.empty())
107+
if (!apb.method->statusName.empty())
108108
{
109109
identify(apb, ident);
110110
fprintf(apb.out, "%s::setVersionError(%s, \"%s%s\", cloopVTable->version, %d);\n",
111-
apb.exceptionClass.c_str(), apb.statusName.c_str(), apb.prefix.c_str(),
111+
apb.exceptionClass.c_str(), apb.method->statusName.c_str(), apb.prefix.c_str(),
112112
apb.interface->name.c_str(), apb.method->version);
113113
identify(apb, ident);
114114
fprintf(apb.out, "%s::checkException(%s);\n",
115-
apb.exceptionClass.c_str(), apb.statusName.c_str());
115+
apb.exceptionClass.c_str(), apb.method->statusName.c_str());
116116
}
117117
break;
118118

119119
case LANGUAGE_PASCAL:
120-
if (!apb.statusName.empty() && !apb.exceptionClass.empty())
120+
if (!apb.method->statusName.empty() && !apb.exceptionClass.empty())
121121
{
122122
identify(apb, ident);
123123
fprintf(apb.out, "%s.setVersionError(%s, \'%s%s\', vTable.version, %d);\n",
124-
apb.exceptionClass.c_str(), apb.statusName.c_str(), apb.prefix.c_str(),
124+
apb.exceptionClass.c_str(), apb.method->statusName.c_str(), apb.prefix.c_str(),
125125
apb.interface->name.c_str(), apb.method->version);
126126
}
127127
break;
128128
}
129129
break;
130+
131+
case DEF_IGNORE:
132+
if (apb.method->returnTypeRef.token.type != Token::TYPE_VOID ||
133+
apb.method->returnTypeRef.isPointer)
134+
{
135+
identify(apb, ident);
136+
137+
switch(apb.language)
138+
{
139+
case LANGUAGE_C:
140+
case LANGUAGE_CPP:
141+
fprintf(apb.out, "return 0;\n");
142+
break;
143+
144+
case LANGUAGE_PASCAL:
145+
{
146+
const char* sResult = "nil";
147+
if (!apb.method->returnTypeRef.isPointer)
148+
{
149+
switch (apb.method->returnTypeRef.token.type)
150+
{
151+
case Token::TYPE_STRING:
152+
break;
153+
154+
case Token::TYPE_BOOLEAN:
155+
sResult = "false";
156+
break;
157+
158+
case Token::TYPE_IDENTIFIER:
159+
if (apb.method->returnTypeRef.type == BaseType::TYPE_INTERFACE)
160+
break;
161+
162+
// fallthru
163+
default:
164+
sResult = "0";
165+
break;
166+
}
167+
}
168+
169+
fprintf(apb.out, "Result := %s;\n", sResult);
170+
}
171+
break;
172+
}
173+
}
174+
break;
130175
}
131176
}
132177

extern/cloop/src/cloop/Action.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ struct ActionParametersBlock
3737
Language language;
3838
const std::string& prefix;
3939
const std::string& exceptionClass;
40-
const std::string& statusName;
4140
Interface* interface;
4241
Method* method;
4342
};
@@ -91,7 +90,7 @@ class CallAction : public Action
9190
class DefAction : public Action
9291
{
9392
public:
94-
enum DefType { DEF_NOT_IMPLEMENTED };
93+
enum DefType { DEF_NOT_IMPLEMENTED, DEF_IGNORE };
9594

9695
DefAction(DefType dt)
9796
: defType(dt)

extern/cloop/src/cloop/Generator.cpp

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,8 @@ void CppGenerator::generate()
357357

358358
fprintf(out, "\n\t\t");
359359

360-
string statusName;
361-
362-
if (!method->parameters.empty() &&
363-
parser->exceptionInterface &&
364-
method->parameters.front()->typeRef.token.text == parser->exceptionInterface->name)
365-
{
366-
statusName = method->parameters.front()->name;
360+
if (!method->statusName.empty())
367361
fprintf(out, "template <typename StatusType> ");
368-
}
369362

370363
fprintf(out, "%s %s(",
371364
convertType(method->returnTypeRef).c_str(), method->name.c_str());
@@ -379,7 +372,7 @@ void CppGenerator::generate()
379372
if (k != method->parameters.begin())
380373
fprintf(out, ", ");
381374

382-
if (k == method->parameters.begin() && !statusName.empty())
375+
if (k == method->parameters.begin() && !method->statusName.empty())
383376
fprintf(out, "StatusType* %s", parameter->name.c_str());
384377
else
385378
{
@@ -397,7 +390,7 @@ void CppGenerator::generate()
397390
fprintf(out, "\t\t\t{\n");
398391

399392
const string exceptionClass("StatusType");
400-
ActionParametersBlock apb = {out, LANGUAGE_CPP, prefix, exceptionClass, statusName, interface, method};
393+
ActionParametersBlock apb = {out, LANGUAGE_CPP, prefix, exceptionClass, interface, method};
401394

402395
if (method->notImplementedAction)
403396
method->notImplementedAction->generate(apb, 4);
@@ -419,11 +412,11 @@ void CppGenerator::generate()
419412
fprintf(out, "\t\t\t}\n");
420413
}
421414

422-
if (!statusName.empty())
415+
if (!method->statusName.empty())
423416
{
424417
fprintf(out, "\t\t\t");
425418

426-
fprintf(out, "StatusType::clearException(%s)", statusName.c_str());
419+
fprintf(out, "StatusType::clearException(%s)", method->statusName.c_str());
427420

428421
fprintf(out, ";\n");
429422
}
@@ -694,7 +687,19 @@ void CppGenerator::generate()
694687
}
695688
}
696689

697-
fprintf(out, ")%s = 0;\n", (method->isConst ? " const" : ""));
690+
fprintf(out, ")%s", (method->isConst ? " const" : ""));
691+
692+
if (method->stubAction)
693+
{
694+
const string exceptionClass("StatusType");
695+
ActionParametersBlock apb = {out, LANGUAGE_CPP, prefix, exceptionClass, interface, method};
696+
697+
fprintf(out, "\n\t\t{\n");
698+
method->stubAction->generate(apb, 3);
699+
fprintf(out, "\t\t}\n");
700+
}
701+
else
702+
fprintf(out, " = 0;\n");
698703
}
699704

700705
fprintf(out, "\t};\n");
@@ -1227,7 +1232,10 @@ void PascalGenerator::generate()
12271232
if (!isProcedure)
12281233
fprintf(out, ": %s", convertType(method->returnTypeRef).c_str());
12291234

1230-
fprintf(out, "; virtual; abstract;\n");
1235+
fprintf(out, "; virtual;");
1236+
if (!method->stubAction)
1237+
fprintf(out, " abstract;");
1238+
fprintf(out, "\n");
12311239
}
12321240

12331241
fprintf(out, "\tend;\n\n");
@@ -1252,15 +1260,6 @@ void PascalGenerator::generate()
12521260
bool isProcedure = method->returnTypeRef.token.type == Token::TYPE_VOID &&
12531261
!method->returnTypeRef.isPointer;
12541262

1255-
string statusName;
1256-
1257-
if (!method->parameters.empty() &&
1258-
parser->exceptionInterface &&
1259-
method->parameters.front()->typeRef.token.text == parser->exceptionInterface->name)
1260-
{
1261-
statusName = method->parameters.front()->name;
1262-
}
1263-
12641263
fprintf(out, "%s %s.%s(",
12651264
(isProcedure ? "procedure" : "function"),
12661265
escapeName(interface->name, true).c_str(),
@@ -1291,8 +1290,7 @@ void PascalGenerator::generate()
12911290
{
12921291
fprintf(out, "\tif (vTable.version < %d) then begin\n", method->version);
12931292

1294-
ActionParametersBlock apb = {out, LANGUAGE_PASCAL, prefix, exceptionClass,
1295-
statusName, interface, method};
1293+
ActionParametersBlock apb = {out, LANGUAGE_PASCAL, prefix, exceptionClass, interface, method};
12961294

12971295
if (method->notImplementedAction)
12981296
method->notImplementedAction->generate(apb, 2);
@@ -1333,8 +1331,8 @@ void PascalGenerator::generate()
13331331
if (ident > 1)
13341332
fprintf(out, "\tend;\n");
13351333

1336-
if (!statusName.empty() && !exceptionClass.empty())
1337-
fprintf(out, "\t%s.checkException(%s);\n", exceptionClass.c_str(), escapeName(statusName).c_str());
1334+
if (!method->statusName.empty() && !exceptionClass.empty())
1335+
fprintf(out, "\t%s.checkException(%s);\n", exceptionClass.c_str(), escapeName(method->statusName).c_str());
13381336

13391337
fprintf(out, "end;\n\n");
13401338
}
@@ -1358,6 +1356,8 @@ void PascalGenerator::generate()
13581356
bool isProcedure = method->returnTypeRef.token.type == Token::TYPE_VOID &&
13591357
!method->returnTypeRef.isPointer;
13601358

1359+
ActionParametersBlock apb = {out, LANGUAGE_PASCAL, prefix, exceptionClass, interface, method};
1360+
13611361
fprintf(out, "%s %sImpl_%sDispatcher(this: %s",
13621362
(isProcedure ? "procedure" : "function"),
13631363
escapeName(interface->name, true).c_str(),
@@ -1380,39 +1380,7 @@ void PascalGenerator::generate()
13801380

13811381
fprintf(out, "; cdecl;\n");
13821382
fprintf(out, "begin\n");
1383-
1384-
if (!isProcedure)
1385-
{
1386-
if (method->returnTypeRef.isPointer) {
1387-
fprintf(out, "\tResult := nil;\n");
1388-
}
1389-
else
1390-
{
1391-
const char* sResult;
1392-
switch (method->returnTypeRef.token.type)
1393-
{
1394-
case Token::TYPE_STRING:
1395-
sResult = "nil";
1396-
break;
1397-
1398-
case Token::TYPE_BOOLEAN:
1399-
sResult = "false";
1400-
break;
1401-
1402-
case Token::TYPE_IDENTIFIER:
1403-
if (method->returnTypeRef.type == BaseType::TYPE_INTERFACE)
1404-
{
1405-
sResult = "nil";
1406-
break;
1407-
}
1408-
// fallthru
1409-
default:
1410-
sResult = "0";
1411-
break;
1412-
}
1413-
fprintf(out, "\tResult := %s;\n", sResult);
1414-
}
1415-
}
1383+
DefAction(DefAction::DEF_IGNORE).generate(apb, 1);
14161384

14171385
if (!exceptionClass.empty())
14181386
fprintf(out, "\ttry\n\t");
@@ -1454,8 +1422,35 @@ void PascalGenerator::generate()
14541422

14551423
fprintf(out, "\tend\n");
14561424
}
1457-
14581425
fprintf(out, "end;\n\n");
1426+
1427+
if (method->stubAction)
1428+
{
1429+
fprintf(out, "%s %sImpl.%s(",
1430+
(isProcedure ? "procedure" : "function"),
1431+
escapeName(interface->name, true).c_str(),
1432+
escapeName(method->name).c_str());
1433+
1434+
for (vector<Parameter*>::iterator k = method->parameters.begin();
1435+
k != method->parameters.end();
1436+
++k)
1437+
{
1438+
Parameter* parameter = *k;
1439+
1440+
fprintf(out, "%s%s",
1441+
k == method->parameters.begin() ? "" : "; ",
1442+
convertParameter(*parameter).c_str());
1443+
}
1444+
1445+
fprintf(out, ")");
1446+
if (!isProcedure)
1447+
fprintf(out, ": %s", convertType(method->returnTypeRef).c_str());
1448+
fprintf(out, ";\n");
1449+
1450+
fprintf(out, "begin\n");
1451+
method->stubAction->generate(apb, 1);
1452+
fprintf(out, "end;\n\n");
1453+
}
14591454
}
14601455

14611456
fprintf(out, "var\n");

extern/cloop/src/cloop/Lexer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Token& Lexer::getToken(Token& token)
112112
token.type = Token::TYPE_CALL;
113113
else if (token.text == "defaultAction")
114114
token.type = Token::TYPE_DEFAULT_ACTION;
115+
else if (token.text == "stub")
116+
token.type = Token::TYPE_STUB;
115117
// types
116118
else if (token.text == "void")
117119
token.type = Token::TYPE_VOID;

extern/cloop/src/cloop/Lexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct Token
4747
TYPE_INTERFACE,
4848
TYPE_NOT_IMPLEMENTED,
4949
TYPE_NOT_IMPLEMENTED_ACTION,
50+
TYPE_STUB,
5051
TYPE_STRUCT,
5152
TYPE_TYPEDEF,
5253
TYPE_VERSION,

0 commit comments

Comments
 (0)