views:

882

answers:

7

At the CocoaHeads Öresund meeting yesterday, peylow had constructed a great ObjC quiz. The competition was intense and three people were left with the same score when the final question was to be evaluated: How many reserved keywords does Objective-C add to C?

Some spirited debate followed. All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords, but how about something like in? It might be a keyword, but it's not a reserved keyword. For example, the following will compile without errors or warnings:

NSArray* in;
for (in in in)
   NSLog(@"bwahahaa");

We concluded that ObjC adds no reserved keywords to C, and someone won a seemingly well-earned book.

But today I tried some more systematic abuse on the compiler by trying things like this:

int self = 45;
self++;
int y = self;

That compiles fine, and the same code works replacing self for BOOL, bycopy, inout, oneway, byref, SEL, and IMP.

Using id as the variable name, the first and last lines compile, but not the second one. The same goes for Protocol, and Class.

Using super, the first line compiles, but not the second and third.

With YES, NO, and NULL, all three lines fail to compile, probably because they are just defined as true, false, and nil.

It looks to me like a lot of this is gcc getting confused and I'm not so sure it reflects what is and isn't a reserved keyword in Objective-C. Why, for example, is it ok to use self as the name of an int, but not super?

The fact that the first assignment always works (except for with YES, NO, and NULL) would seem to support the idea that none of the candidates are technically reserved keywords that are not found in C. Or?

Could someone please give us an authoritative explication of this thorny issue?

Several people's honor is at stake.

EDIT: As Nikolai Ruhe pointed out, we need a clear definition of "keyword" to proceed. Niko cited a Wikipedia article saying that a keyword is "a word or identifier that has a particular meaning".

I think it is reasonable to use this definition from the same article:

In many languages, such as C and similar environments like C++, a keyword is a reserved word which identifies a syntactic form. Words used in control flow constructs, such as if, then, and else are keywords. In these languages, keywords cannot also be used as the names of variables or functions.

Furthermore, as the article states:

Typically, when a programmer attempts to use a keyword for a variable or function name, a compilation error will be triggered.

In this sense, then, are there any reserved keywords that are predefined in the language’s formal specifications and cannot be used as a user-defined name?

+1  A: 

A look at the Objective-C Programming Language gives you some clues about what is possible or not. As some "keywords" are defined in a header file, their substitution can lead to observed (and unexpected) behavior.

Laurent Etiemble
Right. So the question is: are there *any* bona fide reserved keywords in Objective-C (beyond those already reserved in C).
Felixyz
There are no reserved keywords like in other languages sense. It is more like defines that can interact badly with your code. It all depends on the pre-processor work.
Laurent Etiemble
+1  A: 

Wikipedia defines keywords as:

In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language.

I'd translate that to: a keyword in a language is a non-terminal in its grammar. (Btw, the grammar doesn't contain the word "keyword" just by itself, so no confusion there).

The terminals in the grammar would be:

  • void
  • char
  • short
  • int
  • long
  • float
  • double
  • signed
  • unsigned
  • id
  • const
  • volatile
  • in
  • out
  • inout
  • bycopy
  • byref
  • oneway
  • self
  • super
  • @interface
  • @end
  • @implementation
  • @end
  • @interface
  • @end
  • @implementation
  • @end
  • @protoco
  • @end
  • @class
nes1983
The question is which reserved keywords ObjC might **add** to C. As you can see in the examples, you are free to use "in" as the name of a variable so it clearly doesn't count as a reserved keyword.
Felixyz
I mean, the fact that "int float = 45;" fails to compile while "int id = 45;" compiles fine tells us something.
Felixyz
they aren't "examples". It's the complete list, as far as I can see. And well, it depends on what you'd like to call a keyword. I gave you my answer to that question.
nes1983
I meant the example code in the question. "a keyword in a language is a non-terminal". Did you mean "terminal"? I don't think anyone would call #define a keyword, so why should we @interface? Thanks for the offering, but I'm not convinced. To me, anything that can be used as the name of a variable can't very well be a (reserved) keyword.
Felixyz
@Felixyz: It seems that as a prerequisite to answer your question it is important to (generally) define the term keyword. Only then can be answered what is a keyword in the case of Objective-C. Obviously there are differing opinions on that.
Nikolai Ruhe
+1  A: 

You could check out The Objective-C Programming Language.

As far as I can tell, that document doesn't classify anything as a "reserved word". The @implementation, @interface, etc. are called "compiler directives."

Kristopher Johnson
+2  A: 

To find all keywords in Objective-C you need a definition of the term "keyword". This definition should not come from the behavior of one compiler.

