Diagnostics excersice

Hello everyone! I'm new at the compiler and have been reading a lot on how it works, as an exercise I want to try and implement a warning in the code. I'm trying to set a warning if the 2 arguments in the same function have the same type. However I'm not sure on what part of the parser should I work. Taking into account what I have read it should be somewhere after the tokens are brougth from Lex but I can't figure out where are the arguments of a function parsed to call the diagnostic. I have set the message in the DiagnosticsSema.def but I'm still looking for the place to make the diagnostic.

Any help or guide would be appreciated!!

Welcome to the project!

A good place to start looking might be Parser::parseSingleParameterClause in ParsePattern.cpp. This is the function responsible for parsing the parameter lists of functions, subscripts, initializers, etc. I'd recommend taking a look at how it's used by its various callers to understand how it works. Depending on your goals and what you want to learn, this might not be the best place to implement the diagnostic though. If you do go this route and implement it in the Parser, you should also move your diagnostic message from DiagnosticsSema.def to DiagnosticsParse.def.

The reason why implementing this diagnostic in the parser might not do what you want is because it wouldn't be able to detect a situation like this:

typealias Bar = Int
func foo(a: Int, b: Bar) {}

This function has two arguments with the same type, but this can't really be detected in the parser because a typealias was used and the parser uses TypeReprs instead of Types. A TypeRepr describes how a type is written in the source code, but it doesn't include any semantic information, so arguments a and b of foo will have different TypeReprs even though they will eventually have the same underlying type. Later, the Sema phase of the compiler translates from TypeRepr to Type. That's where I'd recommend implementing this diagnostic if you want to handle the situation above. A good place to start investigating in this area might be performAbstractFuncDeclDiagnostics in MiscDiagnostics.cpp

Hopefully this helps clarify things a little. Feel free to ask any other questions you have here or elsewhere on the forums!


Hey! Thanks a lot for your reply! I'm looking through the singleParameterClause and have already change the message to the diagnosticsParse as you said, I put a warning with no conditions and it worked! Now I'm wondering if in the same single paramenterClause I can count the number of parameters in the method. I have seen parseParameterClause and bool isClosure can I count the parameters on those when they get the parameter list? Am I on the right track?

Thanks again!!

It sounds like you're on the right track. If you take a look at the end of parseSingleParameterClause:

  ParserStatus status;
  SmallVector<ParsedParameter, 4> params;
  SourceLoc leftParenLoc, rightParenLoc;
  // Parse the parameter clause.
  status |= parseParameterClause(leftParenLoc, params, rightParenLoc,
                                 defaultArgs, paramContext);

  // Turn the parameter clause into argument and body patterns.
  auto paramList = mapParsedParameters(*this, leftParenLoc, params,
                                       rightParenLoc, namePieces, paramContext);

  return makeParserResult(status, paramList);

you'll see there are two things happening here. First, parseParameterClause is called, taking params, leftParenLoc, rightParamLoc by reference. At the end of this call, the functions parameters will have been parsed into the ParsedParameters of params. ParsedParameter is defined in Parser.h and describes how a parameter is written in the source code.

The second step is mapParsedParameters. Now that the syntax has been parsed, mapParsedParameters does some basic validation of the parameters and then creates a ParameterList*, which is a formal representation of the parameters in the AST. This ParameterList is the end result of parseSingleParameterClause. ParameterList is defined in ParameterList.h.

Based on what you're trying to do, I think you could count the number of parameters either in parseSingleParameterClause after the call to parseParameterClause, or somewhere in mapParsedParameters alongside the other validation diagnostics.