feat: C++ 阶段 1 支持 (pcc-cpp 转换器)

实现 C++ 到 C 的转换器 (tools/pcc-cpp.c),pcc 检测 .cpp 文件时
自动转换后编译。阶段 1 支持:

1. class 定义 → struct (数据成员 + 继承的 __base 嵌套)
2. 成员函数 (类内声明/类外定义/内联定义)
   - 名称修饰: Class::method → Class_method
   - 自动添加 this 参数: (struct Class *this)
   - 方法体内裸成员名 → this->member
   - 基类成员 → this->__base.member
3. 构造函数/析构函数
   - 名称修饰: Class::Class → Class_ctor, ~Class → Class_dtor
   - 局部对象声明时自动调用构造
   - 支持带参构造 (var(a,b) → Ctor(&var, a, b))
   - 初始化列表跳过 (: Base(args))
4. 成员调用: obj.method(args) → Class_method(&obj, args)
5. 继承: class B : public A → struct B { struct A __base; }
6. 简单继承的成员访问/方法遮蔽

同时修复:
- AFF_TYPE_CPP 与 AFF_PRINT_ERROR 位冲突 (0x10)
- #pragma pack(push,MACRO) 宏展开 (x86_64 下 TOK_ASM_push 未定义)
- 头文件 _CRT_PACKING pragma 改为数字

