resolve_names.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/interpreter/resolve_names.h"
  5. #include <set>
  6. #include "explorer/ast/declaration.h"
  7. #include "explorer/ast/expression.h"
  8. #include "explorer/ast/pattern.h"
  9. #include "explorer/ast/statement.h"
  10. #include "explorer/ast/static_scope.h"
  11. #include "llvm/Support/Casting.h"
  12. #include "llvm/Support/Error.h"
  13. using llvm::cast;
  14. namespace Carbon {
  15. // Adds the names exposed by the given AST node to enclosing_scope.
  16. static auto AddExposedNames(const Declaration& declaration,
  17. StaticScope& enclosing_scope) -> ErrorOr<Success> {
  18. switch (declaration.kind()) {
  19. case DeclarationKind::InterfaceDeclaration: {
  20. auto& iface_decl = cast<InterfaceDeclaration>(declaration);
  21. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(iface_decl.name(), &iface_decl,
  22. /*usable=*/false));
  23. break;
  24. }
  25. case DeclarationKind::ImplDeclaration: {
  26. // Nothing to do here
  27. break;
  28. }
  29. case DeclarationKind::FunctionDeclaration: {
  30. auto& func = cast<FunctionDeclaration>(declaration);
  31. CARBON_RETURN_IF_ERROR(
  32. enclosing_scope.Add(func.name(), &func, /*usable=*/false));
  33. break;
  34. }
  35. case DeclarationKind::ClassDeclaration: {
  36. auto& class_decl = cast<ClassDeclaration>(declaration);
  37. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(class_decl.name(), &class_decl,
  38. /*usable=*/false));
  39. break;
  40. }
  41. case DeclarationKind::ChoiceDeclaration: {
  42. auto& choice = cast<ChoiceDeclaration>(declaration);
  43. CARBON_RETURN_IF_ERROR(
  44. enclosing_scope.Add(choice.name(), &choice, /*usable=*/false));
  45. break;
  46. }
  47. case DeclarationKind::VariableDeclaration: {
  48. auto& var = cast<VariableDeclaration>(declaration);
  49. if (var.binding().name() != AnonymousName) {
  50. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(
  51. var.binding().name(), &var.binding(), /*usable=*/false));
  52. }
  53. break;
  54. }
  55. case DeclarationKind::SelfDeclaration: {
  56. auto& self = cast<SelfDeclaration>(declaration);
  57. CARBON_RETURN_IF_ERROR(enclosing_scope.Add("Self", &self));
  58. break;
  59. }
  60. case DeclarationKind::AliasDeclaration: {
  61. auto& alias = cast<AliasDeclaration>(declaration);
  62. CARBON_RETURN_IF_ERROR(
  63. enclosing_scope.Add(alias.name(), &alias, /*usable=*/false));
  64. break;
  65. }
  66. }
  67. return Success();
  68. }
  69. namespace {
  70. enum class ResolveFunctionBodies {
  71. // Do not resolve names in function bodies.
  72. Skip,
  73. // Resolve all names. When visiting a declaration with members, resolve
  74. // names in member function bodies after resolving the names in all member
  75. // declarations, as if the bodies appeared after all the declarations.
  76. AfterDeclarations,
  77. // Resolve names in function bodies immediately. This is appropriate when
  78. // the declarations of all members of enclosing classes, interfaces, and
  79. // similar have already been resolved.
  80. Immediately,
  81. };
  82. } // namespace
  83. // Traverses the sub-AST rooted at the given node, resolving all names within
  84. // it using enclosing_scope, and updating enclosing_scope to add names to
  85. // it as they become available. In scopes where names are only visible below
  86. // their point of declaration (such as block scopes in C++), this is implemented
  87. // as a single pass, recursively calling ResolveNames on the elements of the
  88. // scope in order. In scopes where names are also visible above their point of
  89. // declaration (such as class scopes in C++), this requires three passes: first
  90. // calling AddExposedNames on each element of the scope to populate a
  91. // StaticScope, and then calling ResolveNames on each element, passing it the
  92. // already-populated StaticScope but skipping member function bodies, and
  93. // finally calling ResolvedNames again on each element, and this time resolving
  94. // member function bodies.
  95. static auto ResolveNames(Expression& expression,
  96. const StaticScope& enclosing_scope)
  97. -> ErrorOr<Success>;
  98. static auto ResolveNames(WhereClause& clause,
  99. const StaticScope& enclosing_scope)
  100. -> ErrorOr<Success>;
  101. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  102. -> ErrorOr<Success>;
  103. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  104. -> ErrorOr<Success>;
  105. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope,
  106. ResolveFunctionBodies bodies) -> ErrorOr<Success>;
  107. static auto ResolveNames(Expression& expression,
  108. const StaticScope& enclosing_scope)
  109. -> ErrorOr<Success> {
  110. switch (expression.kind()) {
  111. case ExpressionKind::CallExpression: {
  112. auto& call = cast<CallExpression>(expression);
  113. CARBON_RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope));
  114. CARBON_RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope));
  115. break;
  116. }
  117. case ExpressionKind::FunctionTypeLiteral: {
  118. auto& fun_type = cast<FunctionTypeLiteral>(expression);
  119. CARBON_RETURN_IF_ERROR(
  120. ResolveNames(fun_type.parameter(), enclosing_scope));
  121. CARBON_RETURN_IF_ERROR(
  122. ResolveNames(fun_type.return_type(), enclosing_scope));
  123. break;
  124. }
  125. case ExpressionKind::SimpleMemberAccessExpression:
  126. CARBON_RETURN_IF_ERROR(
  127. ResolveNames(cast<SimpleMemberAccessExpression>(expression).object(),
  128. enclosing_scope));
  129. break;
  130. case ExpressionKind::CompoundMemberAccessExpression: {
  131. auto& access = cast<CompoundMemberAccessExpression>(expression);
  132. CARBON_RETURN_IF_ERROR(ResolveNames(access.object(), enclosing_scope));
  133. CARBON_RETURN_IF_ERROR(ResolveNames(access.path(), enclosing_scope));
  134. break;
  135. }
  136. case ExpressionKind::IndexExpression: {
  137. auto& index = cast<IndexExpression>(expression);
  138. CARBON_RETURN_IF_ERROR(ResolveNames(index.object(), enclosing_scope));
  139. CARBON_RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope));
  140. break;
  141. }
  142. case ExpressionKind::PrimitiveOperatorExpression:
  143. for (Nonnull<Expression*> operand :
  144. cast<PrimitiveOperatorExpression>(expression).arguments()) {
  145. CARBON_RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope));
  146. }
  147. break;
  148. case ExpressionKind::TupleLiteral:
  149. for (Nonnull<Expression*> field :
  150. cast<TupleLiteral>(expression).fields()) {
  151. CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  152. }
  153. break;
  154. case ExpressionKind::StructLiteral:
  155. for (FieldInitializer& init : cast<StructLiteral>(expression).fields()) {
  156. CARBON_RETURN_IF_ERROR(
  157. ResolveNames(init.expression(), enclosing_scope));
  158. }
  159. break;
  160. case ExpressionKind::StructTypeLiteral:
  161. for (FieldInitializer& init :
  162. cast<StructTypeLiteral>(expression).fields()) {
  163. CARBON_RETURN_IF_ERROR(
  164. ResolveNames(init.expression(), enclosing_scope));
  165. }
  166. break;
  167. case ExpressionKind::IdentifierExpression: {
  168. auto& identifier = cast<IdentifierExpression>(expression);
  169. CARBON_ASSIGN_OR_RETURN(
  170. const auto value_node,
  171. enclosing_scope.Resolve(identifier.name(), identifier.source_loc()));
  172. identifier.set_value_node(value_node);
  173. break;
  174. }
  175. case ExpressionKind::DotSelfExpression: {
  176. auto& dot_self = cast<DotSelfExpression>(expression);
  177. CARBON_ASSIGN_OR_RETURN(
  178. const auto value_node,
  179. enclosing_scope.Resolve(".Self", dot_self.source_loc()));
  180. dot_self.set_self_binding(const_cast<GenericBinding*>(
  181. &cast<GenericBinding>(value_node.base())));
  182. break;
  183. }
  184. case ExpressionKind::IntrinsicExpression:
  185. CARBON_RETURN_IF_ERROR(ResolveNames(
  186. cast<IntrinsicExpression>(expression).args(), enclosing_scope));
  187. break;
  188. case ExpressionKind::IfExpression: {
  189. auto& if_expr = cast<IfExpression>(expression);
  190. CARBON_RETURN_IF_ERROR(
  191. ResolveNames(if_expr.condition(), enclosing_scope));
  192. CARBON_RETURN_IF_ERROR(
  193. ResolveNames(if_expr.then_expression(), enclosing_scope));
  194. CARBON_RETURN_IF_ERROR(
  195. ResolveNames(if_expr.else_expression(), enclosing_scope));
  196. break;
  197. }
  198. case ExpressionKind::WhereExpression: {
  199. auto& where = cast<WhereExpression>(expression);
  200. CARBON_RETURN_IF_ERROR(
  201. ResolveNames(where.self_binding().type(), enclosing_scope));
  202. // Introduce `.Self` into scope on the right of the `where` keyword.
  203. StaticScope where_scope;
  204. where_scope.AddParent(&enclosing_scope);
  205. CARBON_RETURN_IF_ERROR(where_scope.Add(".Self", &where.self_binding()));
  206. for (Nonnull<WhereClause*> clause : where.clauses()) {
  207. CARBON_RETURN_IF_ERROR(ResolveNames(*clause, where_scope));
  208. }
  209. break;
  210. }
  211. case ExpressionKind::ArrayTypeLiteral: {
  212. auto& array_literal = cast<ArrayTypeLiteral>(expression);
  213. CARBON_RETURN_IF_ERROR(ResolveNames(
  214. array_literal.element_type_expression(), enclosing_scope));
  215. CARBON_RETURN_IF_ERROR(
  216. ResolveNames(array_literal.size_expression(), enclosing_scope));
  217. break;
  218. }
  219. case ExpressionKind::BoolTypeLiteral:
  220. case ExpressionKind::BoolLiteral:
  221. case ExpressionKind::IntTypeLiteral:
  222. case ExpressionKind::ContinuationTypeLiteral:
  223. case ExpressionKind::IntLiteral:
  224. case ExpressionKind::StringLiteral:
  225. case ExpressionKind::StringTypeLiteral:
  226. case ExpressionKind::TypeTypeLiteral:
  227. case ExpressionKind::ValueLiteral:
  228. break;
  229. case ExpressionKind::InstantiateImpl: // created after name resolution
  230. case ExpressionKind::UnimplementedExpression:
  231. return CompilationError(expression.source_loc()) << "Unimplemented";
  232. }
  233. return Success();
  234. }
  235. static auto ResolveNames(WhereClause& clause,
  236. const StaticScope& enclosing_scope)
  237. -> ErrorOr<Success> {
  238. switch (clause.kind()) {
  239. case WhereClauseKind::IsWhereClause: {
  240. auto& is_clause = cast<IsWhereClause>(clause);
  241. CARBON_RETURN_IF_ERROR(ResolveNames(is_clause.type(), enclosing_scope));
  242. CARBON_RETURN_IF_ERROR(
  243. ResolveNames(is_clause.constraint(), enclosing_scope));
  244. break;
  245. }
  246. case WhereClauseKind::EqualsWhereClause: {
  247. auto& equals_clause = cast<EqualsWhereClause>(clause);
  248. CARBON_RETURN_IF_ERROR(
  249. ResolveNames(equals_clause.lhs(), enclosing_scope));
  250. CARBON_RETURN_IF_ERROR(
  251. ResolveNames(equals_clause.rhs(), enclosing_scope));
  252. break;
  253. }
  254. }
  255. return Success();
  256. }
  257. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  258. -> ErrorOr<Success> {
  259. switch (pattern.kind()) {
  260. case PatternKind::BindingPattern: {
  261. auto& binding = cast<BindingPattern>(pattern);
  262. CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  263. if (binding.name() != AnonymousName) {
  264. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  265. }
  266. break;
  267. }
  268. case PatternKind::GenericBinding: {
  269. auto& binding = cast<GenericBinding>(pattern);
  270. // `.Self` is in scope in the context of the type.
  271. StaticScope self_scope;
  272. self_scope.AddParent(&enclosing_scope);
  273. CARBON_RETURN_IF_ERROR(self_scope.Add(".Self", &binding));
  274. CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), self_scope));
  275. if (binding.name() != AnonymousName) {
  276. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  277. }
  278. break;
  279. }
  280. case PatternKind::TuplePattern:
  281. for (Nonnull<Pattern*> field : cast<TuplePattern>(pattern).fields()) {
  282. CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  283. }
  284. break;
  285. case PatternKind::AlternativePattern: {
  286. auto& alternative = cast<AlternativePattern>(pattern);
  287. CARBON_RETURN_IF_ERROR(
  288. ResolveNames(alternative.choice_type(), enclosing_scope));
  289. CARBON_RETURN_IF_ERROR(
  290. ResolveNames(alternative.arguments(), enclosing_scope));
  291. break;
  292. }
  293. case PatternKind::ExpressionPattern:
  294. CARBON_RETURN_IF_ERROR(ResolveNames(
  295. cast<ExpressionPattern>(pattern).expression(), enclosing_scope));
  296. break;
  297. case PatternKind::AutoPattern:
  298. break;
  299. case PatternKind::VarPattern:
  300. CARBON_RETURN_IF_ERROR(
  301. ResolveNames(cast<VarPattern>(pattern).pattern(), enclosing_scope));
  302. break;
  303. case PatternKind::AddrPattern:
  304. CARBON_RETURN_IF_ERROR(
  305. ResolveNames(cast<AddrPattern>(pattern).binding(), enclosing_scope));
  306. break;
  307. }
  308. return Success();
  309. }
  310. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  311. -> ErrorOr<Success> {
  312. switch (statement.kind()) {
  313. case StatementKind::ExpressionStatement:
  314. CARBON_RETURN_IF_ERROR(ResolveNames(
  315. cast<ExpressionStatement>(statement).expression(), enclosing_scope));
  316. break;
  317. case StatementKind::Assign: {
  318. auto& assign = cast<Assign>(statement);
  319. CARBON_RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope));
  320. CARBON_RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope));
  321. break;
  322. }
  323. case StatementKind::VariableDefinition: {
  324. auto& def = cast<VariableDefinition>(statement);
  325. CARBON_RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope));
  326. CARBON_RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope));
  327. break;
  328. }
  329. case StatementKind::If: {
  330. auto& if_stmt = cast<If>(statement);
  331. CARBON_RETURN_IF_ERROR(
  332. ResolveNames(if_stmt.condition(), enclosing_scope));
  333. CARBON_RETURN_IF_ERROR(
  334. ResolveNames(if_stmt.then_block(), enclosing_scope));
  335. if (if_stmt.else_block().has_value()) {
  336. CARBON_RETURN_IF_ERROR(
  337. ResolveNames(**if_stmt.else_block(), enclosing_scope));
  338. }
  339. break;
  340. }
  341. case StatementKind::Return:
  342. CARBON_RETURN_IF_ERROR(
  343. ResolveNames(cast<Return>(statement).expression(), enclosing_scope));
  344. break;
  345. case StatementKind::Block: {
  346. auto& block = cast<Block>(statement);
  347. StaticScope block_scope;
  348. block_scope.AddParent(&enclosing_scope);
  349. for (Nonnull<Statement*> sub_statement : block.statements()) {
  350. CARBON_RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope));
  351. }
  352. break;
  353. }
  354. case StatementKind::While: {
  355. auto& while_stmt = cast<While>(statement);
  356. CARBON_RETURN_IF_ERROR(
  357. ResolveNames(while_stmt.condition(), enclosing_scope));
  358. CARBON_RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope));
  359. break;
  360. }
  361. case StatementKind::Match: {
  362. auto& match = cast<Match>(statement);
  363. CARBON_RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope));
  364. for (Match::Clause& clause : match.clauses()) {
  365. StaticScope clause_scope;
  366. clause_scope.AddParent(&enclosing_scope);
  367. CARBON_RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope));
  368. CARBON_RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope));
  369. }
  370. break;
  371. }
  372. case StatementKind::Continuation: {
  373. auto& continuation = cast<Continuation>(statement);
  374. CARBON_RETURN_IF_ERROR(enclosing_scope.Add(
  375. continuation.name(), &continuation, /*usable=*/false));
  376. StaticScope continuation_scope;
  377. continuation_scope.AddParent(&enclosing_scope);
  378. CARBON_RETURN_IF_ERROR(ResolveNames(cast<Continuation>(statement).body(),
  379. continuation_scope));
  380. enclosing_scope.MarkUsable(continuation.name());
  381. break;
  382. }
  383. case StatementKind::Run:
  384. CARBON_RETURN_IF_ERROR(
  385. ResolveNames(cast<Run>(statement).argument(), enclosing_scope));
  386. break;
  387. case StatementKind::Await:
  388. case StatementKind::Break:
  389. case StatementKind::Continue:
  390. break;
  391. }
  392. return Success();
  393. }
  394. static auto ResolveMemberNames(llvm::ArrayRef<Nonnull<Declaration*>> members,
  395. StaticScope& scope, ResolveFunctionBodies bodies)
  396. -> ErrorOr<Success> {
  397. for (Nonnull<Declaration*> member : members) {
  398. CARBON_RETURN_IF_ERROR(AddExposedNames(*member, scope));
  399. }
  400. if (bodies != ResolveFunctionBodies::Immediately) {
  401. for (Nonnull<Declaration*> member : members) {
  402. CARBON_RETURN_IF_ERROR(
  403. ResolveNames(*member, scope, ResolveFunctionBodies::Skip));
  404. }
  405. }
  406. if (bodies != ResolveFunctionBodies::Skip) {
  407. for (Nonnull<Declaration*> member : members) {
  408. CARBON_RETURN_IF_ERROR(
  409. ResolveNames(*member, scope, ResolveFunctionBodies::Immediately));
  410. }
  411. }
  412. return Success();
  413. }
  414. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope,
  415. ResolveFunctionBodies bodies) -> ErrorOr<Success> {
  416. switch (declaration.kind()) {
  417. case DeclarationKind::InterfaceDeclaration: {
  418. auto& iface = cast<InterfaceDeclaration>(declaration);
  419. StaticScope iface_scope;
  420. iface_scope.AddParent(&enclosing_scope);
  421. if (iface.params().has_value()) {
  422. CARBON_RETURN_IF_ERROR(ResolveNames(**iface.params(), iface_scope));
  423. }
  424. enclosing_scope.MarkUsable(iface.name());
  425. CARBON_RETURN_IF_ERROR(iface_scope.Add("Self", iface.self()));
  426. CARBON_RETURN_IF_ERROR(
  427. ResolveMemberNames(iface.members(), iface_scope, bodies));
  428. break;
  429. }
  430. case DeclarationKind::ImplDeclaration: {
  431. auto& impl = cast<ImplDeclaration>(declaration);
  432. StaticScope impl_scope;
  433. impl_scope.AddParent(&enclosing_scope);
  434. for (Nonnull<GenericBinding*> binding : impl.deduced_parameters()) {
  435. CARBON_RETURN_IF_ERROR(ResolveNames(binding->type(), impl_scope));
  436. CARBON_RETURN_IF_ERROR(impl_scope.Add(binding->name(), binding));
  437. }
  438. CARBON_RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), impl_scope));
  439. // Only add `Self` to the impl_scope if it is not already in the enclosing
  440. // scope. Add `Self` after we resolve names for the impl_type, so you
  441. // can't write something like `impl Vector(Self) as ...`. Add `Self`
  442. // before resolving names in the interface, so you can write something
  443. // like `impl VeryLongTypeName as AddWith(Self)`
  444. if (!enclosing_scope.Resolve("Self", impl.source_loc()).ok()) {
  445. CARBON_RETURN_IF_ERROR(AddExposedNames(*impl.self(), impl_scope));
  446. }
  447. CARBON_RETURN_IF_ERROR(ResolveNames(impl.interface(), impl_scope));
  448. CARBON_RETURN_IF_ERROR(
  449. ResolveMemberNames(impl.members(), impl_scope, bodies));
  450. break;
  451. }
  452. case DeclarationKind::FunctionDeclaration: {
  453. auto& function = cast<FunctionDeclaration>(declaration);
  454. StaticScope function_scope;
  455. function_scope.AddParent(&enclosing_scope);
  456. for (Nonnull<GenericBinding*> binding : function.deduced_parameters()) {
  457. CARBON_RETURN_IF_ERROR(ResolveNames(*binding, function_scope));
  458. }
  459. if (function.is_method()) {
  460. CARBON_RETURN_IF_ERROR(
  461. ResolveNames(function.me_pattern(), function_scope));
  462. }
  463. CARBON_RETURN_IF_ERROR(
  464. ResolveNames(function.param_pattern(), function_scope));
  465. if (function.return_term().type_expression().has_value()) {
  466. CARBON_RETURN_IF_ERROR(ResolveNames(
  467. **function.return_term().type_expression(), function_scope));
  468. }
  469. enclosing_scope.MarkUsable(function.name());
  470. if (function.body().has_value() &&
  471. bodies != ResolveFunctionBodies::Skip) {
  472. CARBON_RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope));
  473. }
  474. break;
  475. }
  476. case DeclarationKind::ClassDeclaration: {
  477. auto& class_decl = cast<ClassDeclaration>(declaration);
  478. StaticScope class_scope;
  479. class_scope.AddParent(&enclosing_scope);
  480. if (class_decl.type_params().has_value()) {
  481. CARBON_RETURN_IF_ERROR(
  482. ResolveNames(**class_decl.type_params(), class_scope));
  483. }
  484. enclosing_scope.MarkUsable(class_decl.name());
  485. CARBON_RETURN_IF_ERROR(AddExposedNames(*class_decl.self(), class_scope));
  486. CARBON_RETURN_IF_ERROR(
  487. ResolveMemberNames(class_decl.members(), class_scope, bodies));
  488. break;
  489. }
  490. case DeclarationKind::ChoiceDeclaration: {
  491. auto& choice = cast<ChoiceDeclaration>(declaration);
  492. // Alternative names are never used unqualified, so we don't need to
  493. // add the alternatives to a scope, or introduce a new scope; we only
  494. // need to check for duplicates.
  495. std::set<std::string_view> alternative_names;
  496. for (Nonnull<AlternativeSignature*> alternative : choice.alternatives()) {
  497. CARBON_RETURN_IF_ERROR(
  498. ResolveNames(alternative->signature(), enclosing_scope));
  499. if (!alternative_names.insert(alternative->name()).second) {
  500. return CompilationError(alternative->source_loc())
  501. << "Duplicate name `" << alternative->name()
  502. << "` in choice type";
  503. }
  504. }
  505. enclosing_scope.MarkUsable(choice.name());
  506. break;
  507. }
  508. case DeclarationKind::VariableDeclaration: {
  509. auto& var = cast<VariableDeclaration>(declaration);
  510. CARBON_RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope));
  511. if (var.has_initializer()) {
  512. CARBON_RETURN_IF_ERROR(
  513. ResolveNames(var.initializer(), enclosing_scope));
  514. }
  515. break;
  516. }
  517. case DeclarationKind::SelfDeclaration: {
  518. CARBON_FATAL() << "Unreachable: resolving names for `Self` declaration";
  519. }
  520. case DeclarationKind::AliasDeclaration: {
  521. auto& alias = cast<AliasDeclaration>(declaration);
  522. CARBON_RETURN_IF_ERROR(ResolveNames(alias.target(), enclosing_scope));
  523. enclosing_scope.MarkUsable(alias.name());
  524. break;
  525. }
  526. }
  527. return Success();
  528. }
  529. auto ResolveNames(AST& ast) -> ErrorOr<Success> {
  530. StaticScope file_scope;
  531. for (auto declaration : ast.declarations) {
  532. CARBON_RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope));
  533. }
  534. for (auto declaration : ast.declarations) {
  535. CARBON_RETURN_IF_ERROR(ResolveNames(
  536. *declaration, file_scope, ResolveFunctionBodies::AfterDeclarations));
  537. }
  538. return ResolveNames(**ast.main_call, file_scope);
  539. }
  540. } // namespace Carbon