polserver/pol-core/bscript/compiler/ast/ClassDeclaration.h
Kevin Eady ddc1b5d26b
Add Escript support for uninitialized class functions (#808)
* update grammar

* AST nodes and test

* Address self-review comments
- Replace `super` comment with something more explanatory
- Rename `make_user_function`

* add class analysis for uninit functions

* fix ast test after disallowing static uninit funcs

* use std::ranges::find_if

* add tests

* use SUPER constant; add test

* Remove unreachable code leftover from #809

Since #809, the super function is only generated for a class when
registering a parent class, and therefore it will always have a body. A
"No base class defines a constructor" will only occur if super was never
generated, and is handled in the "No function linked through
FunctionResolver" section of semantic analyzer.

* Address review comments
- use std::ranges::move instead of move iterators
- use std::is_same_v<> vs std::is_same<>::value
- use switch vs chained ternary conditionals

* update escript guide, create tests that are in escript guide

* add core-changes

* Error on uninit and defined constructor

* Move methods' FunctionLink construction to builder

This allows the error message for the uninit function to have a "See
Also" that point to the defined function.

* Move error checks for uninit and defined functions

These semantic checks should be in the ... SemanticAnalyzer.

* fix typo in core-changes
2025-09-05 23:23:59 +02:00

56 lines
1.9 KiB
C++

#pragma once
#include <map>
#include "bscript/compiler/ast/Node.h"
#include "bscript/compiler/astbuilder/AvailableParseTree.h"
namespace Pol::Bscript::Compiler
{
class ClassParameterList;
class ClassBody;
class ClassLink;
class ClassParameterDeclaration;
class NodeVisitor;
class VarStatement;
class UninitializedFunctionDeclaration;
class UserFunction;
class FunctionLink;
using ClassMethodMap = std::map<std::string, std::shared_ptr<FunctionLink>, Clib::ci_cmp_pred>;
class ClassDeclaration : public Node
{
public:
ClassDeclaration(
const SourceLocation& source_location, std::string name,
std::unique_ptr<ClassParameterList> parameters,
std::shared_ptr<FunctionLink> constructor_link, ClassMethodMap methods, Node* body,
std::vector<std::shared_ptr<ClassLink>> base_classes,
std::vector<std::unique_ptr<UninitializedFunctionDeclaration>> uninit_functions );
void accept( NodeVisitor& visitor ) override;
void describe_to( std::string& ) const override;
std::vector<std::reference_wrapper<ClassParameterDeclaration>> parameters();
std::vector<std::reference_wrapper<UninitializedFunctionDeclaration>> uninit_functions();
std::string type_tag() const;
const std::string name;
// Only contains functions that are `UserFunctionType::Method`, ie. a first
// `this` parameter and not a constructor.
ClassMethodMap methods;
// Owned by top_level_statements
Node* class_body;
// `nullptr` if class has no constructor defined.
std::shared_ptr<FunctionLink> constructor_link;
// Passed as ctor parameter by UserFunctionBuilder when generating this AST
// node. The class links are immediately registered (inside
// UserFunctionBuilder) with the FunctionResolver so their parse trees will be
// visited by the second-pass UserFunctionVisitor.
std::vector<std::shared_ptr<ClassLink>> base_class_links;
};
} // namespace Pol::Bscript::Compiler