In other languages you get this definition from a more or less official document that describes the language, like the C99 Standard or the Python Language Reference.

In the case of Objective-C the document that comes close to "the official language definition" is Apple’s "The Objective-C Programming Language". In contrary to the aforementioned languages this document does not use "keyword" in a consistent manner.

There are "keyword arguments" (each of the colon separated parts of a selector), nil is called a keyword, some of the @property, @optional, ... words are called keywords. In one place even the gcc __attribute__ language extension is called a keyword. Regarding Apple’s usage of the term, I conclude there is no precise definition of what is a keyword in Objective-C.

Since there’s no "right" answer, everybody might have his own definition. I’d say, all the words starting with @, like @interface, @property, ... are keywords in Objective-C (even though being only preprocessor directives, but still part of the language). Also, self, super, in, etc. are keywords in my opinion, since they are part of the language without explicit declaration. But that’s only my opinion.

Edit: As NSResponder pointed out, the keywords starting with '@' are in fact compiler directives, not preprocessor directives as I stated above.

Nikolai Ruhe
+5  A: 

All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords

Then all were mistaken. #import and #pragma are preprocessor directives. @interface, @implementation, @protocol and so forth are keywords of Objective-C, and they are compiler directives. They haven't been preprocessor directives since NeXT extended GCC to compile Objective-C without a Stepstone's original Objective-C pre-processor implementation.

NSResponder
Then that would mean that @interface is a keyword (in the sense stated in the edit above) but id isn't, because the compiler refuses to compile eg "int @interface = 1;" but happily uses id as the name for an int. But then again, the compiler also doesn't allow "int @icecream = 1;". So where does that leave us?#import -- preprocessor directive@interface -- compiler directive@ -- reserved characterid/self/in -- special meaning in some contexts but not reserved
Felixyz
id is a typedef. You can find it in objc.h
NSResponder
+1  A: 

Re: Nikolai Ruhe's suggest that self is a keyword

self is a parameter to an Objective-C method of type id (similar to how _cmd is also a param, of type SEL) and not a keyword. nil is a macro which expands to NULL. I'm a little disappointed that super doesn't expand to a macro, nor is it a parameter.

From the wikipedia entry:

Objective-C is a thin layer on top of C, and moreover is a strict superset of C. It is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class.

That would preclude Objective-C from adding any restricted keywords to the language.

jon hohle
That's a very good point, although the restriction is apparently not fully respected by gcc.
Felixyz
The conclusion is not true. A superset of a language can add keywords by using terms that are illegal in the original language. And that’s exactly what Objective-C does.
Nikolai Ruhe
Regarding `super` not being a macro: The compiler must be able to recognize `super` since it has to use the correct messaging type (calling `objc_msgSendSuper` instead of `objc_msgSend`). `super` points to the same object as `self`, so there no much point in making it another argument also.
Nikolai Ruhe
Objective-C being a superset does not preclude it from adding additional restricted keywords, but allowing all C programs to compile with an Objective-C compiler does.
jon hohle
@jon: Why? Objective-C does add keywords, as NSResponder pointed out. But the compiler also compiles all C programs.
Nikolai Ruhe
@jon: B.t.w: The implicit `self` argument of instance methods is not of type `id` but statically typed to the class.
Nikolai Ruhe
@Nikolai: I think what jon means is that a C program with a variable called super, which will compile under C, will not be accepted by an Objective-C compiler (at least not gcc), an in this respect it seems that seems to break the strict superset requirement.
Felixyz
@Felixyz: Using `super` as a name for a variable in a C function is compiling fine in gcc4.0, gcc4.2, and clang. Using it in a method is of course an error. No requirement broken. And what I meant was that jon’s reasoning is wrong: A strict superset **can** add keywords to a language without breaking anything.
Nikolai Ruhe
@Nikolai Ruhe: I concede your point: whether super can be used as a variable name inside a method is not important, as long as it compiles in a C function, which it does. And @ is not a valid character in C identifiers, so not allowing @interface etc as variable names doesn't break the strict superset requirement either.
Felixyz
@Felixyz: Thanks for making that clearer than I could.
Nikolai Ruhe
+1  A: 

Grammar in Appendix B of the original 1995 book defines super as "literal symbol" terminal and it's a special-case in message receiver syntax.

in literal symbol is part of protocol-qualifier in ObjC 1.0 and was reused in fast enumeration syntax in ObjC 2.0 (which has no formal grammar definition AFAIK).

id is defined as part of type-specifier alongside void, char, unsigned, typedefed types, etc. If you call void a keyword, then id is too.

porneL