Skip to content

Commit e5f56c2

Browse files
authored
fix(parser): Remove qualifiers from pointer type
1 parent eab8b86 commit e5f56c2

File tree

7 files changed

+28
-43
lines changed

7 files changed

+28
-43
lines changed

Diff for: src/cxx/parser.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ bool Parser::parse_primary_expression(ExpressionAST*& yyast,
574574
ast->thisLoc = thisLoc;
575575

576576
if (auto classSymbol = sem->scope()->owner()->enclosingClass()) {
577-
ast->type = QualifiedType{
578-
types->pointerType(classSymbol->type(), Qualifiers::kNone)};
577+
ast->type = QualifiedType{types->pointerType(classSymbol->type())};
579578
}
580579

581580
return true;

Diff for: src/cxx/qualified_type.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class QualifiedType {
4141
Qualifiers qualifiers() const { return qualifiers_; }
4242
void setQualifiers(Qualifiers qualifiers) { qualifiers_ = qualifiers; }
4343

44-
QualifiedType unqualified() { return QualifiedType(type()); }
44+
QualifiedType unqualified() const { return QualifiedType(type()); }
4545

4646
void mergeWith(const QualifiedType& other) {
4747
qualifiers_ |= other.qualifiers();

Diff for: src/cxx/semantics.cc

+8-9
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ void Semantics::visit(NullptrLiteralExpressionAST* ast) {}
518518
void Semantics::visit(StringLiteralExpressionAST* ast) {
519519
QualifiedType charTy{types_->integerType(IntegerKind::kChar, false)};
520520
charTy.setQualifiers(Qualifiers::kConst);
521-
QualifiedType charPtrTy{types_->pointerType(charTy, Qualifiers::kNone)};
521+
QualifiedType charPtrTy{types_->pointerType(charTy)};
522522
expression_->type = charPtrTy;
523523
}
524524

@@ -602,8 +602,7 @@ void Semantics::visit(UnaryExpressionAST* ast) {
602602
break;
603603
}
604604
case TokenKind::T_AMP: {
605-
expression_->type = QualifiedType(
606-
types_->pointerType(expression.type, Qualifiers::kNone));
605+
expression_->type = QualifiedType(types_->pointerType(expression.type));
607606
break;
608607
}
609608
default: {
@@ -939,11 +938,10 @@ void Semantics::visit(StaticAssertDeclarationAST* ast) {
939938

940939
error(errorLoc, "non-constant condition for static assertion");
941940
} else if (!constValue.value()) {
942-
const auto errorLoc = ast->stringLiteralList
943-
? ast->stringLiteralList->value
944-
: ast->expression
945-
? ast->expression->firstSourceLocation()
946-
: ast->staticAssertLoc;
941+
const auto errorLoc =
942+
ast->stringLiteralList ? ast->stringLiteralList->value
943+
: ast->expression ? ast->expression->firstSourceLocation()
944+
: ast->staticAssertLoc;
947945

948946
if (ast->stringLiteralList) {
949947
std::string message;
@@ -1380,7 +1378,8 @@ void Semantics::visit(PointerOperatorAST* ast) {
13801378
this->specifiers(ast->cvQualifierList, &specifiers);
13811379
auto qualifiers = specifiers.type.qualifiers();
13821380

1383-
QualifiedType ptrTy(types_->pointerType(declarator_->type, qualifiers));
1381+
QualifiedType ptrTy(types_->pointerType(declarator_->type));
1382+
ptrTy.setQualifiers(qualifiers);
13841383

13851384
declarator_->type = ptrTy;
13861385
}

Diff for: src/cxx/type_environment.cc

+8-13
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ struct Hash {
100100
}
101101

102102
std::size_t operator()(const PointerType& type) const {
103-
return hash_combine(type.elementType(), type.qualifiers());
103+
return hash_value(type.elementType());
104104
}
105105

106106
std::size_t operator()(const PointerToMemberType& type) const {
107-
return hash_combine(type.classType(), type.elementType(),
108-
type.qualifiers());
107+
return hash_combine(type.classType(), type.elementType());
109108
}
110109

111110
std::size_t operator()(const ReferenceType& type) const {
@@ -208,15 +207,13 @@ struct EqualTo {
208207
}
209208

210209
bool operator()(const PointerType& type, const PointerType& other) const {
211-
return type.elementType() == other.elementType() &&
212-
type.qualifiers() == other.qualifiers();
210+
return type.elementType() == other.elementType();
213211
}
214212

215213
bool operator()(const PointerToMemberType& type,
216214
const PointerToMemberType& other) const {
217215
return type.classType() == other.classType() &&
218-
type.elementType() == other.elementType() &&
219-
type.qualifiers() == other.qualifiers();
216+
type.elementType() == other.elementType();
220217
}
221218

222219
bool operator()(const ReferenceType& type, const ReferenceType& other) const {
@@ -348,15 +345,13 @@ const ScopedEnumType* TypeEnvironment::scopedEnumType(
348345
}
349346

350347
const PointerType* TypeEnvironment::pointerType(
351-
const QualifiedType& elementType, Qualifiers qualifiers) {
352-
return &*d->pointerTypes.emplace(elementType, qualifiers).first;
348+
const QualifiedType& elementType) {
349+
return &*d->pointerTypes.emplace(elementType).first;
353350
}
354351

355352
const PointerToMemberType* TypeEnvironment::pointerToMemberType(
356-
const ClassType* classType, const QualifiedType& elementType,
357-
Qualifiers qualifiers) {
358-
return &*d->pointerToMemberTypes.emplace(classType, elementType, qualifiers)
359-
.first;
353+
const ClassType* classType, const QualifiedType& elementType) {
354+
return &*d->pointerToMemberTypes.emplace(classType, elementType).first;
360355
}
361356

362357
const ReferenceType* TypeEnvironment::referenceType(

Diff for: src/cxx/type_environment.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ class TypeEnvironment {
5555

5656
const ScopedEnumType* scopedEnumType(ScopedEnumSymbol* symbol);
5757

58-
const PointerType* pointerType(const QualifiedType& elementType,
59-
Qualifiers qualifiers);
58+
const PointerType* pointerType(const QualifiedType& elementType);
6059

6160
const PointerToMemberType* pointerToMemberType(
62-
const ClassType* classType, const QualifiedType& elementType,
63-
Qualifiers qualifiers);
61+
const ClassType* classType, const QualifiedType& elementType);
6462

6563
const ReferenceType* referenceType(const QualifiedType& elementType);
6664

Diff for: src/cxx/type_printer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ void TypePrinter::visit(const ScopedEnumType* type) {
168168

169169
void TypePrinter::visit(const PointerType* type) {
170170
ptrOps_ += "*";
171-
addQualifiers(ptrOps_, type->qualifiers());
172-
accept(type->elementType());
171+
addQualifiers(ptrOps_, type->elementType().qualifiers());
172+
accept(type->elementType().unqualified());
173173
}
174174

175175
void TypePrinter::visit(const PointerToMemberType* type) {

Diff for: src/cxx/types.h

+6-12
Original file line numberDiff line numberDiff line change
@@ -146,34 +146,28 @@ class ScopedEnumType final : public Type, public std::tuple<ScopedEnumSymbol*> {
146146
void accept(TypeVisitor* visitor) const override;
147147
};
148148

149-
class PointerType final : public Type,
150-
public std::tuple<QualifiedType, Qualifiers> {
149+
class PointerType final : public Type, public std::tuple<QualifiedType> {
151150
public:
152-
PointerType(const QualifiedType& elementType, Qualifiers qualifiers) noexcept
153-
: tuple(elementType, qualifiers) {}
151+
explicit PointerType(const QualifiedType& elementType) noexcept
152+
: tuple(elementType) {}
154153

155154
const QualifiedType& elementType() const { return get<0>(*this); }
156155

157-
Qualifiers qualifiers() const { return get<1>(*this); }
158-
159156
void accept(TypeVisitor* visitor) const override;
160157
};
161158

162159
class PointerToMemberType final
163160
: public Type,
164-
public std::tuple<const ClassType*, QualifiedType, Qualifiers> {
161+
public std::tuple<const ClassType*, QualifiedType> {
165162
public:
166163
PointerToMemberType(const ClassType* classType,
167-
const QualifiedType& elementType,
168-
Qualifiers qualifiers) noexcept
169-
: tuple(classType, elementType, qualifiers) {}
164+
const QualifiedType& elementType) noexcept
165+
: tuple(classType, elementType) {}
170166

171167
const ClassType* classType() const { return get<0>(*this); }
172168

173169
const QualifiedType& elementType() const { return get<1>(*this); }
174170

175-
Qualifiers qualifiers() const { return get<2>(*this); }
176-
177171
void accept(TypeVisitor* visitor) const override;
178172
};
179173

0 commit comments

Comments
 (0)