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
42 changes: 37 additions & 5 deletions C/sema/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ void Compilation::computeSemanticModel(const SyntaxTree* tree)
{
PSY_ASSERT_2(P->isDirty_.count(tree), return);
if (P->isDirty_[tree]) {
bindDeclarations();
canonicalizerTypes();
resolveTypedefNameTypes();
checkTypes();
bindDeclarations(tree);
canonicalizeTypes(tree);
resolveTypedefNameTypes(tree);
checkTypes(tree);
P->isDirty_[tree] = false;
}
}
Expand Down Expand Up @@ -244,6 +244,14 @@ const BasicType* Compilation::canonicalBasicType(BasicTypeKind basicTyK) const
return P->tyIntU_.get();
}

void Compilation::bindDeclarations(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->semaModels_.count(tree), return);
auto model = P->semaModels_[tree].get();
DeclarationBinder binder(model, tree);
binder.bindDeclarations();
}

void Compilation::bindDeclarations() const
{
for (const auto& p : P->semaModels_) {
Expand All @@ -252,7 +260,15 @@ void Compilation::bindDeclarations() const
}
}

void Compilation::canonicalizerTypes() const
void Compilation::canonicalizeTypes(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->semaModels_.count(tree), return);
auto model = P->semaModels_[tree].get();
TypeCanonicalizer canonicalizer(model, tree);
canonicalizer.canonicalizeTypes();
}

void Compilation::canonicalizeTypes() const
{
for (const auto& p : P->semaModels_) {
TypeCanonicalizer canonicalizer(p.second.get(), p.first);
Expand All @@ -261,6 +277,14 @@ void Compilation::canonicalizerTypes() const
}


void Compilation::resolveTypedefNameTypes(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->semaModels_.count(tree), return);
auto model = P->semaModels_[tree].get();
TypedefNameTypeResolver resolver(model, tree);
resolver.resolveTypedefNameTypes();
}

void Compilation::resolveTypedefNameTypes() const
{
for (const auto& p : P->semaModels_) {
Expand All @@ -269,6 +293,14 @@ void Compilation::resolveTypedefNameTypes() const
}
}

void Compilation::checkTypes(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->semaModels_.count(tree), return);
auto model = P->semaModels_[tree].get();
TypeChecker checker(model, tree);
checker.checkTypes();
}

