resolve_names.cpp 12 KB

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