Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binder increments/fixes (and separate pp text/file) #146

Merged
merged 1 commit into from
Sep 29, 2024
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
1 change: 1 addition & 0 deletions C/binder/Binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class PSY_C_INTERNAL_API Binder final : protected SyntaxVisitor
virtual Action visitParameterSuffix(const ParameterSuffixSyntax*) override;
virtual Action visitIdentifierDeclarator(const IdentifierDeclaratorSyntax*) override;
virtual Action visitAbstractDeclarator(const AbstractDeclaratorSyntax*) override;
virtual Action visitBitfieldDeclarator(const BitfieldDeclaratorSyntax*) override;

//------------//
// Statements //
Expand Down
57 changes: 39 additions & 18 deletions C/binder/Binder_Declarators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "SyntaxTree.h"

#include "parser/Unparser.h"
#include "binder/Scope.h"
#include "compilation/SemanticModel.h"
#include "symbols/Symbol_ALL.h"
Expand All @@ -35,25 +36,26 @@
#include "../common/infra/Assertions.h"

#include <iostream>
#include <sstream>

using namespace psy;
using namespace C;

void Binder::nameDeclarationAtTop(const Identifier* name)
{
SYM_AT_TOP_V(sym);
DECL_TOP_SYM_ret(sym);
auto nameableDecl = MIXIN_NameableSymbol::from(sym);
PSY_ASSERT_2(nameableDecl, return);
nameableDecl->setName(name);
}

