Skip to content

Commit

Permalink
Introduce DeallocateStmt class
Browse files Browse the repository at this point in the history
Implement full path to produce it in Parser and Sema.

For llvm-fortran#22
  • Loading branch information
ppenzin committed Feb 27, 2019
1 parent f9ea269 commit 18732b8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
17 changes: 17 additions & 0 deletions include/fort/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,23 @@ class AssignmentStmt : public Stmt {
}
};

/// DeallocateStmt
class DeallocateStmt : public Stmt, protected MultiArgumentExpr {
DeallocateStmt(ASTContext &C, SourceLocation Loc, ArrayRef<Expr *> ids,
Expr *StmtLabel);

public:
static DeallocateStmt *Create(ASTContext &C, SourceLocation Loc,
ArrayRef<Expr *> IDs, Expr *StmtLabel);

ArrayRef<Expr *> getIDList() const { return getArguments(); }

static bool classof(const DeallocateStmt *) { return true; }
static bool classof(const Stmt *S) {
return S->getStmtClass() == DeallocateStmtClass;
}
};

/// PrintStmt
class PrintStmt : public Stmt, protected MultiArgumentExpr {
FormatSpec *FS;
Expand Down
1 change: 1 addition & 0 deletions include/fort/Basic/StmtNodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ def StopStmt : Stmt;
def ReturnStmt : Stmt;
def CallStmt : Stmt;
def AssignmentStmt : Stmt;
def DeallocateStmt : Stmt;
def PrintStmt : Stmt;
def WriteStmt : Stmt;
2 changes: 2 additions & 0 deletions include/fort/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ class Sema {
FormatSpec *FS, ArrayRef<ExprResult> OutputItemList,
Expr *StmtLabel);

StmtResult ActOnDeallocateStmt(ASTContext &C, SourceLocation Loc,
ArrayRef<ExprResult> IDList, Expr *StmtLabel);
// FIXME: TODO:

QualType ActOnBuiltinType(ASTContext *Ctx, BuiltinType::TypeSpec TS,
Expand Down
13 changes: 13 additions & 0 deletions lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,19 @@ AssignmentStmt *AssignmentStmt::Create(ASTContext &C, SourceLocation Loc,
return new (C) AssignmentStmt(Loc, LHS, RHS, StmtLabel);
}

//===----------------------------------------------------------------------===//
// Deallocate Statement
//===----------------------------------------------------------------------===//

DeallocateStmt::DeallocateStmt(ASTContext &C, SourceLocation Loc,
ArrayRef<Expr *> ids, Expr *StmtLabel)
: Stmt(DeallocateStmtClass, Loc, StmtLabel), MultiArgumentExpr(C, ids) {}

DeallocateStmt *DeallocateStmt::Create(ASTContext &C, SourceLocation Loc,
ArrayRef<Expr *> IDs, Expr *StmtLabel) {
return new (C) DeallocateStmt(C, Loc, IDs, StmtLabel);
}

//===----------------------------------------------------------------------===//
// Print Statement
//===----------------------------------------------------------------------===//
Expand Down
16 changes: 13 additions & 3 deletions lib/Parse/ParseExec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,21 +719,31 @@ Parser::StmtResult Parser::ParseDEALLOCATEStmt() {
return StmtError();

// allocate-object-list
SmallVector<std::pair<IdentifierInfo, SmallVectorImpl<ArraySpec *>>, 8> Alloc;
SmallVector<ExprResult, 4> Alloc;
bool UnknownID = false;
do {
auto ID = Tok.getIdentifierInfo();
auto IDRange = getTokenRange();
auto IDLoc = Tok.getLocation();
if (!ExpectAndConsume(tok::identifier))
return StmtError();
auto VD = Actions.ExpectVarRefOrDeclImplicitVar(IDLoc, ID);
if (VD) {
auto Var = VarExpr::Create(Context, IDRange, VD);
Alloc.push_back(Var);
} else
UnknownID = true; // Accumulate errors
} while (ConsumeIfPresent(tok::comma));

if (UnknownID)
return StmtError();

// TODO [ , dealloc-opt-list ]

if (!ExpectAndConsume(tok::r_paren))
return StmtError();

// FIXME Sema
return StmtResult();
return Actions.ActOnDeallocateStmt(Context, Loc, Alloc, StmtLabel);
}

Parser::StmtResult Parser::ParseAmbiguousAssignmentStmt() {
Expand Down
17 changes: 17 additions & 0 deletions lib/Sema/SemaExecStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,21 @@ StmtResult Sema::ActOnCallStmt(ASTContext &C, SourceLocation Loc,
return Result;
}

StmtResult Sema::ActOnDeallocateStmt(ASTContext &C, SourceLocation Loc,
ArrayRef<ExprResult> IDList,
Expr *StmtLabel) {
SmallVector<Expr *, 4> DeallocList;
for (auto ID : IDList)
DeallocList.push_back(ID.take());

Stmt *Result = DeallocateStmt::Create(C, Loc, DeallocList, StmtLabel);

getCurrentBody()->Append(Result);

if (StmtLabel)
DeclareStatementLabel(StmtLabel, Result);

return Result;
}

} // end namespace fort
1 change: 1 addition & 0 deletions test/Parser/dynamicAssociation.f95
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ program a
deallocate(.5) ! expected-error {{expected identifier}}
deallocate(c ! expected-error {{expected ')'}}
deallocate(a,) ! expected-error {{expected identifier}}
deallocate(a)
end program
7 changes: 7 additions & 0 deletions test/Sema/dynamicAssociation.f95
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
! RUN: %fort -fsyntax-only -verify %s
program a
implicit none
integer, allocatable :: a(10)
deallocate(b) ! expected-error {{use of undeclared identifier 'b'}}
deallocate(a)
end program

0 comments on commit 18732b8

Please sign in to comment.