Search code examples
cstructdereference

Where can i find the source code for the -> dereference operator?


I'm on Ubuntu Saucy and I was hoping someone would tell me in want file the c struct dereference operator -> is in..when I

grep -R -> . 

i get

 bash: .: Is a directory

and when I omit the '.' i get

bash: syntax error near unexpected token `newline' 

id like to reproduce this functionality in another language but also google had nothing..any help is much appreciated.


Solution

  • bash reports that error message because, in grep -R -> ., the > looks like redirection of standard output, so bash attempts to open . as a file, to which output would be written. This fails because . stands for the current directory, and a directory cannot be opened in the same way as a file.

    You can avoid bash interpreting the > this way by using quotes, such as grep -R "->" .. However, then grep interprets the - as indicating a command-line option, rather than as a pattern to search for. You can avoid this by marking the - with a backslash:

    grep -R "\->" .
    

    or using -- to tell grep where the options end:

    grep -R -- "->" .
    

    Unfortunately, this will only find files that contain the string “->”; it will not distinguish files that implement the C -> operator. And there will be many files containing that string which are unrelated to what you are searching for.

    I expect the C -> operator will be implemented something like this:

    • Some code in some file involved in lexical analysis will recognize the string “->” and will respond by producing a token, which may have an arbitrary name (one chosen by the author of this particular code, not mandated by any particular standard or specification) such as DEREFERENCE.
    • Some code in some file involved in grammatical analysis will either process the DEREFERENCE token by emitting code (likely in an intermediate language for the compiler’s own use) that performs the dereference operation. Alternately, the code might convert expressions such as a->b to (*a).b and then emitting code for the latter.

    Neither the process of emitting this code nor the code itself necessarily are single operations. It may involve checking the type of the left operand, calculating the offset of the right operand, relating the expression to things in its context (to support optimization), and generating the actual intermediate code. That intermediate code may be further processed by optimization or other parts of the compiler and will be used later to generate final machine code.

    In summary, there is no single place where -> is implemented. It is part of a complex process and interacts with other parts of the compiler.