void Binder::typeDeclarationAtTopWithTypeAtTop()
{
SYM_AT_TOP_V(sym);
DECL_TOP_SYM_ret(sym);
auto typeableDecl = MIXIN_TypeableSymbol::from(sym);
PSY_ASSERT_2(typeableDecl, return);

TY_AT_TOP_V(ty);
DECL_TOP_TY_ret(ty);
typeableDecl->setType(ty);

if (!pendingFunTys_.empty()) {
Expand All @@ -64,16 +66,23 @@ void Binder::typeDeclarationAtTopWithTypeAtTop()

void Binder::popTypesUntilNonDerivedDeclaratorType()
{
PSY_ASSERT_2(!tys_.empty(), return);
auto ty = tys_.top();
DECL_TOP_TY_ret(ty);
while (true) {
switch (ty->kind()) {
case TypeKind::Qualified: {
auto unqualTyK = ty->asQualifiedType()->unqualifiedType()->kind();
if (!(unqualTyK == TypeKind::Array
|| unqualTyK == TypeKind::Function
|| unqualTyK == TypeKind::Pointer)) {
return;
}
[[fallthrough]];
}
case TypeKind::Array:
case TypeKind::Function:
case TypeKind::Pointer:
popType();
PSY_ASSERT_2(!tys_.empty(), return);
ty = tys_.top();
TOP_TY_ret(ty);
continue;
default:
return;
Expand All @@ -88,7 +97,7 @@ SyntaxVisitor::Action Binder::visitDeclaration_AtMultipleDeclarators_COMMON(
{
for (auto decltorIt = node->declarators(); decltorIt; decltorIt = decltorIt->next) {
visit(decltorIt->value);
SYM_AT_TOP(sym);
DECL_TOP_SYM_retQ(sym);
switch (sym->kind()) {
case SymbolKind::Declaration: {
auto decl = sym->asDeclaration();
Expand All @@ -103,7 +112,7 @@ SyntaxVisitor::Action Binder::visitDeclaration_AtMultipleDeclarators_COMMON(
typeDeclarationAtTopWithTypeAtTop();
popTypesUntilNonDerivedDeclaratorType();
popSymbol();
SCOPE_AT_TOP(scope);
DECL_TOP_SCOPE_retQ(scope);
scope->addDeclaration(decl);
break;
}
Expand Down Expand Up @@ -169,7 +178,7 @@ SyntaxVisitor::Action Binder::visitFunctionDefinition_AtDeclarator(const Functio

auto decl = popSymbolAsDeclaration();
PSY_ASSERT_2(decl, return Action::Quit);
SCOPE_AT_TOP(scope);
DECL_TOP_SCOPE_retQ(scope);
scope->addDeclaration(decl);

popType();
Expand All @@ -191,13 +200,13 @@ SyntaxVisitor::Action Binder::visitArrayOrFunctionDeclarator(const ArrayOrFuncti

switch (node->suffix()->kind()) {
case SyntaxKind::SubscriptSuffix: {
TY_AT_TOP(ty);
DECL_TOP_TY_retQ(ty);
pushType(makeType<ArrayType>(ty));
break;
}

case SyntaxKind::ParameterSuffix: {
TY_AT_TOP(ty);
DECL_TOP_TY_retQ(ty);
switch (ty->kind()) {
case TypeKind::Function:
diagReporter_.FunctionReturningFunction(
Expand Down Expand Up @@ -258,7 +267,7 @@ SyntaxVisitor::Action Binder::visitParameterSuffix(const ParameterSuffixSyntax*

SyntaxVisitor::Action Binder::visitPointerDeclarator(const PointerDeclaratorSyntax* node)
{
TY_AT_TOP(ty);
DECL_TOP_TY_retQ(ty);
pushType(makeType<PointerType>(ty));

for (auto specIt = node->qualifiersAndAttributes(); specIt; specIt = specIt->next)
Expand All @@ -276,13 +285,25 @@ SyntaxVisitor::Action Binder::visitParenthesizedDeclarator(const ParenthesizedDe
return Action::Skip;
}

SyntaxVisitor::Action Binder::visitBitfieldDeclarator(const BitfieldDeclaratorSyntax* node)
{
if (node->innerDeclarator()) {
visit(node->innerDeclarator());
} else {
bindObjectOrFunctionAndPushSymbol(node);
nameDeclarationAtTop(tree_->findIdentifier("", 0));
}

return Action::Skip;
}

void Binder::bindObjectOrFunctionAndPushSymbol(const SyntaxNode* node)
{
SCOPE_AT_TOP_V(scope);
DECL_TOP_SCOPE_ret(scope);
switch (scope->kind()) {
case ScopeKind::File:
case ScopeKind::Block: {
TY_AT_TOP_V(ty);
DECL_TOP_TY_ret(ty);
switch (ty->kind()) {
case TypeKind::Function:
bindAndPushSymbol<Function>(node);
Expand All @@ -295,7 +316,7 @@ void Binder::bindObjectOrFunctionAndPushSymbol(const SyntaxNode* node)
case TypeKind::Qualified:
case TypeKind::Typedef:
case TypeKind::Tag: {
SYM_AT_TOP_V(sym);
DECL_TOP_SYM_ret(sym);
switch (sym->kind()) {
case SymbolKind::Declaration: {
auto decl = sym->asDeclaration();
Expand Down Expand Up @@ -346,7 +367,7 @@ void Binder::bindObjectOrFunctionAndPushSymbol(const SyntaxNode* node)
break;

case ScopeKind::FunctionPrototype: {
TY_AT_TOP_V(ty);
DECL_TOP_TY_ret(ty);
switch (ty->kind()) {
case TypeKind::Array: {
/*
Expand All @@ -355,7 +376,7 @@ void Binder::bindObjectOrFunctionAndPushSymbol(const SyntaxNode* node)
* shall be adjusted to “qualified pointer to type”...
*/
popType();
TY_AT_TOP_V(otherTy);
DECL_TOP_TY_ret(otherTy);
auto ptrTy = makeType<PointerType>(otherTy);
pushType(ptrTy);
ptrTy->markAsArisingFromArrayDecay();
Expand Down
2 changes: 1 addition & 1 deletion C/binder/Binder_End.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ SyntaxVisitor::Action Binder::visitDeclaration_AtEnd_COMMON(const DeclarationSyn
{
auto decl = popSymbolAsDeclaration();
PSY_ASSERT_2(decl, return Action::Quit);
SCOPE_AT_TOP(scope);
DECL_TOP_SCOPE_retQ(scope);
scope->addDeclaration(decl->asDeclaration());

return Action::Skip;
Expand Down
Loading
Loading