resolve_names.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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(
  140. ResolveNames(*if_expr.then_expression(), enclosing_scope));
  141. RETURN_IF_ERROR(
  142. ResolveNames(*if_expr.else_expression(), enclosing_scope));
  143. break;
  144. }
  145. case ExpressionKind::ArrayTypeLiteral: {
  146. auto& array_literal = cast<ArrayTypeLiteral>(expression);
  147. RETURN_IF_ERROR(ResolveNames(array_literal.element_type_expression(),
  148. enclosing_scope));
  149. RETURN_IF_ERROR(
  150. ResolveNames(array_literal.size_expression(), enclosing_scope));
  151. break;
  152. }
  153. case ExpressionKind::BoolTypeLiteral:
  154. case ExpressionKind::BoolLiteral:
  155. case ExpressionKind::IntTypeLiteral:
  156. case ExpressionKind::ContinuationTypeLiteral:
  157. case ExpressionKind::IntLiteral:
  158. case ExpressionKind::StringLiteral:
  159. case ExpressionKind::StringTypeLiteral:
  160. case ExpressionKind::TypeTypeLiteral:
  161. break;
  162. case ExpressionKind::UnimplementedExpression:
  163. return FATAL_COMPILATION_ERROR(expression.source_loc())
  164. << "Unimplemented";
  165. }
  166. return Success();
  167. }
  168. static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
  169. -> ErrorOr<Success> {
  170. switch (pattern.kind()) {
  171. case PatternKind::BindingPattern: {
  172. auto& binding = cast<BindingPattern>(pattern);
  173. RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  174. if (binding.name() != AnonymousName) {
  175. RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  176. }
  177. break;
  178. }
  179. case PatternKind::GenericBinding: {
  180. auto& binding = cast<GenericBinding>(pattern);
  181. RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
  182. if (binding.name() != AnonymousName) {
  183. RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
  184. }
  185. break;
  186. }
  187. case PatternKind::TuplePattern:
  188. for (Nonnull<Pattern*> field : cast<TuplePattern>(pattern).fields()) {
  189. RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
  190. }
  191. break;
  192. case PatternKind::AlternativePattern: {
  193. auto& alternative = cast<AlternativePattern>(pattern);
  194. RETURN_IF_ERROR(ResolveNames(alternative.choice_type(), enclosing_scope));
  195. RETURN_IF_ERROR(ResolveNames(alternative.arguments(), enclosing_scope));
  196. break;
  197. }
  198. case PatternKind::ExpressionPattern:
  199. RETURN_IF_ERROR(ResolveNames(
  200. cast<ExpressionPattern>(pattern).expression(), enclosing_scope));
  201. break;
  202. case PatternKind::AutoPattern:
  203. break;
  204. case PatternKind::VarPattern:
  205. RETURN_IF_ERROR(
  206. ResolveNames(cast<VarPattern>(pattern).pattern(), enclosing_scope));
  207. break;
  208. }
  209. return Success();
  210. }
  211. static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
  212. -> ErrorOr<Success> {
  213. switch (statement.kind()) {
  214. case StatementKind::ExpressionStatement:
  215. RETURN_IF_ERROR(ResolveNames(
  216. cast<ExpressionStatement>(statement).expression(), enclosing_scope));
  217. break;
  218. case StatementKind::Assign: {
  219. auto& assign = cast<Assign>(statement);
  220. RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope));
  221. RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope));
  222. break;
  223. }
  224. case StatementKind::VariableDefinition: {
  225. auto& def = cast<VariableDefinition>(statement);
  226. RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope));
  227. RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope));
  228. break;
  229. }
  230. case StatementKind::If: {
  231. auto& if_stmt = cast<If>(statement);
  232. RETURN_IF_ERROR(ResolveNames(if_stmt.condition(), enclosing_scope));
  233. RETURN_IF_ERROR(ResolveNames(if_stmt.then_block(), enclosing_scope));
  234. if (if_stmt.else_block().has_value()) {
  235. RETURN_IF_ERROR(ResolveNames(**if_stmt.else_block(), enclosing_scope));
  236. }
  237. break;
  238. }
  239. case StatementKind::Return:
  240. RETURN_IF_ERROR(
  241. ResolveNames(cast<Return>(statement).expression(), enclosing_scope));
  242. break;
  243. case StatementKind::Block: {
  244. auto& block = cast<Block>(statement);
  245. StaticScope block_scope;
  246. block_scope.AddParent(&enclosing_scope);
  247. for (Nonnull<Statement*> sub_statement : block.statements()) {
  248. RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope));
  249. }
  250. break;
  251. }
  252. case StatementKind::While: {
  253. auto& while_stmt = cast<While>(statement);
  254. RETURN_IF_ERROR(ResolveNames(while_stmt.condition(), enclosing_scope));
  255. RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope));
  256. break;
  257. }
  258. case StatementKind::Match: {
  259. auto& match = cast<Match>(statement);
  260. RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope));
  261. for (Match::Clause& clause : match.clauses()) {
  262. StaticScope clause_scope;
  263. clause_scope.AddParent(&enclosing_scope);
  264. RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope));
  265. RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope));
  266. }
  267. break;
  268. }
  269. case StatementKind::Continuation: {
  270. auto& continuation = cast<Continuation>(statement);
  271. RETURN_IF_ERROR(enclosing_scope.Add(continuation.name(), &continuation));
  272. StaticScope continuation_scope;
  273. continuation_scope.AddParent(&enclosing_scope);
  274. RETURN_IF_ERROR(ResolveNames(cast<Continuation>(statement).body(),
  275. continuation_scope));
  276. break;
  277. }
  278. case StatementKind::Run:
  279. RETURN_IF_ERROR(
  280. ResolveNames(cast<Run>(statement).argument(), enclosing_scope));
  281. break;
  282. case StatementKind::Await:
  283. case StatementKind::Break:
  284. case StatementKind::Continue:
  285. break;
  286. }
  287. return Success();
  288. }
  289. static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
  290. -> ErrorOr<Success> {
  291. switch (declaration.kind()) {
  292. case DeclarationKind::InterfaceDeclaration: {
  293. auto& iface = cast<InterfaceDeclaration>(declaration);
  294. StaticScope iface_scope;
  295. iface_scope.AddParent(&enclosing_scope);
  296. RETURN_IF_ERROR(iface_scope.Add("Self", iface.self()));
  297. for (Nonnull<Declaration*> member : iface.members()) {
  298. RETURN_IF_ERROR(AddExposedNames(*member, iface_scope));
  299. }
  300. for (Nonnull<Declaration*> member : iface.members()) {
  301. RETURN_IF_ERROR(ResolveNames(*member, iface_scope));
  302. }
  303. break;
  304. }
  305. case DeclarationKind::ImplDeclaration: {
  306. auto& impl = cast<ImplDeclaration>(declaration);
  307. RETURN_IF_ERROR(ResolveNames(impl.interface(), enclosing_scope));
  308. RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), enclosing_scope));
  309. for (Nonnull<Declaration*> member : impl.members()) {
  310. RETURN_IF_ERROR(AddExposedNames(*member, enclosing_scope));
  311. }
  312. for (Nonnull<Declaration*> member : impl.members()) {
  313. RETURN_IF_ERROR(ResolveNames(*member, enclosing_scope));
  314. }
  315. break;
  316. }
  317. case DeclarationKind::FunctionDeclaration: {
  318. auto& function = cast<FunctionDeclaration>(declaration);
  319. StaticScope function_scope;
  320. function_scope.AddParent(&enclosing_scope);
  321. for (Nonnull<GenericBinding*> binding : function.deduced_parameters()) {
  322. RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope));
  323. RETURN_IF_ERROR(function_scope.Add(binding->name(), binding));
  324. }
  325. if (function.is_method()) {
  326. RETURN_IF_ERROR(ResolveNames(function.me_pattern(), function_scope));
  327. }
  328. RETURN_IF_ERROR(ResolveNames(function.param_pattern(), function_scope));
  329. if (function.return_term().type_expression().has_value()) {
  330. RETURN_IF_ERROR(ResolveNames(**function.return_term().type_expression(),
  331. function_scope));
  332. }
  333. if (function.body().has_value()) {
  334. RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope));
  335. }
  336. break;
  337. }
  338. case DeclarationKind::ClassDeclaration: {
  339. auto& class_decl = cast<ClassDeclaration>(declaration);
  340. StaticScope class_scope;
  341. class_scope.AddParent(&enclosing_scope);
  342. RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl));
  343. if (class_decl.type_params().has_value()) {
  344. RETURN_IF_ERROR(ResolveNames(**class_decl.type_params(), class_scope));
  345. }
  346. // TODO: Disable unqualified access of members by other members for now.
  347. // Put it back later, but in a way that turns unqualified accesses
  348. // into qualified ones, so that generic classes and impls
  349. // behave the in the right way. -Jeremy
  350. // for (Nonnull<Declaration*> member : class_decl.members()) {
  351. // AddExposedNames(*member, class_scope);
  352. // }
  353. for (Nonnull<Declaration*> member : class_decl.members()) {
  354. RETURN_IF_ERROR(ResolveNames(*member, class_scope));
  355. }
  356. break;
  357. }
  358. case DeclarationKind::ChoiceDeclaration: {
  359. auto& choice = cast<ChoiceDeclaration>(declaration);
  360. // Alternative names are never used unqualified, so we don't need to
  361. // add the alternatives to a scope, or introduce a new scope; we only
  362. // need to check for duplicates.
  363. std::set<std::string_view> alternative_names;
  364. for (Nonnull<AlternativeSignature*> alternative : choice.alternatives()) {
  365. RETURN_IF_ERROR(
  366. ResolveNames(alternative->signature(), enclosing_scope));
  367. if (!alternative_names.insert(alternative->name()).second) {
  368. return FATAL_COMPILATION_ERROR(alternative->source_loc())
  369. << "Duplicate name `" << alternative->name()
  370. << "` in choice type";
  371. }
  372. }
  373. break;
  374. }
  375. case DeclarationKind::VariableDeclaration: {
  376. auto& var = cast<VariableDeclaration>(declaration);
  377. RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope));
  378. if (var.has_initializer()) {
  379. RETURN_IF_ERROR(ResolveNames(var.initializer(), enclosing_scope));
  380. }
  381. break;
  382. }
  383. }
  384. return Success();
  385. }
  386. auto ResolveNames(AST& ast) -> ErrorOr<Success> {
  387. StaticScope file_scope;
  388. for (auto declaration : ast.declarations) {
  389. RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope));
  390. }
  391. for (auto declaration : ast.declarations) {
  392. RETURN_IF_ERROR(ResolveNames(*declaration, file_scope));
  393. }
  394. return ResolveNames(**ast.main_call, file_scope);
  395. }
  396. } // namespace Carbon