resolve_names.cpp 12 KB

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