Skip to content

Commit

Permalink
type resolver increment, binder fix, and symbol addition
Browse files Browse the repository at this point in the history
  • Loading branch information
ltcmelo committed Sep 7, 2024
1 parent c0767c2 commit 7eadc57
Show file tree
Hide file tree
Showing 52 changed files with 707 additions and 329 deletions.
1 change: 1 addition & 0 deletions C/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ set(CFE_SOURCES
${PROJECT_SOURCE_DIR}/symbols/Symbol_Declaration.cpp
${PROJECT_SOURCE_DIR}/symbols/Symbol_Declaration.h
${PROJECT_SOURCE_DIR}/symbols/DeclarationKind.h
${PROJECT_SOURCE_DIR}/symbols/Declaration__IMPL__.inc
${PROJECT_SOURCE_DIR}/symbols/Declaration_Function.cpp
${PROJECT_SOURCE_DIR}/symbols/Declaration_Function.h
${PROJECT_SOURCE_DIR}/symbols/Declaration_Object.cpp
Expand Down
6 changes: 4 additions & 2 deletions C/binder/Binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "Binder__MACROS__.inc"

#include "SyntaxTree.h"

#include "binder/Scope.h"
#include "compilation/Compilation.h"
#include "symbols/Symbol_ALL.h"
Expand Down Expand Up @@ -143,7 +142,10 @@ const Identifier* Binder::identifier(const SyntaxToken& tk) const

SyntaxVisitor::Action Binder::visitTranslationUnit(const TranslationUnitSyntax* node)
{
std::unique_ptr<TranslationUnit> unit(new TranslationUnit(tree_));
std::unique_ptr<TranslationUnit> unit(
new TranslationUnit(
semaModel_->compilation()->program(),
tree_));
auto rawUnit = semaModel_->keepTranslationUnit(node, std::move(unit));
pushSymbol(rawUnit);
scopes_.push(rawUnit->enclosedScope_.get());
Expand Down
6 changes: 3 additions & 3 deletions C/binder/Binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PSY_C_INTERNAL_API Binder final : protected SyntaxVisitor

PSY_INTERNAL:
PSY_GRANT_INTERNAL_ACCESS(BinderTester);
PSY_GRANT_INTERNAL_ACCESS(SemanticModel);
PSY_GRANT_INTERNAL_ACCESS(Compilation);

Binder(SemanticModel* semaModel, const SyntaxTree* tree);
Binder(const Binder&) = delete;
Expand Down Expand Up @@ -212,8 +212,8 @@ class PSY_C_INTERNAL_API Binder final : protected SyntaxVisitor
template <class SymT, class... SymTArgs>
SymT* Binder::bindAndPushSymbol(const SyntaxNode* node, SymTArgs... args)
{
std::unique_ptr<SymT> sym(new SymT(tree_,
syms_.top(),
std::unique_ptr<SymT> sym(new SymT(syms_.top(),
tree_,
scopes_.top(),
std::forward<SymTArgs>(args)...));
auto rawSym = static_cast<SymT*>(semaModel_->keepBinding(node, std::move(sym)));
Expand Down
1 change: 1 addition & 0 deletions C/binder/Binder_Specifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ SyntaxVisitor::Action Binder::visitTypeQualifier(const TypeQualifierSyntax* node

default:
qualTy = makeType<QualifiedType>(ty);
popType();
pushType(qualTy);
break;
}
Expand Down
69 changes: 42 additions & 27 deletions C/binder/TypeResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "TypeResolver.h"

#include "binder/Scope.h"
#include "compilation/Compilation.h"
#include "compilation/SemanticModel.h"
#include "symbols/Symbol_ALL.h"
#include "syntax/Lexeme_Identifier.h"
Expand All @@ -42,24 +43,9 @@ TypeResolver::TypeResolver(SemanticModel* semaModel, const SyntaxTree* tree)
void TypeResolver::resolveTypes()
{
visit(tree_->root());
}

SyntaxVisitor::Action TypeResolver::visitTranslationUnit(const TranslationUnitSyntax* node)
{
for (auto declIt = node->declarations(); declIt; declIt = declIt->next)
visit(declIt->value);

return Action::Skip;
}

SyntaxVisitor::Action TypeResolver::visitVariableAndOrFunctionDeclaration(
const VariableAndOrFunctionDeclarationSyntax* node)
{
for (auto decltorIt = node->declarators(); decltorIt; decltorIt = decltorIt->next) {
visit(decltorIt->value);
}

return Action::Skip;
for (const auto& ty : discardedTys_)
semaModel_->dropType(ty);
}

SyntaxVisitor::Action TypeResolver::visitDeclarator_COMMON(const DeclaratorSyntax* node)
Expand All @@ -73,10 +59,7 @@ SyntaxVisitor::Action TypeResolver::visitDeclarator_COMMON(const DeclaratorSynta
auto typeableDecl = MIXIN_TypeableSymbol::from(decl);
auto ty = typeableDecl->retypeableType();
auto resolvedTy = resolveType(ty, decl->enclosingScope());
if (resolvedTy != ty) {
typeableDecl->setType(resolvedTy);
semaModel_->dropType(ty);
}
typeableDecl->setType(resolvedTy);
break;
}
}
Expand All @@ -97,7 +80,8 @@ SyntaxVisitor::Action TypeResolver::visitParenthesizedDeclarator(
return visitDeclarator_COMMON(node->innerDeclarator());
}

SyntaxVisitor::Action TypeResolver::visitIdentifierDeclarator(const IdentifierDeclaratorSyntax* node)
SyntaxVisitor::Action TypeResolver::visitIdentifierDeclarator(
const IdentifierDeclaratorSyntax* node)
{
return visitDeclarator_COMMON(node);
}
Expand All @@ -109,31 +93,61 @@ const Type* TypeResolver::resolveType(const Type* ty, const Scope* scope) const
auto arrTy = ty->asArrayType();
auto elemTy = arrTy->elementType();
auto resolvedTy = resolveType(elemTy, scope);
if (resolvedTy != elemTy)
if (resolvedTy != elemTy) {
arrTy->resetElementType(resolvedTy);
discardedTys_.insert(elemTy);
}
break;
}

case TypeKind::Basic: {
auto basicTy = ty->asBasicType();
auto resolvedTy =
semaModel_->compilation()->program()->canonicalBasicType(basicTy->kind());
if (resolvedTy != basicTy) {
discardedTys_.insert(ty);
return resolvedTy;
}
break;
}

case TypeKind::Basic:
case TypeKind::Void:
case TypeKind::Void: {
auto voidTy = ty->asVoidType();
auto resolvedTy = semaModel_->compilation()->program()->canonicalVoidType();
if (resolvedTy != voidTy) {
discardedTys_.insert(ty);
return voidTy;
}
break;
}

case TypeKind::Function: {
// TODO: parameters
auto funcTy = ty->asFunctionType();
auto retTy = funcTy->returnType();
auto resolvedTy = resolveType(retTy, scope);
if (resolvedTy != retTy)
funcTy->setReturnType(resolvedTy);
const auto parms = funcTy->parameterTypes();
const auto parmsSize = parms.size();
for (FunctionType::ParameterTypes::size_type idx = 0; idx < parmsSize; ++idx) {
const Type* parmTy = parms[idx];
resolvedTy = resolveType(parmTy, scope);
if (resolvedTy != parmTy) {
funcTy->setParameterType(idx, resolvedTy);
discardedTys_.insert(parmTy);
}
}
break;
}

case TypeKind::Pointer: {
auto ptrTy = ty->asPointerType();
auto refedTy = ptrTy->referencedType();
auto resolvedTy = resolveType(refedTy, scope);
if (resolvedTy != refedTy)
if (resolvedTy != refedTy) {
ptrTy->resetReferencedType(resolvedTy);
discardedTys_.insert(refedTy);
}
break;
}

Expand Down Expand Up @@ -171,6 +185,7 @@ const Type* TypeResolver::resolveType(const Type* ty, const Scope* scope) const
resolvedTy->kind() == TypeKind::Qualified
? resolvedTy->asQualifiedType()->unqualifiedType()
: resolvedTy);
discardedTys_.insert(unqualTy);
}
break;
}
Expand Down
11 changes: 4 additions & 7 deletions C/binder/TypeResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@

