Files
carbon-lang/executable_semantics/ast/function_definition.h
T
Jon Meow fd89bcb4aa Convert Pattern and Expression to Ptr (#787)
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding.

That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution).

I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution).

I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
2021-08-27 09:16:20 -07:00

54 lines
1.9 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
#define EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
#include "common/ostream.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/source_location.h"
#include "executable_semantics/ast/statement.h"
#include "llvm/Support/Compiler.h"
namespace Carbon {
// TODO: expand the kinds of things that can be deduced parameters.
// For now, only generic parameters are supported.
struct GenericBinding {
std::string name;
Ptr<const Expression> type;
};
struct FunctionDefinition {
FunctionDefinition(SourceLocation source_location, std::string name,
std::vector<GenericBinding> deduced_params,
Ptr<const TuplePattern> param_pattern,
Ptr<const Pattern> return_type,
bool is_omitted_return_type, const Statement* body)
: source_location(source_location),
name(std::move(name)),
deduced_parameters(deduced_params),
param_pattern(param_pattern),
return_type(return_type),
is_omitted_return_type(is_omitted_return_type),
body(body) {}
void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
void PrintDepth(int depth, llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
SourceLocation source_location;
std::string name;
std::vector<GenericBinding> deduced_parameters;
Ptr<const TuplePattern> param_pattern;
Ptr<const Pattern> return_type;
bool is_omitted_return_type;
const Statement* body;
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_