resolve_names.cpp 16 KB

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