resolve_names.cpp 15 KB

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