#include "../common/infra/AccessSpecifiers.h"

#include <unordered_set>

namespace psy {
namespace C {

class PSY_C_INTERNAL_API TypeResolver final : protected SyntaxVisitor
{
PSY_INTERNAL:
PSY_GRANT_INTERNAL_ACCESS(SemanticModel);
PSY_GRANT_INTERNAL_ACCESS(Compilation);

TypeResolver(SemanticModel* semaModel, const SyntaxTree* tree);
TypeResolver(const TypeResolver&) = delete;
Expand All @@ -44,15 +46,10 @@ class PSY_C_INTERNAL_API TypeResolver final : protected SyntaxVisitor

private:
SemanticModel* semaModel_;
mutable std::unordered_set<const Type*> discardedTys_;

const Type* resolveType(const Type* ty, const Scope* scope) const;

//--------------//
// Declarations //
//--------------//
virtual Action visitTranslationUnit(const TranslationUnitSyntax*) override;
virtual Action visitVariableAndOrFunctionDeclaration(const VariableAndOrFunctionDeclarationSyntax*) override;

/* Declarators */
Action visitDeclarator_COMMON(const DeclaratorSyntax*);
virtual Action visitPointerDeclarator(const PointerDeclaratorSyntax*) override;
Expand Down
39 changes: 33 additions & 6 deletions C/compilation/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "SemanticModel.h"
#include "SyntaxTree.h"

#include "binder/Binder.h"
#include "binder/TypeResolver.h"
#include "symbols/Symbol_ALL.h"

#include <algorithm>
Expand Down Expand Up @@ -84,7 +86,11 @@ void Compilation::addSyntaxTree(const SyntaxTree* tree)
if (it != P->semaModels_.end())
return;

P->semaModels_.insert(it, std::make_pair(tree, nullptr));
P->semaModels_.insert(
it,
std::make_pair(
tree,
new SemanticModel(tree, const_cast<Compilation*>(this))));
P->isDirty_[tree] = true;
tree->attachCompilation(this);
}
Expand All @@ -105,14 +111,35 @@ std::vector<const SyntaxTree*> Compilation::syntaxTrees() const
return trees;
}

const SemanticModel* Compilation::computeSemanticModel(const SyntaxTree* tree) const
const SemanticModel* Compilation::analyze(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->isDirty_.count(tree), return nullptr);
if (P->isDirty_[tree]) {
std::unique_ptr<SemanticModel> semaModel(new SemanticModel(tree, const_cast<Compilation*>(this)));
semaModel->applyBinder();
semaModel->applyTypeResolver();
P->semaModels_[tree] = std::move(semaModel);
bind();
resolveTypes();
P->isDirty_[tree] = false;
}
return semanticModel(tree);
}

