resolve_names.cpp 13 KB

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