resolve_names.cpp 13 KB

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