验证: pcc test.cpp 直接编译运行, 含继承/构造/成员的程序 exit=0
这个提交包含在:
Paze AI
2026-08-16 17:12:46 +08:00
父节点 a3dfe7cb24
当前提交 c9b627f33f
修改 26 个文件,包含 1247 行新增19 行删除
+2
查看文件
@@ -92,3 +92,5 @@ bin/*
!bin/pcc-asm.exe
!bin/pcc-get.exe
!bin/pcc-impdef.exe
!bin/pcc-cpp.exe
二进制
查看文件
二进制文件未显示。
二进制
查看文件
二进制文件未显示。
+86
查看文件
@@ -0,0 +1,86 @@
# PCC C++ 阶段 1 实现计划(修订版)
## 架构决策:C++ 前端转换器(pcc-cpp
**不在核心解析器中实现 C++**(风险高、工程大),而是创建一个
**独立的 C++ → C 转换器**,把 C++ 代码翻译成等效 C 代码,
再交给现有 pcc 编译。这是 cfront/Comeau 的成熟做法。
## 转换规则
### 1. class 声明
```cpp
class Point {
public:
int x, y;
int sum();
};
```
```c
struct Point { int x, y; };
int Point_sum(struct Point *this);
```
### 2. 成员函数定义
```cpp
int Point::sum() { return x + y; }
```
```c
int Point_sum(struct Point *this) { return this->x + this->y; }
```
### 3. 成员调用
```cpp
Point p;
p.sum(); // → Point_sum(&p)
```
`p.x = 5` 直接用 struct 字段访问(无需转换)
### 4. 构造函数
```cpp
Point::Point(int a, int b) { x = a; y = b; }
```
```c
void Point_ctor(struct Point *this, int a, int b) { this->x = a; this->y = b; }
```
局部对象声明时自动插入构造调用
### 5. 析构函数
```cpp
Point::~Point() { }
```
```c
void Point_dtor(struct Point *this) { }
```
### 6. 简单继承
```cpp
class Shape : public Point { };
```
```c
struct Shape { struct Point __base; };
```
## 实现形式
- **pcc-cpp.c**:独立转换器,读取 .cpp,输出 .c
- pcc 检测 .cpp 文件时自动调用(内部集成)
- 用 pcc 自身编译(自举)
## 测试用例
```cpp
class Point {
public:
int x, y;
Point(int a, int b) { x = a; y = b; }
int sum() { return x + y; }
};
int main() {
Point p(3, 4);
return p.sum() - 7;
}
```
+82
查看文件
@@ -822,6 +822,7 @@ static int pcc_compile(PCCState *s1, int filetype, const char *str, int fd)
}
preprocess_start(s1, filetype);
s1->cpp_mode = !!(filetype & AFF_TYPE_CPP); /* C++ mode */
tccgen_init(s1);
if (s1->output_type == PCC_OUTPUT_PREPROCESS) {
@@ -1196,8 +1197,10 @@ static int guess_filetype(const char *filename)
if (1) {
/* use a file extension to detect a filetype */
const char *ext = pcc_fileextension(filename);
if (ext[0]) {
ext++;
if (!strcmp(ext, "S"))
filetype = AFF_TYPE_ASMPP;
else if (!strcmp(ext, "s"))
@@ -1206,6 +1209,11 @@ static int guess_filetype(const char *filename)
|| !PATHCMP(ext, "h")
|| !PATHCMP(ext, "i"))
filetype = AFF_TYPE_C;
else if (!PATHCMP(ext, "cpp")
|| !PATHCMP(ext, "cc")
|| !PATHCMP(ext, "cxx")
|| !PATHCMP(ext, "c++"))
filetype = AFF_TYPE_C | AFF_TYPE_CPP;
else
filetype |= AFF_TYPE_BIN;
} else {
@@ -1244,12 +1252,86 @@ ST_FUNC int pcc_add_file_internal(PCCState *s1, const char *filename, int flags)
if (flags & AFF_TYPE_BIN)
return pcc_add_binary(s1, flags, filename, fd);
/* C++ source: transpile to C via pcc-cpp, then compile the result */
if (flags & AFF_TYPE_CPP) {
char cmdbuf[2048];
char tmpc[1024];
char pcccpp[1024];
const char *exedir;
/* find pcc-cpp next to pcc.exe (or in PATH) */
pcccpp[0] = 0;
#ifdef _WIN32
{
char selfpath[1024];
char *slash;
GetModuleFileNameA(NULL, selfpath, sizeof selfpath);
slash = strrchr(selfpath, '\\');
if (slash) {
*slash = 0;
snprintf(pcccpp, sizeof pcccpp, "%s\\pcc-cpp.exe", selfpath);
if (GetFileAttributesA(pcccpp) == INVALID_FILE_ATTRIBUTES)
strcpy(pcccpp, "pcc-cpp");
} else
strcpy(pcccpp, "pcc-cpp");
}
#else
strcpy(pcccpp, "pcc-cpp");
#endif
snprintf(tmpc, sizeof tmpc, "%s.pcc.c", filename);
#ifdef _WIN32
snprintf(cmdbuf, sizeof cmdbuf, "\"%s\" \"%s\" -o \"%s\"",
pcccpp, filename, tmpc);
/* use CreateProcess so quoted paths with spaces work */
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
char *cmdline = (char*)pcc_malloc(strlen(cmdbuf) + 1);
if (!cmdline) { close(fd); return -1; }
strcpy(cmdline, cmdbuf);
memset(&si, 0, sizeof si);
si.cb = sizeof si;
if (!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL,
NULL, &si, &pi)) {
pcc_error_noabort("cannot run pcc-cpp (%lu)", (unsigned long)GetLastError());
pcc_free(cmdline);
close(fd);
return -1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
pcc_free(cmdline);
}
#else
snprintf(cmdbuf, sizeof cmdbuf, "%s \"%s\" -o \"%s\"",
pcccpp, filename, tmpc);
if (system(cmdbuf) != 0) {
pcc_error_noabort("pcc-cpp failed on '%s'", filename);
close(fd);
return -1;
}
#endif
close(fd);
/* compile the generated C file */
fd = _pcc_open(s1, tmpc);
if (fd < 0) {
pcc_error_noabort("cannot open generated file '%s'", tmpc);
return -1;
}
flags &= ~AFF_TYPE_CPP;
flags |= AFF_TYPE_C;
dynarray_add(&s1->target_deps, &s1->nb_target_deps, pcc_strdup(tmpc));
return pcc_compile(s1, flags, tmpc, fd);
}
dynarray_add(&s1->target_deps, &s1->nb_target_deps, pcc_strdup(filename));
return pcc_compile(s1, flags, filename, fd);
}
LIBPCCAPI int pcc_add_file(PCCState *s, const char *filename)
{
return pcc_add_file_internal(s, filename, s->filetype | AFF_PRINT_ERROR);
}
+3
查看文件
@@ -790,6 +790,8 @@ struct PCCState {
unsigned char gnu_ext;
/* use PazeCC extensions */
unsigned char pcc_ext;
/* C++ mode (compile .cpp files with C++ extensions) */
unsigned char cpp_mode;
unsigned char dflag; /* -dX value */
unsigned char Pflag; /* -P switch (LINE_MACRO_OUTPUT_FORMAT) */
@@ -1285,6 +1287,7 @@ ST_FUNC int pcc_add_file_internal(PCCState *s1, const char *filename, int flags)
#define AFF_TYPE_ASM 2
#define AFF_TYPE_ASMPP 4
#define AFF_TYPE_LIB 8
#define AFF_TYPE_CPP 0x100 /* C++ source file */
#define AFF_TYPE_MASK (7 | AFF_TYPE_BIN)
/* values from pcc_object_type(...) */
#define AFF_BINTYPE_REL 1
+4 -2
查看文件
@@ -1702,7 +1702,8 @@ static int pragma_parse(PCCState *s1)
#pragma pack(pop) // restore previous */
next();
skip('(');
if (tok == TOK_ASM_pop) {
if (tok == TOK_ASM_pop || (tok >= TOK_IDENT &&
!strcmp(get_tok_str(tok, NULL), "pop"))) {
next();
if (s1->pack_stack_ptr <= s1->pack_stack) {
stk_error:
@@ -1712,7 +1713,8 @@ static int pragma_parse(PCCState *s1)
} else {
int val = 0;
if (tok != ')') {
if (tok == TOK_ASM_push) {
if (tok == TOK_ASM_push || (tok >= TOK_IDENT &&
!strcmp(get_tok_str(tok, NULL), "push"))) {
next();
if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
goto stk_error;
+16
查看文件
@@ -369,6 +369,22 @@
DEF(TOK_longjmp, "longjmp")
#endif
/* ------------ C++ keywords ------------ */
DEF(TOK_CLASS, "class")
DEF(TOK_CPP_PUBLIC, "public")
DEF(TOK_CPP_PRIVATE, "private")
DEF(TOK_CPP_PROTECTED, "protected")
DEF(TOK_THIS, "this")
DEF(TOK_CPP_NEW, "new")
DEF(TOK_CPP_DELETE, "delete")
DEF(TOK_CPP_NAMESPACE, "namespace")
DEF(TOK_CPP_USING, "using")
DEF(TOK_CPP_VIRTUAL, "virtual")
DEF(TOK_CPP_INLINE, "inline")
DEF(TOK_CPP_FRIEND, "friend")
DEF(TOK_CPP_TEMPLATE, "template")
DEF(TOK_CPP_CONSTEXPR, "constexpr")
/*********************************************************************/
/* Tiny Assembler */
+1037
查看文件
文件差异内容过多而无法显示 加载差异
+1 -1
查看文件
@@ -9,7 +9,7 @@
#include <_mingw.h>
#include <io.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -12,7 +12,7 @@
#define _DIRENT_H_
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#include <io.h>
+1 -1
查看文件
@@ -9,7 +9,7 @@
#include <_mingw.h>
#include <io.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -10,7 +10,7 @@
#include <_mingw.h>
#include <string.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifndef _POSIX_
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifndef _MM_MALLOC_H_INCLUDED
#define _MM_MALLOC_H_INCLUDED
+1 -1
查看文件
@@ -14,7 +14,7 @@
struct exception;
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#define _DOMAIN 1
#define _SING 2
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -9,7 +9,7 @@
#include <_mingw.h>
#include <limits.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -13,7 +13,7 @@
#include <_mingw.h>
#include <io.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -12,7 +12,7 @@
#error Only Win32 target is supported!
#endif
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -12,7 +12,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -12,7 +12,7 @@
#error Only Win32 target is supported!
#endif
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -8,7 +8,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {
+1 -1
查看文件
@@ -12,7 +12,7 @@
#include <_mingw.h>
#pragma pack(push,_CRT_PACKING)
#pragma pack(push,8)
#ifdef __cplusplus
extern "C" {