Skip to content

Commit 81d492f

Browse files
committed
Round 17 + registry: coroutines, namespace scope, fn-ptr disambig, noexcept
Parser: - looksLikeFunctionPointerDeclarator: (*name) only fn-ptr when followed by ( or [ - postfix ++/-- -> prefix universally - float literals get f suffix - final -> constexpr CodeGen: - NamespaceDecl: inject using namespace ::std inside user namespaces - Friend binary operator template prefix before friend keyword CppBuild: - User NamespaceDecl: std specializations hoisted to preNs (global scope) - Out-of-class function definitions (X::f()) hoisted to namespace scope - noexcept restored on final_suspend/initial_suspend in output - coroutine promise noexcept regex on generated output Processing.h: - #include <coroutine> - shader(PShader*), texture(PImage*), addChild(PShape*) pointer overloads - PShader::set() double overloads
1 parent 31f1087 commit 81d492f

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

mode/CppMode.jar

265 Bytes
Binary file not shown.

src/Processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <dirent.h>
1313
#endif
1414
#include <functional>
15+
#include <coroutine>
1516
// =============================================================================
1617
// Processing.h -- processing-cpp API
1718
// =============================================================================

src/java/CodeGen.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ private static void emitNamespaceDecl(StringBuilder sb, NamespaceDecl nd, int de
149149
indent(sb, depth);
150150
if (nd.isInline()) sb.append("inline ");
151151
sb.append("namespace ").append(nd.name()).append(" {\n");
152+
// Inject ::std alias so std:: inside user namespaces resolves correctly
153+
// even when the namespace is emitted inside namespace Processing.
154+
if (!nd.name().equals("std")) {
155+
indent(sb, depth + 1);
156+
sb.append("using namespace ::std;\n");
157+
}
152158
for (TopLevelItem item : nd.items()) {
153159
emitTopLevelItem(sb, item, depth + 1);
154160
}

src/java/CppBuild.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,9 @@ private File writeSketchImpl(RunnerListener listener) throws IOException {
10291029

10301030
String code = sanitize(raw.toString());
10311031
code = removeUserIncludes(code);
1032+
// Restore noexcept on coroutine promise methods (parser drops noexcept specifier)
1033+
code = code.replaceAll("(\\bfinal_suspend\\s*\\([^)]*\\))(\\s*\\{)", "$1 noexcept$2");
1034+
code = code.replaceAll("(\\binitial_suspend\\s*\\([^)]*\\))(\\s*\\{)", "$1 noexcept$2");
10321035
// Remove no-op translate(0, 0) and translate(0, 0, 0) calls (Problem 7.2)
10331036
code = code.replaceAll("\\btranslate\\s*\\(\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*\\)\\s*;", "");
10341037
code = code.replaceAll("\\btranslate\\s*\\(\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*,\\s*0+\\.?0*f?\\s*\\)\\s*;", "");
@@ -1240,6 +1243,10 @@ private File writeSketchImpl(RunnerListener listener) throws IOException {
12401243
// Out-of-class static member definition: "int Agent::totalAgents = 0"
12411244
// must be at namespace scope, never inside struct Sketch
12421245
autoHoisted.add(item);
1246+
} else if (item instanceof FunctionDecl fdoc && fdoc.name().contains("::")) {
1247+
// Out-of-class function definition: "Generator17 GeneratorPromise17::get_return_object()"
1248+
// must be at namespace scope, never inside struct Sketch
1249+
autoHoisted.add(item);
12431250
} else {
12441251
filteredRest.add(item);
12451252
}
@@ -1268,10 +1275,16 @@ private File writeSketchImpl(RunnerListener listener) throws IOException {
12681275
} else {
12691276
out.append(CodeGen.generateNode(item, 0));
12701277
}
1271-
} else if (item instanceof NamespaceDecl) {
1272-
// User namespace declarations must be at true file scope, not inside
1273-
// namespace Processing -- otherwise std:: becomes Processing::std::
1274-
preNs.append(CodeGen.generateNode(item, 0));
1278+
} else if (item instanceof NamespaceDecl nd) {
1279+
// User namespace: emit inside namespace Processing.
1280+
// CodeGen injects "using namespace ::std" inside namespace bodies
1281+
// so std:: resolves to global ::std not Processing::std.
1282+
// std specializations (namespace std { ... }) go to preNs (global scope).
1283+
if (nd.name().equals("std")) {
1284+
preNs.append(CodeGen.generateNode(nd, 0));
1285+
} else {
1286+
out.append(CodeGen.generateNode(nd, 0));
1287+
}
12751288
} else if (item instanceof UsingNamespaceDecl) {
12761289
out.append(CodeGen.generateNode(item, 0));
12771290
} else {
@@ -1409,7 +1422,11 @@ private File writeSketchImpl(RunnerListener listener) throws IOException {
14091422
out.append("#endif\n");
14101423

14111424
File dest = new File(buildDir, "Sketch_run.cpp");
1412-
Files.writeString(dest.toPath(), out.toString());
1425+
String outStr = out.toString();
1426+
// Restore noexcept on coroutine promise methods (parser drops noexcept specifier)
1427+
outStr = outStr.replaceAll("(\\bfinal_suspend\\b[^{;]*)(\\{)", "$1noexcept $2");
1428+
outStr = outStr.replaceAll("(\\binitial_suspend\\b[^{;]*)(\\{)", "$1noexcept $2");
1429+
Files.writeString(dest.toPath(), outStr);
14131430
return dest;
14141431
}
14151432

0 commit comments

Comments
 (0)