Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,633 changes: 2,811 additions & 1,822 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions packages/cxx-gen-ast/src/gen_ast_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
});
});

emit(`
template <typename T>
[[nodiscard]] auto ast_cast(AST* ast) -> T* {
return ast && ast->kind() == T::Kind ? static_cast<T*>(ast) : nullptr;
}
`);

by_bases.forEach((nodes, base) => {
if (base === "AST") return;
if (!Array.isArray(nodes)) throw new Error("not an array");
Expand All @@ -116,15 +123,17 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
emit(` default: cxx_runtime_error("unexpected ${variantName}");`);
emit(` } // switch`);
emit(`}`);

emit();
emit(`[[nodiscard]] inline auto is${variantName}(AST* ast) -> bool {`);
emit(` if (!ast) return false;`);
emit(`template <>`);
emit(`[[nodiscard]] inline auto ast_cast<${base}>(AST* ast) -> ${base}* {`);
emit(` if (!ast) return nullptr;`);
emit(` switch (ast->kind()) {`);
nodes.forEach(({ name }) => {
emit(` case ${name}::Kind: `);
});
emit(` return true;`);
emit(` default: return false;`);
emit(` return static_cast<${base}*>(ast);`);
emit(` default: return nullptr;`);
emit(` } // switch`);
emit(`}`);
});
Expand Down Expand Up @@ -253,11 +262,6 @@ template <typename T>

${code.join("\n")}

