function.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "toolchain/check/function.h"
  5. #include "toolchain/check/merge.h"
  6. #include "toolchain/check/subst.h"
  7. namespace Carbon::Check {
  8. // Returns true if there was an error in declaring the function, which will have
  9. // previously been diagnosed.
  10. static auto FunctionDeclHasError(Context& context, const SemIR::Function& fn)
  11. -> bool {
  12. if (fn.return_type_id == SemIR::TypeId::Error) {
  13. return true;
  14. }
  15. for (auto param_refs_id : {fn.implicit_param_refs_id, fn.param_refs_id}) {
  16. if (param_refs_id != SemIR::InstBlockId::Empty) {
  17. for (auto param_id : context.inst_blocks().Get(param_refs_id)) {
  18. if (context.insts().Get(param_id).type_id() == SemIR::TypeId::Error) {
  19. return true;
  20. }
  21. }
  22. }
  23. }
  24. return false;
  25. }
  26. // Returns false if a param differs for a redeclaration. The caller is expected
  27. // to provide a diagnostic.
  28. static auto CheckRedeclParam(Context& context,
  29. llvm::StringLiteral param_diag_label,
  30. int32_t param_index,
  31. SemIR::InstId new_param_ref_id,
  32. SemIR::InstId prev_param_ref_id,
  33. Substitutions substitutions) -> bool {
  34. // TODO: Consider differentiating between type and name mistakes. For now,
  35. // taking the simpler approach because I also think we may want to refactor
  36. // params.
  37. auto diagnose = [&]() {
  38. CARBON_DIAGNOSTIC(FunctionRedeclParamDiffers, Error,
  39. "Function redeclaration differs at {0}parameter {1}.",
  40. llvm::StringLiteral, int32_t);
  41. CARBON_DIAGNOSTIC(FunctionRedeclParamPrevious, Note,
  42. "Previous declaration's corresponding {0}parameter here.",
  43. llvm::StringLiteral);
  44. context.emitter()
  45. .Build(new_param_ref_id, FunctionRedeclParamDiffers, param_diag_label,
  46. param_index + 1)
  47. .Note(prev_param_ref_id, FunctionRedeclParamPrevious, param_diag_label)
  48. .Emit();
  49. };
  50. auto new_param_ref = context.insts().Get(new_param_ref_id);
  51. auto prev_param_ref = context.insts().Get(prev_param_ref_id);
  52. if (new_param_ref.kind() != prev_param_ref.kind() ||
  53. new_param_ref.type_id() !=
  54. SubstType(context, prev_param_ref.type_id(), substitutions)) {
  55. diagnose();
  56. return false;
  57. }
  58. if (new_param_ref.Is<SemIR::AddrPattern>()) {
  59. new_param_ref =
  60. context.insts().Get(new_param_ref.As<SemIR::AddrPattern>().inner_id);
  61. prev_param_ref =
  62. context.insts().Get(prev_param_ref.As<SemIR::AddrPattern>().inner_id);
  63. if (new_param_ref.kind() != prev_param_ref.kind()) {
  64. diagnose();
  65. return false;
  66. }
  67. }
  68. if (new_param_ref.Is<SemIR::AnyBindName>()) {
  69. new_param_ref =
  70. context.insts().Get(new_param_ref.As<SemIR::AnyBindName>().value_id);
  71. prev_param_ref =
  72. context.insts().Get(prev_param_ref.As<SemIR::AnyBindName>().value_id);
  73. }
  74. auto new_param = new_param_ref.As<SemIR::Param>();
  75. auto prev_param = prev_param_ref.As<SemIR::Param>();
  76. if (new_param.name_id != prev_param.name_id) {
  77. diagnose();
  78. return false;
  79. }
  80. return true;
  81. }
  82. // Returns false if the param refs differ for a redeclaration.
  83. static auto CheckRedeclParams(Context& context, SemIR::InstId new_decl_id,
  84. SemIR::InstBlockId new_param_refs_id,
  85. SemIR::InstId prev_decl_id,
  86. SemIR::InstBlockId prev_param_refs_id,
  87. llvm::StringLiteral param_diag_label,
  88. Substitutions substitutions) -> bool {
  89. // This will often occur for empty params.
  90. if (new_param_refs_id == prev_param_refs_id) {
  91. return true;
  92. }
  93. const auto new_param_ref_ids = context.inst_blocks().Get(new_param_refs_id);
  94. const auto prev_param_ref_ids = context.inst_blocks().Get(prev_param_refs_id);
  95. if (new_param_ref_ids.size() != prev_param_ref_ids.size()) {
  96. CARBON_DIAGNOSTIC(
  97. FunctionRedeclParamCountDiffers, Error,
  98. "Function redeclaration differs because of {0}parameter count of {1}.",
  99. llvm::StringLiteral, int32_t);
  100. CARBON_DIAGNOSTIC(FunctionRedeclParamCountPrevious, Note,
  101. "Previously declared with {0}parameter count of {1}.",
  102. llvm::StringLiteral, int32_t);
  103. context.emitter()
  104. .Build(new_decl_id, FunctionRedeclParamCountDiffers, param_diag_label,
  105. new_param_ref_ids.size())
  106. .Note(prev_decl_id, FunctionRedeclParamCountPrevious, param_diag_label,
  107. prev_param_ref_ids.size())
  108. .Emit();
  109. return false;
  110. }
  111. for (auto [index, new_param_ref_id, prev_param_ref_id] :
  112. llvm::enumerate(new_param_ref_ids, prev_param_ref_ids)) {
  113. if (!CheckRedeclParam(context, param_diag_label, index, new_param_ref_id,
  114. prev_param_ref_id, substitutions)) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. // Returns false if the provided function declarations differ.
  121. static auto CheckRedecl(Context& context, const SemIR::Function& new_function,
  122. const SemIR::Function& prev_function,
  123. Substitutions substitutions) -> bool {
  124. if (FunctionDeclHasError(context, new_function) ||
  125. FunctionDeclHasError(context, prev_function)) {
  126. return false;
  127. }
  128. if (!CheckRedeclParams(
  129. context, new_function.decl_id, new_function.implicit_param_refs_id,
  130. prev_function.decl_id, prev_function.implicit_param_refs_id,
  131. "implicit ", substitutions) ||
  132. !CheckRedeclParams(context, new_function.decl_id,
  133. new_function.param_refs_id, prev_function.decl_id,
  134. prev_function.param_refs_id, "", substitutions)) {
  135. return false;
  136. }
  137. auto prev_return_type_id =
  138. prev_function.return_type_id.is_valid()
  139. ? SubstType(context, prev_function.return_type_id, substitutions)
  140. : SemIR::TypeId::Invalid;
  141. if (new_function.return_type_id != prev_return_type_id) {
  142. CARBON_DIAGNOSTIC(
  143. FunctionRedeclReturnTypeDiffers, Error,
  144. "Function redeclaration differs because return type is `{0}`.",
  145. SemIR::TypeId);
  146. CARBON_DIAGNOSTIC(
  147. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  148. "Function redeclaration differs because no return type is provided.");
  149. auto diag =
  150. new_function.return_type_id.is_valid()
  151. ? context.emitter().Build(new_function.decl_id,
  152. FunctionRedeclReturnTypeDiffers,
  153. new_function.return_type_id)
  154. : context.emitter().Build(new_function.decl_id,
  155. FunctionRedeclReturnTypeDiffersNoReturn);
  156. if (prev_return_type_id.is_valid()) {
  157. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  158. "Previously declared with return type `{0}`.",
  159. SemIR::TypeId);
  160. diag.Note(prev_function.decl_id, FunctionRedeclReturnTypePrevious,
  161. prev_return_type_id);
  162. } else {
  163. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  164. "Previously declared with no return type.");
  165. diag.Note(prev_function.decl_id,
  166. FunctionRedeclReturnTypePreviousNoReturn);
  167. }
  168. diag.Emit();
  169. return false;
  170. }
  171. return true;
  172. }
  173. auto CheckFunctionTypeMatches(Context& context,
  174. SemIR::FunctionId new_function_id,
  175. SemIR::FunctionId prev_function_id,
  176. Substitutions substitutions) -> bool {
  177. return CheckRedecl(context, context.functions().Get(new_function_id),
  178. context.functions().Get(prev_function_id), substitutions);
  179. }
  180. // Checks to see if a structurally valid redeclaration is allowed in context.
  181. // These all still merge.
  182. static auto CheckIsAllowedRedecl(Context& context, SemIR::LocId loc_id,
  183. SemIR::Function& new_function,
  184. bool new_is_definition,
  185. SemIR::Function& prev_function,
  186. bool prev_is_import) -> void {
  187. CARBON_DIAGNOSTIC(FunctionPreviousDecl, Note, "Previously declared here.");
  188. if (prev_is_import) {
  189. // TODO: Allow non-extern declarations in the same library.
  190. if (!new_function.is_extern && !prev_function.is_extern) {
  191. CARBON_DIAGNOSTIC(
  192. FunctionNonExternRedecl, Error,
  193. "Only one library can declare function {0} without `extern`.",
  194. SemIR::NameId);
  195. context.emitter()
  196. .Build(loc_id, FunctionNonExternRedecl, prev_function.name_id)
  197. .Note(prev_function.decl_id, FunctionPreviousDecl)
  198. .Emit();
  199. return;
  200. }
  201. } else {
  202. if (!new_is_definition) {
  203. CARBON_DIAGNOSTIC(FunctionRedecl, Error,
  204. "Redundant redeclaration of function {0}.",
  205. SemIR::NameId);
  206. context.emitter()
  207. .Build(loc_id, FunctionRedecl, prev_function.name_id)
  208. .Note(prev_function.decl_id, FunctionPreviousDecl)
  209. .Emit();
  210. return;
  211. }
  212. if (prev_function.definition_id.is_valid()) {
  213. CARBON_DIAGNOSTIC(FunctionRedefinition, Error,
  214. "Redefinition of function {0}.", SemIR::NameId);
  215. CARBON_DIAGNOSTIC(FunctionPreviousDefinition, Note,
  216. "Previously defined here.");
  217. context.emitter()
  218. .Build(loc_id, FunctionRedefinition, prev_function.name_id)
  219. .Note(prev_function.definition_id, FunctionPreviousDefinition)
  220. .Emit();
  221. return;
  222. }
  223. // `extern` definitions are prevented in handle_function.cpp; this is only
  224. // checking for a non-`extern` definition after an `extern` declaration.
  225. if (prev_function.is_extern) {
  226. CARBON_DIAGNOSTIC(FunctionDefiningExtern, Error,
  227. "Redeclaring `extern` function `{0}` as non-`extern`.",
  228. SemIR::NameId);
  229. CARBON_DIAGNOSTIC(FunctionPreviousExternDecl, Note,
  230. "Previously declared `extern` here.");
  231. context.emitter()
  232. .Build(loc_id, FunctionDefiningExtern, prev_function.name_id)
  233. .Note(prev_function.decl_id, FunctionPreviousExternDecl)
  234. .Emit();
  235. return;
  236. }
  237. }
  238. }
  239. auto MergeFunctionRedecl(Context& context, SemIR::LocId loc_id,
  240. SemIR::Function& new_function, bool new_is_import,
  241. bool new_is_definition,
  242. SemIR::FunctionId prev_function_id,
  243. bool prev_is_import) -> bool {
  244. auto& prev_function = context.functions().Get(prev_function_id);
  245. if (!CheckRedecl(context, new_function, prev_function, {})) {
  246. return false;
  247. }
  248. CheckIsAllowedRedecl(context, loc_id, new_function, new_is_definition,
  249. prev_function, prev_is_import);
  250. if (new_is_definition) {
  251. // Track the signature from the definition, so that IDs in the body
  252. // match IDs in the signature.
  253. prev_function.definition_id = new_function.definition_id;
  254. prev_function.implicit_param_refs_id = new_function.implicit_param_refs_id;
  255. prev_function.param_refs_id = new_function.param_refs_id;
  256. prev_function.return_type_id = new_function.return_type_id;
  257. prev_function.return_slot_id = new_function.return_slot_id;
  258. }
  259. if ((prev_is_import && !new_is_import) ||
  260. (prev_function.is_extern && !new_function.is_extern)) {
  261. prev_function.is_extern = new_function.is_extern;
  262. prev_function.decl_id = new_function.decl_id;
  263. ReplacePrevInstForMerge(context, prev_function.enclosing_scope_id,
  264. prev_function.name_id, new_function.decl_id);
  265. }
  266. return true;
  267. }
  268. } // namespace Carbon::Check