resolve_names.cpp 13 KB

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