| 1 |
// Compiler implementation of the D programming language |
|---|
| 2 |
// Copyright (c) 1999-2006 by Digital Mars |
|---|
| 3 |
// All Rights Reserved |
|---|
| 4 |
// written by Walter Bright |
|---|
| 5 |
// http://www.digitalmars.com |
|---|
| 6 |
// License for redistribution is by either the Artistic License |
|---|
| 7 |
// in artistic.txt, or the GNU General Public License in gnu.txt. |
|---|
| 8 |
// See the included readme.txt for details. |
|---|
| 9 |
|
|---|
| 10 |
#include <stdio.h> |
|---|
| 11 |
#include <string.h> |
|---|
| 12 |
|
|---|
| 13 |
#include "root.h" |
|---|
| 14 |
#include "identifier.h" |
|---|
| 15 |
#include "mars.h" |
|---|
| 16 |
#include "lexer.h" |
|---|
| 17 |
#include "id.h" |
|---|
| 18 |
|
|---|
| 19 |
Identifier::Identifier(const char *string, int value) |
|---|
| 20 |
{ |
|---|
| 21 |
//printf("Identifier('%s', %d)\n", string, value); |
|---|
| 22 |
this->string = string; |
|---|
| 23 |
this->value = value; |
|---|
| 24 |
this->len = strlen(string); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
hash_t Identifier::hashCode() |
|---|
| 28 |
{ |
|---|
| 29 |
return String::calcHash(string); |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
int Identifier::equals(Object *o) |
|---|
| 33 |
{ |
|---|
| 34 |
return this == o || memcmp(string,o->toChars(),len+1) == 0; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
int Identifier::compare(Object *o) |
|---|
| 38 |
{ |
|---|
| 39 |
return memcmp(string, o->toChars(), len + 1); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
char *Identifier::toChars() |
|---|
| 43 |
{ |
|---|
| 44 |
return (char *)string; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
char *Identifier::toHChars2() |
|---|
| 48 |
{ |
|---|
| 49 |
char *p = NULL; |
|---|
| 50 |
|
|---|
| 51 |
if (this == Id::ctor) p = "this"; |
|---|
| 52 |
else if (this == Id::dtor) p = "~this"; |
|---|
| 53 |
else if (this == Id::classInvariant) p = "invariant"; |
|---|
| 54 |
else if (this == Id::unitTest) p = "unittest"; |
|---|
| 55 |
else if (this == Id::staticCtor) p = "static this"; |
|---|
| 56 |
else if (this == Id::staticDtor) p = "static ~this"; |
|---|
| 57 |
else if (this == Id::dollar) p = "$"; |
|---|
| 58 |
else if (this == Id::withSym) p = "with"; |
|---|
| 59 |
else if (this == Id::result) p = "result"; |
|---|
| 60 |
else if (this == Id::returnLabel) p = "return"; |
|---|
| 61 |
else |
|---|
| 62 |
p = toChars(); |
|---|
| 63 |
|
|---|
| 64 |
return p; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
void Identifier::print() |
|---|
| 68 |
{ |
|---|
| 69 |
fprintf(stdmsg, "%s",string); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
int Identifier::dyncast() |
|---|
| 73 |
{ |
|---|
| 74 |
return DYNCAST_IDENTIFIER; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
Identifier *Identifier::generateId(char *prefix) |
|---|
| 78 |
{ OutBuffer buf; |
|---|
| 79 |
char *id; |
|---|
| 80 |
static unsigned i; |
|---|
| 81 |
|
|---|
| 82 |
buf.writestring(prefix); |
|---|
| 83 |
buf.printf("%u", ++i); |
|---|
| 84 |
|
|---|
| 85 |
id = buf.toChars(); |
|---|
| 86 |
buf.data = NULL; |
|---|
| 87 |
return new Identifier(id, TOKidentifier); |
|---|
| 88 |
} |
|---|