void Compilation::bind() const
{
for (const auto& p : P->semaModels_) {
Binder binder(p.second.get(), p.first);
binder.bind();
}
}

void Compilation::resolveTypes() const
{
for (const auto& p : P->semaModels_) {
TypeResolver tyResolver(p.second.get(), p.first);
tyResolver.resolveTypes();
}
}

const SemanticModel* Compilation::semanticModel(const SyntaxTree* tree) const
{
PSY_ASSERT_2(P->semaModels_.count(tree), return nullptr);
return P->semaModels_[tree].get();
}
22 changes: 11 additions & 11 deletions C/compilation/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ class PSY_C_API Compilation
*/
static std::unique_ptr<Compilation> create(const std::string& id);

/**
* The Program in \c this Compilation.
*/
const Program* program() const;

/**
* Add a SyntaxTree to \c this Compilation.
*/
Expand All @@ -77,26 +72,31 @@ class PSY_C_API Compilation
std::vector<const SyntaxTree*> syntaxTrees() const;

/**
* The SemanticModel for the SyntaxTree \p tree in \c this Compilation.
* Analyze the SyntaxTree \p tree.
*
* \note Similar to:
* - \c Microsoft.CodeAnalysis.Compilation.GetSemanticModel of Roslyn.
*/
const SemanticModel* computeSemanticModel(const SyntaxTree* tree) const;
const SemanticModel* analyze(const SyntaxTree* tree) const;

/**
* The Program in \c this Compilation.
*/
const Program* program() const;

PSY_INTERNAL:
PSY_GRANT_INTERNAL_ACCESS(SemanticModel);

Program* program();
void bind() const;
void resolveTypes() const;
const SemanticModel* semanticModel(const SyntaxTree* tree) const;

private:
Compilation();

// Unavailable
DECL_PIMPL(Compilation);
Compilation(const Compilation&) = delete;
Compilation& operator=(const Compilation&) = delete;

DECL_PIMPL(Compilation);
};

} // C
Expand Down
15 changes: 2 additions & 13 deletions C/compilation/SemanticModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,11 @@ struct SemanticModel::SemanticModelImpl

SemanticModel::SemanticModel(const SyntaxTree* tree, Compilation* compilation)
: P(new SemanticModelImpl(tree, compilation))
{}

SemanticModel::~SemanticModel()
{}

void SemanticModel::applyBinder()
{
Binder binder(this, P->tree_);
binder.bind();
}

void SemanticModel::applyTypeResolver()
{
TypeResolver tyResolver(this, P->tree_);
tyResolver.resolveTypes();
}
SemanticModel::~SemanticModel()
{}

const SyntaxTree* SemanticModel::syntaxTree() const
{
Expand Down
3 changes: 0 additions & 3 deletions C/compilation/SemanticModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ class PSY_C_API SemanticModel

SemanticModel(const SyntaxTree* tree, Compilation* compilation);

void applyBinder();
void applyTypeResolver();

TranslationUnit* keepTranslationUnit(
const TranslationUnitSyntax* node,
std::unique_ptr<TranslationUnit> unitSym);
Expand Down
Loading

0 comments on commit 7eadc57

Please sign in to comment.