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