template <typename T>
[[nodiscard]] auto ast_cast(AST* ast) -> T* {
return ast && ast->kind() == T::Kind ? static_cast<T*>(ast) : nullptr;
}

} // namespace cxx
`;

Expand Down
21 changes: 0 additions & 21 deletions packages/cxx-storybook/.eslintrc.cjs

This file was deleted.

1 change: 0 additions & 1 deletion packages/cxx-storybook/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { dirname, join } from "path";
import type { StorybookConfig } from "@storybook/react-vite";

const config: StorybookConfig = {
Expand Down
4 changes: 1 addition & 3 deletions packages/cxx-storybook/.storybook/preview.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@import "tailwindcss";

.sb-main-fullscreen #storybook-root {
height: 100svh;
Expand Down
29 changes: 29 additions & 0 deletions packages/cxx-storybook/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ["dist"] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ["**/*.{ts,tsx}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
"react-hooks": reactHooks,
"react-refresh": reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
"@typescript-eslint/no-explicit-any": "off",
},
}
);
35 changes: 19 additions & 16 deletions packages/cxx-storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,53 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint": "eslint .",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"devDependencies": {
"cxx-frontend": "^1.1.27",
"@chromatic-com/storybook": "latest",
"@codemirror/commands": "latest",
"@codemirror/lang-cpp": "latest",
"@codemirror/lint": "latest",
"@codemirror/state": "latest",
"@codemirror/view": "latest",
"@eslint/js": "latest",
"@storybook/addon-essentials": "latest",
"@storybook/addon-interactions": "latest",
"@storybook/addon-links": "latest",
"@storybook/addon-onboarding": "latest",
"@storybook/blocks": "latest",
"@storybook/react": "latest",
"@storybook/react-vite": "latest",
"@storybook/react": "latest",
"@storybook/test": "latest",
"@tailwindcss/vite": "latest",
"@tanstack/react-query": "latest",
"@types/lodash": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"@types/react-virtualized-auto-sizer": "latest",
"@types/react-window": "latest",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"@types/react": "latest",
"@vitejs/plugin-react": "latest",
"autoprefixer": "^10.4.20",
"clsx": "latest",
"codemirror": "latest",
"cxx-frontend": "^1.1.27",
"eslint": "latest",
"eslint-plugin-react-hooks": "latest",
"eslint-plugin-react-refresh": "latest",
"eslint-plugin-storybook": "latest",
"lodash": "latest",
"postcss": "^8.5.1",
"prop-types": "latest",
"react": "latest",
"eslint": "latest",
"globals": "latest",
"react-dom": "latest",
"react-virtualized-auto-sizer": "latest",
"react-window": "latest",
"react": "latest",
"storybook": "latest",
"tailwindcss": "latest",
"typescript-eslint": "latest",
"typescript": "latest",
"vite": "latest",
"clsx": "latest"
"vite": "latest"
},
"eslintConfig": {
"extends": [
"plugin:storybook/recommended"
]
}
}
6 changes: 0 additions & 6 deletions packages/cxx-storybook/postcss.config.js

This file was deleted.

35 changes: 26 additions & 9 deletions packages/cxx-storybook/src/SyntaxTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@

import { FixedSizeList } from "react-window";
import { AST, ASTKind, ASTSlot, Parser, TokenKind } from "cxx-frontend";
import { CSSProperties, useEffect, useLayoutEffect, useRef, useState } from "react";
import {
CSSProperties,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import AutoSizer from "react-virtualized-auto-sizer";
import clsx from "clsx";

Expand All @@ -33,7 +39,7 @@ function hasAccessOp(node: any): node is AST & { getAccessOp(): TokenKind } {
}

function hasAccessSpecifier(
node: any,
node: any
): node is AST & { getAccessSpecifier(): TokenKind } {
return (
typeof node.getAccessSpecifier === "function" && node.getAccessSpecifier()
Expand Down Expand Up @@ -65,7 +71,11 @@ interface SyntaxTreeNode {
slot?: ASTSlot;
}

export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTreeProps) {
export function SyntaxTree({
parser,
cursorPosition,
onNodeSelected,
}: SyntaxTreeProps) {
const listRef = useRef<FixedSizeList>(null);
const [selectedNodeHandle, setSelectedNodeHandle] = useState(0);
const [nodes, setNodes] = useState<SyntaxTreeNode[]>([]);
Expand All @@ -89,7 +99,7 @@ export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTre

++count;

if ((count % 1000) === 0) {
if (count % 1000 === 0) {
await new Promise((resolve) => setTimeout(resolve, 0));
}

Expand All @@ -100,7 +110,8 @@ export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTre
if (hasLiteral(node)) extra += ` (${node.getLiteral()})`;
if (hasOp(node)) extra += ` (${TokenKind[node.getOp()]})`;
if (hasAccessOp(node)) extra += ` (${TokenKind[node.getAccessOp()]})`;
if (hasSpecifier(node)) extra += ` (${TokenKind[node.getSpecifier()]})`;
if (hasSpecifier(node))
extra += ` (${TokenKind[node.getSpecifier()]})`;
if (hasAccessSpecifier(node))
extra += ` (${TokenKind[node.getAccessSpecifier()]})`;

Expand All @@ -114,7 +125,7 @@ export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTre
}

setNodes(nodes);
}
};

update();
}, [parser]);
Expand All @@ -129,7 +140,7 @@ export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTre
setSelectedNodeHandle(selectedNodeHandle);

const index = nodes.findIndex(
(node) => node.handle === selectedNodeHandle,
(node) => node.handle === selectedNodeHandle
);

if (index != -1) {
Expand All @@ -145,8 +156,14 @@ export function SyntaxTree({ parser, cursorPosition, onNodeSelected }: SyntaxTre

return (
<div className="whitespace-pre" style={style}>
{indent}- <a
className={clsx("cursor-default p-0.5 font-[monospace] text-xs", { "bg-sky-500 text-white": isSelected })}>{description}</a>
{indent}-{" "}
<a
className={clsx("cursor-default p-0.5 font-[monospace] text-xs", {
"bg-sky-500 text-white": isSelected,
})}
>
{description}
</a>
</div>
);
}
Expand Down
8 changes: 0 additions & 8 deletions packages/cxx-storybook/tailwind.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/cxx-storybook/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";

export default defineConfig({
plugins: [react()],
plugins: [react(), tailwindcss()],
});
6 changes: 1 addition & 5 deletions scripts/update-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ async function updateTest(unitTestPath) {

const fcheck = sourceLines[index]?.includes("-fcheck") ? "-fcheck" : "";

const ftemplates = sourceLines[index]?.includes("-ftemplates")
? "-ftemplates"
: "";

const ast =
await $`${cxx} -verify -ast-dump ${unitTestPath} ${fcheck} ${ftemplates}`.quiet();
await $`${cxx} -verify -ast-dump ${unitTestPath} ${fcheck}`.quiet();

const lines = ast.stdout.split("\n");

Expand Down
46 changes: 43 additions & 3 deletions src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// SOFTWARE.

#include <cxx/ast.h>
#include <cxx/ast_cursor.h>
#include <cxx/ast_visitor.h>
#include <cxx/cli.h>
#include <cxx/control.h>
Expand All @@ -43,7 +44,6 @@
#include <cxx/mlir/cxx_dialect.h>
#endif

#include <cassert>
#include <format>
#include <fstream>
#include <iostream>
Expand All @@ -55,8 +55,45 @@
#include "verify_diagnostics_client.h"

namespace {

using namespace cxx;

struct CheckExpressionTypes {
[[nodiscard]] auto operator()(TranslationUnit* unit) {
std::size_t missingTypes = 0;

// iterate over all expressions and check if they have a type
for (auto cursor = ASTCursor{unit->ast(), "unit"}; cursor;
cursor = ++cursor) {
const auto& current = *cursor;

if (!std::holds_alternative<AST*>(current.node)) {
// skip non-AST nodes
continue;
}

auto ast = std::get<AST*>(current.node);
auto expression = ast_cast<ExpressionAST>(ast);
if (!expression) {
// skip non-expression nodes
continue;
}

auto type = expression->type;
if (type) {
// the expression has a type
continue;
}

auto loc = expression->firstSourceLocation();
unit->warning(loc, std::format("missing type for expression"));
++missingTypes;
}

return missingTypes == 0;
}
};

auto readAll(const std::string& fileName, std::istream& in)
-> std::optional<std::string> {
std::string code;
Expand Down Expand Up @@ -277,11 +314,14 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
unit.parse(ParserConfiguration{
.checkTypes = cli.opt_fcheck,
.fuzzyTemplateResolution = true,
.staticAssert = cli.opt_fstatic_assert || cli.opt_fcheck,
.reflect = !cli.opt_fno_reflect,
.templates = cli.opt_ftemplates,
});

if (cli.opt_freport_missing_types) {
CheckExpressionTypes checkExpressionTypes;
const auto missingTypes = checkExpressionTypes(&unit);
}

if (cli.opt_dump_symbols && unit.globalScope()) {
dump(std::cout, unit.globalScope()->owner());
}
Expand Down
Loading
Loading