mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Adds basic support for class functions and methods. (#1057)
* adding methods to the ast * pre commit stuff? * implementation of class functions * implemented methods * some cleanup * more cleanup * add newlines in test programs * pre-commit fixups * added include of return_term.h * a test of a method calling another method * replacing Member with Declaration * removing the member.h etc files * clarify a type annotation * update uses of FunctionDeclaration * remove ReturnTarget, no longer needed * more cleanup * more cleanup * yet more cleanup, playing with pre-commit * did a pre-commit run --all-files * fixed const issue * remove comment * checking dependencies in BUILD files and headers * pre-commit working now * refactor NominalClassType to just hold a pointer to the class declaration * remove Member from rtti * responding to Geoffreys review * change field_types to a non-member function
This commit is contained in:
@@ -22,7 +22,7 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
case DeclarationKind::ClassDeclaration: {
|
||||
const auto& class_decl = cast<ClassDeclaration>(*this);
|
||||
out << "class " << class_decl.name() << " {\n";
|
||||
for (Nonnull<Member*> m : class_decl.members()) {
|
||||
for (Nonnull<Declaration*> m : class_decl.members()) {
|
||||
out << *m;
|
||||
}
|
||||
out << "}\n";
|
||||
@@ -41,7 +41,11 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
|
||||
case DeclarationKind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(*this);
|
||||
out << "var " << var.binding() << " = " << var.initializer() << "\n";
|
||||
out << "var " << var.binding();
|
||||
if (var.has_initializer()) {
|
||||
out << " = " << var.initializer();
|
||||
}
|
||||
out << ";\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +68,31 @@ void ReturnTerm::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
}
|
||||
|
||||
// Look for the `me` parameter in the `deduced_parameters_`
|
||||
// and put it in the `me_pattern_`.
|
||||
void FunctionDeclaration::ResolveDeducedAndReceiver(
|
||||
const std::vector<Nonnull<AstNode*>>& deduced_params) {
|
||||
for (Nonnull<AstNode*> param : deduced_params) {
|
||||
switch (param->kind()) {
|
||||
case AstNodeKind::GenericBinding:
|
||||
deduced_parameters_.push_back(&cast<GenericBinding>(*param));
|
||||
break;
|
||||
case AstNodeKind::BindingPattern: {
|
||||
Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
|
||||
if (me_pattern_.has_value() || bp->name() != "me") {
|
||||
FATAL_COMPILATION_ERROR(source_loc())
|
||||
<< "illegal binding pattern in implicit parameter list";
|
||||
}
|
||||
me_pattern_ = bp;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
FATAL_COMPILATION_ERROR(source_loc())
|
||||
<< "illegal AST node in implicit parameter list";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
out << "fn " << name_ << " ";
|
||||
if (!deduced_parameters_.empty()) {
|
||||
|
||||
Reference in New Issue
Block a user