mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 15:55:48 +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:
@@ -8,7 +8,6 @@
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/member.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/ast/static_scope.h"
|
||||
@@ -21,20 +20,6 @@ namespace Carbon {
|
||||
// Adds the names exposed by the given AST node to enclosing_scope.
|
||||
static void AddExposedNames(const Declaration& declaration,
|
||||
StaticScope& enclosing_scope);
|
||||
static void AddExposedNames(const Member& member, StaticScope& enclosing_scope);
|
||||
|
||||
static void AddExposedNames(const Member& member,
|
||||
StaticScope& enclosing_scope) {
|
||||
switch (member.kind()) {
|
||||
case MemberKind::FieldMember: {
|
||||
const auto& field = cast<FieldMember>(member);
|
||||
if (field.binding().name() != AnonymousName) {
|
||||
enclosing_scope.Add(field.binding().name(), &field.binding());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void AddExposedNames(const Declaration& declaration,
|
||||
StaticScope& enclosing_scope) {
|
||||
@@ -77,7 +62,6 @@ static void ResolveNames(Expression& expression,
|
||||
const StaticScope& enclosing_scope);
|
||||
static void ResolveNames(Pattern& pattern, StaticScope& enclosing_scope);
|
||||
static void ResolveNames(Statement& statement, StaticScope& enclosing_scope);
|
||||
static void ResolveNames(Member& member, StaticScope& enclosing_scope);
|
||||
static void ResolveNames(Declaration& declaration,
|
||||
StaticScope& enclosing_scope);
|
||||
|
||||
@@ -257,13 +241,6 @@ static void ResolveNames(Statement& statement, StaticScope& enclosing_scope) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ResolveNames(Member& member, StaticScope& enclosing_scope) {
|
||||
switch (member.kind()) {
|
||||
case MemberKind::FieldMember:
|
||||
ResolveNames(cast<FieldMember>(member).binding(), enclosing_scope);
|
||||
}
|
||||
}
|
||||
|
||||
static void ResolveNames(Declaration& declaration,
|
||||
StaticScope& enclosing_scope) {
|
||||
switch (declaration.kind()) {
|
||||
@@ -275,6 +252,9 @@ static void ResolveNames(Declaration& declaration,
|
||||
function_scope.Add(binding->name(), binding);
|
||||
ResolveNames(binding->type(), function_scope);
|
||||
}
|
||||
if (function.is_method()) {
|
||||
ResolveNames(function.me_pattern(), function_scope);
|
||||
}
|
||||
ResolveNames(function.param_pattern(), function_scope);
|
||||
if (function.return_term().type_expression().has_value()) {
|
||||
ResolveNames(**function.return_term().type_expression(),
|
||||
@@ -289,10 +269,11 @@ static void ResolveNames(Declaration& declaration,
|
||||
auto& class_decl = cast<ClassDeclaration>(declaration);
|
||||
StaticScope class_scope;
|
||||
class_scope.AddParent(&enclosing_scope);
|
||||
for (Nonnull<Member*> member : class_decl.members()) {
|
||||
class_scope.Add(class_decl.name(), &class_decl);
|
||||
for (Nonnull<Declaration*> member : class_decl.members()) {
|
||||
AddExposedNames(*member, class_scope);
|
||||
}
|
||||
for (Nonnull<Member*> member : class_decl.members()) {
|
||||
for (Nonnull<Declaration*> member : class_decl.members()) {
|
||||
ResolveNames(*member, class_scope);
|
||||
}
|
||||
break;
|
||||
@@ -316,7 +297,9 @@ static void ResolveNames(Declaration& declaration,
|
||||
case DeclarationKind::VariableDeclaration: {
|
||||
auto& var = cast<VariableDeclaration>(declaration);
|
||||
ResolveNames(var.binding(), enclosing_scope);
|
||||
ResolveNames(var.initializer(), enclosing_scope);
|
||||
if (var.has_initializer()) {
|
||||
ResolveNames(var.initializer(), enclosing_scope);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user