resolve_names.cpp 21 KB

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