English 中文(简体)
Reserved keywords in Objective-C?
原标题:

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?

问题回答

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.

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.

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

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.

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.

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."





相关问题
gcc -fPIC seems to muck with optimization flags

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared ...

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Getting rid of pre-compiled headers

OK, I have old Metrowerks code for Mac and Windows where the previous developer used pre-compiled headers for every project that this code base builds. How does one get rid of Pre-compiled headers, ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

How to compile for Mac OS X 10.5

I d like to compile my application for version 10.5 and forward. Ever since I upgraded to Snow Leopard and installed the latest XCode, gcc defaults to 10.6. I ve tried -isysroot /Developer/SDKs/...

热门标签