resolve_names.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "executable_semantics/interpreter/resolve_names.h"
  5. #include <set>
  6. #include "executable_semantics/ast/declaration.h"
  7. #include "executable_semantics/ast/expression.h"
  8. #include "executable_semantics/ast/pattern.h"
  9. #include "executable_semantics/ast/statement.h"
  10. #include "executable_semantics/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. RETURN_IF_ERROR(enclosing_scope.Add(iface_decl.name(), &iface_decl));
  24. break;
  25. }
  26. case DeclarationKind::ImplDeclaration: {
  27. // Nothing to do here
  28. break;
  29. }
  30. case DeclarationKind::FunctionDeclaration: {
  31. auto& func = cast<FunctionDeclaration>(declaration);
  32. RETURN_IF_ERROR(enclosing_scope.Add(func.name(), &func));
  33. break;
  34. }
  35. case DeclarationKind::ClassDeclaration: {
  36. auto& class_decl = cast<ClassDeclaration>(declaration);
  37. RETURN_IF_ERROR(enclosing_scope.Add(class_decl.name(), &class_decl));
  38. break;
  39. }
  40. case DeclarationKind::ChoiceDeclaration: {
  41. auto& choice = cast<ChoiceDeclaration>(declaration);
  42. RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice));
  43. break;
  44. }
  45. case DeclarationKind::VariableDeclaration:
  46. auto& var = cast<VariableDeclaration>(declaration);
  47. if (var.binding().name() != AnonymousName) {
  48. RETURN_IF_ERROR(
  49. enclosing_scope.Add(var.binding().name(), &var.binding()));
  50. }
  51. break;
  52. }
  53. return Success();
  54. }
  55. // Traverses the sub-AST rooted at the given node, resolving all names within
  56. // it using enclosing_scope, and updating enclosing_scope to add names to
  57. // it as they become available. In scopes where names are only visible below
  58. // their point of declaration (such as block scopes in C++), this is implemented
  59. // as a single pass, recursively calling ResolveNames on the elements of the
  60. // scope in order. In scopes where names are also visible above their point of
  61. // declaration (such as class scopes in C++), this requires two passes: first
  62. // calling AddExposedNames on each element of the scope to populate a
  63. // StaticScope, and then calling ResolveNames on each element, passing it the
  64. // already-populated StaticScope.
  65. static auto ResolveNames(Expression& expression,
  66. const StaticScope& enclosing_scope)
  67. -> ErrorOr<Success>;
  68. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  69. -> ErrorOr<Success>;
  70. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  71. -> ErrorOr<Success>;
  72. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
  73. -> ErrorOr<Success>;
  74. static auto ResolveNames(Expression& expression,
  75. const StaticScope& enclosing_scope)
  76. -> ErrorOr<Success> {
  77. switch (expression.kind()) {
  78. case ExpressionKind::CallExpression: {
  79. auto& call = cast<CallExpression>(expression);
  80. RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope));
  81. RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope));
  82. break;
  83. }
  84. case ExpressionKind::FunctionTypeLiteral: {
  85. auto& fun_type = cast<FunctionTypeLiteral>(expression);
  86. RETURN_IF_ERROR(ResolveNames(fun_type.parameter(), enclosing_scope));
  87. RETURN_IF_ERROR(ResolveNames(fun_type.return_type(), enclosing_scope));
  88. break;
  89. }
  90. case ExpressionKind::FieldAccessExpression:
  91. RETURN_IF_ERROR(
  92. ResolveNames(cast<FieldAccessExpression>(expression).aggregate(),
  93. enclosing_scope));
  94. break;
  95. case ExpressionKind::IndexExpression: {
  96. auto& index = cast<IndexExpression>(expression);
  97. RETURN_IF_ERROR(ResolveNames(index.aggregate(), enclosing_scope));
  98. RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope));
  99. break;
  100. }
  101. case ExpressionKind::PrimitiveOperatorExpression:
  102. for (Nonnull<Expression*> operand :
  103. cast<PrimitiveOperatorExpression>(expression).arguments()) {
  104. RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope));
  105. }
  106. break;
  107. case ExpressionKind::TupleLiteral:
  108. for (Nonnull<Expression*> field :
  109. cast<TupleLiteral>(expression).fields()) {
  110. RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  111. }
  112. break;
  113. case ExpressionKind::StructLiteral:
  114. for (FieldInitializer& init : cast<StructLiteral>(expression).fields()) {
  115. RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope));
  116. }
  117. break;
  118. case ExpressionKind::StructTypeLiteral:
  119. for (FieldInitializer& init :
  120. cast<StructTypeLiteral>(expression).fields()) {
  121. RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope));
  122. }
  123. break;
  124. case ExpressionKind::IdentifierExpression: {
  125. auto& identifier = cast<IdentifierExpression>(expression);
  126. ASSIGN_OR_RETURN(
  127. const auto value_node,
  128. enclosing_scope.Resolve(identifier.name(), identifier.source_loc()));
  129. identifier.set_value_node(value_node);
  130. break;
  131. }
  132. case ExpressionKind::IntrinsicExpression:
  133. RETURN_IF_ERROR(ResolveNames(cast<IntrinsicExpression>(expression).args(),
  134. enclosing_scope));
  135. break;
  136. case ExpressionKind::IfExpression: {
  137. auto& if_expr = cast<IfExpression>(expression);
  138. RETURN_IF_ERROR(ResolveNames(if_expr.condition(), enclosing_scope));
  139. RETURN_IF_ERROR(ResolveNames(if_expr.then_expression(), enclosing_scope));
  140. RETURN_IF_ERROR(ResolveNames(if_expr.else_expression(), enclosing_scope));
  141. break;
  142. }
  143. case ExpressionKind::ArrayTypeLiteral: {
  144. auto& array_literal = cast<ArrayTypeLiteral>(expression);
  145. RETURN_IF_ERROR(ResolveNames(array_literal.element_type_expression(),
  146. enclosing_scope));
  147. RETURN_IF_ERROR(
  148. ResolveNames(array_literal.size_expression(), enclosing_scope));
  149. break;
  150. }
  151. case ExpressionKind::BoolTypeLiteral:
  152. case ExpressionKind::BoolLiteral:
  153. case ExpressionKind::IntTypeLiteral:
  154. case ExpressionKind::ContinuationTypeLiteral:
  155. case ExpressionKind::IntLiteral:
  156. case ExpressionKind::StringLiteral:
  157. case ExpressionKind::StringTypeLiteral:
  158. case ExpressionKind::TypeTypeLiteral:
  159. break;
  160. case ExpressionKind::InstantiateImpl: // created after name resolution
  161. case ExpressionKind::UnimplementedExpression:
  162. return CompilationError(expression.source_loc()) << "Unimplemented";
  163. }
  164. return Success();
  165. }
  166. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  167. -> ErrorOr<Success> {
  168. switch (pattern.kind()) {
  169. case PatternKind::BindingPattern: {
  170. auto& binding = cast<BindingPattern>(pattern);
  171. RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  172. if (binding.name() != AnonymousName) {
  173. RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  174. }
  175. break;
  176. }
  177. case PatternKind::GenericBinding: {
  178. auto& binding = cast<GenericBinding>(pattern);
  179. RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  180. if (binding.name() != AnonymousName) {
  181. RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  182. }
  183. break;
  184. }
  185. case PatternKind::TuplePattern:
  186. for (Nonnull<Pattern*> field : cast<TuplePattern>(pattern).fields()) {
  187. RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  188. }
  189. break;
  190. case PatternKind::AlternativePattern: {
  191. auto& alternative = cast<AlternativePattern>(pattern);
  192. RETURN_IF_ERROR(ResolveNames(alternative.choice_type(), enclosing_scope));
  193. RETURN_IF_ERROR(ResolveNames(alternative.arguments(), enclosing_scope));
  194. break;
  195. }
  196. case PatternKind::ExpressionPattern:
  197. RETURN_IF_ERROR(ResolveNames(
  198. cast<ExpressionPattern>(pattern).expression(), enclosing_scope));
  199. break;
  200. case PatternKind::AutoPattern:
  201. break;
  202. case PatternKind::VarPattern:
  203. RETURN_IF_ERROR(
  204. ResolveNames(cast<VarPattern>(pattern).pattern(), enclosing_scope));
  205. break;
  206. }
  207. return Success();
  208. }
  209. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  210. -> ErrorOr<Success> {
  211. switch (statement.kind()) {
  212. case StatementKind::ExpressionStatement:
  213. RETURN_IF_ERROR(ResolveNames(
  214. cast<ExpressionStatement>(statement).expression(), enclosing_scope));
  215. break;
  216. case StatementKind::Assign: {
  217. auto& assign = cast<Assign>(statement);
  218. RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope));
  219. RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope));
  220. break;
  221. }
  222. case StatementKind::VariableDefinition: {
  223. auto& def = cast<VariableDefinition>(statement);
  224. RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope));
  225. RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope));
  226. break;
  227. }
  228. case StatementKind::If: {
  229. auto& if_stmt = cast<If>(statement);
  230. RETURN_IF_ERROR(ResolveNames(if_stmt.condition(), enclosing_scope));
  231. RETURN_IF_ERROR(ResolveNames(if_stmt.then_block(), enclosing_scope));
  232. if (if_stmt.else_block().has_value()) {
  233. RETURN_IF_ERROR(ResolveNames(**if_stmt.else_block(), enclosing_scope));
  234. }
  235. break;
  236. }
  237. case StatementKind::Return:
  238. RETURN_IF_ERROR(
  239. ResolveNames(cast<Return>(statement).expression(), enclosing_scope));
  240. break;
  241. case StatementKind::Block: {
  242. auto& block = cast<Block>(statement);
  243. StaticScope block_scope;
  244. block_scope.AddParent(&enclosing_scope);
  245. for (Nonnull<Statement*> sub_statement : block.statements()) {
  246. RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope));
  247. }
  248. break;
  249. }
  250. case StatementKind::While: {
  251. auto& while_stmt = cast<While>(statement);
  252. RETURN_IF_ERROR(ResolveNames(while_stmt.condition(), enclosing_scope));
  253. RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope));
  254. break;
  255. }
  256. case StatementKind::Match: {
  257. auto& match = cast<Match>(statement);
  258. RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope));
  259. for (Match::Clause& clause : match.clauses()) {
  260. StaticScope clause_scope;
  261. clause_scope.AddParent(&enclosing_scope);
  262. RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope));
  263. RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope));
  264. }
  265. break;
  266. }
  267. case StatementKind::Continuation: {
  268. auto& continuation = cast<Continuation>(statement);
  269. RETURN_IF_ERROR(enclosing_scope.Add(continuation.name(), &continuation));
  270. StaticScope continuation_scope;
  271. continuation_scope.AddParent(&enclosing_scope);
  272. RETURN_IF_ERROR(ResolveNames(cast<Continuation>(statement).body(),
  273. continuation_scope));
  274. break;
  275. }
  276. case StatementKind::Run:
  277. RETURN_IF_ERROR(
  278. ResolveNames(cast<Run>(statement).argument(), enclosing_scope));
  279. break;
  280. case StatementKind::Await:
  281. case StatementKind::Break:
  282. case StatementKind::Continue:
  283. break;
  284. }
  285. return Success();
  286. }
  287. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
  288. -> ErrorOr<Success> {
  289. switch (declaration.kind()) {
  290. case DeclarationKind::InterfaceDeclaration: {
  291. auto& iface = cast<InterfaceDeclaration>(declaration);
  292. StaticScope iface_scope;
  293. iface_scope.AddParent(&enclosing_scope);
  294. if (iface.params().has_value()) {
  295. RETURN_IF_ERROR(ResolveNames(**iface.params(), iface_scope));
  296. }
  297. RETURN_IF_ERROR(iface_scope.Add("Self", iface.self()));
  298. for (Nonnull<Declaration*> member : iface.members()) {
  299. RETURN_IF_ERROR(AddExposedNames(*member, iface_scope));
  300. }
  301. for (Nonnull<Declaration*> member : iface.members()) {
  302. RETURN_IF_ERROR(ResolveNames(*member, iface_scope));
  303. }
  304. break;
  305. }
  306. case DeclarationKind::ImplDeclaration: {
  307. auto& impl = cast<ImplDeclaration>(declaration);
  308. StaticScope impl_scope;
  309. impl_scope.AddParent(&enclosing_scope);
  310. for (Nonnull<GenericBinding*> binding : impl.deduced_parameters()) {
  311. RETURN_IF_ERROR(ResolveNames(binding->type(), impl_scope));
  312. RETURN_IF_ERROR(impl_scope.Add(binding->name(), binding));
  313. }
  314. RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), impl_scope));
  315. RETURN_IF_ERROR(ResolveNames(impl.interface(), enclosing_scope));
  316. for (Nonnull<Declaration*> member : impl.members()) {
  317. RETURN_IF_ERROR(AddExposedNames(*member, impl_scope));
  318. }
  319. for (Nonnull<Declaration*> member : impl.members()) {
  320. RETURN_IF_ERROR(ResolveNames(*member, impl_scope));
  321. }
  322. break;
  323. }
  324. case DeclarationKind::FunctionDeclaration: {
  325. auto& function = cast<FunctionDeclaration>(declaration);
  326. StaticScope function_scope;
  327. function_scope.AddParent(&enclosing_scope);
  328. for (Nonnull<GenericBinding*> binding : function.deduced_parameters()) {
  329. RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope));
  330. RETURN_IF_ERROR(function_scope.Add(binding->name(), binding));
  331. }
  332. if (function.is_method()) {
  333. RETURN_IF_ERROR(ResolveNames(function.me_pattern(), function_scope));
  334. }
  335. RETURN_IF_ERROR(ResolveNames(function.param_pattern(), function_scope));
  336. if (function.return_term().type_expression().has_value()) {
  337. RETURN_IF_ERROR(ResolveNames(**function.return_term().type_expression(),
  338. function_scope));
  339. }
  340. if (function.body().has_value()) {
  341. RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope));
  342. }
  343. break;
  344. }
  345. case DeclarationKind::ClassDeclaration: {
  346. auto& class_decl = cast<ClassDeclaration>(declaration);
  347. StaticScope class_scope;
  348. class_scope.AddParent(&enclosing_scope);
  349. RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl));
  350. if (class_decl.type_params().has_value()) {
  351. RETURN_IF_ERROR(ResolveNames(**class_decl.type_params(), class_scope));
  352. }
  353. // TODO: Disable unqualified access of members by other members for now.
  354. // Put it back later, but in a way that turns unqualified accesses
  355. // into qualified ones, so that generic classes and impls
  356. // behave the in the right way. -Jeremy
  357. // for (Nonnull<Declaration*> member : class_decl.members()) {
  358. // AddExposedNames(*member, class_scope);
  359. // }
  360. for (Nonnull<Declaration*> member : class_decl.members()) {
  361. RETURN_IF_ERROR(ResolveNames(*member, class_scope));
  362. }
  363. break;
  364. }
  365. case DeclarationKind::ChoiceDeclaration: {
  366. auto& choice = cast<ChoiceDeclaration>(declaration);
  367. // Alternative names are never used unqualified, so we don't need to
  368. // add the alternatives to a scope, or introduce a new scope; we only
  369. // need to check for duplicates.
  370. std::set<std::string_view> alternative_names;
  371. for (Nonnull<AlternativeSignature*> alternative : choice.alternatives()) {
  372. RETURN_IF_ERROR(
  373. ResolveNames(alternative->signature(), enclosing_scope));
  374. if (!alternative_names.insert(alternative->name()).second) {
  375. return CompilationError(alternative->source_loc())
  376. << "Duplicate name `" << alternative->name()
  377. << "` in choice type";
  378. }
  379. }
  380. break;
  381. }
  382. case DeclarationKind::VariableDeclaration: {
  383. auto& var = cast<VariableDeclaration>(declaration);
  384. RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope));
  385. if (var.has_initializer()) {
  386. RETURN_IF_ERROR(ResolveNames(var.initializer(), enclosing_scope));
  387. }
  388. break;
  389. }
  390. }
  391. return Success();
  392. }
  393. auto ResolveNames(AST& ast) -> ErrorOr<Success> {
  394. StaticScope file_scope;
  395. for (auto declaration : ast.declarations) {
  396. RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope));
  397. }
  398. for (auto declaration : ast.declarations) {
  399. RETURN_IF_ERROR(ResolveNames(*declaration, file_scope));
  400. }
  401. return ResolveNames(**ast.main_call, file_scope);
  402. }
  403. } // namespace Carbon