void Compilation::checkTypes() const
{
for (const auto& p : P->semaModels_) {
Expand Down
6 changes: 5 additions & 1 deletion C/sema/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ class PSY_C_API Compilation

ProgramSymbol* program();

void bindDeclarations(const SyntaxTree* tree) const;
void bindDeclarations() const;
void canonicalizerTypes() const;
void canonicalizeTypes(const SyntaxTree* tree) const;
void canonicalizeTypes() const;
void resolveTypedefNameTypes(const SyntaxTree* tree) const;
void resolveTypedefNameTypes() const;
void checkTypes(const SyntaxTree* tree) const;
void checkTypes() const;

private:
Expand Down
7 changes: 4 additions & 3 deletions C/sema/DeclarationBinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ DeclarationSymbol* DeclarationBinder::popDeclaration()
}


void DeclarationBinder::finishDeclaration()
DeclarationSymbol* DeclarationBinder::finishDeclaration()
{
auto decl = popDeclaration();
PSY_ASSERT_2(decl, return);
SCOPE_AT_TOP(auto scope, );
PSY_ASSERT_2(decl, return nullptr);
SCOPE_AT_TOP(auto scope, nullptr);
scope->addDeclaration(decl->asDeclaration());
return decl;
}

void DeclarationBinder::pushType(Type* ty)
Expand Down
2 changes: 1 addition & 1 deletion C/sema/DeclarationBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class PSY_C_INTERNAL_API DeclarationBinder final : protected SyntaxVisitor
void bindAnonymousFieldDeclaration(const SyntaxNode* node);
void nameDeclarationAtTop(const Identifier* name);
void typeDeclarationAtTopWithTypeAtTop();
void finishDeclaration();
void handleNonTypedefDeclarator(const DeclaratorSyntax* node);
DeclarationSymbol* finishDeclaration();

using TypeStack = std::stack<Type*>;
TypeStack tys_;
Expand Down
5 changes: 4 additions & 1 deletion C/sema/DeclarationBinder_End.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ SyntaxVisitor::Action DeclarationBinder::visitParameterDeclaration_AtEnd(
SyntaxVisitor::Action DeclarationBinder::visitFunctionDefinition_AtEnd(
const FunctionDefinitionSyntax* node)
{
finishDeclaration();
auto decl = finishDeclaration();
PSY_ASSERT_2(decl->kind() == SymbolKind::FunctionDeclaration, return Action::Quit);
auto funcDecl = decl->asFunctionDeclaration();
funcDecl->setIsDefinition(true);
popType();

/*
Expand Down
14 changes: 13 additions & 1 deletion C/symbols/Declaration_Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ struct FunctionDeclarationSymbol::FunctionImpl : DeclarationImpl
NameSpace::OrdinaryIdentifiers)
, name_(nullptr)
, ty_(nullptr)
{}
{
F_.isDef_ = 0;
}

const Identifier* name_;
const Type* ty_;
Expand Down Expand Up @@ -83,6 +85,16 @@ void FunctionDeclarationSymbol::setType(const Type* ty)
P_CAST->ty_ = ty;
}

bool FunctionDeclarationSymbol::isDefinition() const
{
return P_CAST->F_.isDef_;
}

void FunctionDeclarationSymbol::setIsDefinition(bool isDef)
{
P_CAST->F_.isDef_ = static_cast<std::uint32_t>(isDef);
}

namespace psy {
namespace C {

Expand Down
6 changes: 6 additions & 0 deletions C/symbols/Declaration_Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class PSY_C_API FunctionDeclarationSymbol final
*/
const Type* type() const override;

/**
* Whether \c this FunctionDeclarationSymbol is of a definition.
*/
bool isDefinition() const;

PSY_INTERNAL:
PSY_GRANT_INTERNAL_ACCESS(DeclarationBinder);
PSY_GRANT_INTERNAL_ACCESS(TypeChecker);
Expand All @@ -73,6 +78,7 @@ class PSY_C_API FunctionDeclarationSymbol final
virtual void setName(const Identifier* name) override;
virtual void setType(const Type* ty) override;
Type* type();
void setIsDefinition(bool isDef);
};

PSY_C_API std::ostream& operator<<(std::ostream& os, const FunctionDeclarationSymbol* func);
Expand Down
5 changes: 5 additions & 0 deletions C/symbols/Symbol_Declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const NameSpace DeclarationSymbol::nameSpace() const
return NameSpace(P->F_.ns_);
}

const SyntaxTree *DeclarationSymbol::declaringTree() const
{
return P_CAST->tree_;
}

DeclarationCategory DeclarationSymbol::category() const
{
switch (kind()) {
Expand Down
5 changes: 5 additions & 0 deletions C/symbols/Symbol_Declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class PSY_C_API DeclarationSymbol : public Symbol
*/
const NameSpace nameSpace() const;

/**
* The declaring SyntaxTree of \c this DeclarationSymbol.
*/
const SyntaxTree* declaringTree() const;

protected:
DECL_PIMPL_SUB(Declaration);
DeclarationSymbol(DeclarationImpl* p);
Expand Down
5 changes: 4 additions & 1 deletion C/symbols/Symbol__IMPL__.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ struct Symbol::SymbolImpl
{
std::uint32_t symK_ : 8;

// DeclarationSymbol.
// DeclarationSymbol
std::uint32_t ns_ : 2;
std::uint32_t static_ : 1;

// FunctionDeclarationSymbol
std::uint32_t isDef_ : 1;
};
union
{
Expand Down
4 changes: 2 additions & 2 deletions C/tests/TestSuite_Internals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ void InternalsTestSuite::canonicalizerAndResolveTypes(std::string text, Expectat
auto compilation = Compilation::create(tree->filePath());
compilation->addSyntaxTree(std::unique_ptr<const SyntaxTree>(tree));
compilation->bindDeclarations();
compilation->canonicalizerTypes();
compilation->canonicalizeTypes();
compilation->resolveTypedefNameTypes();
auto semaModel = compilation->semanticModel(tree);
checkSemanticModel(tree, semaModel, X);
Expand All @@ -944,7 +944,7 @@ void InternalsTestSuite::checkTypes(std::string text, Expectation X)
auto compilation = Compilation::create(tree->filePath());
compilation->addSyntaxTree(std::unique_ptr<const SyntaxTree>(tree));
compilation->bindDeclarations();
compilation->canonicalizerTypes();
compilation->canonicalizeTypes();
compilation->resolveTypedefNameTypes();
compilation->checkTypes();
auto semaModel = compilation->semanticModel(tree);
Expand Down