ide/debugger:compiler/libec: Fixed memory corruption (resulting in a crash) occurring...
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 #ifdef _DEBUG
38 Time findSymbolTotalTime;
39 #endif
40
41 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
42 /*static */public void PrintExpression(Expression exp, char * string)
43 {
44    //if(inCompiler)
45    {
46       TempFile f { };
47       int count;
48
49       if(exp)
50          OutputExpression(exp, f);
51       f.Seek(0, start);
52       count = strlen(string);
53       count += f.Read(string + count, 1, 1023);
54       string[count] = '\0';
55       delete f;
56    }
57 }
58
59 int64 _strtoi64(char * string, char ** endString, int base)
60 {
61    int64 value = 0;
62    int sign = 1;
63    int c;
64    char ch;
65    for(c = 0; (ch = string[c]) && isspace(ch); c++);
66    if(ch =='+') c++;
67    else if(ch == '-') { sign = -1; c++; };
68    if(!base)
69    {
70       if(ch == 0 && string[c+1] == 'x')
71       {
72          base = 16;
73          c+=2;
74       }
75       else if(ch == '0')
76       {
77          base = 8;
78          c++;
79       }
80       else
81          base = 10;
82    }
83    for( ;(ch = string[c]); c++)
84    {
85       if(ch == '0')
86          ch = 0;
87       else if(ch >= '1' && ch <= '9')
88          ch -= '1';
89       else if(ch >= 'a' && ch <= 'z') 
90          ch -= 'a'; 
91       else if(ch >= 'A' && ch <= 'Z') 
92          ch -= 'A';
93       else
94       {
95          *endString = string + c;
96          // Invalid character
97          break;
98       }
99       if(ch < base)
100       {
101          value *= base;
102          value += ch;
103       }
104       else
105       {
106          *endString = string + c;
107          // Invalid character
108          break;
109       }
110    }
111    return sign*value;
112 }
113
114 uint64 _strtoui64(char * string, char ** endString, int base)
115 {
116    uint64 value = 0;
117    int sign = 1;
118    int c;
119    char ch;
120    for(c = 0; (ch = string[c]) && isspace(ch); c++);
121    if(ch =='+') c++;
122    else if(ch == '-') { sign = -1; c++; };
123    if(!base)
124    {
125       if(ch == 0 && string[c+1] == 'x')
126       {
127          base = 16;
128          c+=2;
129       }
130       else if(ch == '0')
131       {
132          base = 8;
133          c++;
134       }
135       else
136          base = 10;
137    }
138    for( ;(ch = string[c]); c++)
139    {
140       if(ch == '0')
141          ch = 0;
142       else if(ch >= '1' && ch <= '9')
143          ch -= '1';
144       else if(ch >= 'a' && ch <= 'z') 
145          ch -= 'a'; 
146       else if(ch >= 'A' && ch <= 'Z') 
147          ch -= 'A';
148       else
149       {
150          if(endString) *endString = string + c;
151          // Invalid character
152          break;
153       }
154       if(ch < base)
155       {
156          value *= base;
157          value += ch;
158       }
159       else
160       {
161          if(endString)
162             *endString = string + c;
163          // Invalid character
164          break;
165       }
166    }
167    return sign*value;
168 }
169
170 Type ProcessTemplateParameterType(TemplateParameter param)
171 {
172    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
173    {
174       // TOFIX: Will need to free this Type
175       if(!param.baseType)
176       {
177          if(param.dataTypeString)
178             param.baseType = ProcessTypeString(param.dataTypeString, false);
179          else
180             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
181       }
182       return param.baseType;
183    }
184    return null;
185 }
186
187 bool NeedCast(Type type1, Type type2)
188 {
189    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
190
191    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
192    {
193       return false;
194    }
195
196    if(type1.kind == type2.kind)
197    {
198       switch(type1.kind)
199       {
200          case charType:
201          case shortType:
202          case intType:
203          case int64Type:
204             if(type1.passAsTemplate && !type2.passAsTemplate)
205                return true;
206             return type1.isSigned != type2.isSigned;
207          case classType:
208             return type1._class != type2._class;
209          case pointerType:
210             return NeedCast(type1.type, type2.type);
211          default:
212             return true; //false; ????
213       }
214    }
215    return true;
216 }
217
218 static void ReplaceClassMembers(Expression exp, Class _class)
219 {
220    if(exp.type == identifierExp && exp.identifier)
221    {
222       Identifier id = exp.identifier;
223       Context ctx;
224       Symbol symbol = null;
225       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
226       {
227          // First, check if the identifier is declared inside the function
228          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
229          {
230             symbol = (Symbol)ctx.symbols.FindString(id.string);
231             if(symbol) break;
232          }
233       }
234
235       // If it is not, check if it is a member of the _class
236       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
237       {
238          Property prop = eClass_FindProperty(_class, id.string, privateModule);
239          Method method = null;
240          DataMember member = null;
241          ClassProperty classProp;
242          if(!prop)
243          {
244             method = eClass_FindMethod(_class, id.string, privateModule);
245          }
246          if(!prop && !method)
247             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
248          if(!prop && !method && !member)
249          {
250             classProp = eClass_FindClassProperty(_class, id.string);
251          }
252          if(prop || method || member || classProp)
253          {
254             // Replace by this.[member]
255             exp.type = memberExp;
256             exp.member.member = id;
257             exp.member.memberType = unresolvedMember;
258             exp.member.exp = QMkExpId("this");
259             //exp.member.exp.loc = exp.loc;
260             exp.addedThis = true;
261          }
262          else if(_class && _class.templateParams.first)
263          {
264             Class sClass;
265             for(sClass = _class; sClass; sClass = sClass.base)
266             {
267                if(sClass.templateParams.first)
268                {
269                   ClassTemplateParameter param;
270                   for(param = sClass.templateParams.first; param; param = param.next)
271                   {
272                      if(param.type == expression && !strcmp(param.name, id.string))
273                      {
274                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
275
276                         if(argExp)
277                         {
278                            Declarator decl;
279                            OldList * specs = MkList();
280
281                            FreeIdentifier(exp.member.member);
282
283                            ProcessExpressionType(argExp);
284
285                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
286
287                            exp.expType = ProcessType(specs, decl);
288
289                            // *[expType] *[argExp]
290                            exp.type = bracketsExp;
291                            exp.list = MkListOne(MkExpOp(null, '*',
292                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
293                         }
294                      }
295                   }
296                }
297             }
298          }
299       }
300    }
301 }
302
303 ////////////////////////////////////////////////////////////////////////
304 // PRINTING ////////////////////////////////////////////////////////////
305 ////////////////////////////////////////////////////////////////////////
306
307 public char * PrintInt(int64 result)
308 {
309    char temp[100];
310    if(result > MAXINT64)
311       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
312    else
313       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
314    return CopyString(temp);
315 }
316
317 public char * PrintUInt(uint64 result)
318 {
319    char temp[100];
320    if(result > MAXDWORD)
321       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
322    else if(result > MAXINT)
323       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
324    else
325       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
326    return CopyString(temp);
327 }
328
329 public char * PrintInt64(int64 result)
330 {
331    char temp[100];
332    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
333    return CopyString(temp);
334 }
335
336 public char * PrintUInt64(uint64 result)
337 {
338    char temp[100];
339    if(result > MAXINT64)
340       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
341    else
342       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
343    return CopyString(temp);
344 }
345
346 public char * PrintHexUInt(uint64 result)
347 {
348    char temp[100];
349    if(result > MAXDWORD)
350       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
351    else
352       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
353    return CopyString(temp);
354 }
355
356 public char * PrintHexUInt64(uint64 result)
357 {
358    char temp[100];
359    if(result > MAXDWORD)
360       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
361    else
362       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
363    return CopyString(temp);
364 }
365
366 public char * PrintShort(short result)
367 {
368    char temp[100];
369    sprintf(temp, "%d", (unsigned short)result);
370    return CopyString(temp);
371 }
372
373 public char * PrintUShort(unsigned short result)
374 {
375    char temp[100];
376    if(result > 32767)
377       sprintf(temp, "0x%X", (int)result);
378    else
379       sprintf(temp, "%d", result);
380    return CopyString(temp);
381 }
382
383 public char * PrintChar(char result)
384 {
385    char temp[100];
386    if(result > 0 && isprint(result))
387       sprintf(temp, "'%c'", result);
388    else if(result < 0)
389       sprintf(temp, "%d", result);
390    else
391       //sprintf(temp, "%#X", result);
392       sprintf(temp, "0x%X", (unsigned char)result);
393    return CopyString(temp);
394 }
395
396 public char * PrintUChar(unsigned char result)
397 {
398    char temp[100];
399    sprintf(temp, "0x%X", result);
400    return CopyString(temp);
401 }
402
403 public char * PrintFloat(float result)
404 {
405    char temp[350];
406    sprintf(temp, "%.16ff", result);
407    return CopyString(temp);
408 }
409
410 public char * PrintDouble(double result)
411 {
412    char temp[350];
413    sprintf(temp, "%.16f", result);
414    return CopyString(temp);
415 }
416
417 ////////////////////////////////////////////////////////////////////////
418 ////////////////////////////////////////////////////////////////////////
419
420 //public Operand GetOperand(Expression exp);
421
422 #define GETVALUE(name, t) \
423    public bool Get##name(Expression exp, t * value2) \
424    {                                                        \
425       Operand op2 = GetOperand(exp);                        \
426       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
427       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
428       if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
429       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
430       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
431       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
432       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
433       else if(op2.kind == charType) *value2 = (t) op2.uc;                         \
434       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
435       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
436       else if(op2.kind == pointerType) *value2 = (t) op2.ui;                        \
437       else                                                                          \
438          return false;                                                              \
439       return true;                                                                  \
440    }
441
442 GETVALUE(Int, int);
443 GETVALUE(UInt, unsigned int);
444 GETVALUE(Int64, int64);
445 GETVALUE(UInt64, uint64);
446 GETVALUE(Short, short);
447 GETVALUE(UShort, unsigned short);
448 GETVALUE(Char, char);
449 GETVALUE(UChar, unsigned char);
450 GETVALUE(Float, float);
451 GETVALUE(Double, double);
452
453 void ComputeExpression(Expression exp);
454
455 void ComputeClassMembers(Class _class, bool isMember)
456 {
457    DataMember member = isMember ? (DataMember) _class : null;
458    Context context = isMember ? null : SetupTemplatesContext(_class);
459    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) && 
460                  (_class.type == bitClass || _class.structSize == _class.offset) && _class.computeSize))
461    {
462       int c;
463       int unionMemberOffset = 0;
464       int bitFields = 0;
465
466       if(!member && _class.destructionWatchOffset)
467          _class.memberOffset += sizeof(OldList);
468
469       // To avoid reentrancy...
470       //_class.structSize = -1;
471
472       {
473          DataMember dataMember;
474          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
475          {
476             if(!dataMember.isProperty)
477             {
478                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
479                {
480                   /*if(dataMember.dataType)
481                      printf("");*/
482                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
483                   /*if(!dataMember.dataType)
484                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
485                      */
486                }
487             }
488          }
489       }
490
491       {
492          DataMember dataMember;
493          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
494          {
495             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
496             {
497                if(!isMember && _class.type == bitClass && dataMember.dataType)
498                {
499                   BitMember bitMember = (BitMember) dataMember;
500                   uint64 mask = 0;
501                   int d;
502
503                   ComputeTypeSize(dataMember.dataType);
504
505                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
506                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
507
508                   _class.memberOffset = bitMember.pos + bitMember.size;
509                   for(d = 0; d<bitMember.size; d++)
510                   {
511                      if(d)
512                         mask <<= 1;
513                      mask |= 1;
514                   }
515                   bitMember.mask = mask << bitMember.pos;
516                }
517                else if(dataMember.type == normalMember && dataMember.dataType)
518                {
519                   int size;
520                   int alignment = 0;
521
522                   // Prevent infinite recursion
523                   if(dataMember.dataType.kind != classType || 
524                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
525                      _class.type != structClass)))
526                      ComputeTypeSize(dataMember.dataType);
527
528                   if(dataMember.dataType.bitFieldCount)
529                   {
530                      bitFields += dataMember.dataType.bitFieldCount;
531                      size = 0;
532                   }
533                   else
534                   {
535                      if(bitFields)
536                      {
537                         int size = (bitFields + 7) / 8;
538
539                         if(isMember)
540                         {
541                            // TESTING THIS PADDING CODE
542                            if(alignment)
543                            {
544                               member.structAlignment = Max(member.structAlignment, alignment);
545
546                               if(member.memberOffset % alignment)
547                                  member.memberOffset += alignment - (member.memberOffset % alignment);
548                            }
549
550                            dataMember.offset = member.memberOffset;
551                            if(member.type == unionMember)
552                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
553                            else
554                            {
555                               member.memberOffset += size;
556                            }
557                         }
558                         else
559                         {
560                            // TESTING THIS PADDING CODE
561                            if(alignment)
562                            {
563                               _class.structAlignment = Max(_class.structAlignment, alignment);
564
565                               if(_class.memberOffset % alignment)
566                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
567                            }
568
569                            dataMember.offset = _class.memberOffset;
570                            _class.memberOffset += size;
571                         }
572                         bitFields = 0;
573                      }
574                      size = dataMember.dataType.size;
575                      alignment = dataMember.dataType.alignment;
576                   }
577
578 #ifdef _DEBUG
579                   if(!size)
580                   {
581                      // printf("");
582                   }
583 #endif
584                   if(isMember)
585                   {
586                      // TESTING THIS PADDING CODE
587                      if(alignment)
588                      {
589                         member.structAlignment = Max(member.structAlignment, alignment);
590
591                         if(member.memberOffset % alignment)
592                            member.memberOffset += alignment - (member.memberOffset % alignment);
593                      }
594
595                      dataMember.offset = member.memberOffset;
596                      if(member.type == unionMember)
597                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
598                      else
599                      {
600                         member.memberOffset += size;
601                      }
602                   }
603                   else
604                   {
605                      // TESTING THIS PADDING CODE
606                      if(alignment)
607                      {
608                         _class.structAlignment = Max(_class.structAlignment, alignment);
609
610                         if(_class.memberOffset % alignment)
611                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
612                      }
613
614                      dataMember.offset = _class.memberOffset;
615                      _class.memberOffset += size;
616                   }
617                }
618                else
619                {
620                   ComputeClassMembers((Class)dataMember, true);
621
622                   if(isMember)
623                   {
624                      // THERE WASN'T A MAX HERE ? member.structAlignment = dataMember.structAlignment;
625                      member.structAlignment = Max(member.structAlignment, dataMember.structAlignment);
626                      dataMember.offset = member.memberOffset;
627                      if(member.type == unionMember)
628                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
629                      else
630                         member.memberOffset += dataMember.memberOffset;
631                   }
632                   else
633                   {
634                      _class.structAlignment = Max(_class.structAlignment, dataMember.structAlignment);
635                      dataMember.offset = _class.memberOffset;
636                      _class.memberOffset += dataMember.memberOffset;
637                   }
638                }
639             }
640          }
641          if(bitFields)
642          {
643             int alignment = 0;
644             int size = (bitFields + 7) / 8;
645
646             if(isMember)
647             {
648                // TESTING THIS PADDING CODE
649                if(alignment)
650                {
651                   member.structAlignment = Max(member.structAlignment, alignment);
652
653                   if(member.memberOffset % alignment)
654                      member.memberOffset += alignment - (member.memberOffset % alignment);
655                }
656
657                if(member.type == unionMember)
658                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
659                else
660                {
661                   member.memberOffset += size;
662                }
663             }
664             else
665             {
666                // TESTING THIS PADDING CODE
667                if(alignment)
668                {
669                   _class.structAlignment = Max(_class.structAlignment, alignment);
670
671                   if(_class.memberOffset % alignment)
672                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
673                }
674                _class.memberOffset += size;
675             }
676             bitFields = 0;
677          }
678       }
679       if(member && member.type == unionMember)
680       {
681          member.memberOffset = unionMemberOffset;
682       }
683       
684       if(!isMember)
685       {
686          /*if(_class.type == structClass)
687             _class.size = _class.memberOffset;
688          else
689          */
690
691          if(_class.type != bitClass)
692          {
693             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset;
694             if(!member)
695             {
696                Property prop;
697                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
698                {
699                   if(prop.isProperty && prop.isWatchable)
700                   {
701                      prop.watcherOffset = _class.structSize;
702                      _class.structSize += sizeof(OldList);
703                   }
704                }
705             }
706
707             // Fix Derivatives
708             {
709                OldLink derivative;
710                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
711                {
712                   Class deriv = derivative.data;
713
714                   if(deriv.computeSize)
715                   {
716                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
717                      deriv.offset = /*_class.offset + */_class.structSize;
718                      deriv.memberOffset = 0;
719                      // ----------------------
720
721                      deriv.structSize = deriv.offset;
722
723                      ComputeClassMembers(deriv, false);
724                   }
725                }
726             }
727          }
728       }
729    }
730    if(context)
731       FinishTemplatesContext(context);
732 }
733
734 public void ComputeModuleClasses(Module module)
735 {
736    Class _class;
737    OldLink subModule;
738    
739    for(subModule = module.modules.first; subModule; subModule = subModule.next)
740       ComputeModuleClasses(subModule.data);
741    for(_class = module.classes.first; _class; _class = _class.next)
742       ComputeClassMembers(_class, false);
743 }
744
745
746 public int ComputeTypeSize(Type type)
747 {
748    uint size = type ? type.size : 0;
749    if(!size && type && !type.computing)
750    {
751       type.computing = true;
752       switch(type.kind)
753       {
754          case charType: type.alignment = size = sizeof(char); break;
755          case intType: type.alignment = size = sizeof(int); break;
756          case int64Type: type.alignment = size = sizeof(int64); break;
757          case longType: type.alignment = size = sizeof(long); break;
758          case shortType: type.alignment = size = sizeof(short); break;
759          case floatType: type.alignment = size = sizeof(float); break;
760          case doubleType: type.alignment = size = sizeof(double); break;
761          case classType:
762          {
763             Class _class = type._class ? type._class.registered : null;
764
765             if(_class && _class.type == structClass)
766             {
767                // Ensure all members are properly registered
768                ComputeClassMembers(_class, false);
769                type.alignment = _class.structAlignment;
770                size = _class.structSize;
771                if(type.alignment && size % type.alignment)
772                   size += type.alignment - (size % type.alignment);
773
774             }
775             else if(_class && (_class.type == unitClass || 
776                    _class.type == enumClass || 
777                    _class.type == bitClass))
778             {
779                if(!_class.dataType)
780                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
781                size = type.alignment = ComputeTypeSize(_class.dataType);
782             }
783             else
784                size = type.alignment = sizeof(Instance *);
785             break;
786          }
787          case pointerType: case subClassType: size = type.alignment = sizeof(void *); break;
788          case arrayType: 
789             if(type.arraySizeExp)
790             {
791                ProcessExpressionType(type.arraySizeExp);
792                ComputeExpression(type.arraySizeExp);
793                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType && 
794                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
795                {
796                   Location oldLoc = yylloc;
797                   // bool isConstant = type.arraySizeExp.isConstant;
798                   char expression[10240];
799                   expression[0] = '\0';
800                   type.arraySizeExp.expType = null;
801                   yylloc = type.arraySizeExp.loc;
802                   if(inCompiler)
803                      PrintExpression(type.arraySizeExp, expression);
804                   Compiler_Error($"Array size not constant int (%s)\n", expression);
805                   yylloc = oldLoc;
806                }
807                GetInt(type.arraySizeExp, &type.arraySize);
808 #ifdef _DEBUG
809                if(!type.arraySize)
810                {
811                   printf("");
812                }
813 #endif
814             }
815             else if(type.enumClass)
816             {
817                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
818                {
819                   type.arraySize = eClass_GetProperty(type.enumClass.registered, "enumSize");
820                }
821                else
822                   type.arraySize = 0;
823             }
824             else
825             {
826                // Unimplemented auto size
827                type.arraySize = 0;
828             }
829
830             size = ComputeTypeSize(type.type) * type.arraySize;
831             type.alignment = type.type.alignment;
832             
833             break;
834          case structType:
835          {
836             Type member;
837             for(member = type.members.first; member; member = member.next)
838             {
839                uint addSize = ComputeTypeSize(member);
840
841                member.offset = size;
842                if(member.alignment && size % member.alignment)
843                   member.offset += member.alignment - (size % member.alignment);
844                size = member.offset;
845
846                type.alignment = Max(type.alignment, member.alignment);
847                size += addSize;
848             }
849             if(type.alignment && size % type.alignment)
850                size += type.alignment - (size % type.alignment);
851             break;
852          }
853          case unionType:
854          {
855             Type member;
856             for(member = type.members.first; member; member = member.next)
857             {
858                uint addSize = ComputeTypeSize(member);
859                
860                member.offset = size;
861                if(member.alignment && size % member.alignment)
862                   member.offset += member.alignment - (size % member.alignment);
863                size = member.offset;
864
865                type.alignment = Max(type.alignment, member.alignment);
866                size = Max(size, addSize);
867             }
868             if(type.alignment && size % type.alignment)
869                size += type.alignment - (size % type.alignment);
870             break;
871          }
872          case templateType:
873          {
874             TemplateParameter param = type.templateParameter;
875             Type baseType = ProcessTemplateParameterType(param);
876             if(baseType)
877                size = ComputeTypeSize(baseType);
878             else
879                size = sizeof(uint64);
880             break;
881          }
882          case enumType:
883          {
884             size = sizeof(enum { test });
885             break;
886          }
887          case thisClassType:
888          {
889             size = sizeof(void *);
890             break;
891          }
892       }
893       type.size = size;
894       type.computing = false;
895    }
896    return size;
897 }
898
899
900 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass)
901 {
902    // This function is in need of a major review when implementing private members etc.
903    DataMember topMember = isMember ? (DataMember) _class : null;
904    uint totalSize = 0;
905    uint maxSize = 0;
906    int alignment, size;
907    DataMember member;
908    Context context = isMember ? null : SetupTemplatesContext(_class);
909
910    if(!isMember && _class.base)
911    {
912       maxSize = _class.structSize;
913       //if(_class.base.type != systemClass) // Commented out with new Instance _class
914       {
915          // DANGER: Testing this noHeadClass here...
916          if(_class.type == structClass || _class.type == noHeadClass)
917             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass);
918          else
919             maxSize -= _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
920       }
921    }
922
923    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
924    {
925       if(!member.isProperty)
926       {
927          switch(member.type)
928          {
929             case normalMember:
930             {
931                if(member.dataTypeString)
932                {
933                   OldList * specs = MkList(), * decls = MkList();
934                   Declarator decl;
935
936                   decl = SpecDeclFromString(member.dataTypeString, specs, 
937                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
938                   ListAdd(decls, MkStructDeclarator(decl, null));
939                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
940
941                   if(!member.dataType)
942                      member.dataType = ProcessType(specs, decl);
943
944                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
945
946                   {
947                      Type type = ProcessType(specs, decl);
948                      DeclareType(member.dataType, false, false);
949                      FreeType(type);
950                   }
951                   /*
952                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
953                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
954                      DeclareStruct(member.dataType._class.string, false);
955                   */
956
957                   ComputeTypeSize(member.dataType);
958                   size = member.dataType.size;
959                   alignment = member.dataType.alignment;
960
961                   if(alignment)
962                   {
963                      if(totalSize % alignment)
964                         totalSize += alignment - (totalSize % alignment);
965                   }
966                   totalSize += size;
967                }
968                break;
969             }
970             case unionMember:
971             case structMember:
972             {
973                OldList * specs = MkList(), * list = MkList();
974                
975                size = 0;
976                AddMembers(list, (Class)member, true, &size, topClass);
977                ListAdd(specs, 
978                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
979                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
980                alignment = member.structAlignment;
981
982                if(alignment)
983                {
984                   if(totalSize % alignment)
985                      totalSize += alignment - (totalSize % alignment);
986                }
987                totalSize += size;
988                break;
989             }
990          }
991       }
992    }
993    if(retSize)
994    {
995       if(topMember && topMember.type == unionMember)
996          *retSize = Max(*retSize, totalSize);
997       else
998          *retSize += totalSize;
999    }
1000    else if(totalSize < maxSize && _class.type != systemClass)
1001    {
1002       char sizeString[50];
1003       sprintf(sizeString, "%d", maxSize - totalSize);
1004       ListAdd(declarations, 
1005          MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)), 
1006          MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1007    }
1008    if(context)
1009       FinishTemplatesContext(context);
1010    return topMember ? topMember.memberID : _class.memberID;
1011 }
1012
1013 static int DeclareMembers(Class _class, bool isMember)
1014 {
1015    DataMember topMember = isMember ? (DataMember) _class : null;
1016    uint totalSize = 0;
1017    DataMember member;
1018    Context context = isMember ? null : SetupTemplatesContext(_class);
1019    
1020    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1021       DeclareMembers(_class.base, false);
1022
1023    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1024    {
1025       if(!member.isProperty)
1026       {
1027          switch(member.type)
1028          {
1029             case normalMember:
1030             {
1031                /*
1032                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1033                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1034                   DeclareStruct(member.dataType._class.string, false);
1035                   */
1036                if(!member.dataType && member.dataTypeString)
1037                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1038                if(member.dataType)
1039                   DeclareType(member.dataType, false, false);
1040                break;
1041             }
1042             case unionMember:
1043             case structMember:
1044             {
1045                DeclareMembers((Class)member, true);
1046                break;
1047             }
1048          }
1049       }
1050    }
1051    if(context)
1052       FinishTemplatesContext(context);
1053
1054    return topMember ? topMember.memberID : _class.memberID;
1055 }
1056
1057 void DeclareStruct(char * name, bool skipNoHead)
1058 {
1059    External external = null;
1060    Symbol classSym = FindClass(name);
1061
1062    if(!inCompiler || !classSym) return null;
1063
1064    // We don't need any declaration for bit classes...
1065    if(classSym.registered && 
1066       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1067       return null;
1068
1069    /*if(classSym.registered.templateClass)
1070       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1071    */
1072
1073    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1074    {
1075       // Add typedef struct
1076       Declaration decl;
1077       OldList * specifiers, * declarators;
1078       OldList * declarations = null;
1079       char structName[1024];
1080       external = (classSym.registered && classSym.registered.type == structClass) ? 
1081          classSym.pointerExternal : classSym.structExternal;
1082
1083       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1084       // Moved this one up because DeclareClass done later will need it
1085
1086       classSym.declaring++;
1087
1088       if(strchr(classSym.string, '<'))
1089       {
1090          if(classSym.registered.templateClass)
1091          {
1092             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1093             classSym.declaring--;
1094          }
1095          return null;
1096       }
1097       
1098       //if(!skipNoHead)
1099          DeclareMembers(classSym.registered, false);
1100
1101       structName[0] = 0;
1102       FullClassNameCat(structName, name, false);
1103
1104       /*if(!external)      
1105          external = MkExternalDeclaration(null);*/
1106
1107       if(!skipNoHead)
1108       {
1109          classSym.declaredStructSym = true;
1110
1111          declarations = MkList();
1112
1113          AddMembers(declarations, classSym.registered, false, null, classSym.registered);
1114
1115          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1116          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1117
1118          if(!declarations->count)
1119          {
1120             FreeList(declarations, null);
1121             declarations = null;
1122          }
1123       }
1124       if(skipNoHead || declarations)
1125       {
1126          if(external && external.declaration)
1127          {
1128             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1129
1130             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1131             {
1132                // TODO: Fix this
1133                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1134
1135                // DANGER
1136                if(classSym.structExternal)
1137                   ast->Move(classSym.structExternal, curExternal.prev);
1138                ast->Move(classSym.pointerExternal, curExternal.prev);
1139
1140                classSym.id = curExternal.symbol.idCode;
1141                classSym.idCode = curExternal.symbol.idCode;
1142                // external = classSym.pointerExternal;
1143                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1144             }
1145          }
1146          else
1147          {
1148             if(!external)      
1149                external = MkExternalDeclaration(null);
1150
1151             specifiers = MkList();
1152             declarators = MkList();
1153             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1154
1155             /*
1156             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1157             ListAdd(declarators, MkInitDeclarator(d, null));
1158             */
1159             external.declaration = decl = MkDeclaration(specifiers, declarators);
1160             if(decl.symbol && !decl.symbol.pointerExternal)
1161                decl.symbol.pointerExternal = external;
1162
1163             // For simple classes, keep the declaration as the external to move around
1164             if(classSym.registered && classSym.registered.type == structClass)
1165             {
1166                char className[1024];
1167                strcpy(className, "__ecereClass_");
1168                FullClassNameCat(className, classSym.string, true);
1169                MangleClassName(className);
1170
1171                // Testing This
1172                DeclareClass(classSym, className);
1173
1174                external.symbol = classSym;
1175                classSym.pointerExternal = external;
1176                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1177                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1178             }
1179             else
1180             {
1181                char className[1024];
1182                strcpy(className, "__ecereClass_");
1183                FullClassNameCat(className, classSym.string, true);
1184                MangleClassName(className);
1185
1186                // TOFIX: TESTING THIS...
1187                classSym.structExternal = external;
1188                DeclareClass(classSym, className);
1189                external.symbol = classSym;
1190             }
1191
1192             //if(curExternal)
1193                ast->Insert(curExternal ? curExternal.prev : null, external);
1194          }
1195       }
1196
1197       classSym.declaring--;
1198    }
1199    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1200    {
1201       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1202       // Moved this one up because DeclareClass done later will need it
1203
1204       // TESTING THIS:
1205       classSym.declaring++;
1206
1207       //if(!skipNoHead)
1208       {
1209          if(classSym.registered)
1210             DeclareMembers(classSym.registered, false);
1211       }
1212
1213       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1214       {
1215          // TODO: Fix this
1216          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1217
1218          // DANGER
1219          if(classSym.structExternal)
1220             ast->Move(classSym.structExternal, curExternal.prev);
1221          ast->Move(classSym.pointerExternal, curExternal.prev);
1222
1223          classSym.id = curExternal.symbol.idCode;
1224          classSym.idCode = curExternal.symbol.idCode;
1225          // external = classSym.pointerExternal;
1226          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1227       }
1228
1229       classSym.declaring--;
1230    }
1231    //return external;
1232 }
1233
1234 void DeclareProperty(Property prop, char * setName, char * getName)
1235 {
1236    Symbol symbol = prop.symbol;
1237    char propName[1024];
1238
1239    strcpy(setName, "__ecereProp_");
1240    FullClassNameCat(setName, prop._class.fullName, false);
1241    strcat(setName, "_Set_");
1242    // strcat(setName, prop.name);
1243    FullClassNameCat(setName, prop.name, true);
1244
1245    strcpy(getName, "__ecereProp_");
1246    FullClassNameCat(getName, prop._class.fullName, false);
1247    strcat(getName, "_Get_");
1248    FullClassNameCat(getName, prop.name, true);
1249    // strcat(getName, prop.name);
1250
1251    strcpy(propName, "__ecereProp_");
1252    FullClassNameCat(propName, prop._class.fullName, false);
1253    strcat(propName, "_");
1254    FullClassNameCat(propName, prop.name, true);
1255    // strcat(propName, prop.name);
1256
1257    // To support "char *" property
1258    MangleClassName(getName);
1259    MangleClassName(setName);
1260    MangleClassName(propName);
1261
1262    if(prop._class.type == structClass)
1263       DeclareStruct(prop._class.fullName, false);
1264
1265    if(!symbol || curExternal.symbol.idCode < symbol.id)
1266    {
1267       bool imported = false;
1268       bool dllImport = false;
1269       if(!symbol || symbol._import)
1270       {
1271          if(!symbol)
1272          {
1273             Symbol classSym;
1274             if(!prop._class.symbol)
1275                prop._class.symbol = FindClass(prop._class.fullName);
1276             classSym = prop._class.symbol;
1277             if(classSym && !classSym._import)
1278             {
1279                ModuleImport module;
1280
1281                if(prop._class.module)
1282                   module = FindModule(prop._class.module);
1283                else
1284                   module = mainModule;
1285
1286                classSym._import = ClassImport
1287                {
1288                   name = CopyString(prop._class.fullName);
1289                   isRemote = prop._class.isRemote;
1290                };
1291                module.classes.Add(classSym._import);
1292             }
1293             symbol = prop.symbol = Symbol { };
1294             symbol._import = (ClassImport)PropertyImport
1295             {
1296                name = CopyString(prop.name);
1297                isVirtual = false; //prop.isVirtual;
1298                hasSet = prop.Set ? true : false;
1299                hasGet = prop.Get ? true : false;
1300             };
1301             if(classSym)
1302                classSym._import.properties.Add(symbol._import);
1303          }
1304          imported = true;
1305          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1306             dllImport = true;
1307       }
1308
1309       if(!symbol.type)
1310       {
1311          Context context = SetupTemplatesContext(prop._class);
1312          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1313          FinishTemplatesContext(context);
1314       }
1315
1316       // Get
1317       if(prop.Get)
1318       {
1319          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1320          {
1321             Declaration decl;
1322             OldList * specifiers, * declarators;
1323             Declarator d;
1324             OldList * params;
1325             Specifier spec;
1326             External external;
1327             Declarator typeDecl;
1328             bool simple = false;
1329
1330             specifiers = MkList();
1331             declarators = MkList();
1332             params = MkList();
1333
1334             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)), 
1335                MkDeclaratorIdentifier(MkIdentifier("this"))));
1336
1337             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1338             //if(imported)
1339             if(dllImport)
1340                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1341
1342             {
1343                Context context = SetupTemplatesContext(prop._class);
1344                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1345                FinishTemplatesContext(context);
1346             }
1347
1348             // Make sure the simple _class's type is declared
1349             for(spec = specifiers->first; spec; spec = spec.next)
1350             {
1351                if(spec.type == nameSpecifier /*SpecifierClass*/)
1352                {
1353                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1354                   {
1355                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1356                      symbol._class = classSym.registered;
1357                      if(classSym.registered && classSym.registered.type == structClass)
1358                      {
1359                         DeclareStruct(spec.name, false);
1360                         simple = true;
1361                      }
1362                   }
1363                }
1364             }
1365
1366             if(!simple)
1367                d = PlugDeclarator(typeDecl, d);
1368             else
1369             {
1370                ListAdd(params, MkTypeName(specifiers, 
1371                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1372                specifiers = MkList();
1373             }
1374
1375             d = MkDeclaratorFunction(d, params);
1376  
1377             //if(imported)
1378             if(dllImport)
1379                specifiers->Insert(null, MkSpecifier(EXTERN));
1380             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1381                specifiers->Insert(null, MkSpecifier(STATIC));
1382             if(simple)
1383                ListAdd(specifiers, MkSpecifier(VOID));
1384
1385             ListAdd(declarators, MkInitDeclarator(d, null));
1386
1387             decl = MkDeclaration(specifiers, declarators);
1388
1389             external = MkExternalDeclaration(decl);
1390             ast->Insert(curExternal.prev, external);
1391             external.symbol = symbol;
1392             symbol.externalGet = external;
1393
1394             ReplaceThisClassSpecifiers(specifiers, prop._class);
1395
1396             if(typeDecl)
1397                FreeDeclarator(typeDecl);
1398          }
1399          else
1400          {
1401             // Move declaration higher...
1402             ast->Move(symbol.externalGet, curExternal.prev);
1403          }
1404       }
1405
1406       // Set
1407       if(prop.Set)
1408       {
1409          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1410          {
1411             Declaration decl;
1412             OldList * specifiers, * declarators;
1413             Declarator d;
1414             OldList * params;
1415             Specifier spec;
1416             External external;
1417             Declarator typeDecl;
1418
1419             declarators = MkList();
1420             params = MkList();
1421
1422             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1423             if(!prop.conversion || prop._class.type == structClass)
1424             {
1425                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)), 
1426                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1427             }
1428
1429             specifiers = MkList();
1430
1431             {
1432                Context context = SetupTemplatesContext(prop._class);
1433                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1434                   MkDeclaratorIdentifier(MkIdentifier("value")));
1435                FinishTemplatesContext(context);
1436             }
1437             ListAdd(params, MkTypeName(specifiers, d));
1438
1439             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1440             //if(imported)
1441             if(dllImport)
1442                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1443             d = MkDeclaratorFunction(d, params);
1444
1445             // Make sure the simple _class's type is declared
1446             for(spec = specifiers->first; spec; spec = spec.next)
1447             {
1448                if(spec.type == nameSpecifier /*SpecifierClass*/)
1449                {
1450                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1451                   {
1452                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1453                      symbol._class = classSym.registered;
1454                      if(classSym.registered && classSym.registered.type == structClass)
1455                         DeclareStruct(spec.name, false);
1456                   }
1457                }
1458             }
1459
1460             ListAdd(declarators, MkInitDeclarator(d, null));
1461
1462             specifiers = MkList();
1463             //if(imported)
1464             if(dllImport)
1465                specifiers->Insert(null, MkSpecifier(EXTERN));
1466             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1467                specifiers->Insert(null, MkSpecifier(STATIC));
1468
1469             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1470             if(!prop.conversion || prop._class.type == structClass)
1471                ListAdd(specifiers, MkSpecifier(VOID));
1472             else
1473                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1474
1475             decl = MkDeclaration(specifiers, declarators);
1476
1477             external = MkExternalDeclaration(decl);
1478             ast->Insert(curExternal.prev, external);
1479             external.symbol = symbol;
1480             symbol.externalSet = external;
1481
1482             ReplaceThisClassSpecifiers(specifiers, prop._class);
1483          }
1484          else
1485          {
1486             // Move declaration higher...
1487             ast->Move(symbol.externalSet, curExternal.prev);
1488          }
1489       }
1490
1491       // Property (for Watchers)
1492       if(!symbol.externalPtr)
1493       {
1494          Declaration decl;
1495          External external;
1496          OldList * specifiers = MkList();
1497
1498          if(imported)
1499             specifiers->Insert(null, MkSpecifier(EXTERN));
1500          else
1501             specifiers->Insert(null, MkSpecifier(STATIC));
1502
1503          ListAdd(specifiers, MkSpecifierName("Property"));
1504
1505          {
1506             OldList * list = MkList();
1507             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1508                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1509
1510             if(!imported)
1511             {
1512                strcpy(propName, "__ecerePropM_");
1513                FullClassNameCat(propName, prop._class.fullName, false);
1514                strcat(propName, "_");
1515                // strcat(propName, prop.name);
1516                FullClassNameCat(propName, prop.name, true);
1517
1518                MangleClassName(propName);
1519
1520                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1521                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1522             }
1523             decl = MkDeclaration(specifiers, list);
1524          }
1525
1526          external = MkExternalDeclaration(decl);
1527          ast->Insert(curExternal.prev, external);
1528          external.symbol = symbol;
1529          symbol.externalPtr = external;
1530       }
1531       else
1532       {
1533          // Move declaration higher...
1534          ast->Move(symbol.externalPtr, curExternal.prev);
1535       }
1536
1537       symbol.id = curExternal.symbol.idCode;
1538    }
1539 }
1540
1541 // ***************** EXPRESSION PROCESSING ***************************
1542 public Type Dereference(Type source)
1543 {
1544    Type type = null;
1545    if(source)
1546    {
1547       if(source.kind == pointerType || source.kind == arrayType)
1548       {
1549          type = source.type;
1550          source.type.refCount++;
1551       }
1552       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1553       {
1554          type = Type
1555          {
1556             kind = charType;
1557             refCount = 1;
1558          };
1559       }
1560       // Support dereferencing of no head classes for now...
1561       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1562       {
1563          type = source;
1564          source.refCount++;
1565       }
1566       else
1567          Compiler_Error($"cannot dereference type\n");
1568    }
1569    return type;
1570 }
1571
1572 static Type Reference(Type source)
1573 {
1574    Type type = null;
1575    if(source)
1576    {
1577       type = Type
1578       {
1579          kind = pointerType;
1580          type = source;
1581          refCount = 1;
1582       };
1583       source.refCount++;
1584    }
1585    return type;
1586 }
1587
1588 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1589 {
1590    Identifier ident = member.identifiers ? member.identifiers->first : null;
1591    bool found = false;
1592    DataMember dataMember = null;
1593    Method method = null;
1594    bool freeType = false;
1595
1596    yylloc = member.loc;
1597
1598    if(!ident)
1599    {
1600       if(curMember)
1601       {
1602          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1603          if(*curMember)
1604          {
1605             found = true;
1606             dataMember = *curMember;
1607          }
1608       }
1609    }
1610    else
1611    {
1612       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1613       DataMember _subMemberStack[256];
1614       int _subMemberStackPos = 0;
1615
1616       // FILL MEMBER STACK
1617       if(!thisMember)
1618          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1619       if(thisMember)
1620       {
1621          dataMember = thisMember;
1622          if(curMember && thisMember.memberAccess == publicAccess)
1623          {
1624             *curMember = thisMember;
1625             *curClass = thisMember._class;
1626             memcpy(subMemberStack, _subMemberStack, sizeof(int) * _subMemberStackPos);
1627             *subMemberStackPos = _subMemberStackPos;
1628          }
1629          found = true;
1630       }
1631       else
1632       {
1633          // Setting a method
1634          method = eClass_FindMethod(_class, ident.string, privateModule);
1635          if(method && method.type == virtualMethod)
1636             found = true;
1637          else
1638             method = null;
1639       }
1640    }
1641
1642    if(found)
1643    {
1644       Type type = null;
1645       if(dataMember)
1646       {
1647          if(!dataMember.dataType && dataMember.dataTypeString)
1648          {
1649             //Context context = SetupTemplatesContext(dataMember._class);
1650             Context context = SetupTemplatesContext(_class);
1651             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1652             FinishTemplatesContext(context);
1653          }
1654          type = dataMember.dataType;
1655       }
1656       else if(method)
1657       {
1658          // This is for destination type...
1659          if(!method.dataType)
1660             ProcessMethodType(method);
1661          //DeclareMethod(method);
1662          // method.dataType = ((Symbol)method.symbol)->type;
1663          type = method.dataType;
1664       }
1665
1666       if(ident && ident.next)
1667       {
1668          for(ident = ident.next; ident && type; ident = ident.next)
1669          {
1670             if(type.kind == classType)
1671             {
1672                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1673                if(!dataMember)
1674                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1675                if(dataMember)
1676                   type = dataMember.dataType;
1677             }
1678             else if(type.kind == structType || type.kind == unionType)
1679             {
1680                Type memberType;
1681                for(memberType = type.members.first; memberType; memberType = memberType.next)
1682                {
1683                   if(!strcmp(memberType.name, ident.string))
1684                   {
1685                      type = memberType;
1686                      break;
1687                   }
1688                }
1689             }
1690          }
1691       }
1692
1693       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1694       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1695       {
1696          int id = 0;
1697          ClassTemplateParameter curParam;
1698          Class sClass;
1699          for(sClass = _class; sClass; sClass = sClass.base)
1700          {
1701             id = 0;
1702             if(sClass.templateClass) sClass = sClass.templateClass;
1703             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1704             {
1705                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1706                {
1707                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1708                   {
1709                      if(sClass.templateClass) sClass = sClass.templateClass;
1710                      id += sClass.templateParams.count;
1711                   }
1712                   break;
1713                }
1714                id++;
1715             }
1716             if(curParam) break;
1717          }
1718
1719          if(curParam)
1720          {
1721             ClassTemplateArgument arg = _class.templateArgs[id];
1722             if(arg.dataTypeString)
1723             {
1724                // FreeType(type);
1725                type = ProcessTypeString(arg.dataTypeString, false);
1726                freeType = true;
1727                if(type && _class.templateClass)
1728                   type.passAsTemplate = true;
1729                if(type)
1730                {
1731                   // type.refCount++;
1732                   /*if(!exp.destType)
1733                   {
1734                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1735                      exp.destType.refCount++;
1736                   }*/
1737                }
1738             }
1739          }
1740       }
1741       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1742       {
1743          Class expClass = type._class.registered;
1744          Class cClass = null;
1745          int c;
1746          int paramCount = 0;
1747          int lastParam = -1;
1748          
1749          char templateString[1024];
1750          ClassTemplateParameter param;
1751          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1752          for(cClass = expClass; cClass; cClass = cClass.base)
1753          {
1754             int p = 0;
1755             if(cClass.templateClass) cClass = cClass.templateClass;
1756             for(param = cClass.templateParams.first; param; param = param.next)
1757             {
1758                int id = p;
1759                Class sClass;
1760                ClassTemplateArgument arg;
1761                for(sClass = cClass.base; sClass; sClass = sClass.base) 
1762                {
1763                   if(sClass.templateClass) sClass = sClass.templateClass;
1764                   id += sClass.templateParams.count;
1765                }
1766                arg = expClass.templateArgs[id];
1767
1768                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1769                {
1770                   ClassTemplateParameter cParam;
1771                   //int p = numParams - sClass.templateParams.count;
1772                   int p = 0;
1773                   Class nextClass;
1774                   if(sClass.templateClass) sClass = sClass.templateClass;
1775
1776                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) 
1777                   {
1778                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1779                      p += nextClass.templateParams.count;
1780                   }
1781                   
1782                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1783                   {
1784                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1785                      {
1786                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1787                         {
1788                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1789                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1790                            break;
1791                         }
1792                      }
1793                   }
1794                }
1795
1796                {
1797                   char argument[256];
1798                   argument[0] = '\0';
1799                   /*if(arg.name)
1800                   {
1801                      strcat(argument, arg.name.string);
1802                      strcat(argument, " = ");
1803                   }*/
1804                   switch(param.type)
1805                   {
1806                      case expression:
1807                      {
1808                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1809                         char expString[1024];
1810                         OldList * specs = MkList();
1811                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1812                         Expression exp;
1813                         char * string = PrintHexUInt64(arg.expression.ui64);
1814                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1815
1816                         ProcessExpressionType(exp);
1817                         ComputeExpression(exp);
1818                         expString[0] = '\0';
1819                         PrintExpression(exp, expString);
1820                         strcat(argument, expString);
1821                         //delete exp;
1822                         FreeExpression(exp);
1823                         break;
1824                      }
1825                      case identifier:
1826                      {
1827                         strcat(argument, arg.member.name);
1828                         break;
1829                      }
1830                      case TemplateParameterType::type:
1831                      {
1832                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1833                            strcat(argument, arg.dataTypeString);
1834                         break;
1835                      }
1836                   }
1837                   if(argument[0])
1838                   {
1839                      if(paramCount) strcat(templateString, ", ");
1840                      if(lastParam != p - 1)
1841                      {
1842                         strcat(templateString, param.name);
1843                         strcat(templateString, " = ");
1844                      }
1845                      strcat(templateString, argument);
1846                      paramCount++;
1847                      lastParam = p;
1848                   }
1849                   p++;
1850                }               
1851             }
1852          }
1853          {
1854             int len = strlen(templateString);
1855             if(templateString[len-1] == '<')
1856                len--;
1857             else
1858             {
1859                if(templateString[len-1] == '>')
1860                   templateString[len++] = ' ';
1861                templateString[len++] = '>';
1862             }
1863             templateString[len++] = '\0';
1864          }
1865          {
1866             Context context = SetupTemplatesContext(_class);
1867             if(freeType) FreeType(type);
1868             type = ProcessTypeString(templateString, false);
1869             freeType = true;
1870             FinishTemplatesContext(context);
1871          }
1872       }
1873
1874       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1875       {
1876          ProcessExpressionType(member.initializer.exp);
1877          if(!member.initializer.exp.expType)
1878          {
1879             if(inCompiler)
1880             {
1881                char expString[10240];
1882                expString[0] = '\0';
1883                PrintExpression(member.initializer.exp, expString);
1884                ChangeCh(expString, '\n', ' ');
1885                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1886             }
1887          }
1888          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1889          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1890          {
1891             Compiler_Error($"incompatible instance method %s\n", ident.string);
1892          }
1893       }
1894       else if(member.initializer)
1895       {
1896          /*
1897          FreeType(member.exp.destType);
1898          member.exp.destType = type;
1899          if(member.exp.destType)
1900             member.exp.destType.refCount++;
1901          ProcessExpressionType(member.exp);
1902          */
1903
1904          ProcessInitializer(member.initializer, type);
1905       }
1906       if(freeType) FreeType(type);
1907    }
1908    else
1909    {
1910       if(_class && _class.type == unitClass)
1911       {
1912          if(member.initializer)
1913          {
1914             /*
1915             FreeType(member.exp.destType);
1916             member.exp.destType = MkClassType(_class.fullName);
1917             ProcessExpressionType(member.initializer, type);
1918             */
1919             Type type = MkClassType(_class.fullName);
1920             ProcessInitializer(member.initializer, type);
1921             FreeType(type);
1922          }
1923       }
1924       else
1925       {
1926          if(member.initializer)
1927          {
1928             //ProcessExpressionType(member.exp);
1929             ProcessInitializer(member.initializer, null);
1930          }
1931          if(ident)
1932          {
1933             if(method)
1934             {
1935                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1936             }
1937             else if(_class)
1938             {
1939                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1940                if(inCompiler)
1941                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1942             }
1943          }
1944          else if(_class)
1945             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1946       }
1947    }
1948 }
1949
1950 void ProcessInstantiationType(Instantiation inst)
1951 {
1952    yylloc = inst.loc;
1953    if(inst._class)
1954    {
1955       MembersInit members;
1956       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1957       Class _class;
1958       
1959       /*if(!inst._class.symbol)
1960          inst._class.symbol = FindClass(inst._class.name);*/
1961       classSym = inst._class.symbol;
1962       _class = classSym ? classSym.registered : null;
1963
1964       // DANGER: Patch for mutex not declaring its struct when not needed
1965       if(!_class || _class.type != noHeadClass)
1966          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1967
1968       afterExternal = afterExternal ? afterExternal : curExternal;
1969
1970       if(inst.exp)
1971          ProcessExpressionType(inst.exp);
1972
1973       inst.isConstant = true;
1974       if(inst.members)
1975       {
1976          DataMember curMember = null;
1977          Class curClass = null;
1978          DataMember subMemberStack[256];
1979          int subMemberStackPos = 0;
1980
1981          for(members = inst.members->first; members; members = members.next)
1982          {
1983             switch(members.type)
1984             {
1985                case methodMembersInit:
1986                {
1987                   char name[1024];
1988                   static uint instMethodID = 0;
1989                   External external = curExternal;
1990                   Context context = curContext;
1991                   Declarator declarator = members.function.declarator;
1992                   Identifier nameID = GetDeclId(declarator);
1993                   char * unmangled = nameID ? nameID.string : null;
1994                   Expression exp;
1995                   External createdExternal = null;
1996
1997                   if(inCompiler)
1998                   {
1999                      char number[16];
2000                      //members.function.dontMangle = true;
2001                      strcpy(name, "__ecereInstMeth_");
2002                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2003                      strcat(name, "_");
2004                      strcat(name, nameID.string);
2005                      strcat(name, "_");
2006                      sprintf(number, "_%08d", instMethodID++);
2007                      strcat(name, number);                     
2008                      nameID.string = CopyString(name);
2009                   }
2010
2011                   // Do modifications here...
2012                   if(declarator)
2013                   {
2014                      Symbol symbol = declarator.symbol;
2015                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2016                                     
2017                      if(method && method.type == virtualMethod)
2018                      {
2019                         symbol.method = method;
2020                         ProcessMethodType(method);
2021
2022                         if(!symbol.type.thisClass)
2023                         {
2024                            if(method.dataType.thisClass && currentClass && 
2025                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2026                            {
2027                               if(!currentClass.symbol)
2028                                  currentClass.symbol = FindClass(currentClass.fullName);
2029                               symbol.type.thisClass = currentClass.symbol;
2030                            }
2031                            else
2032                            {
2033                               if(!_class.symbol)
2034                                  _class.symbol = FindClass(_class.fullName);
2035                               symbol.type.thisClass = _class.symbol;
2036                            }
2037                         }
2038                         // TESTING THIS HERE:
2039                         DeclareType(symbol.type, true, true);
2040
2041                      }
2042                      else if(classSym)
2043                      {
2044                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2045                            unmangled, classSym.string);
2046                      }
2047                   }
2048
2049                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2050                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2051
2052                   if(nameID)
2053                   {
2054                      FreeSpecifier(nameID._class);
2055                      nameID._class = null;
2056                   }
2057
2058                   if(inCompiler)
2059                   {
2060
2061                      Type type = declarator.symbol.type;
2062                      External oldExternal = curExternal;
2063
2064                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2065                      // *** It was commented out for problems such as
2066                      /*
2067                            class VirtualDesktop : Window
2068                            {
2069                               clientSize = Size { };
2070                               Timer timer
2071                               {
2072                                  bool DelayExpired()
2073                                  {
2074                                     clientSize.w;
2075                                     return true;
2076                                  }
2077                               };
2078                            }
2079                      */
2080                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2081
2082                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2083
2084                      /*
2085                      if(strcmp(declarator.symbol.string, name))
2086                      {
2087                         printf("TOCHECK: Look out for this\n");
2088                         delete declarator.symbol.string;
2089                         declarator.symbol.string = CopyString(name);
2090                      }
2091                      
2092                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2093                      {
2094                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2095                         excludedSymbols->Remove(declarator.symbol);
2096                         globalContext.symbols.Add((BTNode)declarator.symbol);
2097                         if(strstr(declarator.symbol.string), "::")
2098                            globalContext.hasNameSpace = true;
2099
2100                      }
2101                      */
2102                   
2103                      //curExternal = curExternal.prev;
2104                      //afterExternal = afterExternal->next;
2105
2106                      //ProcessFunction(afterExternal->function);
2107
2108                      //curExternal = afterExternal;
2109                      {
2110                         External externalDecl;
2111                         externalDecl = MkExternalDeclaration(null);
2112                         ast->Insert(oldExternal.prev, externalDecl);
2113
2114                         // Which function does this process?
2115                         if(createdExternal.function)
2116                         {
2117                            ProcessFunction(createdExternal.function);
2118
2119                            //curExternal = oldExternal;
2120
2121                            {
2122                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2123
2124                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
2125                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2126                      
2127                               //externalDecl = MkExternalDeclaration(decl);
2128                         
2129                               //***** ast->Insert(external.prev, externalDecl);
2130                               //ast->Insert(curExternal.prev, externalDecl);
2131                               externalDecl.declaration = decl;
2132                               if(decl.symbol && !decl.symbol.pointerExternal)
2133                                  decl.symbol.pointerExternal = externalDecl;
2134
2135                               // Trying this out...
2136                               declarator.symbol.pointerExternal = externalDecl;
2137                            }
2138                         }
2139                      }
2140                   }
2141                   else if(declarator)
2142                   {
2143                      curExternal = declarator.symbol.pointerExternal;
2144                      ProcessFunction((FunctionDefinition)members.function);
2145                   }
2146                   curExternal = external;
2147                   curContext = context;
2148
2149                   if(inCompiler)
2150                   {
2151                      FreeClassFunction(members.function);
2152
2153                      // In this pass, turn this into a MemberInitData
2154                      exp = QMkExpId(name);
2155                      members.type = dataMembersInit;
2156                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2157
2158                      delete unmangled;
2159                   }
2160                   break;
2161                }
2162                case dataMembersInit:
2163                {
2164                   if(members.dataMembers && classSym)
2165                   {
2166                      MemberInit member;
2167                      Location oldyyloc = yylloc;
2168                      for(member = members.dataMembers->first; member; member = member.next)
2169                      {
2170                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2171                         if(member.initializer && !member.initializer.isConstant)
2172                            inst.isConstant = false;
2173                      }
2174                      yylloc = oldyyloc;
2175                   }
2176                   break;
2177                }
2178             }
2179          }
2180       }
2181    }
2182 }
2183
2184 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2185 {
2186    // OPTIMIZATIONS: TESTING THIS...
2187    if(inCompiler)
2188    {
2189       if(type.kind == functionType)
2190       {
2191          Type param;
2192          if(declareParams)
2193          {
2194             for(param = type.params.first; param; param = param.next)
2195                DeclareType(param, declarePointers, true);
2196          }
2197          DeclareType(type.returnType, declarePointers, true);
2198       }
2199       else if(type.kind == pointerType && declarePointers)
2200          DeclareType(type.type, declarePointers, false);
2201       else if(type.kind == classType)
2202       {
2203          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2204             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2205       }
2206       else if(type.kind == structType || type.kind == unionType)
2207       {
2208          Type member;
2209          for(member = type.members.first; member; member = member.next)
2210             DeclareType(member, false, false);
2211       }
2212       else if(type.kind == arrayType)
2213          DeclareType(type.arrayType, declarePointers, false);
2214    }
2215 }
2216
2217 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2218 {
2219    ClassTemplateArgument * arg = null;
2220    int id = 0;
2221    ClassTemplateParameter curParam;
2222    Class sClass;
2223    for(sClass = _class; sClass; sClass = sClass.base)
2224    {
2225       id = 0;
2226       if(sClass.templateClass) sClass = sClass.templateClass;
2227       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2228       {
2229          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2230          {
2231             for(sClass = sClass.base; sClass; sClass = sClass.base)
2232             {
2233                if(sClass.templateClass) sClass = sClass.templateClass;
2234                id += sClass.templateParams.count;
2235             }
2236             break;
2237          }
2238          id++;
2239       }
2240       if(curParam) break;
2241    }
2242    if(curParam)
2243    {
2244       arg = &_class.templateArgs[id];
2245       if(arg && param.type == type)
2246          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2247    }
2248    return arg;
2249 }
2250
2251 public Context SetupTemplatesContext(Class _class)
2252 {
2253    Context context = PushContext();
2254    context.templateTypesOnly = true;
2255    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2256    {
2257       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2258       for(; param; param = param.next)
2259       {
2260          if(param.type == type && param.identifier)
2261          {
2262             TemplatedType type { key = (uint)param.identifier.string, param = param };
2263             curContext.templateTypes.Add((BTNode)type);
2264          }
2265       }
2266    }
2267    else if(_class)
2268    {
2269       Class sClass;
2270       for(sClass = _class; sClass; sClass = sClass.base)
2271       {
2272          ClassTemplateParameter p;
2273          for(p = sClass.templateParams.first; p; p = p.next)
2274          {
2275             //OldList * specs = MkList();
2276             //Declarator decl = null;
2277             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2278             if(p.type == type)
2279             {
2280                TemplateParameter param = p.param;
2281                TemplatedType type;
2282                if(!param)
2283                {
2284                   // ADD DATA TYPE HERE...
2285                   p.param = param = TemplateParameter
2286                   {
2287                      identifier = MkIdentifier(p.name), type = p.type, 
2288                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2289                   };
2290                }
2291                type = TemplatedType { key = (uint)p.name, param = param };
2292                curContext.templateTypes.Add((BTNode)type);
2293             }
2294          }
2295       }
2296    }
2297    return context;
2298 }
2299
2300 public void FinishTemplatesContext(Context context)
2301 {
2302    PopContext(context);
2303    FreeContext(context);
2304    delete context;
2305 }
2306
2307 public void ProcessMethodType(Method method)
2308 {
2309    if(!method.dataType)
2310    {
2311       Context context = SetupTemplatesContext(method._class);
2312
2313       method.dataType = ProcessTypeString(method.dataTypeString, false);
2314
2315       FinishTemplatesContext(context);
2316
2317       if(method.type != virtualMethod && method.dataType)
2318       {
2319          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2320          {
2321             if(!method._class.symbol)
2322                method._class.symbol = FindClass(method._class.fullName);
2323             method.dataType.thisClass = method._class.symbol;
2324          }
2325       }
2326
2327       // Why was this commented out? Working fine without now...
2328
2329       /*
2330       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2331          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2332          */
2333    }
2334
2335    /*
2336    if(type)
2337    {
2338       char * par = strstr(type, "(");
2339       char * classOp = null;
2340       int classOpLen = 0;
2341       if(par)
2342       {
2343          int c;
2344          for(c = par-type-1; c >= 0; c++)
2345          {
2346             if(type[c] == ':' && type[c+1] == ':')
2347             {
2348                classOp = type + c - 1;
2349                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2350                {
2351                   classOp--;
2352                   classOpLen++;
2353                }
2354                break;
2355             }
2356             else if(!isspace(type[c]))
2357                break;
2358          }
2359       }
2360       if(classOp)
2361       {
2362          char temp[1024];
2363          int typeLen = strlen(type);
2364          memcpy(temp, classOp, classOpLen);
2365          temp[classOpLen] = '\0';
2366          if(temp[0])
2367             _class = eSystem_FindClass(module, temp);
2368          else
2369             _class = null;
2370          method.dataTypeString = new char[typeLen - classOpLen + 1];
2371          memcpy(method.dataTypeString, type, classOp - type);
2372          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2373       }
2374       else
2375          method.dataTypeString = type;
2376    }
2377    */
2378 }
2379
2380
2381 public void ProcessPropertyType(Property prop)
2382 {
2383    if(!prop.dataType)
2384    {
2385       Context context = SetupTemplatesContext(prop._class);
2386       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2387       FinishTemplatesContext(context);
2388    }
2389 }
2390
2391 public void DeclareMethod(Method method, char * name)
2392 {
2393    Symbol symbol = method.symbol;
2394    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2395    {
2396       bool imported = false;
2397       bool dllImport = false;
2398
2399       if(!method.dataType)
2400          method.dataType = ProcessTypeString(method.dataTypeString, false);
2401
2402       if(!symbol || symbol._import || method.type == virtualMethod)
2403       {
2404          if(!symbol || method.type == virtualMethod)
2405          {
2406             Symbol classSym;
2407             if(!method._class.symbol)
2408                method._class.symbol = FindClass(method._class.fullName);
2409             classSym = method._class.symbol;
2410             if(!classSym._import)
2411             {
2412                ModuleImport module;
2413                
2414                if(method._class.module && method._class.module.name)
2415                   module = FindModule(method._class.module);
2416                else
2417                   module = mainModule;
2418                classSym._import = ClassImport
2419                {
2420                   name = CopyString(method._class.fullName);
2421                   isRemote = method._class.isRemote;
2422                };
2423                module.classes.Add(classSym._import);
2424             }
2425             if(!symbol)
2426             {
2427                symbol = method.symbol = Symbol { };
2428             }
2429             if(!symbol._import)
2430             {
2431                symbol._import = (ClassImport)MethodImport
2432                {
2433                   name = CopyString(method.name);
2434                   isVirtual = method.type == virtualMethod;
2435                };
2436                classSym._import.methods.Add(symbol._import);
2437             }
2438             if(!symbol)
2439             {
2440                // Set the symbol type
2441                /*
2442                if(!type.thisClass)
2443                {
2444                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2445                }
2446                else if(type.thisClass == (void *)-1)
2447                {
2448                   type.thisClass = null;
2449                }
2450                */
2451                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2452                symbol.type = method.dataType;
2453                if(symbol.type) symbol.type.refCount++;
2454             }
2455             /*
2456             if(!method.thisClass || strcmp(method.thisClass, "void"))
2457                symbol.type.params.Insert(null, 
2458                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2459             */
2460          }
2461          if(!method.dataType.dllExport)
2462          {
2463             imported = true;
2464             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2465                dllImport = true;
2466          }
2467       }
2468
2469       /* MOVING THIS UP
2470       if(!method.dataType)
2471          method.dataType = ((Symbol)method.symbol).type;
2472          //ProcessMethodType(method);
2473       */
2474
2475       if(method.type != virtualMethod && method.dataType)
2476          DeclareType(method.dataType, true, true);
2477
2478       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2479       {
2480          // We need a declaration here :)
2481          Declaration decl;
2482          OldList * specifiers, * declarators;
2483          Declarator d;
2484          Declarator funcDecl;
2485          External external;
2486
2487          specifiers = MkList();
2488          declarators = MkList();
2489
2490          //if(imported)
2491          if(dllImport)
2492             ListAdd(specifiers, MkSpecifier(EXTERN));
2493          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2494             ListAdd(specifiers, MkSpecifier(STATIC));
2495
2496          if(method.type == virtualMethod)
2497          {
2498             ListAdd(specifiers, MkSpecifier(INT));
2499             d = MkDeclaratorIdentifier(MkIdentifier(name));
2500          }
2501          else
2502          {
2503             d = MkDeclaratorIdentifier(MkIdentifier(name));
2504             //if(imported)
2505             if(dllImport)
2506                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2507             {
2508                Context context = SetupTemplatesContext(method._class);
2509                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2510                FinishTemplatesContext(context);
2511             }
2512             funcDecl = GetFuncDecl(d);
2513
2514             if(dllImport)
2515             {
2516                Specifier spec, next;
2517                for(spec = specifiers->first; spec; spec = next)
2518                {
2519                   next = spec.next;
2520                   if(spec.type == extendedSpecifier)
2521                   {
2522                      specifiers->Remove(spec);
2523                      FreeSpecifier(spec);
2524                   }
2525                }
2526             }
2527
2528             // Add this parameter if not a static method
2529             if(method.dataType && !method.dataType.staticMethod)
2530             {
2531                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2532                {
2533                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2534                   TypeName thisParam = MkTypeName(MkListOne(
2535                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)), 
2536                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2537                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2538                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2539
2540                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2541                   {
2542                      TypeName param = funcDecl.function.parameters->first;
2543                      funcDecl.function.parameters->Remove(param);
2544                      FreeTypeName(param);
2545                   }
2546
2547                   if(!funcDecl.function.parameters)
2548                      funcDecl.function.parameters = MkList();
2549                   funcDecl.function.parameters->Insert(null, thisParam);
2550                }
2551             }
2552             // Make sure we don't have empty parameter declarations for static methods...
2553             /*
2554             else if(!funcDecl.function.parameters)
2555             {
2556                funcDecl.function.parameters = MkList();
2557                funcDecl.function.parameters->Insert(null, 
2558                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2559             }*/
2560          }
2561          // TESTING THIS:
2562          ProcessDeclarator(d);
2563
2564          ListAdd(declarators, MkInitDeclarator(d, null));
2565
2566          decl = MkDeclaration(specifiers, declarators);
2567
2568          ReplaceThisClassSpecifiers(specifiers, method._class);
2569
2570          // Keep a different symbol for the function definition than the declaration...
2571          if(symbol.pointerExternal)
2572          {
2573             Symbol functionSymbol { };
2574
2575             // Copy symbol
2576             {
2577                *functionSymbol = *symbol;
2578                functionSymbol.string = CopyString(symbol.string);
2579                if(functionSymbol.type)
2580                   functionSymbol.type.refCount++;
2581             }
2582
2583             excludedSymbols->Add(functionSymbol);
2584             symbol.pointerExternal.symbol = functionSymbol;
2585          }
2586          external = MkExternalDeclaration(decl);
2587          if(curExternal)
2588             ast->Insert(curExternal ? curExternal.prev : null, external);
2589          external.symbol = symbol;
2590          symbol.pointerExternal = external;
2591       }
2592       else if(ast)
2593       {
2594          // Move declaration higher...
2595          ast->Move(symbol.pointerExternal, curExternal.prev);
2596       }
2597
2598       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2599    }
2600 }
2601
2602 char * ReplaceThisClass(Class _class)
2603 {
2604    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2605    {
2606       bool first = true;
2607       int p = 0;
2608       ClassTemplateParameter param;
2609       int lastParam = -1;
2610       
2611       char className[1024];
2612       strcpy(className, _class.fullName);
2613       for(param = _class.templateParams.first; param; param = param.next)
2614       {
2615          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2616          {
2617             if(first) strcat(className, "<");
2618             if(!first) strcat(className, ", ");
2619             if(lastParam + 1 != p)
2620             {
2621                strcat(className, param.name);
2622                strcat(className, " = ");
2623             }
2624             strcat(className, param.name);
2625             first = false;
2626             lastParam = p;
2627          }
2628          p++;
2629       }
2630       if(!first)
2631       {
2632          int len = strlen(className);
2633          if(className[len-1] == '>') className[len++] = ' ';
2634          className[len++] = '>';
2635          className[len++] = '\0';
2636       }
2637       return CopyString(className);
2638    }
2639    else
2640       return CopyString(_class.fullName);   
2641 }
2642
2643 Type ReplaceThisClassType(Class _class)
2644 {
2645    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2646    {
2647       bool first = true;
2648       int p = 0;
2649       ClassTemplateParameter param;
2650       int lastParam = -1;
2651       char className[1024];
2652       strcpy(className, _class.fullName);
2653       
2654       for(param = _class.templateParams.first; param; param = param.next)
2655       {
2656          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2657          {
2658             if(first) strcat(className, "<");
2659             if(!first) strcat(className, ", ");
2660             if(lastParam + 1 != p)
2661             {
2662                strcat(className, param.name);
2663                strcat(className, " = ");
2664             }
2665             strcat(className, param.name);
2666             first = false;
2667             lastParam = p;
2668          }
2669          p++;
2670       }
2671       if(!first)
2672       {
2673          int len = strlen(className);
2674          if(className[len-1] == '>') className[len++] = ' ';
2675          className[len++] = '>';
2676          className[len++] = '\0';
2677       }
2678       return MkClassType(className);
2679       //return ProcessTypeString(className, false);
2680    }
2681    else
2682    {
2683       return MkClassType(_class.fullName);
2684       //return ProcessTypeString(_class.fullName, false);
2685    }
2686 }
2687
2688 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2689 {
2690    if(specs != null && _class)
2691    {
2692       Specifier spec;
2693       for(spec = specs.first; spec; spec = spec.next)
2694       {
2695          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2696          {
2697             spec.type = nameSpecifier;
2698             spec.name = ReplaceThisClass(_class);
2699             spec.symbol = FindClass(spec.name); //_class.symbol;
2700          }
2701       }
2702    }
2703 }
2704
2705 // Returns imported or not
2706 bool DeclareFunction(GlobalFunction function, char * name)
2707 {
2708    Symbol symbol = function.symbol;
2709    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2710    {
2711       bool imported = false;
2712       bool dllImport = false;
2713
2714       if(!function.dataType)
2715       {
2716          function.dataType = ProcessTypeString(function.dataTypeString, false);
2717          if(!function.dataType.thisClass)
2718             function.dataType.staticMethod = true;
2719       }
2720
2721       if(inCompiler)
2722       {
2723          if(!symbol)
2724          {
2725             ModuleImport module = FindModule(function.module);
2726             // WARNING: This is not added anywhere...
2727             symbol = function.symbol = Symbol {  };
2728
2729             if(module.name)
2730             {
2731                if(!function.dataType.dllExport)
2732                {
2733                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2734                   module.functions.Add(symbol._import);
2735                }
2736             }
2737             // Set the symbol type
2738             {
2739                symbol.type = ProcessTypeString(function.dataTypeString, false);
2740                if(!symbol.type.thisClass)
2741                   symbol.type.staticMethod = true;
2742             }
2743          }
2744          imported = symbol._import ? true : false;
2745          if(imported && function.module != privateModule && function.module.importType != staticImport)
2746             dllImport = true;
2747       }
2748
2749       DeclareType(function.dataType, true, true);
2750
2751       if(inCompiler)
2752       {
2753          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2754          {
2755             // We need a declaration here :)
2756             Declaration decl;
2757             OldList * specifiers, * declarators;
2758             Declarator d;
2759             Declarator funcDecl;
2760             External external;
2761
2762             specifiers = MkList();
2763             declarators = MkList();
2764
2765             //if(imported)
2766                ListAdd(specifiers, MkSpecifier(EXTERN));
2767             /*
2768             else
2769                ListAdd(specifiers, MkSpecifier(STATIC));
2770             */
2771
2772             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2773             //if(imported)
2774             if(dllImport)
2775                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2776
2777             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2778             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2779             if(function.module.importType == staticImport)
2780             {
2781                Specifier spec;
2782                for(spec = specifiers->first; spec; spec = spec.next)
2783                   if(spec.type == extendedSpecifier && !strcmp(spec.name, "dllexport"))
2784                   {
2785                      specifiers->Remove(spec);
2786                      FreeSpecifier(spec);
2787                      break;
2788                   }
2789             }
2790
2791             funcDecl = GetFuncDecl(d);
2792
2793             // Make sure we don't have empty parameter declarations for static methods...
2794             if(funcDecl && !funcDecl.function.parameters)
2795             {
2796                funcDecl.function.parameters = MkList();
2797                funcDecl.function.parameters->Insert(null, 
2798                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2799             }
2800
2801             ListAdd(declarators, MkInitDeclarator(d, null));
2802
2803             {
2804                Context oldCtx = curContext;
2805                curContext = globalContext;
2806                decl = MkDeclaration(specifiers, declarators);
2807                curContext = oldCtx;
2808             }
2809
2810             // Keep a different symbol for the function definition than the declaration...
2811             if(symbol.pointerExternal)
2812             {
2813                Symbol functionSymbol { };
2814                // Copy symbol
2815                {
2816                   *functionSymbol = *symbol;
2817                   functionSymbol.string = CopyString(symbol.string);
2818                   if(functionSymbol.type)
2819                      functionSymbol.type.refCount++;
2820                }
2821
2822                excludedSymbols->Add(functionSymbol);
2823
2824                symbol.pointerExternal.symbol = functionSymbol;
2825             }
2826             external = MkExternalDeclaration(decl);
2827             if(curExternal)
2828                ast->Insert(curExternal.prev, external);
2829             external.symbol = symbol;
2830             symbol.pointerExternal = external;
2831          }
2832          else
2833          {
2834             // Move declaration higher...
2835             ast->Move(symbol.pointerExternal, curExternal.prev);
2836          }
2837
2838          if(curExternal)
2839             symbol.id = curExternal.symbol.idCode;
2840       }
2841    }
2842    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2843 }
2844
2845 void DeclareGlobalData(GlobalData data)
2846 {
2847    Symbol symbol = data.symbol;
2848    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2849    {
2850       if(inCompiler)
2851       {
2852          if(!symbol)
2853             symbol = data.symbol = Symbol { };
2854       }
2855       if(!data.dataType)
2856          data.dataType = ProcessTypeString(data.dataTypeString, false);
2857       DeclareType(data.dataType, true, true);
2858       if(inCompiler)
2859       {
2860          if(!symbol.pointerExternal)
2861          {
2862             // We need a declaration here :)
2863             Declaration decl;
2864             OldList * specifiers, * declarators;
2865             Declarator d;
2866             External external;
2867
2868             specifiers = MkList();
2869             declarators = MkList();
2870
2871             ListAdd(specifiers, MkSpecifier(EXTERN));
2872             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2873             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2874
2875             ListAdd(declarators, MkInitDeclarator(d, null));
2876
2877             decl = MkDeclaration(specifiers, declarators);
2878             external = MkExternalDeclaration(decl);
2879             if(curExternal)
2880                ast->Insert(curExternal.prev, external);
2881             external.symbol = symbol;
2882             symbol.pointerExternal = external;
2883          }
2884          else
2885          {
2886             // Move declaration higher...
2887             ast->Move(symbol.pointerExternal, curExternal.prev);
2888          }
2889
2890          if(curExternal)
2891             symbol.id = curExternal.symbol.idCode;
2892       }
2893    }
2894 }
2895
2896 class Conversion : struct
2897 {
2898    Conversion prev, next;
2899    Property convert;
2900    bool isGet;
2901    Type resultType;
2902 };
2903
2904 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2905 {
2906    if(source && dest)
2907    {
2908       // Property convert;
2909
2910       if(source.kind == templateType && dest.kind != templateType)
2911       {
2912          Type type = ProcessTemplateParameterType(source.templateParameter);
2913          if(type) source = type;
2914       }
2915
2916       if(dest.kind == templateType && source.kind != templateType)
2917       {
2918          Type type = ProcessTemplateParameterType(dest.templateParameter);
2919          if(type) dest = type;
2920       }
2921
2922       if((dest.classObjectType == typedObject && source.classObjectType != anyObject) || (dest.classObjectType == anyObject && source.classObjectType != typedObject))
2923       {
2924          return true;
2925       }
2926       
2927       if(source.classObjectType == anyObject && dest.classObjectType != typedObject)
2928       {
2929          return true;
2930       }
2931       
2932       if((dest.kind == structType && source.kind == structType) ||
2933          (dest.kind == unionType && source.kind == unionType))
2934       {
2935          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2936              (source.members.first && source.members.first == dest.members.first))
2937             return true;
2938       }
2939
2940       if(dest.kind == ellipsisType && source.kind != voidType)
2941          return true;
2942
2943       if(dest.kind == pointerType && dest.type.kind == voidType &&
2944          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2945          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2946
2947          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2948       
2949          /*&& (source.kind != classType /*|| source._class.registered.type != structClass)*/)
2950          return true;
2951       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2952          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2953          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2954
2955          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2956
2957          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2958          return true;
2959
2960       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2961       {
2962          if(source._class.registered && source._class.registered.type == unitClass)
2963          {
2964             if(conversions != null)
2965             {
2966                if(source._class.registered == dest._class.registered)
2967                   return true;
2968             }
2969             else
2970             {
2971                Class sourceBase, destBase;
2972                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2973                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2974                if(sourceBase == destBase)
2975                   return true;
2976             }
2977          }
2978          // Don't match enum inheriting from other enum if resolving enumeration values
2979          // TESTING: !dest.classObjectType
2980          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2981             (enumBaseType || 
2982                (!source._class.registered || source._class.registered.type != enumClass) || 
2983                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2984             return true;
2985          else
2986          {
2987             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2988             if(enumBaseType && 
2989                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2990                source._class && source._class.registered && source._class.registered.type != enumClass)
2991             {
2992                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2993                {
2994                   return true;
2995                }
2996             }
2997          }
2998       }
2999
3000       // JUST ADDED THIS...
3001       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3002          return true;
3003
3004       if(doConversion)
3005       {
3006          // Just added this for Straight conversion of ColorAlpha => Color
3007          if(source.kind == classType)
3008          {
3009             Class _class;
3010             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3011             {
3012                Property convert;
3013                for(convert = _class.conversions.first; convert; convert = convert.next)
3014                {
3015                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3016                   {
3017                      Conversion after = (conversions != null) ? conversions.last : null;
3018
3019                      if(!convert.dataType)
3020                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3021                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
3022                      {
3023                         if(!conversions && !convert.Get)
3024                            return true;
3025                         else if(conversions != null)
3026                         {
3027                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3028                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3029                               (dest.kind != classType || dest._class.registered != _class.base))
3030                               return true;
3031                            else
3032                            {
3033                               Conversion conv { convert = convert, isGet = true };
3034                               // conversions.Add(conv);
3035                               conversions.Insert(after, conv);
3036                               return true;
3037                            }
3038                         }
3039                      }
3040                   }
3041                }
3042             }
3043          }
3044
3045          // MOVING THIS??
3046
3047          if(dest.kind == classType)
3048          {
3049             Class _class;
3050             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3051             {
3052                Property convert;
3053                for(convert = _class.conversions.first; convert; convert = convert.next)
3054                {
3055                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3056                   {
3057                      // Conversion after = (conversions != null) ? conversions.last : null;
3058
3059                      if(!convert.dataType)
3060                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3061                      // Just added this equality check to prevent recursion.... Make it safer?
3062                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3063                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3064                      {
3065                         if(!conversions && !convert.Set)
3066                            return true;
3067                         else if(conversions != null)
3068                         {
3069                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3070                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3071                               (source.kind != classType || source._class.registered != _class.base))
3072                               return true;
3073                            else
3074                            {
3075                               // *** Testing this! ***
3076                               Conversion conv { convert = convert };
3077                               conversions.Add(conv);
3078                               //conversions.Insert(after, conv);
3079                               return true;
3080                            }
3081                         }
3082                      }
3083                   }
3084                }
3085             }
3086             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3087             {
3088                if(source.kind != voidType && source.kind != structType && source.kind != unionType && 
3089                   (source.kind != classType || source._class.registered.type != structClass))
3090                   return true;
3091             }*/
3092
3093             // TESTING THIS... IS THIS OK??
3094             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3095             {
3096                if(!dest._class.registered.dataType)
3097                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3098                // Only support this for classes...
3099                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3100                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3101                {
3102                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3103                   {
3104                      return true;
3105                   }
3106                }
3107             }
3108          }
3109
3110          // Moved this lower
3111          if(source.kind == classType)
3112          {
3113             Class _class;
3114             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3115             {
3116                Property convert;
3117                for(convert = _class.conversions.first; convert; convert = convert.next)
3118                {
3119                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3120                   {
3121                      Conversion after = (conversions != null) ? conversions.last : null;
3122
3123                      if(!convert.dataType)
3124                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3125                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3126                      {
3127                         if(!conversions && !convert.Get)
3128                            return true;
3129                         else if(conversions != null)
3130                         {
3131                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3132                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3133                               (dest.kind != classType || dest._class.registered != _class.base))
3134                               return true;
3135                            else
3136                            {
3137                               Conversion conv { convert = convert, isGet = true };
3138
3139                               // conversions.Add(conv);
3140                               conversions.Insert(after, conv);
3141                               return true;
3142                            }
3143                         }
3144                      }
3145                   }
3146                }
3147             }
3148
3149             // TESTING THIS... IS THIS OK??
3150             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3151             {
3152                if(!source._class.registered.dataType)
3153                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3154                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3155                {
3156                   return true;
3157                }
3158             }
3159          }
3160       }
3161
3162       if(source.kind == classType || source.kind == subClassType)
3163          ;
3164       else if(dest.kind == source.kind && 
3165          (dest.kind != structType && dest.kind != unionType &&
3166           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3167           return true;
3168       // RECENTLY ADDED THESE
3169       else if(dest.kind == doubleType && source.kind == floatType)
3170          return true;
3171       else if(dest.kind == shortType && source.kind == charType)
3172          return true;
3173       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType))
3174          return true;
3175       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == intType))
3176          return true;
3177       else if(source.kind == enumType &&
3178          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || dest.kind == longType || dest.kind == int64Type))
3179           return true;
3180       else if(dest.kind == enumType &&
3181          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == longType || dest.kind == int64Type))
3182           return true;
3183       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && 
3184               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3185       {
3186          Type paramSource, paramDest;
3187
3188          if(dest.kind == methodType)     
3189             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3190          if(source.kind == methodType)   
3191             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3192
3193          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3194          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3195          if(dest.kind == methodType) 
3196             dest = dest.method.dataType;
3197          if(source.kind == methodType) 
3198             source = source.method.dataType;
3199
3200          paramSource = source.params.first;
3201          if(paramSource && paramSource.kind == voidType) paramSource = null;
3202          paramDest = dest.params.first;
3203          if(paramDest && paramDest.kind == voidType) paramDest = null;
3204
3205      
3206          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) && 
3207             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3208          {
3209             // Source thisClass must be derived from destination thisClass
3210             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3211                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3212             {
3213                if(paramDest && paramDest.kind == classType)
3214                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3215                else
3216                   Compiler_Error($"method class should not take an object\n");
3217                return false;
3218             }
3219             paramDest = paramDest.next;
3220          }
3221          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3222          {
3223             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3224             {
3225                if(dest.thisClass)
3226                {
3227                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3228                   {
3229                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3230                      return false;
3231                   }
3232                }
3233                else
3234                {
3235                   // THIS WAS BACKWARDS:
3236                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3237                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3238                   {
3239                      if(owningClassDest)
3240                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3241                      else
3242                         Compiler_Error($"overriding class expected to be derived from method class\n");      
3243                      return false;
3244                   }
3245                }
3246                paramSource = paramSource.next;
3247             }
3248             else
3249             {
3250                if(dest.thisClass)
3251                {
3252                   // Source thisClass must be derived from destination thisClass
3253                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3254                   {
3255                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3256                      return false;
3257                   }
3258                }
3259                else
3260                {
3261                   // THIS WAS BACKWARDS TOO??
3262                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3263                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3264                   {
3265                      //if(owningClass)
3266                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3267                      //else
3268                         //Compiler_Error($"overriding class expected to be derived from method class\n");      
3269                      return false;
3270                   }
3271                }
3272             }
3273          }
3274
3275
3276          // Source return type must be derived from destination return type
3277          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3278          {
3279             Compiler_Warning($"incompatible return type for function\n");
3280             return false;
3281          }
3282
3283          // Check parameters
3284       
3285          for(; paramDest; paramDest = paramDest.next)
3286          {
3287             if(!paramSource)
3288             {
3289                //Compiler_Warning($"not enough parameters\n");
3290                Compiler_Error($"not enough parameters\n");
3291                return false;
3292             }
3293             {
3294                Type paramDestType = paramDest;
3295                Type paramSourceType = paramSource;
3296                Type type = paramDestType;
3297
3298                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3299                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3300                   paramSource.kind != templateType)
3301                {
3302                   int id = 0;
3303                   ClassTemplateParameter curParam;
3304                   Class sClass;
3305                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3306                   {
3307                      id = 0;
3308                      if(sClass.templateClass) sClass = sClass.templateClass;
3309                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3310                      {
3311                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3312                         {
3313                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3314                            {
3315                               if(sClass.templateClass) sClass = sClass.templateClass;
3316                               id += sClass.templateParams.count;
3317                            }
3318                            break;
3319                         }
3320                         id++;
3321                      }
3322                      if(curParam) break;
3323                   }
3324
3325                   if(curParam)
3326                   {
3327                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3328                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3329                   }
3330                }
3331
3332                // paramDest must be derived from paramSource
3333                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) && 
3334                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3335                {
3336                   char type[1024];
3337                   type[0] = 0;
3338                   PrintType(paramDest, type, false, true);
3339                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3340                   
3341                   if(paramDestType != paramDest)
3342                      FreeType(paramDestType);
3343                   return false;
3344                }
3345                if(paramDestType != paramDest)
3346                   FreeType(paramDestType);
3347             }
3348          
3349             paramSource = paramSource.next;
3350          }
3351          if(paramSource)
3352          {
3353             Compiler_Error($"too many parameters\n");
3354             return false;
3355          }
3356          return true;
3357       }
3358       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3359       {
3360          return true;
3361       }
3362       else if((dest.kind == pointerType || dest.kind == arrayType) && 
3363          (source.kind == arrayType || source.kind == pointerType))
3364       {
3365          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3366             return true;
3367       }
3368    }
3369    return false;
3370 }
3371
3372 static void FreeConvert(Conversion convert)
3373 {
3374    if(convert.resultType)
3375       FreeType(convert.resultType);
3376 }
3377
3378 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest, 
3379                               char * string, OldList conversions)
3380 {
3381    BTNamedLink link;
3382
3383    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3384    {
3385       Class _class = link.data;
3386       if(_class.type == enumClass)
3387       {
3388          OldList converts { };
3389          Type type { };
3390          type.kind = classType;
3391
3392          if(!_class.symbol)
3393             _class.symbol = FindClass(_class.fullName);
3394          type._class = _class.symbol;
3395
3396          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3397          {
3398             NamedLink value;
3399             Class enumClass = eSystem_FindClass(privateModule, "enum");
3400             if(enumClass)
3401             {
3402                Class baseClass;
3403                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3404                {
3405                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3406                   for(value = e.values.first; value; value = value.next)
3407                   {
3408                      if(!strcmp(value.name, string))
3409                         break;
3410                   }
3411                   if(value)
3412                   {
3413                      FreeExpContents(sourceExp);
3414                      FreeType(sourceExp.expType);
3415
3416                      sourceExp.isConstant = true;
3417                      sourceExp.expType = MkClassType(baseClass.fullName);
3418                      //if(inCompiler)
3419                      {
3420                         char constant[256];
3421                         sourceExp.type = constantExp;
3422                         if(!strcmp(baseClass.dataTypeString, "int"))
3423                            sprintf(constant, "%d",value.data);
3424                         else
3425                            sprintf(constant, "0x%X",value.data);
3426                         sourceExp.constant = CopyString(constant);
3427                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3428                      }
3429                   
3430                      while(converts.first)
3431                      {
3432                         Conversion convert = converts.first;
3433                         converts.Remove(convert);
3434                         conversions.Add(convert);
3435                      }
3436                      delete type;
3437                      return true;
3438                   }
3439                }
3440             }
3441          }
3442          if(converts.first)
3443             converts.Free(FreeConvert);
3444          delete type;
3445       }
3446    }
3447    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3448       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3449          return true;
3450    return false;
3451 }
3452
3453 public bool ModuleVisibility(Module searchIn, Module searchFor)
3454 {
3455    SubModule subModule;
3456    
3457    if(searchFor == searchIn)
3458       return true;
3459
3460    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3461    {
3462       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3463       {
3464          if(ModuleVisibility(subModule.module, searchFor))
3465             return true;
3466       }
3467    }
3468    return false;
3469 }
3470
3471 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3472 {
3473    Module module;
3474
3475    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3476       return true;
3477    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3478       return true;
3479    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3480       return true;
3481
3482    for(module = mainModule.application.allModules.first; module; module = module.next)
3483    {
3484       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3485          return true;
3486    }
3487    return false;
3488 }
3489
3490 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3491 {
3492    Type source = sourceExp.expType;
3493    Type realDest = dest;
3494
3495    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3496       return true;
3497
3498    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3499    {
3500        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3501        {
3502           Class sourceBase, destBase;
3503           for(sourceBase = source._class.registered; 
3504               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3505               sourceBase = sourceBase.base);
3506           for(destBase = dest._class.registered; 
3507               destBase && destBase.base && destBase.base.type != systemClass;
3508               destBase = destBase.base);
3509           //if(source._class.registered == dest._class.registered)
3510           if(sourceBase == destBase)
3511              return true;
3512        }
3513    }
3514
3515    if(source)
3516    {
3517       OldList * specs;
3518       bool flag = false;
3519       int64 value = MAXINT;
3520
3521       source.refCount++;
3522       dest.refCount++;
3523
3524       if(sourceExp.type == constantExp)
3525       {
3526          if(source.isSigned)
3527             value = strtoll(sourceExp.constant, null, 0);
3528          else
3529             value = strtoull(sourceExp.constant, null, 0);
3530       }
3531       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3532       {
3533          if(source.isSigned)
3534             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3535          else
3536             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3537       }
3538
3539       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered && 
3540          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3541       {
3542          FreeType(source);
3543          source = Type { kind = intType, isSigned = false, refCount = 1 };
3544       }
3545
3546       if(dest.kind == classType)
3547       {
3548          Class _class = dest._class ? dest._class.registered : null;
3549
3550          if(_class && _class.type == unitClass)
3551          {
3552             if(source.kind != classType)
3553             {
3554                Type tempType { };
3555                Type tempDest, tempSource;
3556
3557                for(; _class.base.type != systemClass; _class = _class.base);
3558                tempSource = dest;
3559                tempDest = tempType;
3560
3561                tempType.kind = classType;
3562                if(!_class.symbol)
3563                   _class.symbol = FindClass(_class.fullName);
3564
3565                tempType._class = _class.symbol;
3566                tempType.truth = dest.truth;
3567                if(tempType._class)
3568                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3569
3570                FreeType(sourceExp.expType);
3571                sourceExp.expType = dest; dest.refCount++;
3572
3573                //sourceExp.expType = MkClassType(_class.fullName);
3574                flag = true;            
3575
3576                delete tempType;
3577             }
3578          }
3579
3580
3581          // Why wasn't there something like this?
3582          if(_class && _class.type == bitClass && source.kind != classType)
3583          {
3584             if(!dest._class.registered.dataType)
3585                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3586             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3587             {
3588                FreeType(source);
3589                FreeType(sourceExp.expType);
3590                source = sourceExp.expType = MkClassType(dest._class.string);
3591                source.refCount++;
3592                
3593                //source.kind = classType;
3594                //source._class = dest._class;
3595             }
3596          }
3597
3598          // Adding two enumerations
3599          /*
3600          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3601          {
3602             if(!source._class.registered.dataType)
3603                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3604             if(!dest._class.registered.dataType)
3605                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3606
3607             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3608             {
3609                FreeType(source);
3610                source = sourceExp.expType = MkClassType(dest._class.string);
3611                source.refCount++;
3612                
3613                //source.kind = classType;
3614                //source._class = dest._class;
3615             }
3616          }*/
3617
3618          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3619          {
3620             OldList * specs = MkList();
3621             Declarator decl;
3622             char string[1024];
3623
3624             ReadString(string, sourceExp.string);
3625             decl = SpecDeclFromString(string, specs, null);
3626
3627             FreeExpContents(sourceExp);
3628             FreeType(sourceExp.expType);
3629
3630             sourceExp.type = classExp;
3631             sourceExp._classExp.specifiers = specs;
3632             sourceExp._classExp.decl = decl;
3633             sourceExp.expType = dest;
3634             dest.refCount++;
3635
3636             FreeType(source);
3637             FreeType(dest);
3638             return true;
3639          }
3640       }
3641       else if(source.kind == classType)
3642       {
3643          Class _class = source._class ? source._class.registered : null;
3644
3645          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3646          {
3647             /*
3648             if(dest.kind != classType)
3649             {
3650                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3651                if(!source._class.registered.dataType)
3652                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3653                
3654                FreeType(dest);
3655                dest = MkClassType(source._class.string);
3656                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3657                //   dest = MkClassType(source._class.string);
3658             }
3659             */
3660             
3661             if(dest.kind != classType)
3662             {
3663                Type tempType { };
3664                Type tempDest, tempSource;
3665
3666                if(!source._class.registered.dataType)
3667                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3668
3669                for(; _class.base.type != systemClass; _class = _class.base);
3670                tempDest = source;
3671                tempSource = tempType;
3672                tempType.kind = classType;
3673                tempType._class = FindClass(_class.fullName);
3674                tempType.truth = source.truth;
3675                tempType.classObjectType = source.classObjectType;
3676
3677                if(tempType._class)
3678                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3679                
3680                // PUT THIS BACK TESTING UNITS?
3681                if(conversions.last)
3682                {
3683                   ((Conversion)(conversions.last)).resultType = dest;
3684                   dest.refCount++;
3685                }
3686                
3687                FreeType(sourceExp.expType);
3688                sourceExp.expType = MkClassType(_class.fullName);
3689                sourceExp.expType.truth = source.truth;
3690                sourceExp.expType.classObjectType = source.classObjectType;
3691
3692                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3693
3694                if(!sourceExp.destType)
3695                {
3696                   FreeType(sourceExp.destType);
3697                   sourceExp.destType = sourceExp.expType;
3698                   if(sourceExp.expType)
3699                      sourceExp.expType.refCount++;
3700                }
3701                //flag = true;
3702                //source = _class.dataType;
3703
3704
3705                // TOCHECK: TESTING THIS NEW CODE
3706                if(!_class.dataType)
3707                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3708                FreeType(dest);
3709                dest = MkClassType(source._class.string);
3710                dest.truth = source.truth;
3711                dest.classObjectType = source.classObjectType;
3712                
3713                FreeType(source);
3714                source = _class.dataType;
3715                source.refCount++;
3716
3717                delete tempType;
3718             }
3719          }
3720       }
3721
3722       if(!flag)
3723       {
3724          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3725          {
3726             FreeType(source);
3727             FreeType(dest);
3728             return true;
3729          }
3730       }
3731
3732       // Implicit Casts
3733       /*
3734       if(source.kind == classType)
3735       {
3736          Class _class = source._class.registered;
3737          if(_class.type == unitClass)
3738          {
3739             if(!_class.dataType)
3740                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3741             source = _class.dataType;
3742          }
3743       }*/
3744
3745       if(dest.kind == classType)
3746       {
3747          Class _class = dest._class ? dest._class.registered : null;
3748          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || 
3749             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3750          {
3751             if(_class.type == normalClass || _class.type == noHeadClass)
3752             {
3753                Expression newExp { };
3754                *newExp = *sourceExp;
3755                if(sourceExp.destType) sourceExp.destType.refCount++;
3756                if(sourceExp.expType)  sourceExp.expType.refCount++;
3757                sourceExp.type = castExp;
3758                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3759                sourceExp.cast.exp = newExp;
3760                FreeType(sourceExp.expType);
3761                sourceExp.expType = null;
3762                ProcessExpressionType(sourceExp);
3763
3764                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3765                if(!inCompiler)
3766                {
3767                   FreeType(sourceExp.expType);
3768                   sourceExp.expType = dest;
3769                }
3770
3771                FreeType(source);
3772                if(inCompiler) FreeType(dest);
3773
3774                return true;
3775             }
3776
3777             if(!_class.dataType)
3778                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3779             FreeType(dest);
3780             dest = _class.dataType;
3781             dest.refCount++;
3782          }
3783
3784          // Accept lower precision types for units, since we want to keep the unit type
3785          if(dest.kind == doubleType && 
3786             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3787              source.kind == charType))
3788          {
3789             specs = MkListOne(MkSpecifier(DOUBLE));
3790          }
3791          else if(dest.kind == floatType && 
3792             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3793             source.kind == doubleType))
3794          {
3795             specs = MkListOne(MkSpecifier(FLOAT));
3796          }
3797          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3798             source.kind == floatType || source.kind == doubleType))
3799          {
3800             specs = MkList();
3801             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3802             ListAdd(specs, MkSpecifier(INT64));
3803          }
3804          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3805             source.kind == floatType || source.kind == doubleType))
3806          {
3807             specs = MkList();
3808             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3809             ListAdd(specs, MkSpecifier(INT));
3810          }
3811          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == intType ||
3812             source.kind == floatType || source.kind == doubleType))
3813          {
3814             specs = MkList();
3815             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3816             ListAdd(specs, MkSpecifier(SHORT));
3817          }
3818          else if(dest.kind == charType && (source.kind == charType || source.kind == shortType || source.kind == intType ||
3819             source.kind == floatType || source.kind == doubleType))
3820          {
3821             specs = MkList();
3822             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3823             ListAdd(specs, MkSpecifier(CHAR));
3824          }
3825          else
3826          {
3827             FreeType(source);
3828             FreeType(dest);
3829             return false;
3830          }
3831       }
3832       else if(dest.kind == doubleType && 
3833          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3834           source.kind == charType))
3835       {
3836          specs = MkListOne(MkSpecifier(DOUBLE));
3837       }
3838       else if(dest.kind == floatType && 
3839          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3840       {
3841          specs = MkListOne(MkSpecifier(FLOAT));
3842       }
3843       else if(dest.kind == charType && (source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) && 
3844          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3845       {
3846          specs = MkList();
3847          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3848          ListAdd(specs, MkSpecifier(CHAR));
3849       }
3850       else if(dest.kind == shortType && (source.kind == enumType || source.kind == charType || source.kind == shortType || 
3851          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3852       {
3853          specs = MkList();
3854          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3855          ListAdd(specs, MkSpecifier(SHORT));
3856       }
3857       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType))
3858       {
3859          specs = MkList();
3860          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3861          ListAdd(specs, MkSpecifier(INT));
3862       }
3863       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3864       {
3865          specs = MkList();
3866          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3867          ListAdd(specs, MkSpecifier(INT64));
3868       }
3869       else if(dest.kind == enumType && 
3870          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3871       {
3872          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3873       }
3874       else
3875       {
3876          FreeType(source);
3877          FreeType(dest);
3878          return false;
3879       }
3880
3881       if(!flag)
3882       {
3883          Expression newExp { };
3884          *newExp = *sourceExp;
3885          newExp.prev = null;
3886          newExp.next = null;
3887          if(sourceExp.destType) sourceExp.destType.refCount++;
3888          if(sourceExp.expType)  sourceExp.expType.refCount++;
3889
3890          sourceExp.type = castExp;
3891          if(realDest.kind == classType)
3892          {
3893             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3894             FreeList(specs, FreeSpecifier);
3895          }
3896          else
3897             sourceExp.cast.typeName = MkTypeName(specs, null);
3898          if(newExp.type == opExp)
3899          {
3900             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3901          }
3902          else
3903             sourceExp.cast.exp = newExp;
3904          
3905          FreeType(sourceExp.expType);
3906          sourceExp.expType = null;
3907          ProcessExpressionType(sourceExp);
3908       }
3909       else
3910          FreeList(specs, FreeSpecifier);
3911
3912       FreeType(dest);
3913       FreeType(source);
3914       return true;
3915    }
3916    else
3917    {
3918       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3919       if(sourceExp.type == identifierExp)
3920       {
3921          Identifier id = sourceExp.identifier;
3922          if(dest.kind == classType)
3923          {
3924             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3925             {
3926                Class _class = dest._class.registered;
3927                Class enumClass = eSystem_FindClass(privateModule, "enum");
3928                if(enumClass)
3929                {
3930                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3931                   {
3932                      NamedLink value;
3933                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3934                      for(value = e.values.first; value; value = value.next)
3935                      {
3936                         if(!strcmp(value.name, id.string))
3937                            break;
3938                      }
3939                      if(value)
3940                      {
3941                         FreeExpContents(sourceExp);
3942                         FreeType(sourceExp.expType);
3943
3944                         sourceExp.isConstant = true;
3945                         sourceExp.expType = MkClassType(_class.fullName);
3946                         //if(inCompiler)
3947                         {
3948                            char constant[256];
3949                            sourceExp.type = constantExp;
3950                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3951                               sprintf(constant, "%d",value.data);
3952                            else
3953                               sprintf(constant, "0x%X",value.data);
3954                            sourceExp.constant = CopyString(constant);
3955                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3956                         }
3957                         return true;
3958                      }
3959                   }
3960                }
3961             }
3962          }
3963
3964          // Loop through all enum classes
3965          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3966             return true;
3967       }
3968    }
3969    return false;
3970 }
3971
3972 #define TERTIARY(o, name, m, t, p) \
3973    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3974    {                                                              \
3975       exp.type = constantExp;                                    \
3976       exp.string = p(op1.m ? op2.m : op3.m);                     \
3977       if(!exp.expType) \
3978          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3979       return true;                                                \
3980    }
3981
3982 #define BINARY(o, name, m, t, p) \
3983    static bool name(Expression exp, Operand op1, Operand op2)   \
3984    {                                                              \
3985       t value2 = op2.m;                                           \
3986       exp.type = constantExp;                                    \
3987       exp.string = p(op1.m o value2);                     \
3988       if(!exp.expType) \
3989          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3990       return true;                                                \
3991    }
3992
3993 #define BINARY_DIVIDE(o, name, m, t, p) \
3994    static bool name(Expression exp, Operand op1, Operand op2)   \
3995    {                                                              \
3996       t value2 = op2.m;                                           \
3997       exp.type = constantExp;                                    \
3998       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3999       if(!exp.expType) \
4000          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4001       return true;                                                \
4002    }
4003
4004 #define UNARY(o, name, m, t, p) \
4005    static bool name(Expression exp, Operand op1)                \
4006    {                                                              \
4007       exp.type = constantExp;                                    \
4008       exp.string = p(o op1.m);                                   \
4009       if(!exp.expType) \
4010          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4011       return true;                                                \
4012    }
4013
4014 #define OPERATOR_ALL(macro, o, name) \
4015    macro(o, Int##name, i, int, PrintInt) \
4016    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4017    macro(o, Short##name, s, short, PrintShort) \
4018    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4019    macro(o, Char##name, c, char, PrintChar) \
4020    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4021    macro(o, Float##name, f, float, PrintFloat) \
4022    macro(o, Double##name, d, double, PrintDouble)
4023
4024 #define OPERATOR_INTTYPES(macro, o, name) \
4025    macro(o, Int##name, i, int, PrintInt) \
4026    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4027    macro(o, Short##name, s, short, PrintShort) \
4028    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4029    macro(o, Char##name, c, char, PrintChar) \
4030    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4031
4032
4033 // binary arithmetic
4034 OPERATOR_ALL(BINARY, +, Add)
4035 OPERATOR_ALL(BINARY, -, Sub)
4036 OPERATOR_ALL(BINARY, *, Mul)
4037 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4038 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4039
4040 // unary arithmetic
4041 OPERATOR_ALL(UNARY, -, Neg)
4042
4043 // unary arithmetic increment and decrement
4044 OPERATOR_ALL(UNARY, ++, Inc)
4045 OPERATOR_ALL(UNARY, --, Dec)
4046
4047 // binary arithmetic assignment
4048 OPERATOR_ALL(BINARY, =, Asign)
4049 OPERATOR_ALL(BINARY, +=, AddAsign)
4050 OPERATOR_ALL(BINARY, -=, SubAsign)
4051 OPERATOR_ALL(BINARY, *=, MulAsign)
4052 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4053 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4054
4055 // binary bitwise
4056 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4057 OPERATOR_INTTYPES(BINARY, |, BitOr)
4058 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4059 OPERATOR_INTTYPES(BINARY, <<, LShift)
4060 OPERATOR_INTTYPES(BINARY, >>, RShift)
4061
4062 // unary bitwise
4063 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4064
4065 // binary bitwise assignment
4066 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4067 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4068 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4069 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4070 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4071
4072 // unary logical negation
4073 OPERATOR_INTTYPES(UNARY, !, Not)
4074
4075 // binary logical equality
4076 OPERATOR_ALL(BINARY, ==, Equ)
4077 OPERATOR_ALL(BINARY, !=, Nqu)
4078
4079 // binary logical
4080 OPERATOR_ALL(BINARY, &&, And)
4081 OPERATOR_ALL(BINARY, ||, Or)
4082
4083 // binary logical relational
4084 OPERATOR_ALL(BINARY, >, Grt)
4085 OPERATOR_ALL(BINARY, <, Sma)
4086 OPERATOR_ALL(BINARY, >=, GrtEqu)
4087 OPERATOR_ALL(BINARY, <=, SmaEqu)
4088
4089 // tertiary condition operator
4090 OPERATOR_ALL(TERTIARY, ?, Cond)
4091
4092 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4093 #define OPERATOR_TABLE_ALL(name, type) \
4094     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4095                           type##Neg, \
4096                           type##Inc, type##Dec, \
4097                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4098                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4099                           type##BitNot, \
4100                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4101                           type##Not, \
4102                           type##Equ, type##Nqu, \
4103                           type##And, type##Or, \
4104                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4105                         }; \
4106
4107 #define OPERATOR_TABLE_INTTYPES(name, type) \
4108     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4109                           type##Neg, \
4110                           type##Inc, type##Dec, \
4111                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4112                           null, null, null, null, null, \
4113                           null, \
4114                           null, null, null, null, null, \
4115                           null, \
4116                           type##Equ, type##Nqu, \
4117                           type##And, type##Or, \
4118                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4119                         }; \
4120
4121 OPERATOR_TABLE_ALL(int, Int)
4122 OPERATOR_TABLE_ALL(uint, UInt)
4123 OPERATOR_TABLE_ALL(short, Short)
4124 OPERATOR_TABLE_ALL(ushort, UShort)
4125 OPERATOR_TABLE_INTTYPES(float, Float)
4126 OPERATOR_TABLE_INTTYPES(double, Double)
4127 OPERATOR_TABLE_ALL(char, Char)
4128 OPERATOR_TABLE_ALL(uchar, UChar)
4129
4130 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4131 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4132 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4133 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4134 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4135 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4136 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4137 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4138
4139 public void ReadString(char * output,  char * string)
4140 {
4141    int len = strlen(string);
4142    int c,d = 0;
4143    bool quoted = false, escaped = false;
4144    for(c = 0; c<len; c++)
4145    {
4146       char ch = string[c];
4147       if(escaped)
4148       {
4149          switch(ch)
4150          {
4151             case 'n': output[d] = '\n'; break;
4152             case 't': output[d] = '\t'; break;
4153             case 'a': output[d] = '\a'; break;
4154             case 'b': output[d] = '\b'; break;
4155             case 'f': output[d] = '\f'; break;
4156             case 'r': output[d] = '\r'; break;
4157             case 'v': output[d] = '\v'; break;
4158             case '\\': output[d] = '\\'; break;
4159             case '\"': output[d] = '\"'; break;
4160             default: output[d++] = '\\'; output[d] = ch;
4161             //default: output[d] = ch;
4162          }
4163          d++;
4164          escaped = false;
4165       }
4166       else 
4167       {
4168          if(ch == '\"') 
4169             quoted ^= true;
4170          else if(quoted)
4171          {
4172             if(ch == '\\')
4173                escaped = true;
4174             else
4175                output[d++] = ch;
4176          }
4177       }
4178    }
4179    output[d] = '\0';
4180 }
4181
4182 public Operand GetOperand(Expression exp)
4183 {
4184    Operand op { };
4185    Type type = exp.expType;
4186    if(type)
4187    {
4188       while(type.kind == classType && 
4189          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4190       {
4191          if(!type._class.registered.dataType)
4192             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4193          type = type._class.registered.dataType;
4194          
4195       }
4196       op.kind = type.kind;
4197       op.type = exp.expType;
4198       if(exp.isConstant && exp.type == constantExp)
4199       {
4200          switch(op.kind)
4201          {
4202             case charType:
4203             {
4204                if(exp.constant[0] == '\'')
4205                   op.c = exp.constant[1];
4206                else if(type.isSigned)
4207                {
4208                   op.c = (char)strtol(exp.constant, null, 0);
4209                   op.ops = charOps;
4210                }
4211                else
4212                {
4213                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4214                   op.ops = ucharOps;
4215                }
4216                break;
4217             }
4218             case shortType:
4219                if(type.isSigned)
4220                {
4221                   op.s = (short)strtol(exp.constant, null, 0);
4222                   op.ops = shortOps;
4223                }
4224                else
4225                {
4226                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4227                   op.ops = ushortOps;
4228                }
4229                break;
4230             case intType:
4231             case longType:
4232                if(type.isSigned)
4233                {
4234                   op.i = (int)strtol(exp.constant, null, 0);
4235                   op.ops = intOps;
4236                }
4237                else
4238                {
4239                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4240                   op.ops = uintOps;
4241                }
4242                op.kind = intType;
4243                break;
4244             case int64Type:
4245                if(type.isSigned)
4246                {
4247                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4248                   op.ops = intOps;
4249                }
4250                else
4251                {
4252                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4253                   op.ops = uintOps;
4254                }
4255                op.kind = intType;
4256                break;
4257             case floatType:
4258                op.f = (float)strtod(exp.constant, null);
4259                op.ops = floatOps;
4260                break;
4261             case doubleType:
4262                op.d = (double)strtod(exp.constant, null);
4263                op.ops = doubleOps;
4264                break;
4265             //case classType:    For when we have operator overloading...
4266             // Pointer additions
4267             //case functionType:
4268             case arrayType:
4269             case pointerType:
4270             case classType:
4271                op.p = (unsigned char *)strtoul(exp.constant, null, 0);
4272                op.kind = pointerType;
4273                op.ops = uintOps;
4274                // op.ptrSize = 
4275                break;
4276          }
4277       }
4278    }
4279    return op;
4280 }
4281
4282 static void UnusedFunction()
4283 {
4284    int a;
4285    a.OnGetString(0,0,0);
4286 }
4287 default:
4288 extern int __ecereVMethodID_class_OnGetString;
4289 public:
4290
4291 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4292 {
4293    DataMember dataMember;
4294    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4295    {
4296       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4297          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4298       else
4299       {
4300          Expression exp { };
4301          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4302          Type type;
4303          void * ptr = inst.data + dataMember.offset + offset;
4304          char * result = null;
4305          exp.loc = member.loc = inst.loc;
4306          ((Identifier)member.identifiers->first).loc = inst.loc;
4307
4308          if(!dataMember.dataType)
4309             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4310          type = dataMember.dataType;
4311          if(type.kind == classType)
4312          {
4313             Class _class = type._class.registered;
4314             if(_class.type == enumClass)
4315             {
4316                Class enumClass = eSystem_FindClass(privateModule, "enum");
4317                if(enumClass)
4318                {
4319                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4320                   NamedLink item;
4321                   for(item = e.values.first; item; item = item.next)
4322                   {
4323                      if((int)item.data == *(int *)ptr)
4324                      {
4325                         result = item.name;
4326                         break;
4327                      }
4328                   }
4329                   if(result)
4330                   {
4331                      exp.identifier = MkIdentifier(result);
4332                      exp.type = identifierExp;
4333                      exp.destType = MkClassType(_class.fullName);
4334                      ProcessExpressionType(exp);
4335                   }
4336                }
4337             }
4338             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4339             {
4340                if(!_class.dataType)
4341                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4342                type = _class.dataType;
4343             }
4344          }
4345          if(!result)
4346          {
4347             switch(type.kind)
4348             {
4349                case floatType:
4350                {
4351                   FreeExpContents(exp);
4352
4353                   exp.constant = PrintFloat(*(float*)ptr);
4354                   exp.type = constantExp;
4355                   break;
4356                }
4357                case doubleType:
4358                {
4359                   FreeExpContents(exp);
4360
4361                   exp.constant = PrintDouble(*(double*)ptr);
4362                   exp.type = constantExp;
4363                   break;
4364                }
4365                case intType:
4366                {
4367                   FreeExpContents(exp);
4368
4369                   exp.constant = PrintInt(*(int*)ptr);
4370                   exp.type = constantExp;
4371                   break;
4372                }
4373                case int64Type:
4374                {
4375                   FreeExpContents(exp);
4376
4377                   exp.constant = PrintInt64(*(int64*)ptr);
4378                   exp.type = constantExp;
4379                   break;
4380                }
4381                default:
4382                   Compiler_Error($"Unhandled type populating instance\n");
4383             }
4384          }
4385          ListAdd(memberList, member);
4386       }
4387
4388       if(parentDataMember.type == unionMember)
4389          break;
4390    }
4391 }
4392
4393 void PopulateInstance(Instantiation inst)
4394 {
4395    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4396    Class _class = classSym.registered;
4397    DataMember dataMember;
4398    OldList * memberList = MkList();
4399    inst.members = MkListOne(MkMembersInitList(memberList));
4400    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4401    {
4402       if(!dataMember.isProperty)
4403       {
4404          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4405             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4406          else
4407          {
4408             Expression exp { };
4409             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4410             Type type;
4411             void * ptr = inst.data + dataMember.offset;
4412             char * result = null;
4413
4414             exp.loc = member.loc = inst.loc;
4415             ((Identifier)member.identifiers->first).loc = inst.loc;
4416
4417             if(!dataMember.dataType)
4418                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4419             type = dataMember.dataType;
4420             if(type.kind == classType)
4421             {
4422                Class _class = type._class.registered;
4423                if(_class.type == enumClass)
4424                {
4425                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4426                   if(enumClass)
4427                   {
4428                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4429                      NamedLink item;
4430                      for(item = e.values.first; item; item = item.next)
4431                      {
4432                         if((int)item.data == *(int *)ptr)
4433                         {
4434                            result = item.name;
4435                            break;
4436                         }
4437                      }
4438                   }
4439                   if(result)
4440                   {
4441                      exp.identifier = MkIdentifier(result);
4442                      exp.type = identifierExp;
4443                      exp.destType = MkClassType(_class.fullName);
4444                      ProcessExpressionType(exp);
4445                   }                     
4446                }
4447                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4448                {
4449                   if(!_class.dataType)
4450                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4451                   type = _class.dataType;
4452                }
4453             }
4454             if(!result)
4455             {
4456                switch(type.kind)
4457                {
4458                   case floatType:
4459                   {
4460                      exp.constant = PrintFloat(*(float*)ptr);
4461                      exp.type = constantExp;
4462                      break;
4463                   }
4464                   case doubleType:
4465                   {
4466                      exp.constant = PrintDouble(*(double*)ptr);
4467                      exp.type = constantExp;
4468                      break;
4469                   }
4470                   case intType:
4471                   {
4472                      exp.constant = PrintInt(*(int*)ptr);
4473                      exp.type = constantExp;
4474                      break;
4475                   }
4476                   case int64Type:
4477                   {
4478                      exp.constant = PrintInt64(*(int64*)ptr);
4479                      exp.type = constantExp;
4480                      break;
4481                   }
4482                   default:
4483                      Compiler_Error($"Unhandled type populating instance\n");
4484                }
4485             }
4486             ListAdd(memberList, member);
4487          }
4488       }
4489    }
4490 }
4491
4492 void ComputeInstantiation(Expression exp)
4493 {
4494    Instantiation inst = exp.instance;
4495    MembersInit members;
4496    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4497    Class _class = classSym ? classSym.registered : null;
4498    DataMember curMember = null;
4499    Class curClass = null;
4500    DataMember subMemberStack[256];
4501    int subMemberStackPos = 0;
4502    uint64 bits = 0;
4503
4504    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4505    {
4506       // Don't recompute the instantiation... 
4507       // Non Simple classes will have become constants by now
4508       if(inst.data) 
4509          return;
4510
4511       if(_class.type == normalClass || _class.type == noHeadClass)
4512          inst.data = (byte *)eInstance_New(_class);
4513       else
4514          inst.data = new0 byte[_class.structSize];
4515    }
4516
4517    if(inst.members)
4518    {
4519       for(members = inst.members->first; members; members = members.next)
4520       {
4521          switch(members.type)
4522          {
4523             case dataMembersInit:
4524             {
4525                if(members.dataMembers)
4526                {
4527                   MemberInit member;
4528                   for(member = members.dataMembers->first; member; member = member.next)
4529                   {
4530                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4531                      bool found = false;
4532
4533                      Property prop = null;
4534                      DataMember dataMember = null;
4535                      Method method = null;
4536                      uint dataMemberOffset;
4537
4538                      if(!ident)
4539                      {
4540                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4541                         if(curMember)
4542                         {
4543                            if(curMember.isProperty)
4544                               prop = (Property)curMember;
4545                            else
4546                            {
4547                               dataMember = curMember;
4548                               
4549                               // CHANGED THIS HERE
4550                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4551                               // dataMemberOffset = dataMember.offset;
4552                            }
4553                            found = true;
4554                         }
4555                      }
4556                      else
4557                      {
4558                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4559                         if(prop)
4560                         {
4561                            found = true;
4562                            if(prop.memberAccess == publicAccess)
4563                            {
4564                               curMember = (DataMember)prop;
4565                               curClass = prop._class;
4566                            }
4567                         }
4568                         else
4569                         {
4570                            DataMember _subMemberStack[256];
4571                            int _subMemberStackPos = 0;
4572
4573                            // FILL MEMBER STACK
4574                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4575
4576                            if(dataMember)
4577                            {
4578                               found = true;
4579                               if(dataMember.memberAccess == publicAccess)
4580                               {
4581                                  curMember = dataMember;
4582                                  curClass = dataMember._class;
4583                                  memcpy(subMemberStack, _subMemberStack, sizeof(int) * _subMemberStackPos);
4584                                  subMemberStackPos = _subMemberStackPos;
4585                               }
4586                            }
4587                         }
4588                      }
4589
4590                      if(found && member.initializer && member.initializer.type == expInitializer)
4591                      {
4592                         Expression value = member.initializer.exp;
4593                         Type type = null;
4594                         if(prop)
4595                         {
4596                            type = prop.dataType;
4597                         }
4598                         else if(dataMember)
4599                         {
4600                            if(!dataMember.dataType)
4601                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4602                            
4603                            type = dataMember.dataType;
4604                         }
4605
4606                         if(ident && ident.next)
4607                         {
4608                            // for(; ident && type; ident = ident.next)
4609                            for(ident = ident.next; ident && type; ident = ident.next)
4610                            {
4611                               if(type.kind == classType)
4612                               {
4613                                  prop = eClass_FindProperty(type._class.registered,
4614                                     ident.string, privateModule);
4615                                  if(prop)
4616                                     type = prop.dataType;
4617                                  else
4618                                  {
4619                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered, 
4620                                        ident.string, &dataMemberOffset, privateModule, null, null);
4621                                     if(dataMember)
4622                                        type = dataMember.dataType;
4623                                  }
4624                               }
4625                               else if(type.kind == structType || type.kind == unionType)
4626                               {
4627                                  Type memberType;
4628                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4629                                  {
4630                                     if(!strcmp(memberType.name, ident.string))
4631                                     {
4632                                        type = memberType;
4633                                        break;
4634                                     }
4635                                  }
4636                               }
4637                            }
4638                         }
4639                         if(value)
4640                         {
4641                            FreeType(value.destType);
4642                            value.destType = type;
4643                            if(type) type.refCount++;
4644                            ComputeExpression(value);
4645                         }
4646                         if(value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4647                         {
4648                            if(type.kind == classType)
4649                            {
4650                               Class _class = type._class.registered;
4651                               if(_class.type == bitClass || _class.type == unitClass ||
4652                                  _class.type == enumClass)
4653                               {
4654                                  if(!_class.dataType)
4655                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4656                                  type = _class.dataType;
4657                               }
4658                            }
4659
4660                            if(dataMember)
4661                            {
4662                               void * ptr = inst.data + dataMemberOffset;
4663                               
4664                               if(value.type == constantExp)
4665                               {
4666                                  switch(type.kind)
4667                                  {
4668                                     case intType:
4669                                     {
4670                                        GetInt(value, (int*)ptr);
4671                                        break;
4672                                     }
4673                                     case int64Type:
4674                                     {
4675                                        GetInt64(value, (int64*)ptr);
4676                                        break;
4677                                     }
4678                                     case floatType:
4679                                     {
4680                                        GetFloat(value, (float*)ptr);
4681                                        break;
4682                                     }
4683                                     case doubleType:
4684                                     {
4685                                        GetDouble(value, (double *)ptr);
4686                                        break;
4687                                     }
4688                                  }
4689                               }
4690                               else if(value.type == instanceExp)
4691                               {
4692                                  if(type.kind == classType)
4693                                  {
4694                                     Class _class = type._class.registered;
4695                                     if(_class.type == structClass)
4696                                     {
4697                                        ComputeTypeSize(type);
4698                                        if(value.instance.data)
4699                                           memcpy(ptr, value.instance.data, type.size);
4700                                     }
4701                                  }
4702                               }
4703                            }
4704                            else if(prop)
4705                            {
4706                               if(value.type == instanceExp && value.instance.data)
4707                               {
4708                                  void (*Set)(void *, void *) = (void *)prop.Set;
4709                                  Set(inst.data, value.instance.data);
4710                                  PopulateInstance(inst);
4711                               }
4712                               else if(value.type == constantExp)
4713                               {
4714                                  switch(type.kind)
4715                                  {
4716                                     case doubleType:
4717                                     {
4718                                        void (*Set)(void *, double) = (void *)prop.Set;
4719                                        Set(inst.data, strtod(value.constant, null) );
4720                                        break;
4721                                     }
4722                                     case floatType:
4723                                     {
4724                                        void (*Set)(void *, float) = (void *)prop.Set;
4725                                        Set(inst.data, (float)(strtod(value.constant, null)));
4726                                        break;
4727                                     }
4728                                     case intType:
4729                                     {
4730                                        void (*Set)(void *, int) = (void *)prop.Set;
4731                                        Set(inst.data, strtol(value.constant, null, 0));
4732                                        break;
4733                                     }
4734                                     case int64Type:
4735                                     {
4736                                        void (*Set)(void *, int64) = (void *)prop.Set;
4737                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4738                                        break;
4739                                     }
4740                                  }
4741                               }
4742                               else if(value.type == stringExp)
4743                               {
4744                                  char temp[1024];
4745                                  ReadString(temp, value.string);
4746                                  prop.Set(inst.data, temp);
4747                               }
4748                            }
4749                         }
4750                         else if(_class.type == unitClass)
4751                         {
4752                            if(prop)
4753                            {
4754                               // Only support converting units to units for now...
4755                               if(value.type == constantExp)
4756                               {
4757                                  if(type.kind == classType)
4758                                  {
4759                                     Class _class = type._class.registered;
4760                                     if(_class.type == unitClass)
4761                                     {
4762                                        if(!_class.dataType)
4763                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4764                                        type = _class.dataType;
4765                                     }
4766                                  }
4767                                  // TODO: Assuming same base type for units...
4768                                  switch(type.kind)
4769                                  {
4770                                     case floatType:
4771                                     {
4772                                        float fValue;
4773                                        float (*Set)(float) = (void *)prop.Set;
4774                                        GetFloat(member.initializer.exp, &fValue);
4775                                        exp.constant = PrintFloat(Set(fValue));
4776                                        exp.type = constantExp;
4777                                        break;
4778                                     }
4779                                     case doubleType:
4780                                     {
4781                                        double dValue;
4782                                        double (*Set)(double) = (void *)prop.Set;
4783                                        GetDouble(member.initializer.exp, &dValue);
4784                                        exp.constant = PrintDouble(Set(dValue));
4785                                        exp.type = constantExp;
4786                                        break;
4787                                     }
4788                                  }
4789                               }
4790                            }
4791                         }
4792                         else if(_class.type == bitClass)
4793                         {
4794                            if(prop)
4795                            {
4796                               if(value.type == instanceExp && value.instance.data)
4797                               {
4798                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4799                                  bits = Set(value.instance.data);
4800                               }
4801                               else if(value.type == constantExp)
4802                               {
4803                               }
4804                            }
4805                            else if(dataMember)
4806                            {
4807                               BitMember bitMember = (BitMember) dataMember;
4808                               Type type;
4809                               int part = 0;
4810                               GetInt(value, &part);
4811                               bits = (bits & ~bitMember.mask);
4812                               if(!bitMember.dataType)
4813                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4814
4815                               type = bitMember.dataType;
4816
4817                               if(type.kind == classType && type._class && type._class.registered)
4818                               {
4819                                  if(!type._class.registered.dataType)
4820                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);                                    
4821                                  type = type._class.registered.dataType;
4822                               }
4823
4824                               switch(type.kind)
4825                               {
4826                                  case charType:
4827                                     if(type.isSigned)
4828                                        bits |= ((char)part << bitMember.pos);
4829                                     else
4830                                        bits |= ((unsigned char)part << bitMember.pos);
4831                                     break;
4832                                  case shortType:
4833                                     if(type.isSigned)
4834                                        bits |= ((short)part << bitMember.pos);
4835                                     else
4836                                        bits |= ((unsigned short)part << bitMember.pos);
4837                                     break;
4838                                  case intType:
4839                                  case longType:
4840                                     if(type.isSigned)
4841                                        bits |= ((int)part << bitMember.pos);
4842                                     else
4843                                        bits |= ((unsigned int)part << bitMember.pos);
4844                                     break;
4845                                  case int64Type:
4846                                     if(type.isSigned)
4847                                        bits |= ((int64)part << bitMember.pos);
4848                                     else
4849                                        bits |= ((uint64)part << bitMember.pos);
4850                                     break;
4851                               }
4852                            }
4853                         }
4854                      }
4855                      else
4856                      {
4857                         if(_class && _class.type == unitClass)
4858                         {
4859                            ComputeExpression(member.initializer.exp);
4860                            exp.constant = member.initializer.exp.constant;
4861                            exp.type = constantExp;
4862                            
4863                            member.initializer.exp.constant = null;
4864                         }
4865                      }
4866                   }
4867                }
4868                break;
4869             }
4870          }
4871       }
4872    }
4873    if(_class && _class.type == bitClass)
4874    {
4875       exp.constant = PrintHexUInt(bits);
4876       exp.type = constantExp;
4877    }
4878    if(exp.type != instanceExp)
4879    {
4880       FreeInstance(inst);
4881    }
4882 }
4883
4884 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4885 {
4886    if(exp.op.op == SIZEOF)
4887    {
4888       FreeExpContents(exp);
4889       exp.type = constantExp;
4890       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
4891    }
4892    else
4893    {
4894       if(!exp.op.exp1)
4895       {
4896          switch(exp.op.op)
4897          {
4898             // unary arithmetic
4899             case '+':
4900             {
4901                // Provide default unary +
4902                Expression exp2 = exp.op.exp2;
4903                exp.op.exp2 = null;
4904                FreeExpContents(exp);
4905                FreeType(exp.expType);
4906                FreeType(exp.destType);
4907                *exp = *exp2;
4908                delete exp2;
4909                break;
4910             }
4911             case '-':
4912                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
4913                break;
4914             // unary arithmetic increment and decrement
4915                   //OPERATOR_ALL(UNARY, ++, Inc)
4916                   //OPERATOR_ALL(UNARY, --, Dec)
4917             // unary bitwise
4918             case '~':
4919                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
4920                break;
4921             // unary logical negation
4922             case '!':
4923                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
4924                break;
4925          }
4926       }
4927       else
4928       {
4929          switch(exp.op.op)
4930          {
4931             // binary arithmetic
4932             case '+':
4933                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
4934                break;
4935             case '-':
4936                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
4937                break;
4938             case '*':
4939                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
4940                break;
4941             case '/':
4942                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
4943                break;
4944             case '%':
4945                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
4946                break;
4947             // binary arithmetic assignment
4948                   //OPERATOR_ALL(BINARY, =, Asign)
4949                   //OPERATOR_ALL(BINARY, +=, AddAsign)
4950                   //OPERATOR_ALL(BINARY, -=, SubAsign)
4951                   //OPERATOR_ALL(BINARY, *=, MulAsign)
4952                   //OPERATOR_ALL(BINARY, /=, DivAsign)
4953                   //OPERATOR_ALL(BINARY, %=, ModAsign)
4954             // binary bitwise
4955             case '&':
4956                if(exp.op.exp2)
4957                {
4958                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
4959                }
4960                break;
4961             case '|':
4962                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
4963                break;
4964             case '^':
4965                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
4966                break;
4967             case LEFT_OP:
4968                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
4969                break;
4970             case RIGHT_OP:
4971                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
4972                break;
4973             // binary bitwise assignment
4974                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4975                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4976                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4977                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4978                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4979             // binary logical equality
4980             case EQ_OP:
4981                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
4982                break;
4983             case NE_OP:
4984                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
4985                break;
4986             // binary logical
4987             case AND_OP:
4988                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
4989                break;
4990             case OR_OP:
4991                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
4992                break;
4993             // binary logical relational
4994             case '>':
4995                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
4996                break;
4997             case '<':
4998                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
4999                break;
5000             case GE_OP:
5001                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5002                break;
5003             case LE_OP:
5004                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5005                break;
5006          }
5007       }
5008    }
5009 }
5010
5011 void ComputeExpression(Expression exp)
5012 {
5013    char expString[10240];
5014    expString[0] = '\0';
5015 #ifdef _DEBUG
5016    PrintExpression(exp, expString);
5017 #endif
5018
5019    switch(exp.type)
5020    {
5021       case instanceExp:
5022       {
5023          ComputeInstantiation(exp);
5024          break;
5025       }
5026       /*
5027       case constantExp:
5028          break;
5029       */
5030       case opExp:
5031       {
5032          Expression exp1, exp2 = null;
5033          Operand op1 { };
5034          Operand op2 { };
5035
5036          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5037          if(exp.op.exp2)
5038             ComputeExpression(exp.op.exp2);
5039          if(exp.op.exp1)
5040          {
5041             ComputeExpression(exp.op.exp1);
5042             exp1 = exp.op.exp1;
5043             exp2 = exp.op.exp2;
5044             op1 = GetOperand(exp1);
5045             if(op1.type) op1.type.refCount++;
5046             if(exp2)
5047             {
5048                op2 = GetOperand(exp2);
5049                if(op2.type) op2.type.refCount++;
5050             }
5051          }
5052          else 
5053          {
5054             exp1 = exp.op.exp2;
5055             op1 = GetOperand(exp1);
5056             if(op1.type) op1.type.refCount++;
5057          }
5058
5059          CallOperator(exp, exp1, exp2, op1, op2);
5060          /*
5061          switch(exp.op.op)
5062          {
5063             // Unary operators
5064             case '&':
5065                // Also binary
5066                if(exp.op.exp1 && exp.op.exp2)
5067                {
5068                   // Binary And
5069                   if(op1.ops.BitAnd)
5070                   {
5071                      FreeExpContents(exp);
5072                      op1.ops.BitAnd(exp, op1, op2);
5073                   }
5074                }
5075                break;
5076             case '*':
5077                if(exp.op.exp1)
5078                {
5079                   if(op1.ops.Mul)
5080                   {
5081                      FreeExpContents(exp);
5082                      op1.ops.Mul(exp, op1, op2);
5083                   }
5084                }
5085                break;
5086             case '+':
5087                if(exp.op.exp1)
5088                {
5089                   if(op1.ops.Add)
5090                   {
5091                      FreeExpContents(exp);
5092                      op1.ops.Add(exp, op1, op2);
5093                   }
5094                }
5095                else
5096                {
5097                   // Provide default unary +
5098                   Expression exp2 = exp.op.exp2;
5099                   exp.op.exp2 = null;
5100                   FreeExpContents(exp);
5101                   FreeType(exp.expType);
5102                   FreeType(exp.destType);
5103
5104                   *exp = *exp2;
5105                   delete exp2;
5106                }
5107                break;
5108             case '-':
5109                if(exp.op.exp1)
5110                {
5111                   if(op1.ops.Sub) 
5112                   {
5113                      FreeExpContents(exp);
5114                      op1.ops.Sub(exp, op1, op2);
5115                   }
5116                }
5117                else
5118                {
5119                   if(op1.ops.Neg) 
5120                   {
5121                      FreeExpContents(exp);
5122                      op1.ops.Neg(exp, op1);
5123                   }
5124                }
5125                break;
5126             case '~':
5127                if(op1.ops.BitNot)
5128                {
5129                   FreeExpContents(exp);
5130                   op1.ops.BitNot(exp, op1);
5131                }
5132                break;
5133             case '!':
5134                if(op1.ops.Not)
5135                {
5136                   FreeExpContents(exp);
5137                   op1.ops.Not(exp, op1);
5138                }
5139                break;
5140             // Binary only operators
5141             case '/':
5142                if(op1.ops.Div) 
5143                {
5144                   FreeExpContents(exp);
5145                   op1.ops.Div(exp, op1, op2);
5146                }
5147                break;
5148             case '%':
5149                if(op1.ops.Mod)
5150                {
5151                   FreeExpContents(exp);
5152                   op1.ops.Mod(exp, op1, op2);
5153                }
5154                break;
5155             case LEFT_OP:
5156                break;
5157             case RIGHT_OP:
5158                break;
5159             case '<':
5160                if(exp.op.exp1)
5161                {
5162                   if(op1.ops.Sma)
5163                   {
5164                      FreeExpContents(exp);
5165                      op1.ops.Sma(exp, op1, op2);
5166                   }
5167                }
5168                break;
5169             case '>':
5170                if(exp.op.exp1)
5171                {
5172                   if(op1.ops.Grt)
5173                   {
5174                      FreeExpContents(exp);
5175                      op1.ops.Grt(exp, op1, op2);
5176                   }
5177                }
5178                break;
5179             case LE_OP:
5180                if(exp.op.exp1)
5181                {
5182                   if(op1.ops.SmaEqu)
5183                   {
5184                      FreeExpContents(exp);
5185                      op1.ops.SmaEqu(exp, op1, op2);
5186                   }
5187                }
5188                break;
5189             case GE_OP:
5190                if(exp.op.exp1)
5191                {
5192                   if(op1.ops.GrtEqu)
5193                   {
5194                      FreeExpContents(exp);
5195                      op1.ops.GrtEqu(exp, op1, op2);
5196                   }
5197                }
5198                break;
5199             case EQ_OP:
5200                if(exp.op.exp1)
5201                {
5202                   if(op1.ops.Equ)
5203                   {
5204                      FreeExpContents(exp);
5205                      op1.ops.Equ(exp, op1, op2);
5206                   }
5207                }
5208                break;
5209             case NE_OP:
5210                if(exp.op.exp1)
5211                {
5212                   if(op1.ops.Nqu)
5213                   {
5214                      FreeExpContents(exp);
5215                      op1.ops.Nqu(exp, op1, op2);
5216                   }
5217                }
5218                break;
5219             case '|':
5220                if(op1.ops.BitOr) 
5221                {
5222                   FreeExpContents(exp);
5223                   op1.ops.BitOr(exp, op1, op2);
5224                }
5225                break;
5226             case '^':
5227                if(op1.ops.BitXor) 
5228                {
5229                   FreeExpContents(exp);
5230                   op1.ops.BitXor(exp, op1, op2);
5231                }
5232                break;
5233             case AND_OP:
5234                break;
5235             case OR_OP:
5236                break;
5237             case SIZEOF:
5238                FreeExpContents(exp);
5239                exp.type = constantExp;
5240                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5241                break;
5242          }
5243          */
5244          if(op1.type) FreeType(op1.type);
5245          if(op2.type) FreeType(op2.type);
5246          break;
5247       }
5248       case bracketsExp:
5249       case extensionExpressionExp:
5250       {
5251          Expression e, n;
5252          for(e = exp.list->first; e; e = n)
5253          {
5254             n = e.next;
5255             if(!n)
5256             {
5257                OldList * list = exp.list;
5258                ComputeExpression(e);
5259                //FreeExpContents(exp);
5260                FreeType(exp.expType);
5261                FreeType(exp.destType);
5262                *exp = *e;
5263                delete e;
5264                delete list;
5265             }
5266             else
5267             {
5268                FreeExpression(e);
5269             }
5270          }
5271          break;
5272       }
5273       /*
5274
5275       case ExpIndex:
5276       {
5277          Expression e;
5278          exp.isConstant = true;
5279
5280          ComputeExpression(exp.index.exp);
5281          if(!exp.index.exp.isConstant)
5282             exp.isConstant = false;
5283
5284          for(e = exp.index.index->first; e; e = e.next)
5285          {
5286             ComputeExpression(e);
5287             if(!e.next)
5288             {
5289                // Check if this type is int
5290             }
5291             if(!e.isConstant)
5292                exp.isConstant = false;
5293          }
5294          exp.expType = Dereference(exp.index.exp.expType);
5295          break;
5296       }
5297       */
5298       case memberExp:
5299       {
5300          Expression memberExp = exp.member.exp;
5301          Identifier memberID = exp.member.member;
5302
5303          Type type;
5304          ComputeExpression(exp.member.exp);
5305          type = exp.member.exp.expType;
5306          if(type)
5307          {
5308             Class _class = (exp.member.member && exp.member.member.classSym) ? exp.member.member.classSym.registered : (((type.kind == classType || type.kind == subClassType) && type._class) ? type._class.registered : null);
5309             Property prop = null;
5310             DataMember member = null;
5311             Class convertTo = null;
5312             if(type.kind == subClassType && exp.member.exp.type == classExp)
5313                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5314
5315             if(!_class)
5316             {
5317                char string[256];
5318                Symbol classSym;
5319                string[0] = '\0';
5320                PrintType(type, string, false, true);
5321                classSym = FindClass(string);
5322                _class = classSym ? classSym.registered : null;
5323             }
5324
5325             if(exp.member.member)
5326             {
5327                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5328                if(!prop)
5329                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5330             }
5331             if(!prop && !member && _class && exp.member.member)
5332             {
5333                Symbol classSym = FindClass(exp.member.member.string);
5334                convertTo = _class;
5335                _class = classSym ? classSym.registered : null;
5336                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5337             }
5338       
5339             if(prop)
5340             {
5341                if(prop.compiled)
5342                {
5343                   Type type = prop.dataType;
5344                   // TODO: Assuming same base type for units...
5345                   if(_class.type == unitClass)
5346                   {
5347                      if(type.kind == classType)
5348                      {
5349                         Class _class = type._class.registered;
5350                         if(_class.type == unitClass)
5351                         {
5352                            if(!_class.dataType)
5353                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5354                            type = _class.dataType;
5355                         }
5356                      }
5357                      switch(type.kind)
5358                      {
5359                         case floatType:
5360                         {
5361                            float value;
5362                            float (*Get)(float) = (void *)prop.Get;
5363                            GetFloat(exp.member.exp, &value);
5364                            exp.constant = PrintFloat(Get ? Get(value) : value);
5365                            exp.type = constantExp;
5366                            break;
5367                         }
5368                         case doubleType:
5369                         {
5370                            double value;
5371                            double (*Get)(double);
5372                            GetDouble(exp.member.exp, &value);
5373                      
5374                            if(convertTo)
5375                               Get = (void *)prop.Set;
5376                            else
5377                               Get = (void *)prop.Get;
5378                            exp.constant = PrintDouble(Get ? Get(value) : value);
5379                            exp.type = constantExp;
5380                            break;
5381                         }
5382                      }
5383                   }
5384                   else
5385                   {
5386                      if(convertTo)
5387                      {
5388                         Expression value = exp.member.exp;
5389                         Type type;
5390                         if(!prop.dataType)
5391                            ProcessPropertyType(prop);
5392
5393                         type = prop.dataType;
5394                         if(!type)
5395                         {
5396                             // printf("Investigate this\n");
5397                         }
5398                         else if(_class.type == structClass)
5399                         {
5400                            switch(type.kind)
5401                            {
5402                               case classType:
5403                               {
5404                                  Class propertyClass = type._class.registered;
5405                                  if(propertyClass.type == structClass && value.type == instanceExp)
5406                                  {
5407                                     void (*Set)(void *, void *) = (void *)prop.Set;
5408                                     exp.instance = Instantiation { };
5409                                     exp.instance.data = new0 byte[_class.structSize];
5410                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5411                                     exp.instance.loc = exp.loc;
5412                                     exp.type = instanceExp;
5413                                     Set(exp.instance.data, value.instance.data);
5414                                     PopulateInstance(exp.instance);
5415                                  }
5416                                  break;
5417                               }
5418                               case intType:
5419                               {
5420                                  int intValue;
5421                                  void (*Set)(void *, int) = (void *)prop.Set;
5422
5423                                  exp.instance = Instantiation { };
5424                                  exp.instance.data = new0 byte[_class.structSize];
5425                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5426                                  exp.instance.loc = exp.loc;
5427                                  exp.type = instanceExp;
5428                               
5429                                  GetInt(value, &intValue);
5430
5431                                  Set(exp.instance.data, intValue);
5432                                  PopulateInstance(exp.instance);
5433                                  break;
5434                               }
5435                               case int64Type:
5436                               {
5437                                  int64 intValue;
5438                                  void (*Set)(void *, int64) = (void *)prop.Set;
5439
5440                                  exp.instance = Instantiation { };
5441                                  exp.instance.data = new0 byte[_class.structSize];
5442                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5443                                  exp.instance.loc = exp.loc;
5444                                  exp.type = instanceExp;
5445                               
5446                                  GetInt64(value, &intValue);
5447
5448                                  Set(exp.instance.data, intValue);
5449                                  PopulateInstance(exp.instance);
5450                                  break;
5451                               }
5452                               case doubleType:
5453                               {
5454                                  double doubleValue;
5455                                  void (*Set)(void *, double) = (void *)prop.Set;
5456
5457                                  exp.instance = Instantiation { };
5458                                  exp.instance.data = new0 byte[_class.structSize];
5459                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5460                                  exp.instance.loc = exp.loc;
5461                                  exp.type = instanceExp;
5462                               
5463                                  GetDouble(value, &doubleValue);
5464
5465                                  Set(exp.instance.data, doubleValue);
5466                                  PopulateInstance(exp.instance);
5467                                  break;
5468                               }
5469                            }
5470                         }
5471                         else if(_class.type == bitClass)
5472                         {
5473                            switch(type.kind)
5474                            {
5475                               case classType:
5476                               {
5477                                  Class propertyClass = type._class.registered;
5478                                  if(propertyClass.type == structClass && value.instance.data)
5479                                  {
5480                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5481                                     unsigned int bits = Set(value.instance.data);
5482                                     exp.constant = PrintHexUInt(bits);
5483                                     exp.type = constantExp;
5484                                     break;
5485                                  }
5486                                  else if(_class.type == bitClass)
5487                                  {
5488                                     unsigned int value;
5489                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5490                                     unsigned int bits;
5491
5492                                     GetUInt(exp.member.exp, &value);
5493                                     bits = Set(value);
5494                                     exp.constant = PrintHexUInt(bits);
5495                                     exp.type = constantExp;
5496                                  }
5497                               }
5498                            }
5499                         }
5500                      }
5501                      else
5502                      {
5503                         if(_class.type == bitClass)
5504                         {
5505                            unsigned int value;
5506                            GetUInt(exp.member.exp, &value);
5507
5508                            switch(type.kind)
5509                            {
5510                               case classType:
5511                               {
5512                                  Class _class = type._class.registered;
5513                                  if(_class.type == structClass)
5514                                  {
5515                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5516
5517                                     exp.instance = Instantiation { };
5518                                     exp.instance.data = new0 byte[_class.structSize];
5519                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5520                                     exp.instance.loc = exp.loc;
5521                                     //exp.instance.fullSet = true;
5522                                     exp.type = instanceExp;
5523                                     Get(value, exp.instance.data);
5524                                     PopulateInstance(exp.instance);
5525                                  }
5526                                  else if(_class.type == bitClass)
5527                                  {
5528                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5529                                     uint64 bits = Get(value);
5530                                     exp.constant = PrintHexUInt64(bits);
5531                                     exp.type = constantExp;
5532                                  }
5533                                  break;
5534                               }
5535                            }
5536                         }
5537                         else if(_class.type == structClass)
5538                         {
5539                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5540                            switch(type.kind)
5541                            {
5542                               case classType:
5543                               {
5544                                  Class _class = type._class.registered;
5545                                  if(_class.type == structClass && value)
5546                                  {
5547                                     void (*Get)(void *, void *) = (void *)prop.Get;
5548
5549                                     exp.instance = Instantiation { };
5550                                     exp.instance.data = new0 byte[_class.structSize];
5551                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5552                                     exp.instance.loc = exp.loc;
5553                                     //exp.instance.fullSet = true;
5554                                     exp.type = instanceExp;
5555                                     Get(value, exp.instance.data);
5556                                     PopulateInstance(exp.instance);
5557                                  }
5558                                  break;
5559                               }
5560                            }
5561                         }
5562                         /*else
5563                         {
5564                            char * value = exp.member.exp.instance.data;
5565                            switch(type.kind)
5566                            {
5567                               case classType:
5568                               {
5569                                  Class _class = type._class.registered;
5570                                  if(_class.type == normalClass)
5571                                  {
5572                                     void *(*Get)(void *) = (void *)prop.Get;
5573
5574                                     exp.instance = Instantiation { };
5575                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5576                                     exp.type = instanceExp;
5577                                     exp.instance.data = Get(value, exp.instance.data);
5578                                  }
5579                                  break;
5580                               }
5581                            }
5582                         }
5583                         */
5584                      }
5585                   }
5586                }
5587                else
5588                {
5589                   exp.isConstant = false;
5590                }
5591             }
5592             else if(member)
5593             {
5594             }
5595          }
5596
5597          if(exp.type != ExpressionType::memberExp)
5598          {
5599             FreeExpression(memberExp);
5600             FreeIdentifier(memberID);
5601          }
5602          break;
5603       }
5604       case typeSizeExp:
5605       {
5606          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5607          FreeExpContents(exp);
5608          exp.constant = PrintUInt(ComputeTypeSize(type));
5609          exp.type = constantExp;         
5610          FreeType(type);
5611          break;
5612       }
5613       case classSizeExp:
5614       {
5615          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5616          if(classSym && classSym.registered)
5617          {
5618             if(classSym.registered.fixed)
5619             {
5620                FreeSpecifier(exp._class);
5621                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5622                exp.type = constantExp;
5623             }
5624             else
5625             {
5626                char className[1024];
5627                strcpy(className, "__ecereClass_");
5628                FullClassNameCat(className, classSym.string, true);
5629                MangleClassName(className);
5630
5631                FreeExpContents(exp);
5632                exp.type = pointerExp;
5633                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5634                exp.member.member = MkIdentifier("structSize");
5635             }
5636          }
5637          break;
5638       }
5639       case castExp:
5640       //case constantExp:
5641       {
5642          Type type;
5643          Expression e = exp;
5644          if(exp.type == castExp)
5645          {
5646             if(exp.cast.exp)
5647                ComputeExpression(exp.cast.exp);
5648             e = exp.cast.exp;
5649          }
5650          if(e && exp.expType)
5651          {
5652             /*if(exp.destType)
5653                type = exp.destType;
5654             else*/
5655                type = exp.expType;
5656             if(type.kind == classType)
5657             {
5658                Class _class = type._class.registered;
5659                if(_class && (_class.type == unitClass || _class.type == bitClass))
5660                {
5661                   if(!_class.dataType)
5662                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5663                   type = _class.dataType;
5664                }
5665             }
5666             
5667             switch(type.kind)
5668             {
5669                case charType:
5670                   if(type.isSigned)
5671                   {
5672                      char value;
5673                      GetChar(e, &value);
5674                      FreeExpContents(exp);
5675                      exp.constant = PrintChar(value);
5676                      exp.type = constantExp;
5677                   }
5678                   else
5679                   {
5680                      unsigned char value;
5681                      GetUChar(e, &value);
5682                      FreeExpContents(exp);
5683                      exp.constant = PrintUChar(value);
5684                      exp.type = constantExp;
5685                   }
5686                   break;
5687                case shortType:
5688                   if(type.isSigned)
5689                   {
5690                      short value;
5691                      GetShort(e, &value);
5692                      FreeExpContents(exp);
5693                      exp.constant = PrintShort(value);
5694                      exp.type = constantExp;
5695                   }
5696                   else
5697                   {
5698                      unsigned short value;
5699                      GetUShort(e, &value);
5700                      FreeExpContents(exp);
5701                      exp.constant = PrintUShort(value);
5702                      exp.type = constantExp;
5703                   }
5704                   break;
5705                case intType:
5706                   if(type.isSigned)
5707                   {
5708                      int value;
5709                      GetInt(e, &value);
5710                      FreeExpContents(exp);
5711                      exp.constant = PrintInt(value);
5712                      exp.type = constantExp;
5713                   }
5714                   else
5715                   {
5716                      unsigned int value;
5717                      GetUInt(e, &value);
5718                      FreeExpContents(exp);
5719                      exp.constant = PrintUInt(value);
5720                      exp.type = constantExp;
5721                   }
5722                   break;
5723                case int64Type:
5724                   if(type.isSigned)
5725                   {
5726                      int64 value;
5727                      GetInt64(e, &value);
5728                      FreeExpContents(exp);
5729                      exp.constant = PrintInt64(value);
5730                      exp.type = constantExp;
5731                   }
5732                   else
5733                   {
5734                      uint64 value;
5735                      GetUInt64(e, &value);
5736                      FreeExpContents(exp);
5737                      exp.constant = PrintUInt64(value);
5738                      exp.type = constantExp;
5739                   }
5740                   break;
5741                case floatType:
5742                {
5743                   float value;
5744                   GetFloat(e, &value);
5745                   FreeExpContents(exp);
5746                   exp.constant = PrintFloat(value);
5747                   exp.type = constantExp;
5748                   break;
5749                }
5750                case doubleType:
5751                {  
5752                   double value;
5753                   GetDouble(e, &value);
5754                   FreeExpContents(exp);
5755                   exp.constant = PrintDouble(value);
5756                   exp.type = constantExp;
5757                   break;
5758                }
5759             }
5760          }
5761          break;
5762       }
5763       case conditionExp:
5764       {
5765          Operand op1 { };
5766          Operand op2 { };
5767          Operand op3 { };
5768
5769          if(exp.cond.exp)
5770             // Caring only about last expression for now...
5771             ComputeExpression(exp.cond.exp->last);
5772          if(exp.cond.elseExp)
5773             ComputeExpression(exp.cond.elseExp);
5774          if(exp.cond.cond)
5775             ComputeExpression(exp.cond.cond);
5776
5777          op1 = GetOperand(exp.cond.cond);
5778          if(op1.type) op1.type.refCount++;
5779          op2 = GetOperand(exp.cond.exp->last);
5780          if(op2.type) op2.type.refCount++;
5781          op3 = GetOperand(exp.cond.elseExp);
5782          if(op3.type) op3.type.refCount++;
5783
5784          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5785          if(op1.type) FreeType(op1.type);
5786          if(op2.type) FreeType(op2.type);
5787          if(op3.type) FreeType(op3.type);
5788          break;
5789       }  
5790    }
5791 }
5792
5793 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5794 {
5795    bool result = true;
5796    if(destType)
5797    {
5798       OldList converts { };
5799       Conversion convert;
5800
5801       if(destType.kind == voidType)
5802          return false;
5803
5804       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5805          result = false;
5806       if(converts.count)
5807       {
5808          // for(convert = converts.last; convert; convert = convert.prev)
5809          for(convert = converts.first; convert; convert = convert.next)
5810          {
5811             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5812             if(!empty)
5813             {
5814                Expression newExp { };
5815                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
5816
5817                // TODO: Check this...
5818                *newExp = *exp;
5819                newExp.destType = null;
5820
5821                if(convert.isGet)
5822                {
5823                   // [exp].ColorRGB
5824                   exp.type = memberExp;
5825                   exp.addedThis = true;
5826                   exp.member.exp = newExp;
5827                   FreeType(exp.member.exp.expType);
5828
5829                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
5830                   exp.member.exp.expType.classObjectType = objectType;
5831                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
5832                   exp.member.memberType = propertyMember;
5833                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
5834                   // TESTING THIS... for (int)degrees
5835                   exp.needCast = true;
5836                   if(exp.expType) exp.expType.refCount++;
5837                   ApplyAnyObjectLogic(exp.member.exp);
5838                }
5839                else
5840                {
5841                
5842                   /*if(exp.isConstant)
5843                   {
5844                      // Color { ColorRGB = [exp] };
5845                      exp.type = instanceExp;
5846                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
5847                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
5848                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
5849                   }
5850                   else*/
5851                   {
5852                      // If not constant, don't turn it yet into an instantiation
5853                      // (Go through the deep members system first)
5854                      exp.type = memberExp;
5855                      exp.addedThis = true;
5856                      exp.member.exp = newExp;
5857
5858                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
5859                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
5860                         newExp.expType._class.registered.type == noHeadClass)
5861                      {
5862                         newExp.byReference = true;
5863                      }
5864
5865                      FreeType(exp.member.exp.expType);
5866                      /*exp.member.exp.expType = convert.convert.dataType;
5867                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
5868                      exp.member.exp.expType = null;
5869                      if(convert.convert.dataType)
5870                      {
5871                         exp.member.exp.expType = { };
5872                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
5873                         exp.member.exp.expType.refCount = 1;
5874                         exp.member.exp.expType.classObjectType = objectType;
5875                         ApplyAnyObjectLogic(exp.member.exp);
5876                      }
5877
5878                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
5879                      exp.member.memberType = reverseConversionMember;
5880                      exp.expType = convert.resultType ? convert.resultType :
5881                         MkClassType(convert.convert._class.fullName);
5882                      exp.needCast = true;
5883                      if(convert.resultType) convert.resultType.refCount++;
5884                   }
5885                }
5886             }
5887             else
5888             {
5889                FreeType(exp.expType);
5890                if(convert.isGet)
5891                {
5892                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
5893                   exp.needCast = true;
5894                   if(exp.expType) exp.expType.refCount++;
5895                }
5896                else
5897                {
5898                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
5899                   exp.needCast = true;
5900                   if(convert.resultType)
5901                      convert.resultType.refCount++;
5902                }
5903             }
5904          }
5905          if(exp.isConstant && inCompiler)
5906             ComputeExpression(exp);
5907
5908          converts.Free(FreeConvert);
5909       }
5910
5911       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
5912       {
5913          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
5914       }
5915       if(!result && exp.expType && exp.destType)
5916       {
5917          if((exp.destType.kind == classType && exp.expType.kind == pointerType && 
5918              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
5919             (exp.expType.kind == classType && exp.destType.kind == pointerType && 
5920             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
5921             result = true;
5922       }
5923    }
5924    // if(result) CheckTemplateTypes(exp);
5925    return result;
5926 }
5927
5928 void CheckTemplateTypes(Expression exp)
5929 {
5930    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
5931    {
5932       Expression newExp { };
5933       Statement compound;
5934       Context context;
5935       *newExp = *exp;
5936       if(exp.destType) exp.destType.refCount++;
5937       if(exp.expType)  exp.expType.refCount++;
5938       newExp.prev = null;
5939       newExp.next = null;
5940
5941       switch(exp.expType.kind)
5942       {
5943          case doubleType:
5944             exp.type = opExp;
5945             exp.op.exp1 = null;
5946             context = PushContext();               
5947             exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)), 
5948                MkExpExtensionCompound(compound = MkCompoundStmt(
5949                   MkListOne(MkDeclaration(MkListOne(MkSpecifier(DOUBLE)), 
5950                      MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal")), MkInitializerAssignment(newExp))))),
5951                   MkListOne(MkExpressionStmt(MkListOne(MkExpOp(null, '&', MkExpIdentifier(MkIdentifier("__internal")))))))));
5952             compound.compound.context = context;
5953             PopContext(context);
5954             exp.op.op = '*';
5955             break;
5956          default:
5957             exp.type = castExp;
5958             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
5959             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
5960             break;
5961       }
5962    }
5963    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
5964    {
5965       Expression newExp { };
5966       Statement compound;
5967       Context context;
5968       *newExp = *exp;
5969       if(exp.destType) exp.destType.refCount++;
5970       if(exp.expType)  exp.expType.refCount++;
5971       newExp.prev = null;
5972       newExp.next = null;
5973
5974       switch(exp.expType.kind)
5975       {
5976          case doubleType:
5977             exp.type = opExp;
5978             exp.op.exp1 = null;
5979             context = PushContext();               
5980             exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifier(DOUBLE)), MkDeclaratorPointer(MkPointer(null, null), null)), 
5981                MkExpExtensionCompound(compound = MkCompoundStmt(
5982                   MkListOne(MkDeclaration(MkListOne(MkSpecifierName("uint64")), 
5983                      MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal")), MkInitializerAssignment(newExp))))),
5984                   MkListOne(MkExpressionStmt(MkListOne(MkExpOp(null, '&', MkExpIdentifier(MkIdentifier("__internal")))))))));
5985             compound.compound.context = context;
5986             PopContext(context);
5987             exp.op.op = '*';
5988             ProcessExpressionType(exp.op.exp2);
5989             break;
5990          case classType:
5991          {
5992             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
5993             {
5994                exp.type = bracketsExp;
5995                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
5996                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
5997                ProcessExpressionType(exp.list->first);
5998                break;
5999             }
6000             else
6001             {
6002                exp.type = bracketsExp;
6003                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6004                newExp.needCast = true;
6005                ProcessExpressionType(exp.list->first);
6006                break;
6007             }
6008          }
6009          default:
6010          {
6011             if(exp.expType.kind == templateType)
6012             {
6013                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6014                if(type)
6015                {
6016                   FreeType(exp.destType);
6017                   FreeType(exp.expType);
6018                   delete newExp;
6019                   break;
6020                }
6021             }
6022             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6023             {
6024                exp.type = opExp;
6025                exp.op.op = '*';
6026                exp.op.exp1 = null;
6027                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6028                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6029             }
6030             else
6031             {
6032                char typeString[1024];
6033                Declarator decl;
6034                OldList * specs = MkList();
6035                typeString[0] = '\0';
6036                PrintType(exp.expType, typeString, false, false);
6037                decl = SpecDeclFromString(typeString, specs, null);
6038                
6039                exp.type = castExp;
6040                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6041                exp.cast.typeName = MkTypeName(specs, decl);
6042                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6043                exp.cast.exp.needCast = true;
6044             }
6045             break;
6046          }
6047       }
6048    }
6049 }
6050 // TODO: The Symbol tree should be reorganized by namespaces
6051 // Name Space:
6052 //    - Tree of all symbols within (stored without namespace)
6053 //    - Tree of sub-namespaces
6054
6055 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6056 {
6057    int nsLen = strlen(nameSpace);
6058    Symbol symbol;
6059    // Start at the name space prefix
6060    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6061    {
6062       char * s = symbol.string;
6063       if(!strncmp(s, nameSpace, nsLen))
6064       {
6065          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6066          int c;
6067          char * namePart;
6068          for(c = strlen(s)-1; c >= 0; c--)
6069             if(s[c] == ':')
6070                break;
6071
6072          namePart = s+c+1;
6073          if(!strcmp(namePart, name))
6074          {
6075             // TODO: Error on ambiguity
6076             return symbol;
6077          }
6078       }
6079       else
6080          break;
6081    }
6082    return null;
6083 }
6084
6085 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6086 {
6087    int c;
6088    char nameSpace[1024];
6089    char * namePart;
6090    bool gotColon = false;
6091
6092    nameSpace[0] = '\0';
6093    for(c = strlen(name)-1; c >= 0; c--)
6094       if(name[c] == ':')
6095       {
6096          gotColon = true;
6097          break;
6098       }
6099
6100    namePart = name+c+1;
6101    while(c >= 0 && name[c] == ':') c--;
6102    if(c >= 0)
6103    {
6104       // Try an exact match first
6105       Symbol symbol = (Symbol)tree.FindString(name);
6106       if(symbol)
6107          return symbol;
6108
6109       // Namespace specified
6110       memcpy(nameSpace, name, c + 1);
6111       nameSpace[c+1] = 0;
6112
6113       return ScanWithNameSpace(tree, nameSpace, namePart);
6114    }
6115    else if(gotColon)
6116    {
6117       // Looking for a global symbol, e.g. ::Sleep()
6118       Symbol symbol = (Symbol)tree.FindString(namePart);
6119       return symbol;
6120    }
6121    else
6122    {
6123       // Name only (no namespace specified)
6124       Symbol symbol = (Symbol)tree.FindString(namePart);
6125       if(symbol)
6126          return symbol;
6127       return ScanWithNameSpace(tree, "", namePart);
6128    }
6129    return null;
6130 }
6131
6132 static void ProcessDeclaration(Declaration decl);
6133
6134 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6135 {
6136 #ifdef _DEBUG
6137    //Time startTime = GetTime();
6138 #endif
6139    // Optimize this later? Do this before/less?
6140    Context ctx;
6141    Symbol symbol = null;
6142    // First, check if the identifier is declared inside the function
6143    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6144
6145    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6146    {
6147       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6148       {
6149          symbol = null;
6150          if(thisNameSpace)
6151          {
6152             char curName[1024];
6153             strcpy(curName, thisNameSpace);
6154             strcat(curName, "::");
6155             strcat(curName, name);
6156             // Try to resolve in current namespace first
6157             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6158          }
6159          if(!symbol)
6160             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6161       }
6162       else
6163          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6164
6165       if(symbol || ctx == endContext) break;
6166    }
6167    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6168    {
6169       if(symbol.pointerExternal.type == functionExternal)
6170       {
6171          FunctionDefinition function = symbol.pointerExternal.function;
6172
6173          // Modified this recently...
6174          Context tmpContext = curContext;
6175          curContext = null;         
6176          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6177          curContext = tmpContext;
6178
6179          symbol.pointerExternal.symbol = symbol;
6180
6181          // TESTING THIS:
6182          DeclareType(symbol.type, true, true);
6183
6184          ast->Insert(curExternal.prev, symbol.pointerExternal);
6185
6186          symbol.id = curExternal.symbol.idCode;
6187
6188       }
6189       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6190       {
6191          ast->Move(symbol.pointerExternal, curExternal.prev);
6192          symbol.id = curExternal.symbol.idCode;
6193       }
6194    }
6195 #ifdef _DEBUG
6196    //findSymbolTotalTime += GetTime() - startTime;
6197 #endif
6198    return symbol;
6199 }
6200
6201 static void GetTypeSpecs(Type type, OldList * specs)
6202 {
6203    if(!type.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
6204    switch(type.kind)
6205    {
6206       case classType: 
6207       {
6208          if(type._class.registered)
6209          {
6210             if(!type._class.registered.dataType)
6211                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6212             GetTypeSpecs(type._class.registered.dataType, specs);
6213          }
6214          break;
6215       }
6216       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6217       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6218       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6219       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6220       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6221       case intType: 
6222       default:
6223          ListAdd(specs, MkSpecifier(INT)); break;
6224    }
6225 }
6226
6227 // WARNING : This function expects a null terminated string since it recursively concatenate...
6228 static void _PrintType(Type type, char * string, bool printName, bool printFunction, bool fullName)
6229 {
6230    if(type)
6231    {
6232       switch(type.kind)
6233       {
6234          case classType:
6235             if(type._class && type._class.string)
6236             {
6237                // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6238                //       look into merging with thisclass ?
6239                if(type.classObjectType == typedObject)
6240                   strcat(string, "typed_object");
6241                else if(fullName)
6242                   strcat(string, type._class.string);
6243                else
6244                {
6245                   if(type._class.registered)
6246                      strcat(string, type._class.registered.name);
6247                   else
6248                      strcat(string, type._class.string);                     
6249                }
6250             }
6251             break;
6252          case pointerType:
6253          {
6254             /*Type funcType;
6255             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6256             if(funcType && funcType.kind == functionType)
6257             {
6258                Type param;
6259                PrintType(funcType.returnType, string, false, fullName);
6260                strcat(string, "(*");
6261                if(printName || funcType.thisClass)
6262                {
6263                   strcat(string, " ");
6264                   if(funcType.thisClass)
6265                   {
6266                      strcat(string, funcType.thisClass.string);
6267                      strcat(string, "::");
6268                   }
6269                   if(type.name)
6270                      strcat(string, type.name);
6271                }
6272                strcat(string, ")(");
6273                for(param = funcType.params.first; param; param = param.next)
6274                {
6275                   PrintType(param, string, false, fullName);
6276                   if(param.next) strcat(string, ", ");
6277                }
6278                strcat(string, ")");               
6279             }
6280             else*/
6281             {
6282                _PrintType(type.type, string, false /*printName*/, printFunction, fullName);
6283                strcat(string, " *");
6284             }
6285             break;
6286          }
6287          case voidType: strcat(string, "void"); break;
6288          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6289          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6290          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6291          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6292          case floatType: strcat(string, "float"); break;
6293          case doubleType: strcat(string, "double"); break;
6294          case structType:
6295             if(type.enumName)
6296             {
6297                strcat(string, "struct ");
6298                strcat(string, type.enumName);
6299             }
6300             else if(type.typeName)
6301             {
6302                strcat(string, type.typeName);
6303             }
6304             else
6305             {
6306                /*
6307                strcat(string, "struct ");
6308                strcat(string,"(unnamed)");
6309                */
6310                Type member;
6311                strcat(string, "struct {");
6312                for(member = type.members.first; member; member = member.next)
6313                {
6314                   PrintType(member, string, true, fullName);
6315                   strcat(string,"; ");
6316                }
6317                strcat(string,"}");
6318             }
6319             break;
6320          case unionType:
6321             if(type.enumName)
6322             {
6323                strcat(string, "union ");
6324                strcat(string, type.enumName);
6325             }
6326             else if(type.typeName)
6327             {
6328                strcat(string, type.typeName);
6329             }
6330             else
6331             {
6332                strcat(string, "union ");
6333                strcat(string,"(unnamed)");
6334             }
6335             break;
6336          case enumType:
6337             if(type.enumName)
6338             {
6339                strcat(string, "enum ");
6340                strcat(string, type.enumName);
6341             }
6342             else if(type.typeName)
6343             {
6344                strcat(string, type.typeName);
6345             }
6346             else
6347                strcat(string, "enum");
6348             break;
6349          case functionType:
6350          {
6351             if(printFunction)
6352             {
6353                if(type.dllExport)
6354                   strcat(string, "dllexport ");
6355                PrintType(type.returnType, string, false, fullName);
6356                strcat(string, " ");
6357             }
6358             
6359             // DANGER: Testing This
6360             if(printName)
6361             {
6362                if(type.name)
6363                {
6364                   if(fullName)
6365                      strcat(string, type.name);
6366                   else
6367                   {
6368                      char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6369                      if(name) name += 2; else name = type.name;
6370                      strcat(string, name);
6371                   }
6372                }
6373 #ifdef _DEBUG
6374                else
6375                {
6376                   printf("");
6377                }
6378 #endif
6379             }
6380
6381             if(printFunction)
6382             {
6383                Type param;
6384                strcat(string, "(");
6385                for(param = type.params.first; param; param = param.next)
6386                {
6387                   PrintType(param, string, true, fullName);
6388                   if(param.next) strcat(string, ", ");
6389                }
6390                strcat(string, ")");
6391             }
6392             break;
6393          }
6394          case arrayType:
6395          {
6396             /*Type funcType;
6397             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6398             if(funcType && funcType.kind == functionType)
6399             {
6400                Type param;
6401                PrintType(funcType.returnType, string, false, fullName);
6402                strcat(string, "(*");
6403                if(printName || funcType.thisClass)
6404                {
6405                   strcat(string, " ");
6406                   if(funcType.thisClass)
6407                   {
6408                      strcat(string, funcType.thisClass.string);
6409                      strcat(string, "::");
6410                   }
6411                   if(type.name)
6412                      strcat(string, type.name);
6413                }
6414                strcat(string, ")(");
6415                for(param = funcType.params.first; param; param = param.next)
6416                {
6417                   PrintType(param, string, false, fullName);
6418                   if(param.next) strcat(string, ", ");
6419                }
6420                strcat(string, ")");               
6421             }
6422             else*/
6423             {
6424                char baseType[1024], size[256];
6425                Type arrayType = type;
6426                baseType[0] = '\0';
6427                size[0] = '\0';
6428
6429                while(arrayType.kind == TypeKind::arrayType)
6430                {
6431                   strcat(size, "[");
6432                   if(arrayType.enumClass)
6433                      strcat(size, arrayType.enumClass.string);
6434                   else if(arrayType.arraySizeExp)
6435                      PrintExpression(arrayType.arraySizeExp, size);
6436                   //sprintf(string, "%s[%s]", baseType, size); 
6437                   strcat(size, "]");
6438
6439                   arrayType = arrayType.arrayType;
6440                }
6441                _PrintType(arrayType, baseType, printName, printFunction, fullName);
6442                strcat(string, baseType);
6443                strcat(string, size);
6444             }
6445
6446             /*
6447                PrintType(type.arrayType, baseType, printName, fullName);
6448                if(type.enumClass)
6449                   strcpy(size, type.enumClass.string);
6450                else if(type.arraySizeExp)
6451                   PrintExpression(type.arraySizeExp, size);
6452                //sprintf(string, "%s[%s]", baseType, size); 
6453                strcat(string, baseType);
6454                strcat(string, "[");
6455                strcat(string, size); 
6456                strcat(string, "]");
6457                */
6458
6459             printName = false;
6460             break;
6461          }
6462          case ellipsisType:
6463             strcat(string, "...");
6464             break;
6465          case methodType:
6466             _PrintType(type.method.dataType, string, false, printFunction, fullName);
6467             break;
6468          case subClassType:
6469             strcat(string, "subclass(");
6470             strcat(string, type._class ? type._class.string : "int");
6471             strcat(string, ")");                  
6472             break;
6473          case templateType:
6474             strcat(string, type.templateParameter.identifier.string);
6475             break;
6476          case thisClassType:
6477             strcat(string, "thisclass");
6478             break;
6479          case vaListType:
6480          strcat(string, "__builtin_va_list");
6481             break;
6482 #ifdef _DEBUG
6483          default:
6484             printf("");
6485 #endif
6486       }
6487       if(type.name && printName && type.kind != functionType && (type.kind != pointerType || type.type.kind != functionType))
6488       {
6489          strcat(string, " ");
6490          strcat(string, type.name);
6491       }
6492    }
6493 }
6494
6495 void PrintType(Type type, char * string, bool printName, bool fullName)
6496 {
6497    Type funcType;
6498    for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6499    if(funcType && funcType.kind == functionType && type != funcType)
6500    {
6501       char typeString[1024];
6502       Type param;
6503
6504       PrintType(funcType.returnType, string, false, fullName);
6505       strcat(string, "(");
6506       _PrintType(type, string, printName, false, fullName);
6507       strcat(string, ")");
6508       /*
6509       if(type.name)
6510          strcat(string, type.name);
6511       else
6512       {
6513          printf("");
6514       }
6515       */
6516       strcat(string, "(");
6517       for(param = funcType.params.first; param; param = param.next)
6518       {
6519          PrintType(param, string, true, fullName);
6520          if(param.next) strcat(string, ", ");
6521       }
6522       strcat(string, ")");
6523    }
6524    else
6525       _PrintType(type, string, printName, true, fullName);
6526    if(type.bitFieldCount)
6527    {
6528       char count[100];
6529       sprintf(count, ":%d", type.bitFieldCount);
6530       strcat(string, count);
6531    }
6532 }
6533
6534 static Type FindMember(Type type, char * string)
6535 {
6536    Type memberType;
6537    for(memberType = type.members.first; memberType; memberType = memberType.next)
6538    {
6539       if(!memberType.name)
6540       {
6541          Type subType = FindMember(memberType, string);
6542          if(subType)
6543             return subType;
6544       }
6545       else if(!strcmp(memberType.name, string))
6546          return memberType;
6547    }
6548    return null;
6549 }
6550
6551 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6552 {
6553    Type memberType;
6554    for(memberType = type.members.first; memberType; memberType = memberType.next)
6555    {
6556       if(!memberType.name)
6557       {
6558          Type subType = FindMember(memberType, string);
6559          if(subType)
6560          {
6561             *offset += memberType.offset;
6562             return subType;
6563          }
6564       }
6565       else if(!strcmp(memberType.name, string))
6566       {
6567          *offset += memberType.offset;
6568          return memberType;
6569       }
6570    }
6571    return null;
6572 }
6573
6574 Expression ParseExpressionString(char * expression)
6575 {
6576    fileInput = TempFile { };
6577    fileInput.Write(expression, 1, strlen(expression));
6578    fileInput.Seek(0, start);
6579
6580    echoOn = false;
6581    parsedExpression = null;
6582    resetScanner();
6583    expression_yyparse();
6584    delete fileInput;
6585
6586    return parsedExpression;
6587 }
6588
6589 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6590 {
6591    Identifier id = exp.identifier;
6592    Method method = null;
6593    Property prop = null;
6594    DataMember member = null;
6595    ClassProperty classProp = null;
6596
6597    if(_class && _class.type == enumClass)
6598    {
6599       NamedLink value = null;
6600       Class enumClass = eSystem_FindClass(privateModule, "enum");
6601       if(enumClass)
6602       {
6603          Class baseClass;
6604          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6605          {
6606             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6607             for(value = e.values.first; value; value = value.next)
6608             {
6609                if(!strcmp(value.name, id.string))
6610                   break;
6611             }
6612             if(value)
6613             {
6614                char constant[256];
6615
6616                FreeExpContents(exp);
6617
6618                exp.type = constantExp;
6619                exp.isConstant = true;
6620                if(!strcmp(baseClass.dataTypeString, "int"))
6621                   sprintf(constant, "%d",value.data);
6622                else
6623                   sprintf(constant, "0x%X",value.data);
6624                exp.constant = CopyString(constant);
6625                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6626                exp.expType = MkClassType(baseClass.fullName);
6627                break;
6628             }
6629          }
6630       }
6631       if(value)
6632          return true;
6633    }
6634    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6635    {
6636       ProcessMethodType(method);
6637       exp.expType = Type
6638       {
6639          refCount = 1;
6640          kind = methodType;
6641          method = method;
6642          // Crash here?
6643          // TOCHECK: Put it back to what it was...
6644          // methodClass = _class;
6645          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6646       };
6647       //id._class = null;
6648       return true;
6649    }
6650    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6651    {
6652       if(!prop.dataType)
6653          ProcessPropertyType(prop);
6654       exp.expType = prop.dataType;
6655       if(prop.dataType) prop.dataType.refCount++;
6656       return true;
6657    }
6658    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6659    {
6660       if(!member.dataType)
6661          member.dataType = ProcessTypeString(member.dataTypeString, false);
6662       exp.expType = member.dataType;
6663       if(member.dataType) member.dataType.refCount++;
6664       return true;
6665    }
6666    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6667    {
6668       if(!classProp.dataType)
6669          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6670
6671       if(classProp.constant)
6672       {
6673          FreeExpContents(exp);
6674
6675          exp.isConstant = true;
6676          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6677          {
6678             //char constant[256];
6679             exp.type = stringExp;
6680             exp.constant = QMkString((char *)classProp.Get(_class));
6681          }
6682          else
6683          {
6684             char constant[256];
6685             exp.type = constantExp;
6686             sprintf(constant, "%d",classProp.Get(_class));
6687             exp.constant = CopyString(constant);
6688          }
6689       }
6690       else
6691       {
6692          // TO IMPLEMENT...
6693       }
6694
6695       exp.expType = classProp.dataType;
6696       if(classProp.dataType) classProp.dataType.refCount++;
6697       return true;
6698    }
6699    return false;
6700 }
6701
6702 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6703 {
6704    BinaryTree * tree = &nameSpace.functions;
6705    GlobalData data = (GlobalData)tree->FindString(name);
6706    NameSpace * child;
6707    if(!data)
6708    {
6709       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6710       {
6711          data = ScanGlobalData(child, name);
6712          if(data)
6713             break;
6714       }
6715    }
6716    return data;
6717 }
6718
6719 static GlobalData FindGlobalData(char * name)
6720 {
6721    int start = 0, c;
6722    NameSpace * nameSpace;
6723    nameSpace = globalData;
6724    for(c = 0; name[c]; c++)
6725    {
6726       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6727       {
6728          NameSpace * newSpace;
6729          char * spaceName = new char[c - start + 1];
6730          strncpy(spaceName, name + start, c - start);
6731          spaceName[c-start] = '\0';
6732          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6733          delete spaceName;
6734          if(!newSpace)
6735             return null;
6736          nameSpace = newSpace;
6737          if(name[c] == ':') c++;
6738          start = c+1;
6739       }
6740    }
6741    if(c - start)
6742    {
6743       return ScanGlobalData(nameSpace, name + start);
6744    }
6745    return null;
6746 }
6747
6748 static int definedExpStackPos;
6749 static void * definedExpStack[512];
6750
6751 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6752 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6753 {
6754    Expression prev = checkedExp.prev, next = checkedExp.next;
6755
6756    FreeExpContents(checkedExp);
6757    FreeType(checkedExp.expType);
6758    FreeType(checkedExp.destType);
6759
6760    *checkedExp = *newExp;
6761
6762    delete newExp;
6763
6764    checkedExp.prev = prev;
6765    checkedExp.next = next;
6766 }
6767
6768 void ApplyAnyObjectLogic(Expression e)
6769 {
6770    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6771    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6772    {
6773       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6774       //ellipsisDestType = destType;
6775       if(e && e.expType)
6776       {
6777          Type type = e.expType;
6778          Class _class = null;
6779          //Type destType = e.destType;
6780
6781          if(type.kind == classType && type._class && type._class.registered)
6782          {
6783             _class = type._class.registered;
6784          }
6785          else if(type.kind == subClassType)
6786          {
6787             _class = FindClass("ecere::com::Class").registered;
6788          }
6789          else
6790          {
6791             char string[1024] = "";
6792             Symbol classSym;
6793
6794             PrintType(type, string, false, true);
6795             classSym = FindClass(string);
6796             if(classSym) _class = classSym.registered;
6797          }
6798
6799          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "ecere::com::Class")) || // Patched so that class isn't considered SYSTEM...
6800             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
6801             destType.byReference)))
6802          {
6803             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6804             {
6805                Expression checkedExp = e, newExp;
6806
6807                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6808                {
6809                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6810                   {
6811                      if(checkedExp.type == extensionCompoundExp)
6812                      {
6813                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6814                      }
6815                      else
6816                         checkedExp = checkedExp.list->last;
6817                   }
6818                   else if(checkedExp.type == castExp)
6819                      checkedExp = checkedExp.cast.exp;
6820                }
6821
6822                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6823                {
6824                   newExp = checkedExp.op.exp2;
6825                   checkedExp.op.exp2 = null;
6826                   FreeExpContents(checkedExp);
6827                   
6828                   if(e.expType && e.expType.passAsTemplate)
6829                   {
6830                      char size[100];
6831                      ComputeTypeSize(e.expType);
6832                      sprintf(size, "%d", e.expType.size);
6833                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
6834                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
6835                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
6836                   }
6837
6838                   ReplaceExpContents(checkedExp, newExp);
6839                   e.byReference = true;
6840                }
6841                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
6842                {
6843                   Expression checkedExp, newExp;
6844
6845                   {
6846                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
6847                      bool hasAddress =
6848                         e.type == identifierExp ||
6849                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
6850                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
6851                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
6852                         e.type == indexExp;
6853
6854                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
6855                      {
6856                         Context context = PushContext();
6857                         Declarator decl;
6858                         OldList * specs = MkList();
6859                         char typeString[1024];
6860                         Expression newExp { };
6861
6862                         typeString[0] = '\0';
6863                         *newExp = *e;
6864
6865                         //if(e.destType) e.destType.refCount++;
6866                         // if(exp.expType) exp.expType.refCount++;
6867                         newExp.prev = null;
6868                         newExp.next = null;
6869                         newExp.expType = null;
6870
6871                         PrintType(e.expType, typeString, false, true);
6872                         decl = SpecDeclFromString(typeString, specs, null);
6873                         newExp.destType = ProcessType(specs, decl);
6874
6875                         curContext = context;
6876                         e.type = extensionCompoundExp;
6877                         e.compound = MkCompoundStmt(
6878                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
6879                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))), 
6880
6881                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
6882                         
6883                         {
6884                            Type type = e.destType;
6885                            e.destType = { };
6886                            CopyTypeInto(e.destType, type);
6887                            e.destType.refCount = 1;
6888                            e.destType.classObjectType = none;
6889                            FreeType(type);
6890                         }
6891
6892                         e.compound.compound.context = context;
6893                         PopContext(context);
6894                         curContext = context.parent;
6895                      }
6896                   }
6897
6898                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
6899                   checkedExp = e;
6900                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6901                   {
6902                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6903                      {
6904                         if(checkedExp.type == extensionCompoundExp)
6905                         {
6906                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6907                         }
6908                         else
6909                            checkedExp = checkedExp.list->last;
6910                      }
6911                      else if(checkedExp.type == castExp)
6912                         checkedExp = checkedExp.cast.exp;
6913                   }
6914                   {
6915                      Expression operand { };
6916                      operand = *checkedExp;
6917                      checkedExp.destType = null;
6918                      checkedExp.expType = null;
6919                      checkedExp.Clear();
6920                      checkedExp.type = opExp;
6921                      checkedExp.op.op = '&';
6922                      checkedExp.op.exp1 = null;
6923                      checkedExp.op.exp2 = operand;
6924
6925                      //newExp = MkExpOp(null, '&', checkedExp);
6926                   }
6927                   //ReplaceExpContents(checkedExp, newExp);
6928                }
6929             }
6930          }
6931       }
6932    }
6933    {
6934       // If expression type is a simple class, make it an address
6935       // FixReference(e, true);
6936    }
6937 //#if 0
6938    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
6939       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
6940          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
6941    {
6942       if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
6943       {
6944          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
6945       }
6946       else
6947       {
6948          Expression thisExp { };
6949
6950          *thisExp = *e;
6951          thisExp.prev = null;
6952          thisExp.next = null;
6953          e.Clear();
6954
6955          e.type = bracketsExp;
6956          e.list = MkListOne(MkExpOp(null, '*', MkExpBrackets(MkListOne(thisExp))));
6957          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
6958             ((Expression)e.list->first).byReference = true;
6959
6960          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
6961          {
6962             e.expType = thisExp.expType;
6963             e.expType.refCount++;
6964          }
6965          else*/
6966          {
6967             e.expType = { };
6968             CopyTypeInto(e.expType, thisExp.expType);
6969             e.expType.byReference = false;
6970             e.expType.refCount = 1;
6971
6972             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
6973                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
6974             {
6975                e.expType.classObjectType = none;
6976             }
6977          }
6978       }
6979    }
6980 // TOFIX: Try this for a nice IDE crash!
6981 //#endif
6982    // The other way around
6983    else 
6984 //#endif
6985    if(destType && e.expType && 
6986          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
6987          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
6988          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
6989    {
6990       if(destType.kind == ellipsisType)
6991       {
6992          Compiler_Error($"Unspecified type\n");
6993       }
6994       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
6995       {
6996          bool byReference = e.expType.byReference;
6997          Expression thisExp { };
6998          Declarator decl;
6999          OldList * specs = MkList();
7000          char typeString[1024]; // Watch buffer overruns
7001          Type type;
7002          ClassObjectType backupClassObjectType;
7003
7004          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7005             type = e.expType;
7006          else
7007             type = destType;            
7008
7009          backupClassObjectType = type.classObjectType;
7010
7011          type.classObjectType = none;
7012
7013          typeString[0] = '\0';
7014          PrintType(type, typeString, false, true);
7015          decl = SpecDeclFromString(typeString, specs, null);
7016
7017          type.classObjectType = backupClassObjectType;
7018
7019          *thisExp = *e;
7020          thisExp.prev = null;
7021          thisExp.next = null;
7022          e.Clear();
7023
7024          if( ( type.kind == classType && type._class && type._class.registered && strcmp(type._class.registered.fullName, "ecere::com::Instance") &&
7025                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass || 
7026                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7027              (type.kind != pointerType && type.kind != arrayType && type.kind != classType) ||
7028              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7029          {
7030             e.type = opExp;
7031             e.op.op = '*';
7032             e.op.exp1 = null;
7033             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7034          }
7035          else
7036          {
7037             e.type = castExp;
7038             e.cast.typeName = MkTypeName(specs, decl);
7039             e.cast.exp = thisExp;
7040             e.byReference = true;
7041          }
7042          e.expType = type;
7043          e.destType = destType;
7044          type.refCount++;
7045          destType.refCount++;
7046       }
7047    }
7048 }
7049
7050 void ProcessExpressionType(Expression exp)
7051 {
7052    bool unresolved = false;
7053    Location oldyylloc = yylloc;
7054    bool notByReference = false;
7055 #ifdef _DEBUG   
7056    char debugExpString[4096];
7057    debugExpString[0] = '\0';
7058    PrintExpression(exp, debugExpString);
7059 #endif
7060    if(!exp || exp.expType) 
7061       return;
7062
7063    //eSystem_Logf("%s\n", expString);
7064    
7065    // Testing this here
7066    yylloc = exp.loc;
7067    switch(exp.type)
7068    {
7069       case identifierExp:
7070       {
7071          Identifier id = exp.identifier;
7072          if(!id) return;
7073
7074          // DOING THIS LATER NOW...
7075          if(id._class && id._class.name)
7076          {
7077             id.classSym = id._class.symbol; // FindClass(id._class.name);
7078             /* TODO: Name Space Fix ups
7079             if(!id.classSym)
7080                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7081             */
7082          }
7083
7084          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7085          {
7086             exp.expType = ProcessTypeString("Module", true);
7087             break;
7088          }
7089          else */if(strstr(id.string, "__ecereClass") == id.string)
7090          {
7091             exp.expType = ProcessTypeString("ecere::com::Class", true);
7092             break;
7093          }
7094          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7095          {
7096             // Added this here as well
7097             ReplaceClassMembers(exp, thisClass);
7098             if(exp.type != identifierExp)
7099             {
7100                ProcessExpressionType(exp);
7101                break;
7102             }
7103
7104             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7105                break;
7106          }
7107          else
7108          {
7109             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7110             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7111             if(!symbol/* && exp.destType*/)
7112             {
7113                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7114                   break;
7115                else
7116                {
7117                   if(thisClass)
7118                   {
7119                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7120                      if(exp.type != identifierExp)
7121                      {
7122                         ProcessExpressionType(exp);
7123                         break;
7124                      }
7125                   }
7126                   // Static methods called from inside the _class
7127                   else if(currentClass && !id._class)
7128                   {
7129                      if(ResolveIdWithClass(exp, currentClass, true))
7130                         break;
7131                   }
7132                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7133                }
7134             }
7135
7136             // If we manage to resolve this symbol
7137             if(symbol)
7138             {
7139                Type type = symbol.type;
7140                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7141
7142                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7143                {
7144                   Context context = SetupTemplatesContext(_class);
7145                   type = ReplaceThisClassType(_class);
7146                   FinishTemplatesContext(context);
7147                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7148                }
7149
7150                FreeSpecifier(id._class);
7151                id._class = null;
7152                delete id.string;
7153                id.string = CopyString(symbol.string);
7154
7155                id.classSym = null;
7156                exp.expType = type;
7157                if(type)
7158                   type.refCount++;
7159                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7160                   // Add missing cases here... enum Classes...
7161                   exp.isConstant = true;
7162
7163                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7164                if(symbol.isParam || !strcmp(id.string, "this"))
7165                {
7166                   if(_class && _class.type == structClass)
7167                      exp.byReference = true;
7168                   
7169                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7170                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) && 
7171                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) || 
7172                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7173                   {
7174                      Identifier id = exp.identifier;
7175                      exp.type = bracketsExp;
7176                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7177                   }*/
7178                }
7179
7180                if(symbol.isIterator)
7181                {
7182                   if(symbol.isIterator == 3)
7183                   {
7184                      exp.type = bracketsExp;
7185                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7186                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7187                      exp.expType = null;
7188                      ProcessExpressionType(exp);                     
7189                   }
7190                   else if(symbol.isIterator != 4)
7191                   {
7192                      exp.type = memberExp;
7193                      exp.member.exp = MkExpIdentifier(exp.identifier);
7194                      exp.member.exp.expType = exp.expType;
7195                      /*if(symbol.isIterator == 6)
7196                         exp.member.member = MkIdentifier("key");
7197                      else*/
7198                         exp.member.member = MkIdentifier("data");
7199                      exp.expType = null;
7200                      ProcessExpressionType(exp);
7201                   }
7202                }
7203                break;
7204             }
7205             else
7206             {
7207                DefinedExpression definedExp = null;
7208                if(thisNameSpace && !(id._class && !id._class.name))
7209                {
7210                   char name[1024];
7211                   strcpy(name, thisNameSpace);
7212                   strcat(name, "::");
7213                   strcat(name, id.string);
7214                   definedExp = eSystem_FindDefine(privateModule, name);
7215                }
7216                if(!definedExp)
7217                   definedExp = eSystem_FindDefine(privateModule, id.string);
7218                if(definedExp)
7219                {
7220                   int c;
7221                   for(c = 0; c<definedExpStackPos; c++)
7222                      if(definedExpStack[c] == definedExp)
7223                         break;
7224                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7225                   {
7226                      Location backupYylloc = yylloc;
7227                      definedExpStack[definedExpStackPos++] = definedExp;
7228                      fileInput = TempFile { };
7229                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7230                      fileInput.Seek(0, start);
7231
7232                      echoOn = false;
7233                      parsedExpression = null;
7234                      resetScanner();
7235                      expression_yyparse();
7236                      delete fileInput;
7237
7238                      yylloc = backupYylloc;
7239
7240                      if(parsedExpression)
7241                      {
7242                         FreeIdentifier(id);
7243                         exp.type = bracketsExp;
7244                         exp.list = MkListOne(parsedExpression);
7245                         parsedExpression.loc = yylloc;
7246                         ProcessExpressionType(exp);
7247                         definedExpStackPos--;
7248                         return;
7249                      }
7250                      definedExpStackPos--;
7251                   }
7252                   else
7253                   {
7254                      if(inCompiler)
7255                      {
7256                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7257                      }
7258                   }
7259                }
7260                else
7261                {
7262                   GlobalData data = null;
7263                   if(thisNameSpace && !(id._class && !id._class.name))
7264                   {
7265                      char name[1024];
7266                      strcpy(name, thisNameSpace);
7267                      strcat(name, "::");
7268                      strcat(name, id.string);
7269                      data = FindGlobalData(name);
7270                   }
7271                   if(!data)
7272                      data = FindGlobalData(id.string);
7273                   if(data)
7274                   {
7275                      DeclareGlobalData(data);
7276                      exp.expType = data.dataType;
7277                      if(data.dataType) data.dataType.refCount++;
7278
7279                      delete id.string;
7280                      id.string = CopyString(data.fullName);
7281                      FreeSpecifier(id._class);
7282                      id._class = null;
7283
7284                      break;
7285                   }
7286                   else
7287                   {
7288                      GlobalFunction function = null;
7289                      if(thisNameSpace && !(id._class && !id._class.name))
7290                      {
7291                         char name[1024];
7292                         strcpy(name, thisNameSpace);
7293                         strcat(name, "::");
7294                         strcat(name, id.string);
7295                         function = eSystem_FindFunction(privateModule, name);
7296                      }
7297                      if(!function)
7298                         function = eSystem_FindFunction(privateModule, id.string);
7299                      if(function)
7300                      {
7301                         char name[1024];
7302                         delete id.string;
7303                         id.string = CopyString(function.name);
7304                         name[0] = 0;
7305
7306                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7307                            strcpy(name, "__ecereFunction_");
7308                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7309                         if(DeclareFunction(function, name))
7310                         {
7311                            delete id.string;
7312                            id.string = CopyString(name);
7313                         }
7314                         exp.expType = function.dataType;
7315                         if(function.dataType) function.dataType.refCount++;
7316
7317                         FreeSpecifier(id._class);
7318                         id._class = null;
7319
7320                         break;
7321                      }
7322                   }
7323                }
7324             }
7325          }
7326          unresolved = true;
7327          break;
7328       }
7329       case instanceExp:
7330       {
7331          Class _class;
7332          // Symbol classSym;
7333
7334          if(!exp.instance._class)
7335          {
7336             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7337             {
7338                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7339             }
7340 #ifdef _DEBUG
7341             else 
7342             {
7343                printf("");               
7344             }
7345 #endif
7346          }
7347
7348          //classSym = FindClass(exp.instance._class.fullName);
7349          //_class = classSym ? classSym.registered : null;
7350
7351          ProcessInstantiationType(exp.instance);
7352          exp.isConstant = exp.instance.isConstant;
7353
7354          /*
7355          if(_class.type == unitClass && _class.base.type != systemClass)
7356          {
7357             {
7358                Type destType = exp.destType;
7359
7360                exp.destType = MkClassType(_class.base.fullName);
7361                exp.expType = MkClassType(_class.fullName);
7362                CheckExpressionType(exp, exp.destType, true);
7363
7364                exp.destType = destType;
7365             }
7366             exp.expType = MkClassType(_class.fullName);
7367          }
7368          else*/
7369          if(exp.instance._class)
7370          {
7371             exp.expType = MkClassType(exp.instance._class.name);
7372             /*if(exp.expType._class && exp.expType._class.registered && 
7373                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7374                exp.expType.byReference = true;*/
7375          }         
7376          break;
7377       }
7378       case constantExp:
7379       {
7380          if(!exp.expType)
7381          {
7382             Type type
7383             {
7384                refCount = 1;
7385                constant = true;
7386             };
7387             exp.expType = type;
7388
7389             if(exp.constant[0] == '\'')
7390             {
7391                if((int)((byte *)exp.constant)[1] > 127)
7392                {
7393                   int nb;
7394                   unichar ch = UTF8GetChar(exp.constant + 1, &nb);
7395                   if(nb < 2) ch = exp.constant[1];
7396                   delete exp.constant;
7397                   exp.constant = PrintUInt(ch);
7398                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7399                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7400                   type._class = FindClass("unichar");
7401
7402                   type.isSigned = false;
7403                }
7404                else
7405                {
7406                   type.kind = charType;
7407                   type.isSigned = true;
7408                }
7409             }
7410             else if(strchr(exp.constant, '.'))
7411             {
7412                char ch = exp.constant[strlen(exp.constant)-1];
7413                if(ch == 'f')
7414                   type.kind = floatType;
7415                else
7416                   type.kind = doubleType;
7417                type.isSigned = true;
7418             }
7419             else
7420             {
7421                if(exp.constant[0] == '0' && exp.constant[1])
7422                   type.isSigned = false;
7423                else if(strchr(exp.constant, 'L') || strchr(exp.constant, 'l'))
7424                   type.isSigned = false;
7425                else if(strtoll(exp.constant, null, 0) > MAXINT)
7426                   type.isSigned = false;
7427                else
7428                   type.isSigned = true;
7429                type.kind = intType;
7430             }
7431             exp.isConstant = true;
7432          }
7433          break;
7434       }
7435       case stringExp:
7436       {
7437          exp.isConstant = true;      // Why wasn't this constant?
7438          exp.expType = Type
7439          {
7440             refCount = 1;
7441             kind = pointerType;
7442             type = Type
7443             {
7444                refCount = 1;
7445                kind = charType;
7446                constant = true;
7447             }
7448          };
7449          break;
7450       }
7451       case newExp:
7452       case new0Exp:
7453          ProcessExpressionType(exp._new.size);
7454          exp.expType = Type
7455          {
7456             refCount = 1;
7457             kind = pointerType;
7458             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7459          };
7460          DeclareType(exp.expType.type, false, false);
7461          break;
7462       case renewExp:
7463       case renew0Exp:
7464          ProcessExpressionType(exp._renew.size);
7465          ProcessExpressionType(exp._renew.exp);
7466          exp.expType = Type
7467          {
7468             refCount = 1;
7469             kind = pointerType;
7470             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7471          };
7472          DeclareType(exp.expType.type, false, false);
7473          break;
7474       case opExp:
7475       {
7476          bool assign = false, boolResult = false, boolOps = false;
7477          Type type1 = null, type2 = null;
7478          bool useDestType = false, useSideType = false;
7479          Location oldyylloc = yylloc;
7480          bool useSideUnit = false;
7481
7482          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7483          Type dummy
7484          {
7485             count = 1;
7486             refCount = 1;
7487          };
7488
7489          switch(exp.op.op)
7490          {
7491             // Assignment Operators
7492             case '=': 
7493             case MUL_ASSIGN:
7494             case DIV_ASSIGN:
7495             case MOD_ASSIGN:
7496             case ADD_ASSIGN:
7497             case SUB_ASSIGN:
7498             case LEFT_ASSIGN:
7499             case RIGHT_ASSIGN:
7500             case AND_ASSIGN:
7501             case XOR_ASSIGN:
7502             case OR_ASSIGN:
7503                assign = true;
7504                break;
7505             // boolean Operators
7506             case '!':
7507                // Expect boolean operators
7508                //boolOps = true;
7509                //boolResult = true;
7510                break;
7511             case AND_OP:
7512             case OR_OP:
7513                // Expect boolean operands
7514                boolOps = true;
7515                boolResult = true;
7516                break;
7517             // Comparisons
7518             case EQ_OP:
7519             case '<':
7520             case '>':
7521             case LE_OP:
7522             case GE_OP:
7523             case NE_OP:
7524                // Gives boolean result
7525                boolResult = true;
7526                useSideType = true;
7527                break;
7528             case '+':
7529             case '-':
7530                useSideUnit = true;
7531
7532                // Just added these... testing
7533             case '|':
7534             case '&':
7535             case '^':
7536
7537             // DANGER: Verify units
7538             case '/':
7539             case '%':
7540             case '*':
7541                
7542                if(exp.op.op != '*' || exp.op.exp1)
7543                {
7544                   useSideType = true;
7545                   useDestType = true;
7546                }
7547                break;
7548
7549             /*// Implement speed etc.
7550             case '*':
7551             case '/':
7552                break;
7553             */
7554          }
7555          if(exp.op.op == '&')
7556          {
7557             // Added this here earlier for Iterator address as key
7558             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7559             {
7560                Identifier id = exp.op.exp2.identifier;
7561                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7562                if(symbol && symbol.isIterator == 2)
7563                {
7564                   exp.type = memberExp;
7565                   exp.member.exp = exp.op.exp2;
7566                   exp.member.member = MkIdentifier("key");
7567                   exp.expType = null;
7568                   exp.op.exp2.expType = symbol.type;
7569                   symbol.type.refCount++;
7570                   ProcessExpressionType(exp);
7571                   FreeType(dummy);
7572                   break;
7573                }
7574                // exp.op.exp2.usage.usageRef = true;
7575             }
7576          }
7577
7578          //dummy.kind = TypeDummy;
7579
7580          if(exp.op.exp1)
7581          {
7582             if(exp.destType && exp.destType.kind == classType &&
7583                exp.destType._class && exp.destType._class.registered && useDestType &&
7584                
7585               ((exp.destType._class.registered.type == unitClass && useSideUnit) || 
7586                exp.destType._class.registered.type == enumClass ||
7587                exp.destType._class.registered.type == bitClass
7588                )) 
7589
7590               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7591             {
7592                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7593                exp.op.exp1.destType = exp.destType;
7594                if(exp.destType)
7595                   exp.destType.refCount++;
7596             }
7597             else if(!assign)
7598             {
7599                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7600                exp.op.exp1.destType = dummy;
7601                dummy.refCount++;               
7602             }
7603
7604             // TESTING THIS HERE...
7605             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7606             ProcessExpressionType(exp.op.exp1);
7607             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7608
7609             if(exp.op.exp1.destType == dummy)
7610             {
7611                FreeType(dummy);
7612                exp.op.exp1.destType = null;
7613             }
7614             type1 = exp.op.exp1.expType;
7615          }
7616
7617          if(exp.op.exp2)
7618          {
7619             char expString[10240];
7620             expString[0] = '\0';
7621             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7622             {
7623                if(exp.op.exp1)
7624                {
7625                   exp.op.exp2.destType = exp.op.exp1.expType;
7626                   if(exp.op.exp1.expType)
7627                      exp.op.exp1.expType.refCount++;
7628                }
7629                else
7630                {
7631                   exp.op.exp2.destType = exp.destType;
7632                   if(exp.destType)
7633                      exp.destType.refCount++;
7634                }
7635
7636                if(type1) type1.refCount++;
7637                exp.expType = type1;
7638             }
7639             else if(assign)
7640             {
7641                if(inCompiler)
7642                   PrintExpression(exp.op.exp2, expString);
7643
7644                if(type1 && type1.kind == pointerType)
7645                {
7646                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN ||
7647                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7648                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7649                   else if(exp.op.op == '=')
7650                   {
7651                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7652                      exp.op.exp2.destType = type1;
7653                      if(type1)
7654                         type1.refCount++;
7655                   }
7656                }
7657                else
7658                {
7659                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;) 
7660                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN/* ||
7661                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7662                   else
7663                   {
7664                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7665                      exp.op.exp2.destType = type1;
7666                      if(type1)
7667                         type1.refCount++;
7668                   }
7669                }
7670                if(type1) type1.refCount++;
7671                exp.expType = type1;
7672             }
7673             else if(exp.destType && exp.destType.kind == classType &&
7674                exp.destType._class && exp.destType._class.registered && 
7675                
7676                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) || 
7677                   (exp.destType._class.registered.type == enumClass && useDestType)) 
7678                   )
7679             {
7680                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7681                exp.op.exp2.destType = exp.destType;
7682                if(exp.destType)
7683                   exp.destType.refCount++;
7684             }
7685             else
7686             {
7687                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7688                exp.op.exp2.destType = dummy;
7689                dummy.refCount++;
7690             }
7691
7692             // TESTING THIS HERE... (DANGEROUS)
7693             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered && 
7694                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7695             {
7696                FreeType(exp.op.exp2.destType);
7697                exp.op.exp2.destType = type1;
7698                type1.refCount++;
7699             }
7700             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7701             ProcessExpressionType(exp.op.exp2);
7702             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7703
7704             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7705             {
7706                if(exp.op.exp2.expType.kind == int64Type || exp.op.exp2.expType.kind == intType || exp.op.exp2.expType.kind == shortType || exp.op.exp2.expType.kind == charType)
7707                {
7708                   if(exp.op.op != '=' && type1.type.kind == voidType) 
7709                      Compiler_Error($"void *: unknown size\n");
7710                }
7711                else if(exp.op.exp2.expType.kind == pointerType || exp.op.exp2.expType.kind == arrayType || exp.op.exp2.expType.kind == functionType || exp.op.exp2.expType.kind == methodType|| 
7712                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7713                               (exp.op.exp2.expType._class.registered.type == normalClass || 
7714                               exp.op.exp2.expType._class.registered.type == structClass ||
7715                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7716                {
7717                   if(exp.op.op == ADD_ASSIGN)
7718                      Compiler_Error($"cannot add two pointers\n");                   
7719                }
7720                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType && 
7721                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7722                {
7723                   if(exp.op.op == ADD_ASSIGN)
7724                      Compiler_Error($"cannot add two pointers\n");                   
7725                }
7726                else if(inCompiler)
7727                {
7728                   char type1String[1024];
7729                   char type2String[1024];
7730                   type1String[0] = '\0';
7731                   type2String[0] = '\0';
7732                   
7733                   PrintType(exp.op.exp2.expType, type1String, false, true);
7734                   PrintType(type1, type2String, false, true);
7735                   ChangeCh(expString, '\n', ' ');
7736                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7737                }
7738             }
7739
7740             if(exp.op.exp2.destType == dummy)
7741             {
7742                FreeType(dummy);
7743                exp.op.exp2.destType = null;
7744             }
7745
7746             type2 = exp.op.exp2.expType;
7747          }
7748
7749          dummy.kind = voidType;
7750
7751          if(exp.op.op == SIZEOF)
7752          {
7753             exp.expType = Type
7754             {
7755                refCount = 1;
7756                kind = intType;
7757             };
7758             exp.isConstant = true;
7759          }
7760          // Get type of dereferenced pointer
7761          else if(exp.op.op == '*' && !exp.op.exp1)
7762          {
7763             exp.expType = Dereference(type2);
7764             if(type2 && type2.kind == classType)
7765                notByReference = true;
7766          }
7767          else if(exp.op.op == '&' && !exp.op.exp1)
7768             exp.expType = Reference(type2);
7769          else if(!assign)
7770          {
7771             if(boolOps)
7772             {
7773                if(exp.op.exp1) 
7774                {
7775                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7776                   exp.op.exp1.destType = MkClassType("bool");
7777                   exp.op.exp1.destType.truth = true;
7778                   if(!exp.op.exp1.expType)
7779                      ProcessExpressionType(exp.op.exp1);
7780                   else
7781                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
7782                   FreeType(exp.op.exp1.expType);
7783                   exp.op.exp1.expType = MkClassType("bool");
7784                   exp.op.exp1.expType.truth = true;
7785                }
7786                if(exp.op.exp2) 
7787                {
7788                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7789                   exp.op.exp2.destType = MkClassType("bool");
7790                   exp.op.exp2.destType.truth = true;
7791                   if(!exp.op.exp2.expType)
7792                      ProcessExpressionType(exp.op.exp2);
7793                   else
7794                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
7795                   FreeType(exp.op.exp2.expType);
7796                   exp.op.exp2.expType = MkClassType("bool");
7797                   exp.op.exp2.expType.truth = true;
7798                }
7799             }
7800             else if(exp.op.exp1 && exp.op.exp2 && 
7801                ((useSideType /*&& 
7802                      (useSideUnit || 
7803                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
7804                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
7805                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) && 
7806                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
7807             {
7808                if(type1 && type2 &&
7809                   // If either both are class or both are not class
7810                   ((type1.kind == classType && strcmp(type1._class.string, "String")) == (type2.kind == classType && strcmp(type2._class.string, "String"))))
7811                {
7812                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7813                   exp.op.exp2.destType = type1;
7814                   type1.refCount++;
7815                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7816                   exp.op.exp1.destType = type2;
7817                   type2.refCount++;
7818                   // Warning here for adding Radians + Degrees with no destination type
7819                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) && 
7820                      type1._class.registered && type1._class.registered.type == unitClass && 
7821                      type2._class.registered && type2._class.registered.type == unitClass && 
7822                      type1._class.registered != type2._class.registered)
7823                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
7824                         type1._class.string, type2._class.string, type1._class.string);
7825
7826                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
7827                   {
7828                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
7829                      if(argExp)
7830                      {
7831                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
7832
7833                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
7834                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), 
7835                            exp.op.exp1)));
7836
7837                         ProcessExpressionType(exp.op.exp1);
7838
7839                         if(type2.kind != pointerType)
7840                         {
7841                            ProcessExpressionType(classExp);
7842
7843                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', 
7844                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
7845                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
7846                                  // noHeadClass
7847                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
7848                                     OR_OP, 
7849                                  // normalClass
7850                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
7851                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
7852                                        MkPointer(null, null), null)))),                                  
7853                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
7854
7855                            if(!exp.op.exp2.expType)
7856                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
7857
7858                            ProcessExpressionType(exp.op.exp2);
7859                         }
7860                      }
7861                   }
7862                   
7863                   if(!boolResult && ((type1.kind == pointerType || type1.kind == arrayType || (type1.kind == classType && !strcmp(type1._class.string, "String"))) && (type2.kind == int64Type || type2.kind == intType || type2.kind == shortType || type2.kind == charType)))
7864                   {
7865                      if(type1.kind != classType && type1.type.kind == voidType) 
7866                         Compiler_Error($"void *: unknown size\n");
7867                      exp.expType = type1;
7868                      if(type1) type1.refCount++;
7869                   }
7870                   else if(!boolResult && ((type2.kind == pointerType || type2.kind == arrayType || (type2.kind == classType && !strcmp(type2._class.string, "String"))) && (type1.kind == int64Type || type1.kind == intType || type1.kind == shortType || type1.kind == charType)))
7871                   {
7872                      if(type2.kind != classType && type2.type.kind == voidType) 
7873                         Compiler_Error($"void *: unknown size\n");
7874                      exp.expType = type2;
7875                      if(type2) type2.refCount++;
7876                   }
7877                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
7878                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
7879                   {
7880                      Compiler_Warning($"different levels of indirection\n");
7881                   }
7882                   else 
7883                   {
7884                      bool success = false;
7885                      if(type1.kind == pointerType && type2.kind == pointerType)
7886                      {
7887                         if(exp.op.op == '+')
7888                            Compiler_Error($"cannot add two pointers\n");
7889                         else if(exp.op.op == '-')
7890                         {
7891                            // Pointer Subtraction gives integer
7892                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
7893                            {
7894                               exp.expType = Type
7895                               {
7896                                  kind = intType;
7897                                  refCount = 1;
7898                               };
7899                               success = true;
7900
7901                               if(type1.type.kind == templateType)
7902                               {
7903                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
7904                                  if(argExp)
7905                                  {
7906                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
7907
7908                                     ProcessExpressionType(classExp);
7909
7910                                     exp.type = bracketsExp;
7911                                     exp.list = MkListOne(MkExpOp(
7912                                        MkExpBrackets(MkListOne(MkExpOp(
7913                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
7914                                              , exp.op.op, 
7915                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/', 
7916                                           
7917                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
7918
7919                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
7920                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
7921                                                 // noHeadClass
7922                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
7923                                                    OR_OP, 
7924                                                 // normalClass
7925                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
7926                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
7927                                                       MkPointer(null, null), null)))),                                  
7928                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
7929
7930                                              
7931                                              ));
7932                                     
7933                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
7934                                     FreeType(dummy);
7935                                     return;                                       
7936                                  }
7937                               }
7938                            }
7939                         }
7940                      }
7941
7942                      if(!success && exp.op.exp1.type == constantExp)
7943                      {
7944                         // If first expression is constant, try to match that first
7945                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
7946                         {
7947                            if(exp.expType) FreeType(exp.expType);
7948                            exp.expType = exp.op.exp1.destType;
7949                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
7950                            success = true;
7951                         }
7952                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
7953                         {
7954                            if(exp.expType) FreeType(exp.expType);
7955                            exp.expType = exp.op.exp2.destType;
7956                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
7957                            success = true;
7958                         }
7959                      }
7960                      else if(!success)
7961                      {
7962                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
7963                         {
7964                            if(exp.expType) FreeType(exp.expType);
7965                            exp.expType = exp.op.exp2.destType;
7966                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
7967                            success = true;
7968                         }
7969                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
7970                         {
7971                            if(exp.expType) FreeType(exp.expType);
7972                            exp.expType = exp.op.exp1.destType;
7973                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
7974                            success = true;
7975                         }
7976                      }
7977                      if(!success)
7978                      {
7979                         char expString1[10240];
7980                         char expString2[10240];
7981                         char type1[1024];
7982                         char type2[1024];
7983                         expString1[0] = '\0';
7984                         expString2[0] = '\0';
7985                         type1[0] = '\0';
7986                         type2[0] = '\0';
7987                         if(inCompiler)
7988                         {
7989                            PrintExpression(exp.op.exp1, expString1);
7990                            ChangeCh(expString1, '\n', ' ');
7991                            PrintExpression(exp.op.exp2, expString2);
7992                            ChangeCh(expString2, '\n', ' ');
7993                            PrintType(exp.op.exp1.expType, type1, false, true);
7994                            PrintType(exp.op.exp2.expType, type2, false, true);
7995                         }
7996
7997                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
7998                      }
7999                   }
8000                }
8001                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8002                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8003                {
8004                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8005                   // Convert e.g. / 4 into / 4.0
8006                   exp.op.exp1.destType = type2._class.registered.dataType;
8007                   if(type2._class.registered.dataType)
8008                      type2._class.registered.dataType.refCount++;
8009                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8010                   exp.expType = type2;
8011                   if(type2) type2.refCount++;
8012                }
8013                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8014                {
8015                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8016                   // Convert e.g. / 4 into / 4.0
8017                   exp.op.exp2.destType = type1._class.registered.dataType;
8018                   if(type1._class.registered.dataType)
8019                      type1._class.registered.dataType.refCount++;
8020                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8021                   exp.expType = type1;
8022                   if(type1) type1.refCount++;
8023                }
8024                else if(type1)
8025                {
8026                   bool valid = false;
8027
8028                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8029                   {
8030                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8031
8032                      if(!type1._class.registered.dataType)
8033                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8034                      exp.op.exp2.destType = type1._class.registered.dataType;
8035                      exp.op.exp2.destType.refCount++;
8036
8037                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8038                      type2 = exp.op.exp2.destType;
8039
8040                      exp.expType = type2;
8041                      type2.refCount++;
8042                   }
8043                   
8044                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8045                   {
8046                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8047
8048                      if(!type2._class.registered.dataType)
8049                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8050                      exp.op.exp1.destType = type2._class.registered.dataType;
8051                      exp.op.exp1.destType.refCount++;
8052
8053                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8054                      type1 = exp.op.exp1.destType;
8055                      exp.expType = type1;
8056                      type1.refCount++;
8057                   }
8058
8059                   // TESTING THIS NEW CODE
8060                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8061                   {
8062                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8063                      {
8064                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8065                         {
8066                            if(exp.expType) FreeType(exp.expType);
8067                            exp.expType = exp.op.exp1.expType;
8068                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8069                            valid = true;
8070                         }
8071                      }
8072
8073                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8074                      {
8075                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8076                         {
8077                            if(exp.expType) FreeType(exp.expType);
8078                            exp.expType = exp.op.exp2.expType;
8079                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8080                            valid = true;
8081                         }
8082                      }
8083                   }
8084
8085                   if(!valid)
8086                   {
8087                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8088                      exp.op.exp2.destType = type1;
8089                      type1.refCount++;
8090
8091                      /*
8092                      // Maybe this was meant to be an enum...
8093                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8094                      {
8095                         Type oldType = exp.op.exp2.expType;
8096                         exp.op.exp2.expType = null;
8097                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8098                            FreeType(oldType);
8099                         else
8100                            exp.op.exp2.expType = oldType;
8101                      }
8102                      */
8103
8104                      /*
8105                      // TESTING THIS HERE... LATEST ADDITION
8106                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8107                      {
8108                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8109                         exp.op.exp2.destType = type2._class.registered.dataType;
8110                         if(type2._class.registered.dataType)
8111                            type2._class.registered.dataType.refCount++;
8112                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8113                         
8114                         //exp.expType = type2._class.registered.dataType; //type2;
8115                         //if(type2) type2.refCount++;
8116                      }
8117
8118                      // TESTING THIS HERE... LATEST ADDITION
8119                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8120                      {
8121                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8122                         exp.op.exp1.destType = type1._class.registered.dataType;
8123                         if(type1._class.registered.dataType)
8124                            type1._class.registered.dataType.refCount++;
8125                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8126                         exp.expType = type1._class.registered.dataType; //type1;
8127                         if(type1) type1.refCount++;
8128                      }
8129                      */
8130
8131                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8132                      {
8133                         if(exp.expType) FreeType(exp.expType);
8134                         exp.expType = exp.op.exp2.destType;
8135                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8136                      }
8137                      else if(type1 && type2)
8138                      {
8139                         char expString1[10240];
8140                         char expString2[10240];
8141                         char type1String[1024];
8142                         char type2String[1024];
8143                         expString1[0] = '\0';
8144                         expString2[0] = '\0';
8145                         type1String[0] = '\0';
8146                         type2String[0] = '\0';
8147                         if(inCompiler)
8148                         {
8149                            PrintExpression(exp.op.exp1, expString1);
8150                            ChangeCh(expString1, '\n', ' ');
8151                            PrintExpression(exp.op.exp2, expString2);
8152                            ChangeCh(expString2, '\n', ' ');
8153                            PrintType(exp.op.exp1.expType, type1String, false, true);
8154                            PrintType(exp.op.exp2.expType, type2String, false, true);
8155                         }
8156
8157                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8158
8159                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8160                         {
8161                            exp.expType = exp.op.exp1.expType;
8162                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8163                         }
8164                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8165                         {
8166                            exp.expType = exp.op.exp2.expType;
8167                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8168                         }
8169                      }
8170                   }
8171                }
8172                else if(type2)
8173                {
8174                   // Maybe this was meant to be an enum...
8175                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8176                   {
8177                      Type oldType = exp.op.exp1.expType;
8178                      exp.op.exp1.expType = null;
8179                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8180                         FreeType(oldType);
8181                      else
8182                         exp.op.exp1.expType = oldType;
8183                   }
8184
8185                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8186                   exp.op.exp1.destType = type2;
8187                   type2.refCount++;
8188                   /*
8189                   // TESTING THIS HERE... LATEST ADDITION
8190                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8191                   {
8192                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8193                      exp.op.exp1.destType = type1._class.registered.dataType;
8194                      if(type1._class.registered.dataType)
8195                         type1._class.registered.dataType.refCount++;
8196                   }
8197
8198                   // TESTING THIS HERE... LATEST ADDITION
8199                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8200                   {
8201                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8202                      exp.op.exp2.destType = type2._class.registered.dataType;
8203                      if(type2._class.registered.dataType)
8204                         type2._class.registered.dataType.refCount++;
8205                   }
8206                   */
8207
8208                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8209                   {
8210                      if(exp.expType) FreeType(exp.expType);
8211                      exp.expType = exp.op.exp1.destType;
8212                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8213                   }
8214                }
8215             }
8216             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8217             {
8218                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8219                {
8220                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8221                   // Convert e.g. / 4 into / 4.0
8222                   exp.op.exp1.destType = type2._class.registered.dataType;
8223                   if(type2._class.registered.dataType)
8224                      type2._class.registered.dataType.refCount++;
8225                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8226                }
8227                if(exp.op.op == '!')
8228                {
8229                   exp.expType = MkClassType("bool");
8230                   exp.expType.truth = true;
8231                }
8232                else
8233                {
8234                   exp.expType = type2;
8235                   if(type2) type2.refCount++;
8236                }
8237             }
8238             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8239             {
8240                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8241                {
8242                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8243                   // Convert e.g. / 4 into / 4.0
8244                   exp.op.exp2.destType = type1._class.registered.dataType;
8245                   if(type1._class.registered.dataType)
8246                      type1._class.registered.dataType.refCount++;
8247                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8248                }
8249                exp.expType = type1;
8250                if(type1) type1.refCount++;
8251             }
8252          }
8253          
8254          yylloc = exp.loc;
8255          if(exp.op.exp1 && !exp.op.exp1.expType)
8256          {
8257             char expString[10000];
8258             expString[0] = '\0';
8259             if(inCompiler)
8260             {
8261                PrintExpression(exp.op.exp1, expString);
8262                ChangeCh(expString, '\n', ' ');
8263             }
8264             if(expString[0])
8265                Compiler_Error($"couldn't determine type of %s\n", expString);
8266          }
8267          if(exp.op.exp2 && !exp.op.exp2.expType)
8268          {
8269             char expString[10240];
8270             expString[0] = '\0';
8271             if(inCompiler)
8272             {
8273                PrintExpression(exp.op.exp2, expString);
8274                ChangeCh(expString, '\n', ' ');
8275             }
8276             if(expString[0])
8277                Compiler_Error($"couldn't determine type of %s\n", expString);
8278          }
8279
8280          if(boolResult)
8281          {
8282             FreeType(exp.expType);
8283             exp.expType = MkClassType("bool");
8284             exp.expType.truth = true;
8285          }
8286
8287          if(exp.op.op != SIZEOF)
8288             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8289                (!exp.op.exp2 || exp.op.exp2.isConstant);
8290
8291          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8292          {
8293             DeclareType(exp.op.exp2.expType, false, false);
8294          }
8295
8296          yylloc = oldyylloc;
8297
8298          FreeType(dummy);
8299          break;
8300       }
8301       case bracketsExp:
8302       case extensionExpressionExp:
8303       {
8304          Expression e;
8305          exp.isConstant = true;
8306          for(e = exp.list->first; e; e = e.next)
8307          {
8308             bool inced = false;
8309             if(!e.next)
8310             {
8311                FreeType(e.destType);
8312                e.destType = exp.destType;
8313                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8314             }
8315             ProcessExpressionType(e);
8316             if(inced)
8317                exp.destType.count--;
8318             if(!exp.expType && !e.next)
8319             {
8320                exp.expType = e.expType;
8321                if(e.expType) e.expType.refCount++;
8322             }
8323             if(!e.isConstant)
8324                exp.isConstant = false;
8325          }
8326
8327          // In case a cast became a member...
8328          e = exp.list->first;
8329          if(!e.next && e.type == memberExp)
8330          {
8331             // Preserve prev, next
8332             Expression next = exp.next, prev = exp.prev;
8333
8334
8335             FreeType(exp.expType);
8336             FreeType(exp.destType);
8337             delete exp.list;
8338             
8339             *exp = *e;
8340
8341             exp.prev = prev;
8342             exp.next = next;
8343
8344             delete e;
8345
8346             ProcessExpressionType(exp);
8347          }
8348          break;
8349       }
8350       case indexExp:
8351       {
8352          Expression e;
8353          exp.isConstant = true;
8354
8355          ProcessExpressionType(exp.index.exp);
8356          if(!exp.index.exp.isConstant)
8357             exp.isConstant = false;
8358
8359          if(exp.index.exp.expType)
8360          {
8361             Type source = exp.index.exp.expType;
8362             if(source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
8363                eClass_IsDerived(source._class.registered, containerClass) && 
8364                source._class.registered.templateArgs)
8365             {
8366                Class _class = source._class.registered;
8367                exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8368
8369                if(exp.index.index && exp.index.index->last)
8370                {
8371                   ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8372                }
8373             }
8374          }
8375
8376          for(e = exp.index.index->first; e; e = e.next)
8377          {
8378             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8379             {
8380                if(e.destType) FreeType(e.destType);
8381                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8382             }
8383             ProcessExpressionType(e);
8384             if(!e.next)
8385             {
8386                // Check if this type is int
8387             }
8388             if(!e.isConstant)
8389                exp.isConstant = false;
8390          }
8391
8392          if(!exp.expType)
8393             exp.expType = Dereference(exp.index.exp.expType);
8394          if(exp.expType)
8395             DeclareType(exp.expType, false, false);
8396          break;
8397       }
8398       case callExp:
8399       {
8400          Expression e;
8401          Type functionType;
8402          Type methodType = null;
8403          char name[1024];
8404          name[0] = '\0';
8405
8406          if(inCompiler)
8407          {
8408             PrintExpression(exp.call.exp,  name);
8409             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8410             {
8411                //exp.call.exp.expType = null;
8412                PrintExpression(exp.call.exp,  name);
8413             }
8414          }
8415          if(exp.call.exp.type == identifierExp)
8416          {
8417             Expression idExp = exp.call.exp;
8418             Identifier id = idExp.identifier;
8419             if(!strcmp(id.string, "__ENDIAN_PAD"))
8420             {
8421                exp.expType = ProcessTypeString("int", true);
8422                if(exp.call.arguments && exp.call.arguments->first)
8423                   ProcessExpressionType(exp.call.arguments->first);
8424                break;
8425             }
8426             else if(!strcmp(id.string, "Max") ||
8427                !strcmp(id.string, "Min") ||
8428                !strcmp(id.string, "Sgn") ||
8429                !strcmp(id.string, "Abs"))
8430             {
8431                Expression a = null;
8432                Expression b = null;
8433                Expression tempExp1 = null, tempExp2 = null;
8434                if((!strcmp(id.string, "Max") ||
8435                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8436                {
8437                   a = exp.call.arguments->first;
8438                   b = exp.call.arguments->last;
8439                   tempExp1 = a;
8440                   tempExp2 = b;
8441                }
8442                else if(exp.call.arguments->count == 1)
8443                {
8444                   a = exp.call.arguments->first;
8445                   tempExp1 = a;
8446                }
8447                
8448                if(a)
8449                {
8450                   exp.call.arguments->Clear();
8451                   idExp.identifier = null;
8452
8453                   FreeExpContents(exp);
8454
8455                   ProcessExpressionType(a);
8456                   if(b)
8457                      ProcessExpressionType(b);
8458
8459                   exp.type = bracketsExp;
8460                   exp.list = MkList();
8461
8462                   if(a.expType && (!b || b.expType))
8463                   {
8464                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8465                      {
8466                         // Use the simpleStruct name/ids for now...
8467                         if(inCompiler)
8468                         {
8469                            OldList * specs = MkList();
8470                            OldList * decls = MkList();
8471                            Declaration decl;
8472                            char temp1[1024], temp2[1024];
8473
8474                            GetTypeSpecs(a.expType, specs);
8475
8476                            if(a && !a.isConstant && a.type != identifierExp)
8477                            {
8478                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8479                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8480                               tempExp1 = QMkExpId(temp1);
8481                               tempExp1.expType = a.expType;
8482                               if(a.expType)
8483                                  a.expType.refCount++;
8484                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8485                            }
8486                            if(b && !b.isConstant && b.type != identifierExp)
8487                            {
8488                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8489                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8490                               tempExp2 = QMkExpId(temp2);
8491                               tempExp2.expType = b.expType;
8492                               if(b.expType)
8493                                  b.expType.refCount++;
8494                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8495                            }                        
8496
8497                            decl = MkDeclaration(specs, decls);
8498                            if(!curCompound.compound.declarations)
8499                               curCompound.compound.declarations = MkList();
8500                            curCompound.compound.declarations->Insert(null, decl);
8501                         }
8502                      }
8503                   }
8504
8505                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8506                   {
8507                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8508                      ListAdd(exp.list, 
8509                         MkExpCondition(MkExpBrackets(MkListOne(
8510                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8511                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8512                      exp.expType = a.expType;
8513                      if(a.expType)
8514                         a.expType.refCount++;
8515                   }
8516                   else if(!strcmp(id.string, "Abs"))
8517                   {
8518                      ListAdd(exp.list, 
8519                         MkExpCondition(MkExpBrackets(MkListOne(
8520                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8521                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8522                      exp.expType = a.expType;
8523                      if(a.expType)
8524                         a.expType.refCount++;
8525                   }
8526                   else if(!strcmp(id.string, "Sgn"))
8527                   {
8528                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8529                      ListAdd(exp.list, 
8530                         MkExpCondition(MkExpBrackets(MkListOne(
8531                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8532                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8533                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8534                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8535                      exp.expType = ProcessTypeString("int", false);
8536                   }
8537
8538                   FreeExpression(tempExp1);
8539                   if(tempExp2) FreeExpression(tempExp2);
8540
8541                   FreeIdentifier(id);
8542                   break;
8543                }
8544             }
8545          }
8546
8547          {
8548             Type dummy
8549             {
8550                count = 1;
8551                refCount = 1;
8552             };
8553             if(!exp.call.exp.destType)
8554             {
8555                exp.call.exp.destType = dummy;
8556                dummy.refCount++;
8557             }
8558             ProcessExpressionType(exp.call.exp);
8559             if(exp.call.exp.destType == dummy)
8560             {
8561                FreeType(dummy);
8562                exp.call.exp.destType = null;
8563             }
8564             FreeType(dummy);
8565          }
8566
8567          // Check argument types against parameter types
8568          functionType = exp.call.exp.expType;
8569
8570          if(functionType && functionType.kind == TypeKind::methodType)
8571          {
8572             methodType = functionType;
8573             functionType = methodType.method.dataType;
8574             
8575             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8576             // TOCHECK: Instead of doing this here could this be done per param?
8577             if(exp.call.exp.expType.usedClass)
8578             {
8579                char typeString[1024];
8580                typeString[0] = '\0';
8581                PrintType(functionType, typeString, true, true);
8582                if(strstr(typeString, "thisclass"))
8583                {
8584                   OldList * specs = MkList();
8585                   Declarator decl;
8586                   {
8587                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8588
8589                      decl = SpecDeclFromString(typeString, specs, null);
8590                      
8591                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8592                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8593                         exp.call.exp.expType.usedClass))
8594                         thisClassParams = false;
8595                      
8596                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8597                      {
8598                         Class backupThisClass = thisClass;
8599                         thisClass = exp.call.exp.expType.usedClass;
8600                         ProcessDeclarator(decl);
8601                         thisClass = backupThisClass;
8602                      }
8603
8604                      thisClassParams = true;
8605
8606                      functionType = ProcessType(specs, decl);
8607                      functionType.refCount = 0;
8608                      FinishTemplatesContext(context);
8609                   }
8610
8611                   FreeList(specs, FreeSpecifier);
8612                   FreeDeclarator(decl);
8613                 }
8614             }
8615          }
8616          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8617          {
8618             Type type = functionType.type;
8619             if(!functionType.refCount)
8620             {
8621                functionType.type = null;
8622                FreeType(functionType);
8623             }
8624             //methodType = functionType;
8625             functionType = type;
8626          }
8627          if(functionType && functionType.kind != TypeKind::functionType)
8628          {
8629             Compiler_Error($"called object %s is not a function\n", name);
8630          }
8631          else if(functionType)
8632          {
8633             bool emptyParams = false, noParams = false;
8634             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8635             Type type = functionType.params.first;
8636             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8637             int extra = 0;
8638             Location oldyylloc = yylloc;
8639
8640             if(!type) emptyParams = true;
8641
8642             // WORKING ON THIS:
8643             if(functionType.extraParam && e)
8644             {
8645                e.destType = MkClassType(functionType.thisClass.string);
8646                e = e.next;
8647             }
8648
8649             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8650             if(!functionType.staticMethod)
8651             {
8652                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType && 
8653                   memberExp.member.exp.expType._class)
8654                {
8655                   type = MkClassType(memberExp.member.exp.expType._class.string);
8656                   if(e)
8657                   {
8658                      e.destType = type;
8659                      e = e.next;
8660                      type = functionType.params.first;
8661                   }
8662                   else
8663                      type.refCount = 0;
8664                }
8665                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8666                {
8667                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8668                   if(e)
8669                   {
8670                      e.destType = type;
8671                      e = e.next;
8672                      type = functionType.params.first;
8673                   }
8674                   else
8675                      type.refCount = 0;
8676                   //extra = 1;
8677                }
8678             }
8679
8680             if(type && type.kind == voidType)
8681             {
8682                noParams = true;
8683                if(!type.refCount) FreeType(type);
8684                type = null;
8685             }
8686
8687             for( ; e; e = e.next)
8688             {
8689                if(!type && !emptyParams)
8690                {
8691                   yylloc = e.loc;
8692                   if(methodType && methodType.methodClass)
8693                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8694                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8695                         noParams ? 0 : functionType.params.count);
8696                   else
8697                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8698                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8699                         noParams ? 0 : functionType.params.count);
8700                   break;
8701                }
8702
8703                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8704                {
8705                   Type templatedType = null;
8706                   Class _class = methodType.usedClass;
8707                   ClassTemplateParameter curParam = null;
8708                   int id = 0;
8709                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8710                   {
8711                      Class sClass;
8712                      for(sClass = _class; sClass; sClass = sClass.base)
8713                      {
8714                         if(sClass.templateClass) sClass = sClass.templateClass;
8715                         id = 0;
8716                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8717                         {
8718                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8719                            {
8720                               Class nextClass;
8721                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
8722                               {
8723                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
8724                                  id += nextClass.templateParams.count;
8725                               }
8726                               break;
8727                            }
8728                            id++;
8729                         }
8730                         if(curParam) break;
8731                      }
8732                   }
8733                   if(curParam && _class.templateArgs[id].dataTypeString)
8734                   {
8735                      ClassTemplateArgument arg = _class.templateArgs[id];
8736                      {
8737                         Context context = SetupTemplatesContext(_class);
8738                      
8739                         /*if(!arg.dataType)
8740                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
8741                         templatedType = ProcessTypeString(arg.dataTypeString, false);
8742                         FinishTemplatesContext(context);
8743                      }
8744                      e.destType = templatedType;
8745                      if(templatedType)
8746                      {
8747                         templatedType.passAsTemplate = true;
8748                         // templatedType.refCount++;
8749                      }
8750                   }
8751                   else
8752                   {
8753                      e.destType = type;
8754                      if(type) type.refCount++;
8755                   }
8756                }
8757                else
8758                {
8759                   e.destType = type;
8760                   if(type) type.refCount++;
8761                }
8762                // Don't reach the end for the ellipsis
8763                if(type && type.kind != ellipsisType)
8764                {
8765                   Type next = type.next;
8766                   if(!type.refCount) FreeType(type);
8767                   type = next;
8768                }
8769             }
8770
8771             if(type && type.kind != ellipsisType)
8772             {
8773                if(methodType && methodType.methodClass)
8774                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
8775                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
8776                      functionType.params.count + extra);
8777                else
8778                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
8779                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
8780                      functionType.params.count + extra);
8781             }
8782             yylloc = oldyylloc;
8783             if(type && !type.refCount) FreeType(type);
8784          }
8785          else
8786          {
8787             functionType = Type
8788             {
8789                refCount = 0;
8790                kind = TypeKind::functionType;
8791             };
8792
8793             if(exp.call.exp.type == identifierExp)
8794             {
8795                char * string = exp.call.exp.identifier.string;
8796                if(inCompiler)
8797                {
8798                   Symbol symbol;
8799                   Location oldyylloc = yylloc;
8800
8801                   yylloc = exp.call.exp.identifier.loc;
8802                   if(strstr(string, "__builtin_") == string);
8803                   else
8804                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
8805                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
8806                   globalContext.symbols.Add((BTNode)symbol);
8807                   if(strstr(symbol.string, "::"))
8808                      globalContext.hasNameSpace = true;
8809
8810                   yylloc = oldyylloc;
8811                }
8812             }
8813             else if(exp.call.exp.type == memberExp)
8814             {
8815                /*Compiler_Warning($"%s undefined; assuming returning int\n",
8816                   exp.call.exp.member.member.string);*/
8817             }
8818             else
8819                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
8820
8821             if(!functionType.returnType)
8822             {
8823                functionType.returnType = Type
8824                {
8825                   refCount = 1;
8826                   kind = intType;
8827                };
8828             }
8829          }
8830          if(functionType && functionType.kind == TypeKind::functionType)
8831          {
8832             exp.expType = functionType.returnType;
8833
8834             if(functionType.returnType)
8835                functionType.returnType.refCount++;
8836
8837             if(!functionType.refCount)
8838                FreeType(functionType);
8839          }
8840
8841          if(exp.call.arguments)
8842          {
8843             for(e = exp.call.arguments->first; e; e = e.next)
8844             {
8845                Type destType = e.destType;
8846                ProcessExpressionType(e);
8847             }
8848          }
8849          break;
8850       }
8851       case memberExp:
8852       {
8853          Type type;
8854          Location oldyylloc = yylloc;
8855          bool thisPtr = (exp.member.exp && exp.member.exp.type == identifierExp && !strcmp(exp.member.exp.identifier.string, "this"));
8856          exp.thisPtr = thisPtr;
8857
8858          // DOING THIS LATER NOW...
8859          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
8860          {
8861             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
8862             /* TODO: Name Space Fix ups
8863             if(!exp.member.member.classSym)
8864                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
8865             */
8866          }
8867
8868          ProcessExpressionType(exp.member.exp);
8869          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class && 
8870             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
8871          {
8872             exp.isConstant = false;
8873          }
8874          else
8875             exp.isConstant = exp.member.exp.isConstant;
8876          type = exp.member.exp.expType;
8877
8878          yylloc = exp.loc;
8879
8880          if(type && (type.kind == templateType))
8881          {
8882             Class _class = thisClass ? thisClass : currentClass;
8883             ClassTemplateParameter param = null;
8884             if(_class)
8885             {
8886                for(param = _class.templateParams.first; param; param = param.next)
8887                {
8888                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
8889                      break;
8890                }
8891             }
8892             if(param && param.defaultArg.member)
8893             {
8894                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
8895                if(argExp)
8896                {
8897                   Expression expMember = exp.member.exp;
8898                   Declarator decl;
8899                   OldList * specs = MkList();
8900                   char thisClassTypeString[1024];
8901
8902                   FreeIdentifier(exp.member.member);
8903
8904                   ProcessExpressionType(argExp);
8905
8906                   {
8907                      char * colon = strstr(param.defaultArg.memberString, "::");
8908                      if(colon)
8909                      {
8910                         char className[1024];
8911                         Class sClass;
8912
8913                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
8914                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
8915                      }
8916                      else
8917                         strcpy(thisClassTypeString, _class.fullName);
8918                   }
8919
8920                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
8921
8922                   exp.expType = ProcessType(specs, decl);
8923                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
8924                   {
8925                      Class expClass = exp.expType._class.registered;
8926                      Class cClass = null;
8927                      int c;
8928                      int paramCount = 0;
8929                      int lastParam = -1;
8930                      
8931                      char templateString[1024];
8932                      ClassTemplateParameter param;
8933                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
8934                      for(cClass = expClass; cClass; cClass = cClass.base)
8935                      {
8936                         int p = 0;
8937                         for(param = cClass.templateParams.first; param; param = param.next)
8938                         {
8939                            int id = p;
8940                            Class sClass;
8941                            ClassTemplateArgument arg;
8942                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
8943                            arg = expClass.templateArgs[id];
8944
8945                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
8946                            {
8947                               ClassTemplateParameter cParam;
8948                               //int p = numParams - sClass.templateParams.count;
8949                               int p = 0;
8950                               Class nextClass;
8951                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
8952                               
8953                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
8954                               {
8955                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
8956                                  {
8957                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
8958                                     {
8959                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
8960                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
8961                                        break;
8962                                     }
8963                                  }
8964                               }
8965                            }
8966
8967                            {
8968                               char argument[256];
8969                               argument[0] = '\0';
8970                               /*if(arg.name)
8971                               {
8972                                  strcat(argument, arg.name.string);
8973                                  strcat(argument, " = ");
8974                               }*/
8975                               switch(param.type)
8976                               {
8977                                  case expression:
8978                                  {
8979                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
8980                                     char expString[1024];
8981                                     OldList * specs = MkList();
8982                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
8983                                     Expression exp;
8984                                     char * string = PrintHexUInt64(arg.expression.ui64);
8985                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
8986
8987                                     ProcessExpressionType(exp);
8988                                     ComputeExpression(exp);
8989                                     expString[0] = '\0';
8990                                     PrintExpression(exp, expString);
8991                                     strcat(argument, expString);
8992                                     // delete exp;
8993                                     FreeExpression(exp);
8994                                     break;
8995                                  }
8996                                  case identifier:
8997                                  {
8998                                     strcat(argument, arg.member.name);
8999                                     break;
9000                                  }
9001                                  case TemplateParameterType::type:
9002                                  {
9003                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9004                                     {
9005                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9006                                           strcat(argument, thisClassTypeString);
9007                                        else
9008                                           strcat(argument, arg.dataTypeString);
9009                                     }
9010                                     break;
9011                                  }
9012                               }
9013                               if(argument[0])
9014                               {
9015                                  if(paramCount) strcat(templateString, ", ");
9016                                  if(lastParam != p - 1)
9017                                  {
9018                                     strcat(templateString, param.name);
9019                                     strcat(templateString, " = ");
9020                                  }
9021                                  strcat(templateString, argument);
9022                                  paramCount++;
9023                                  lastParam = p;
9024                               }
9025                               p++;
9026                            }               
9027                         }
9028                      }
9029                      {
9030                         int len = strlen(templateString);
9031                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9032                         templateString[len++] = '>';
9033                         templateString[len++] = '\0';
9034                      }
9035                      {
9036                         Context context = SetupTemplatesContext(_class);
9037                         FreeType(exp.expType);
9038                         exp.expType = ProcessTypeString(templateString, false);
9039                         FinishTemplatesContext(context);
9040                      }                     
9041                   }
9042
9043                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9044                   exp.type = bracketsExp;
9045                   exp.list = MkListOne(MkExpOp(null, '*',
9046                   /*opExp;
9047                   exp.op.op = '*';
9048                   exp.op.exp1 = null;
9049                   exp.op.exp2 = */
9050                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9051                      MkExpBrackets(MkListOne(
9052                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9053                            '+',  
9054                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")), 
9055                            '+',
9056                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9057                            
9058                            ));
9059                }
9060             }
9061             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type && 
9062                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9063             {
9064                type = ProcessTemplateParameterType(type.templateParameter);
9065             }
9066          }
9067
9068          if(type && (type.kind == templateType));
9069          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9070          {
9071             Identifier id = exp.member.member;
9072             TypeKind typeKind = type.kind;
9073             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9074             if(typeKind == subClassType && exp.member.exp.type == classExp)
9075             {
9076                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9077                typeKind = classType;
9078             }
9079
9080             if(id && (typeKind == intType || typeKind == enumType))
9081                _class = eSystem_FindClass(privateModule, "int");
9082
9083             if(_class && id)
9084             {
9085                /*bool thisPtr = 
9086                   (exp.member.exp.type == identifierExp && 
9087                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9088                Property prop = null;
9089                Method method = null;
9090                DataMember member = null;
9091                Property revConvert = null;
9092                ClassProperty classProp = null;
9093
9094                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9095                   exp.member.memberType = propertyMember;
9096
9097                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9098                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9099
9100                if(typeKind != subClassType)
9101                {
9102                   // Prioritize data members over properties for "this"
9103                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9104                   {
9105                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9106                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9107                      {
9108                         prop = eClass_FindProperty(_class, id.string, privateModule);
9109                         if(prop)
9110                            member = null;
9111                      }
9112                      if(!member && !prop)
9113                         prop = eClass_FindProperty(_class, id.string, privateModule);
9114                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9115                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9116                         exp.member.thisPtr = true;
9117                   }
9118                   // Prioritize properties over data members otherwise
9119                   else
9120                   {
9121                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9122                      if(!id.classSym)
9123                      {
9124                         prop = eClass_FindProperty(_class, id.string, null);
9125                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9126                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9127                      }
9128
9129                      if(!prop && !member)
9130                      {
9131                         method = eClass_FindMethod(_class, id.string, null);
9132                         if(!method)
9133                         {
9134                            prop = eClass_FindProperty(_class, id.string, privateModule);
9135                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9136                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9137                         }
9138                      }
9139
9140                      if(member && prop)
9141                      {
9142                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9143                            prop = null;
9144                         else
9145                            member = null;
9146                      }
9147                   }
9148                }
9149                if(!prop && !member)
9150                   method = eClass_FindMethod(_class, id.string, privateModule);
9151                if(!prop && !member && !method)
9152                {
9153                   if(typeKind == subClassType)
9154                   {
9155                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9156                      if(classProp)
9157                      {
9158                         exp.member.memberType = classPropertyMember;
9159                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9160                      }
9161                      else
9162                      {
9163                         // Assume this is a class_data member
9164                         char structName[1024];
9165                         Identifier id = exp.member.member;
9166                         Expression classExp = exp.member.exp;
9167                         type.refCount++;
9168
9169                         FreeType(classExp.expType);
9170                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9171                      
9172                         strcpy(structName, "__ecereClassData_");
9173                         FullClassNameCat(structName, type._class.string, false);
9174                         exp.type = pointerExp;
9175                         exp.member.member = id;
9176
9177                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9178                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
9179                               MkExpBrackets(MkListOne(MkExpOp(
9180                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
9181                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9182                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9183                                  )));
9184
9185                         FreeType(type);
9186
9187                         ProcessExpressionType(exp);
9188                         return;
9189                      }
9190                   }
9191                   else
9192                   {
9193                      // Check for reverse conversion
9194                      // (Convert in an instantiation later, so that we can use
9195                      //  deep properties system)
9196                      Symbol classSym = FindClass(id.string);
9197                      if(classSym)
9198                      {
9199                         Class convertClass = classSym.registered;
9200                         if(convertClass)
9201                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9202                      }
9203                   }
9204                }
9205       
9206                if(prop)
9207                {
9208                   exp.member.memberType = propertyMember;
9209                   if(!prop.dataType)
9210                      ProcessPropertyType(prop);
9211                   exp.expType = prop.dataType;                     
9212                   if(prop.dataType) prop.dataType.refCount++;
9213                }
9214                else if(member)
9215                {
9216                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9217                   {
9218                      FreeExpContents(exp);
9219                      exp.type = identifierExp;
9220                      exp.identifier = MkIdentifier("class");
9221                      ProcessExpressionType(exp);
9222                      return;
9223                   }
9224
9225                   exp.member.memberType = dataMember;
9226                   DeclareStruct(_class.fullName, false);
9227                   if(!member.dataType)
9228                   {
9229                      Context context = SetupTemplatesContext(_class);
9230                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9231                      FinishTemplatesContext(context);
9232                   }
9233                   exp.expType = member.dataType;
9234                   if(member.dataType) member.dataType.refCount++;
9235                }
9236                else if(revConvert)
9237                {
9238                   exp.member.memberType = reverseConversionMember;
9239                   exp.expType = MkClassType(revConvert._class.fullName);
9240                }
9241                else if(method)
9242                {
9243                   if(inCompiler)
9244                   {
9245                      /*if(id._class)
9246                      {
9247                         exp.type = identifierExp;
9248                         exp.identifier = exp.member.member;
9249                      }
9250                      else*/
9251                         exp.member.memberType = methodMember;
9252                   }
9253                   if(!method.dataType)
9254                      ProcessMethodType(method);
9255                   exp.expType = Type
9256                   {
9257                      refCount = 1;
9258                      kind = methodType;
9259                      method = method;
9260                   };
9261
9262                   // Tricky spot here... To use instance versus class virtual table
9263                   // Put it back to what it was... What did we break?
9264
9265                   // Had to put it back for overriding Main of Thread global instance
9266
9267                   //exp.expType.methodClass = _class;
9268                   exp.expType.methodClass = (id && id._class) ? _class : null;
9269
9270                   // Need the actual class used for templated classes
9271                   exp.expType.usedClass = _class;
9272                }
9273                else if(!classProp)
9274                {
9275                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9276                   {
9277                      FreeExpContents(exp);
9278                      exp.type = identifierExp;
9279                      exp.identifier = MkIdentifier("class");
9280                      ProcessExpressionType(exp);
9281                      return;
9282                   }
9283                   yylloc = exp.member.member.loc;
9284                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9285                   if(inCompiler)
9286                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9287                }
9288
9289                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9290                {
9291                   Class tClass;
9292
9293                   tClass = _class;
9294                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9295
9296                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9297                   {
9298                      int id = 0;
9299                      ClassTemplateParameter curParam;
9300                      Class sClass;
9301
9302                      for(sClass = tClass; sClass; sClass = sClass.base)
9303                      {
9304                         id = 0;
9305                         if(sClass.templateClass) sClass = sClass.templateClass;
9306                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9307                         {
9308                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9309                            {
9310                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9311                                  id += sClass.templateParams.count;
9312                               break;
9313                            }
9314                            id++;
9315                         }
9316                         if(curParam) break;
9317                      }
9318
9319                      if(curParam && tClass.templateArgs[id].dataTypeString)
9320                      {
9321                         ClassTemplateArgument arg = tClass.templateArgs[id];
9322                         Context context = SetupTemplatesContext(tClass);
9323                         /*if(!arg.dataType)
9324                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9325                         FreeType(exp.expType);
9326                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9327                         if(exp.expType)
9328                         {
9329                            if(exp.expType.kind == thisClassType)
9330                            {
9331                               FreeType(exp.expType);
9332                               exp.expType = ReplaceThisClassType(_class);
9333                            }
9334
9335                            if(tClass.templateClass)
9336                               exp.expType.passAsTemplate = true;
9337                            //exp.expType.refCount++;
9338                            if(!exp.destType)
9339                            {
9340                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9341                               //exp.destType.refCount++;
9342
9343                               if(exp.destType.kind == thisClassType)
9344                               {
9345                                  FreeType(exp.destType);
9346                                  exp.destType = ReplaceThisClassType(_class);
9347                               }
9348                            }
9349                         }
9350                         FinishTemplatesContext(context);
9351                      }
9352                   }
9353                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9354                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9355                   {
9356                      int id = 0;
9357                      ClassTemplateParameter curParam;
9358                      Class sClass;
9359
9360                      for(sClass = tClass; sClass; sClass = sClass.base)
9361                      {
9362                         id = 0;
9363                         if(sClass.templateClass) sClass = sClass.templateClass;
9364                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9365                         {
9366                            if(curParam.type == TemplateParameterType::type && 
9367                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9368                            {
9369                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9370                                  id += sClass.templateParams.count;
9371                               break;
9372                            }
9373                            id++;
9374                         }
9375                         if(curParam) break;
9376                      }
9377
9378                      if(curParam)
9379                      {
9380                         ClassTemplateArgument arg = tClass.templateArgs[id];
9381                         Context context = SetupTemplatesContext(tClass);
9382                         Type basicType;
9383                         /*if(!arg.dataType)
9384                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9385                         
9386                         basicType = ProcessTypeString(arg.dataTypeString, false);
9387                         if(basicType)
9388                         {
9389                            if(basicType.kind == thisClassType)
9390                            {
9391                               FreeType(basicType);
9392                               basicType = ReplaceThisClassType(_class);
9393                            }
9394
9395                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9396                            if(tClass.templateClass)
9397                               basicType.passAsTemplate = true;
9398                            */
9399                            
9400                            FreeType(exp.expType);
9401
9402                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9403                            //exp.expType.refCount++;
9404                            if(!exp.destType)
9405                            {
9406                               exp.destType = exp.expType;
9407                               exp.destType.refCount++;
9408                            }
9409
9410                            {
9411                               Expression newExp { };
9412                               OldList * specs = MkList();
9413                               Declarator decl;
9414                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9415                               *newExp = *exp;
9416                               if(exp.destType) exp.destType.refCount++;
9417                               if(exp.expType)  exp.expType.refCount++;
9418                               exp.type = castExp;
9419                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9420                               exp.cast.exp = newExp;
9421                               //FreeType(exp.expType);
9422                               //exp.expType = null;
9423                               //ProcessExpressionType(sourceExp);
9424                            }
9425                         }
9426                         FinishTemplatesContext(context);
9427                      }
9428                   }
9429                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9430                   {
9431                      Class expClass = exp.expType._class.registered;
9432                      if(expClass)
9433                      {
9434                         Class cClass = null;
9435                         int c;
9436                         int p = 0;
9437                         int paramCount = 0;
9438                         int lastParam = -1;
9439                         char templateString[1024];
9440                         ClassTemplateParameter param;
9441                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9442                         while(cClass != expClass)
9443                         {
9444                            Class sClass;
9445                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9446                            cClass = sClass;
9447
9448                            for(param = cClass.templateParams.first; param; param = param.next)
9449                            {
9450                               Class cClassCur = null;
9451                               int c;
9452                               int cp = 0;
9453                               ClassTemplateParameter paramCur = null;
9454                               ClassTemplateArgument arg;
9455                               while(cClassCur != tClass && !paramCur)
9456                               {
9457                                  Class sClassCur;
9458                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9459                                  cClassCur = sClassCur;
9460
9461                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9462                                  {
9463                                     if(!strcmp(paramCur.name, param.name))
9464                                     {
9465                                        
9466                                        break;
9467                                     }
9468                                     cp++;
9469                                  }
9470                               }
9471                               if(paramCur && paramCur.type == TemplateParameterType::type)
9472                                  arg = tClass.templateArgs[cp];
9473                               else
9474                                  arg = expClass.templateArgs[p];
9475
9476                               {
9477                                  char argument[256];
9478                                  argument[0] = '\0';
9479                                  /*if(arg.name)
9480                                  {
9481                                     strcat(argument, arg.name.string);
9482                                     strcat(argument, " = ");
9483                                  }*/
9484                                  switch(param.type)
9485                                  {
9486                                     case expression:
9487                                     {
9488                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9489                                        char expString[1024];
9490                                        OldList * specs = MkList();
9491                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9492                                        Expression exp;
9493                                        char * string = PrintHexUInt64(arg.expression.ui64);
9494                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9495
9496                                        ProcessExpressionType(exp);
9497                                        ComputeExpression(exp);
9498                                        expString[0] = '\0';
9499                                        PrintExpression(exp, expString);
9500                                        strcat(argument, expString);
9501                                        // delete exp;
9502                                        FreeExpression(exp);
9503                                        break;
9504                                     }
9505                                     case identifier:
9506                                     {
9507                                        strcat(argument, arg.member.name);
9508                                        break;
9509                                     }
9510                                     case TemplateParameterType::type:
9511                                     {
9512                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9513                                           strcat(argument, arg.dataTypeString);
9514                                        break;
9515                                     }
9516                                  }
9517                                  if(argument[0])
9518                                  {
9519                                     if(paramCount) strcat(templateString, ", ");
9520                                     if(lastParam != p - 1)
9521                                     {
9522                                        strcat(templateString, param.name);
9523                                        strcat(templateString, " = ");
9524                                     }                                       
9525                                     strcat(templateString, argument);
9526                                     paramCount++;
9527                                     lastParam = p;
9528                                  }
9529                               }
9530                               p++;
9531                            }
9532                         }
9533                         {
9534                            int len = strlen(templateString);
9535                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9536                            templateString[len++] = '>';
9537                            templateString[len++] = '\0';
9538                         }
9539
9540                         FreeType(exp.expType);
9541                         {
9542                            Context context = SetupTemplatesContext(tClass);
9543                            exp.expType = ProcessTypeString(templateString, false);
9544                            FinishTemplatesContext(context);
9545                         }
9546                      }
9547                   }
9548                }
9549             }
9550             else
9551                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9552          }
9553          else if(type && (type.kind == structType || type.kind == unionType))
9554          {
9555             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9556             if(memberType)
9557             {
9558                exp.expType = memberType;
9559                if(memberType)
9560                   memberType.refCount++;
9561             }
9562          }
9563          else 
9564          {
9565             char expString[10240];
9566             expString[0] = '\0';
9567             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9568             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9569          }
9570
9571          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9572          {
9573             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9574             {
9575                Identifier id = exp.member.member;
9576                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9577                if(_class)
9578                {
9579                   FreeType(exp.expType);
9580                   exp.expType = ReplaceThisClassType(_class);
9581                }
9582             }
9583          }         
9584          yylloc = oldyylloc;
9585          break;
9586       }
9587       // Convert x->y into (*x).y
9588       case pointerExp:
9589       {
9590          Type destType = exp.destType;
9591
9592          // DOING THIS LATER NOW...
9593          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9594          {
9595             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9596             /* TODO: Name Space Fix ups
9597             if(!exp.member.member.classSym)
9598                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9599             */
9600          }
9601
9602          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9603          exp.type = memberExp;
9604          if(destType)
9605             destType.count++;
9606          ProcessExpressionType(exp);
9607          if(destType)
9608             destType.count--;
9609          break;
9610       }
9611       case classSizeExp:
9612       {
9613          //ComputeExpression(exp);
9614
9615          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9616          if(classSym && classSym.registered)
9617          {
9618             if(classSym.registered.type == noHeadClass)
9619             {
9620                char name[1024];
9621                name[0] = '\0';
9622                DeclareStruct(classSym.string, false);
9623                FreeSpecifier(exp._class);
9624                exp.type = typeSizeExp;
9625                FullClassNameCat(name, classSym.string, false);
9626                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9627             }
9628             else
9629             {
9630                if(classSym.registered.fixed)
9631                {
9632                   FreeSpecifier(exp._class);
9633                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9634                   exp.type = constantExp;
9635                }
9636                else
9637                {
9638                   char className[1024];
9639                   strcpy(className, "__ecereClass_");
9640                   FullClassNameCat(className, classSym.string, true);
9641                   MangleClassName(className);
9642
9643                   DeclareClass(classSym, className);
9644
9645                   FreeExpContents(exp);
9646                   exp.type = pointerExp;
9647                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9648                   exp.member.member = MkIdentifier("structSize");
9649                }
9650             }
9651          }
9652
9653          exp.expType = Type
9654          {
9655             refCount = 1;
9656             kind = intType;
9657          };
9658          // exp.isConstant = true;
9659          break;
9660       }
9661       case typeSizeExp:
9662       {
9663          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
9664
9665          exp.expType = Type
9666          {
9667             refCount = 1;
9668             kind = intType;
9669          };
9670          exp.isConstant = true;
9671
9672          DeclareType(type, false, false);
9673          FreeType(type);
9674          break;
9675       }
9676       case castExp:
9677       {
9678          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
9679          type.count = 1;
9680          FreeType(exp.cast.exp.destType);
9681          exp.cast.exp.destType = type;
9682          type.refCount++;
9683          ProcessExpressionType(exp.cast.exp);
9684          type.count = 0;
9685          exp.expType = type;
9686          //type.refCount++;
9687          
9688          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
9689          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
9690          {
9691             void * prev = exp.prev, * next = exp.next;
9692             Type expType = exp.cast.exp.destType;
9693             Expression castExp = exp.cast.exp;
9694             Type destType = exp.destType;
9695
9696             if(expType) expType.refCount++;
9697
9698             //FreeType(exp.destType);
9699             FreeType(exp.expType);
9700             FreeTypeName(exp.cast.typeName);
9701             
9702             *exp = *castExp;
9703             FreeType(exp.expType);
9704             FreeType(exp.destType);
9705
9706             exp.expType = expType;
9707             exp.destType = destType;
9708
9709             delete castExp;
9710
9711             exp.prev = prev;
9712             exp.next = next;
9713
9714          }
9715          else
9716          {
9717             exp.isConstant = exp.cast.exp.isConstant;
9718          }
9719          //FreeType(type);
9720          break;
9721       }
9722       case extensionInitializerExp:
9723       {
9724          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
9725          type.refCount++;
9726          // ProcessInitializer(exp.initializer.initializer, type);
9727          exp.expType = type;
9728          break;
9729       }
9730       case vaArgExp:
9731       {
9732          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
9733          ProcessExpressionType(exp.vaArg.exp);
9734          type.refCount++;
9735          exp.expType = type;
9736          break;
9737       }
9738       case conditionExp:
9739       {
9740          Expression e;
9741          exp.isConstant = true;
9742
9743          FreeType(exp.cond.cond.destType);
9744          exp.cond.cond.destType = MkClassType("bool");
9745          exp.cond.cond.destType.truth = true;
9746          ProcessExpressionType(exp.cond.cond);
9747          if(!exp.cond.cond.isConstant)
9748             exp.isConstant = false;
9749          for(e = exp.cond.exp->first; e; e = e.next)
9750          {
9751             if(!e.next)
9752             {
9753                FreeType(e.destType);
9754                e.destType = exp.destType;
9755                if(e.destType) e.destType.refCount++;
9756             }
9757             ProcessExpressionType(e);
9758             if(!e.next)
9759             {
9760                exp.expType = e.expType;
9761                if(e.expType) e.expType.refCount++;
9762             }
9763             if(!e.isConstant)
9764                exp.isConstant = false;
9765          }
9766
9767          FreeType(exp.cond.elseExp.destType);
9768          // Added this check if we failed to find an expType
9769          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
9770
9771          // Reversed it...
9772          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
9773
9774          if(exp.cond.elseExp.destType)
9775             exp.cond.elseExp.destType.refCount++;
9776          ProcessExpressionType(exp.cond.elseExp);
9777
9778          // FIXED THIS: Was done before calling process on elseExp
9779          if(!exp.cond.elseExp.isConstant)
9780             exp.isConstant = false;
9781          break;
9782       }
9783       case extensionCompoundExp:
9784       {
9785          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
9786          {
9787             Statement last = exp.compound.compound.statements->last;
9788             if(last.type == expressionStmt && last.expressions && last.expressions->last)
9789             {
9790                ((Expression)last.expressions->last).destType = exp.destType;
9791                if(exp.destType)
9792                   exp.destType.refCount++;
9793             }
9794             ProcessStatement(exp.compound);
9795             exp.expType = ((Expression)last.expressions->last).expType;
9796             if(((Expression)last.expressions->last).expType)
9797                exp.expType.refCount++;
9798          }
9799          break;
9800       }
9801       case classExp:
9802       {
9803          Specifier spec = exp._classExp.specifiers->first;
9804          if(spec && spec.type == nameSpecifier)
9805          {
9806             exp.expType = MkClassType(spec.name);
9807             exp.expType.kind = subClassType;
9808             exp.byReference = true;
9809          }
9810          else
9811          {
9812             exp.expType = MkClassType("ecere::com::Class");
9813             exp.byReference = true;
9814          }
9815          break;
9816       }
9817       case classDataExp:
9818       {
9819          Class _class = thisClass ? thisClass : currentClass;
9820          if(_class)
9821          {
9822             Identifier id = exp.classData.id;
9823             char structName[1024];
9824             Expression classExp;
9825             strcpy(structName, "__ecereClassData_");
9826             FullClassNameCat(structName, _class.fullName, false);
9827             exp.type = pointerExp;
9828             exp.member.member = id;
9829             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
9830                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
9831             else
9832                classExp = MkExpIdentifier(MkIdentifier("class"));
9833
9834             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9835                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
9836                   MkExpBrackets(MkListOne(MkExpOp(
9837                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
9838                         MkExpMember(classExp, MkIdentifier("data"))), '+',
9839                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
9840                      )));
9841
9842             ProcessExpressionType(exp);
9843             return;
9844          }
9845          break;
9846       }
9847       case arrayExp:
9848       {
9849          Type type = null;
9850          char * typeString = null;
9851          char typeStringBuf[1024];
9852          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
9853             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
9854          {
9855             Class templateClass = exp.destType._class.registered;
9856             typeString = templateClass.templateArgs[2].dataTypeString;
9857          }
9858          else if(exp.list)
9859          {
9860             // Guess type from expressions in the array
9861             Expression e;
9862             for(e = exp.list->first; e; e = e.next)
9863             {
9864                ProcessExpressionType(e);
9865                if(e.expType)
9866                {
9867                   if(!type) { type = e.expType; type.refCount++; }
9868                   else
9869                   {
9870                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
9871                      if(!MatchTypeExpression(e, type, null, false))
9872                      {
9873                         FreeType(type);
9874                         type = e.expType;
9875                         e.expType = null;
9876                         
9877                         e = exp.list->first;
9878                         ProcessExpressionType(e);
9879                         if(e.expType)
9880                         {
9881                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
9882                            if(!MatchTypeExpression(e, type, null, false))
9883                            {
9884                               FreeType(e.expType);
9885                               e.expType = null;
9886                               FreeType(type);
9887                               type = null;
9888                               break;
9889                            }                           
9890                         }
9891                      }
9892                   }
9893                   if(e.expType)
9894                   {
9895                      FreeType(e.expType);
9896                      e.expType = null;
9897                   }
9898                }
9899             }
9900             if(type)
9901             {
9902                typeStringBuf[0] = '\0';
9903                PrintType(type, typeStringBuf, false, true);
9904                typeString = typeStringBuf;
9905                FreeType(type);
9906                type = null;
9907             }
9908          }
9909          if(typeString)
9910          {
9911             /*
9912             (Container)(__extension__( { 
9913                int __arrayMembers[] = { 1, 7, 3, 4, 5 };
9914                BuiltInContainer __baseContainer
9915                {
9916                   data = __arrayMembers,
9917                   count = 5,
9918                   type = class(int),
9919                   _vTbl = class(BuiltInContainer)._vTbl,
9920                   _class = class(BuiltInContainer) };
9921                &__baseContainer;
9922              }))
9923             */
9924             
9925             char templateString[1024];
9926             OldList * declarations = MkList();
9927             OldList * instMembers = MkList();
9928             OldList * specs = MkList();
9929             OldList * initializers = MkList();
9930             char count[128];
9931             Expression e;
9932             Expression expExt;
9933             Declarator decl = SpecDeclFromString(typeString, specs, null);
9934             Context context = PushContext();
9935
9936             // sprintf(templateString, "Container<%s >", typeString);
9937             sprintf(templateString, "Container<%s>", typeString);
9938    
9939             ListAdd(instMembers, MkMemberInit(MkListOne(MkIdentifier("data")), MkInitializerAssignment(MkExpIdentifier(MkIdentifier("__internalList")))));
9940
9941             sprintf(count, "%d", exp.list->count);
9942             ListAdd(instMembers, MkMemberInit(MkListOne(MkIdentifier("count")), MkInitializerAssignment(MkExpConstant(count))));
9943
9944             ListAdd(instMembers, MkMemberInit(MkListOne(MkIdentifier("type")), MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), 
9945                CopyDeclarator(decl)))));
9946
9947             ListAdd(instMembers, MkMemberInit(MkListOne(MkIdentifier("_vTbl")), MkInitializerAssignment(MkExpMember(
9948                MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl")))));
9949
9950             ListAdd(instMembers, MkMemberInit(MkListOne(MkIdentifier("_class")), MkInitializerAssignment(
9951                MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null))));
9952
9953             if(exp.list)
9954             {
9955                type = ProcessTypeString(typeString, false);
9956                while(e = exp.list->first)
9957                {
9958                   exp.list->Remove(e);
9959                   e.destType = type;
9960                   type.refCount++;
9961                   ProcessExpressionType(e);
9962                   ListAdd(initializers, MkInitializerAssignment(e));
9963                }
9964                FreeType(type);
9965                delete exp.list;
9966             }
9967             
9968             ListAdd(declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorArray(PlugDeclarator(decl, 
9969                MkDeclaratorIdentifier(MkIdentifier("__internalList"))), null),
9970                MkInitializerList(initializers)))));
9971             ListAdd(declarations, MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName("BuiltInContainer")),
9972                MkExpIdentifier(MkIdentifier("__internalContainer")), MkListOne(MkMembersInitList(instMembers)))));
9973
9974             exp.expType = ProcessTypeString(templateString, false);
9975             exp.type = bracketsExp;
9976             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
9977                (expExt = MkExpExtensionCompound(MkCompoundStmt(
9978                   declarations, MkListOne(MkExpressionStmt(MkListOne(MkExpOp(null, '&', MkExpIdentifier(MkIdentifier("__internalContainer")))))))))));
9979             expExt.compound.compound.context = context;
9980             PopContext(context);
9981             ProcessExpressionType(expExt);
9982          }
9983          else
9984          {
9985             exp.expType = ProcessTypeString("Container", false);
9986             Compiler_Error($"Couldn't determine type of array elements\n");
9987          }
9988          break;
9989       }
9990    }
9991
9992    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
9993    {
9994       FreeType(exp.expType);
9995       exp.expType = ReplaceThisClassType(thisClass);
9996    }
9997
9998    // Resolve structures here
9999    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10000    {
10001       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10002       // TODO: Fix members reference...
10003       if(symbol)
10004       {
10005          if(exp.expType.kind != enumType)
10006          {
10007             Type member;
10008             String enumName = CopyString(exp.expType.enumName);
10009
10010             // Fixed a memory leak on self-referencing C structs typedefs
10011             // by instantiating a new type rather than simply copying members
10012             // into exp.expType
10013             FreeType(exp.expType);
10014             exp.expType = Type { };
10015             exp.expType.kind = symbol.type.kind;
10016             exp.expType.refCount++;
10017             exp.expType.enumName = enumName;
10018
10019             exp.expType.members = symbol.type.members;
10020             for(member = symbol.type.members.first; member; member = member.next)
10021                member.refCount++;
10022          }
10023          else
10024          {
10025             NamedLink member;
10026             for(member = symbol.type.members.first; member; member = member.next)
10027             {
10028                NamedLink value { name = CopyString(member.name) };
10029                exp.expType.members.Add(value);
10030             }
10031          }
10032       }
10033    }
10034
10035    yylloc = exp.loc;
10036    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10037    else if(exp.destType && !exp.destType.keepCast)
10038    {
10039       if(!CheckExpressionType(exp, exp.destType, false))
10040       {
10041          if(!exp.destType.count || unresolved)
10042          {
10043             if(!exp.expType)
10044             {
10045                yylloc = exp.loc;
10046                if(exp.destType.kind != ellipsisType)
10047                {
10048                   char type2[1024];
10049                   type2[0] = '\0';
10050                   if(inCompiler)
10051                   {
10052                      char expString[10240];
10053                      expString[0] = '\0';
10054
10055                      PrintType(exp.destType, type2, false, true);
10056
10057                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10058                      if(unresolved)
10059                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10060                      else if(exp.type != dummyExp)
10061                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10062                   }
10063                }
10064                else
10065                {
10066                   char expString[10240] ;
10067                   expString[0] = '\0';
10068                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10069
10070                   if(unresolved)
10071                      Compiler_Error($"unresolved identifier %s\n", expString);
10072                   else if(exp.type != dummyExp)
10073                      Compiler_Error($"couldn't determine type of %s\n", expString);
10074                }
10075             }
10076             else
10077             {
10078                char type1[1024];
10079                char type2[1024];
10080                type1[0] = '\0';
10081                type2[0] = '\0';
10082                if(inCompiler)
10083                {
10084                   PrintType(exp.expType, type1, false, true);
10085                   PrintType(exp.destType, type2, false, true);
10086                }
10087
10088                //CheckExpressionType(exp, exp.destType, false);
10089
10090                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10091                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType && 
10092                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10093                else
10094                {
10095                   char expString[10240];
10096                   expString[0] = '\0';
10097                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10098
10099 #ifdef _DEBUG
10100                   CheckExpressionType(exp, exp.destType, false);
10101 #endif
10102                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10103
10104                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10105                   FreeType(exp.expType);
10106                   exp.destType.refCount++;
10107                   exp.expType = exp.destType;
10108                }
10109             }
10110          }
10111       }
10112       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10113       {
10114          Expression newExp { };
10115          char typeString[1024];
10116          OldList * specs = MkList();
10117          Declarator decl;
10118
10119          typeString[0] = '\0';
10120
10121          *newExp = *exp;
10122
10123          if(exp.expType)  exp.expType.refCount++;
10124          if(exp.expType)  exp.expType.refCount++;
10125          exp.type = castExp;
10126          newExp.destType = exp.expType;
10127
10128          PrintType(exp.expType, typeString, false, false);
10129          decl = SpecDeclFromString(typeString, specs, null);
10130          
10131          exp.cast.typeName = MkTypeName(specs, decl);
10132          exp.cast.exp = newExp;
10133       }
10134    }
10135    else if(unresolved)
10136    {
10137       if(exp.identifier._class && exp.identifier._class.name)
10138          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10139       else if(exp.identifier.string && exp.identifier.string[0])
10140          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10141    }
10142    else if(!exp.expType && exp.type != dummyExp)
10143    {
10144       char expString[10240];
10145       expString[0] = '\0';
10146       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10147       Compiler_Error($"couldn't determine type of %s\n", expString);
10148    }
10149
10150    // Let's try to support any_object & typed_object here:
10151    ApplyAnyObjectLogic(exp);
10152
10153    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10154       exp.expType._class.registered.type == noHeadClass)
10155    {
10156       exp.byReference = true;
10157    }
10158    /*else if(!notByReference && exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10159       exp.destType._class.registered.type == noHeadClass)
10160    {
10161       exp.byReference = true;
10162    }*/
10163    yylloc = oldyylloc;
10164 }
10165
10166 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10167 {
10168    // THIS CODE WILL FIND NEXT MEMBER...
10169    if(*curMember) 
10170    {
10171       *curMember = (*curMember).next;
10172
10173       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10174       {
10175          *curMember = subMemberStack[--(*subMemberStackPos)];
10176          *curMember = (*curMember).next;
10177       }
10178
10179       // SKIP ALL PROPERTIES HERE...
10180       while((*curMember) && (*curMember).isProperty)
10181          *curMember = (*curMember).next;
10182
10183       if(subMemberStackPos)
10184       {
10185          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10186          {
10187             subMemberStack[(*subMemberStackPos)++] = *curMember;
10188
10189             *curMember = (*curMember).members.first;
10190             while(*curMember && (*curMember).isProperty)
10191                *curMember = (*curMember).next;                     
10192          }
10193       }
10194    }
10195    while(!*curMember)
10196    {
10197       if(!*curMember)
10198       {
10199          if(subMemberStackPos && *subMemberStackPos)
10200          {
10201             *curMember = subMemberStack[--(*subMemberStackPos)];
10202             *curMember = (*curMember).next;
10203          }
10204          else
10205          {
10206             Class lastCurClass = *curClass;
10207
10208             if(*curClass == _class) break;     // REACHED THE END
10209
10210             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10211             *curMember = (*curClass).membersAndProperties.first;
10212          }
10213
10214          while((*curMember) && (*curMember).isProperty)
10215             *curMember = (*curMember).next;
10216          if(subMemberStackPos)
10217          {
10218             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10219             {
10220                subMemberStack[(*subMemberStackPos)++] = *curMember;
10221
10222                *curMember = (*curMember).members.first;
10223                while(*curMember && (*curMember).isProperty)
10224                   *curMember = (*curMember).next;                     
10225             }
10226          }
10227       }
10228    }
10229 }
10230
10231
10232 static void ProcessInitializer(Initializer init, Type type)
10233 {
10234    switch(init.type)
10235    {
10236       case expInitializer:
10237          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10238          {
10239             // TESTING THIS FOR SHUTTING = 0 WARNING
10240             if(init.exp && !init.exp.destType)
10241             {
10242                FreeType(init.exp.destType);
10243                init.exp.destType = type;
10244                if(type) type.refCount++;
10245             }
10246             if(init.exp)
10247             {
10248                ProcessExpressionType(init.exp);
10249                init.isConstant = init.exp.isConstant;
10250             }
10251             break;
10252          }
10253          else
10254          {
10255             Expression exp = init.exp;
10256             Instantiation inst = exp.instance;
10257             MembersInit members;
10258
10259             init.type = listInitializer;
10260             init.list = MkList();
10261
10262             if(inst.members)
10263             {
10264                for(members = inst.members->first; members; members = members.next)
10265                {
10266                   if(members.type == dataMembersInit)
10267                   {
10268                      MemberInit member;
10269                      for(member = members.dataMembers->first; member; member = member.next)
10270                      {
10271                         ListAdd(init.list, member.initializer);
10272                         member.initializer = null;
10273                      }
10274                   }
10275                   // Discard all MembersInitMethod
10276                }
10277             }
10278             FreeExpression(exp);
10279          }
10280       case listInitializer:
10281       {
10282          Initializer i;
10283          Type initializerType = null;
10284          Class curClass = null;
10285          DataMember curMember = null;
10286          DataMember subMemberStack[256];
10287          int subMemberStackPos = 0;
10288
10289          if(type && type.kind == arrayType)
10290             initializerType = Dereference(type);
10291          else if(type && (type.kind == structType || type.kind == unionType))
10292             initializerType = type.members.first;
10293
10294          for(i = init.list->first; i; i = i.next)
10295          {
10296             if(type && type.kind == classType && type._class && type._class.registered)
10297             {
10298                // THIS IS FOR A C STYLE INSTANTIATION OF STRUCT CLASSES ONLY... WE ONLY CARE ABOUT DATA MEMBERS, AND ACTUAL MEMORY ORDER (PRIVATE MEMBERS ARE INCLUDED)
10299                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10300                // TODO: Generate error on initializing a private data member this way from another module...
10301                if(curMember)
10302                {
10303                   if(!curMember.dataType)
10304                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10305                   initializerType = curMember.dataType;
10306                }
10307             }
10308             ProcessInitializer(i, initializerType);
10309             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10310                initializerType = initializerType.next;
10311             if(!i.isConstant)
10312                init.isConstant = false;
10313          }
10314
10315          if(type && type.kind == arrayType)
10316             FreeType(initializerType);
10317
10318          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10319          {
10320             Compiler_Error($"Assigning list initializer to non list\n");
10321          }
10322          break;
10323       }
10324    }
10325 }
10326
10327 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10328 {
10329    switch(spec.type)
10330    {
10331       case baseSpecifier:
10332       {
10333          if(spec.specifier == THISCLASS)
10334          {
10335             if(thisClass)
10336             {
10337                spec.type = nameSpecifier;
10338                spec.name = ReplaceThisClass(thisClass);
10339                spec.symbol = FindClass(spec.name);
10340                ProcessSpecifier(spec, declareStruct);
10341             }
10342          }
10343          break;
10344       }
10345       case nameSpecifier:
10346       {
10347          Symbol symbol = FindType(curContext, spec.name);
10348          if(symbol)
10349             DeclareType(symbol.type, true, true);
10350          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10351             DeclareStruct(spec.name, false);
10352          break;
10353       }
10354       case enumSpecifier:
10355       {
10356          Enumerator e;
10357          if(spec.list)
10358          {
10359             for(e = spec.list->first; e; e = e.next)
10360             {
10361                if(e.exp)
10362                   ProcessExpressionType(e.exp);
10363             }
10364          }
10365          break;
10366       }
10367       case structSpecifier:
10368       case unionSpecifier:
10369       {
10370          if(spec.definitions)
10371          {
10372             ClassDef def;
10373             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10374             //if(symbol)
10375                ProcessClass(spec.definitions, symbol);
10376             /*else
10377             {
10378                for(def = spec.definitions->first; def; def = def.next)
10379                {
10380                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10381                      ProcessDeclaration(def.decl);
10382                }
10383             }*/
10384          }
10385          break;
10386       }
10387       /*
10388       case classSpecifier:
10389       {
10390          Symbol classSym = FindClass(spec.name);
10391          if(classSym && classSym.registered && classSym.registered.type == structClass)
10392             DeclareStruct(spec.name, false);
10393          break;
10394       }
10395       */
10396    }
10397 }
10398
10399
10400 static void ProcessDeclarator(Declarator decl)
10401 {
10402    switch(decl.type)
10403    {
10404       case identifierDeclarator:
10405          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10406          {
10407             FreeSpecifier(decl.identifier._class);
10408             decl.identifier._class = null;
10409          }
10410          break;
10411       case arrayDeclarator:
10412          if(decl.array.exp)
10413             ProcessExpressionType(decl.array.exp);
10414       case structDeclarator:
10415       case bracketsDeclarator:
10416       case functionDeclarator:
10417       case pointerDeclarator:
10418       case extendedDeclarator:
10419       case extendedDeclaratorEnd:
10420          if(decl.declarator)
10421             ProcessDeclarator(decl.declarator);
10422          if(decl.type == functionDeclarator)
10423          {
10424             Identifier id = GetDeclId(decl);
10425             if(id && id._class)
10426             {
10427                TypeName param
10428                {
10429                   qualifiers = MkListOne(id._class);
10430                   declarator = null;
10431                };
10432                if(!decl.function.parameters)
10433                   decl.function.parameters = MkList();               
10434                decl.function.parameters->Insert(null, param);
10435                id._class = null;
10436             }
10437             if(decl.function.parameters)
10438             {
10439                TypeName param;
10440                
10441                for(param = decl.function.parameters->first; param; param = param.next)
10442                {
10443                   if(param.qualifiers && param.qualifiers->first)
10444                   {
10445                      Specifier spec = param.qualifiers->first;
10446                      if(spec && spec.specifier == TYPED_OBJECT)
10447                      {
10448                         Declarator d = param.declarator;
10449                         TypeName newParam
10450                         {
10451                            qualifiers = MkListOne(MkSpecifier(VOID));
10452                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10453                         };
10454                         
10455                         FreeList(param.qualifiers, FreeSpecifier);
10456
10457                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10458                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10459
10460                         decl.function.parameters->Insert(param, newParam);
10461                         param = newParam;
10462                      }
10463                      else if(spec && spec.specifier == ANY_OBJECT)
10464                      {
10465                         Declarator d = param.declarator;
10466                         
10467                         FreeList(param.qualifiers, FreeSpecifier);
10468
10469                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10470                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);                        
10471                      }
10472                      else if(spec.specifier == THISCLASS)
10473                      {
10474                         if(thisClass)
10475                         {
10476                            spec.type = nameSpecifier;
10477                            spec.name = ReplaceThisClass(thisClass);
10478                            spec.symbol = FindClass(spec.name);
10479                            ProcessSpecifier(spec, false);
10480                         }
10481                      }
10482                   }
10483
10484                   if(param.declarator)
10485                      ProcessDeclarator(param.declarator);
10486                }
10487             }
10488          }
10489          break;
10490    }
10491 }
10492
10493 static void ProcessDeclaration(Declaration decl)
10494 {
10495    yylloc = decl.loc;
10496    switch(decl.type)
10497    {
10498       case initDeclaration:
10499       {
10500          bool declareStruct = false;
10501          /*
10502          lineNum = decl.pos.line;
10503          column = decl.pos.col;
10504          */
10505
10506          if(decl.declarators)
10507          {
10508             InitDeclarator d;
10509          
10510             for(d = decl.declarators->first; d; d = d.next)
10511             {
10512                Type type, subType;
10513                ProcessDeclarator(d.declarator);
10514
10515                type = ProcessType(decl.specifiers, d.declarator);
10516
10517                if(d.initializer)
10518                {
10519                   ProcessInitializer(d.initializer, type);
10520
10521                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }                  
10522                   
10523                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10524                      d.initializer.exp.type == instanceExp)
10525                   {
10526                      if(type.kind == classType && type._class == 
10527                         d.initializer.exp.expType._class)
10528                      {
10529                         Instantiation inst = d.initializer.exp.instance;
10530                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10531                         
10532                         d.initializer.exp.instance = null;
10533                         if(decl.specifiers)
10534                            FreeList(decl.specifiers, FreeSpecifier);
10535                         FreeList(decl.declarators, FreeInitDeclarator);
10536
10537                         d = null;
10538
10539                         decl.type = instDeclaration;
10540                         decl.inst = inst;
10541                      }
10542                   }
10543                }
10544                for(subType = type; subType;)
10545                {
10546                   if(subType.kind == classType)
10547                   {
10548                      declareStruct = true;
10549                      break;
10550                   }
10551                   else if(subType.kind == pointerType)
10552                      break;
10553                   else if(subType.kind == arrayType)
10554                      subType = subType.arrayType;
10555                   else
10556                      break;
10557                }
10558
10559                FreeType(type);
10560                if(!d) break;
10561             }
10562          }
10563
10564          if(decl.specifiers)
10565          {
10566             Specifier s;
10567             for(s = decl.specifiers->first; s; s = s.next)
10568             {
10569                ProcessSpecifier(s, declareStruct);
10570             }
10571          }
10572          break;
10573       }
10574       case instDeclaration:
10575       {
10576          ProcessInstantiationType(decl.inst);
10577          break;
10578       }
10579       case structDeclaration:
10580       {
10581          Specifier spec;
10582          Declarator d;
10583          bool declareStruct = false;
10584
10585          if(decl.declarators)
10586          {
10587             for(d = decl.declarators->first; d; d = d.next)
10588             {
10589                Type type = ProcessType(decl.specifiers, d.declarator);
10590                Type subType;
10591                ProcessDeclarator(d);
10592                for(subType = type; subType;)
10593                {
10594                   if(subType.kind == classType)
10595                   {
10596                      declareStruct = true;
10597                      break;
10598                   }
10599                   else if(subType.kind == pointerType)
10600                      break;
10601                   else if(subType.kind == arrayType)
10602                      subType = subType.arrayType;
10603                   else
10604                      break;
10605                }
10606                FreeType(type);
10607             }
10608          }
10609          if(decl.specifiers)
10610          {
10611             for(spec = decl.specifiers->first; spec; spec = spec.next)
10612                ProcessSpecifier(spec, declareStruct);
10613          }
10614          break;
10615       }
10616    }
10617 }
10618
10619 static FunctionDefinition curFunction;
10620
10621 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10622 {
10623    char propName[1024], propNameM[1024];
10624    char getName[1024], setName[1024];
10625    OldList * args;
10626
10627    DeclareProperty(prop, setName, getName);
10628
10629    // eInstance_FireWatchers(object, prop);
10630    strcpy(propName, "__ecereProp_");
10631    FullClassNameCat(propName, prop._class.fullName, false);
10632    strcat(propName, "_");
10633    // strcat(propName, prop.name);
10634    FullClassNameCat(propName, prop.name, true);
10635    MangleClassName(propName);
10636
10637    strcpy(propNameM, "__ecerePropM_");
10638    FullClassNameCat(propNameM, prop._class.fullName, false);
10639    strcat(propNameM, "_");
10640    // strcat(propNameM, prop.name);
10641    FullClassNameCat(propNameM, prop.name, true);
10642    MangleClassName(propNameM);
10643
10644    if(prop.isWatchable)
10645    {
10646       args = MkList();
10647       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10648       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10649       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10650
10651       args = MkList();
10652       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10653       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10654       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10655    }
10656
10657    
10658    {
10659       args = MkList();
10660       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10661       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10662       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10663
10664       args = MkList();
10665       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10666       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10667       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10668    }
10669    
10670    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) && 
10671       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
10672       curFunction.propSet.fireWatchersDone = true;
10673 }
10674
10675 static void ProcessStatement(Statement stmt)
10676 {
10677    yylloc = stmt.loc;
10678    /*
10679    lineNum = stmt.pos.line;
10680    column = stmt.pos.col;
10681    */
10682    switch(stmt.type)
10683    {
10684       case labeledStmt:
10685          ProcessStatement(stmt.labeled.stmt);
10686          break;
10687       case caseStmt:
10688          // This expression should be constant...
10689          if(stmt.caseStmt.exp)
10690          {
10691             FreeType(stmt.caseStmt.exp.destType);
10692             stmt.caseStmt.exp.destType = curSwitchType;
10693             if(curSwitchType) curSwitchType.refCount++;
10694             ProcessExpressionType(stmt.caseStmt.exp);
10695             ComputeExpression(stmt.caseStmt.exp);
10696          }
10697          if(stmt.caseStmt.stmt)
10698             ProcessStatement(stmt.caseStmt.stmt);
10699          break;
10700       case compoundStmt:
10701       {
10702          if(stmt.compound.context)
10703          {
10704             Declaration decl;
10705             Statement s;
10706
10707             Statement prevCompound = curCompound;
10708             Context prevContext = curContext;
10709
10710             if(!stmt.compound.isSwitch)
10711             {
10712                curCompound = stmt;
10713                curContext = stmt.compound.context;
10714             }
10715
10716             if(stmt.compound.declarations)
10717             {
10718                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
10719                   ProcessDeclaration(decl);
10720             }
10721             if(stmt.compound.statements)
10722             {
10723                for(s = stmt.compound.statements->first; s; s = s.next)
10724                   ProcessStatement(s);
10725             }
10726
10727             curContext = prevContext;
10728             curCompound = prevCompound;
10729          }
10730          break;
10731       }
10732       case expressionStmt:
10733       {
10734          Expression exp;
10735          if(stmt.expressions)
10736          {
10737             for(exp = stmt.expressions->first; exp; exp = exp.next)
10738                ProcessExpressionType(exp);
10739          }
10740          break;
10741       }
10742       case ifStmt:
10743       {
10744          Expression exp;
10745
10746          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
10747          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
10748          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
10749          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
10750          {
10751             ProcessExpressionType(exp);
10752          }
10753          if(stmt.ifStmt.stmt)
10754             ProcessStatement(stmt.ifStmt.stmt);
10755          if(stmt.ifStmt.elseStmt)
10756             ProcessStatement(stmt.ifStmt.elseStmt);
10757          break;
10758       }
10759       case switchStmt:
10760       {
10761          Type oldSwitchType = curSwitchType;
10762          if(stmt.switchStmt.exp)
10763          {
10764             Expression exp;
10765             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
10766             {
10767                if(!exp.next)
10768                {
10769                   /*
10770                   Type destType
10771                   {
10772                      kind = intType;
10773                      refCount = 1;
10774                   };
10775                   e.exp.destType = destType;
10776                   */
10777
10778                   ProcessExpressionType(exp);
10779                }
10780                if(!exp.next)
10781                   curSwitchType = exp.expType;
10782             }
10783          }
10784          ProcessStatement(stmt.switchStmt.stmt);
10785          curSwitchType = oldSwitchType;
10786          break;
10787       }
10788       case whileStmt:
10789       {
10790          if(stmt.whileStmt.exp)
10791          {
10792             Expression exp;
10793
10794             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
10795             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
10796             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
10797             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
10798             {
10799                ProcessExpressionType(exp);
10800             }
10801          }
10802          if(stmt.whileStmt.stmt)
10803             ProcessStatement(stmt.whileStmt.stmt);
10804          break;
10805       }
10806       case doWhileStmt:
10807       {
10808          if(stmt.doWhile.exp)
10809          {
10810             Expression exp;
10811
10812             if(stmt.doWhile.exp->last)
10813             {
10814                FreeType(((Expression)stmt.doWhile.exp->last).destType);
10815                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
10816                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
10817             }
10818             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
10819             {
10820                ProcessExpressionType(exp);
10821             }
10822          }
10823          if(stmt.doWhile.stmt)
10824             ProcessStatement(stmt.doWhile.stmt);
10825          break;
10826       }
10827       case forStmt:
10828       {
10829          Expression exp;
10830          if(stmt.forStmt.init)
10831             ProcessStatement(stmt.forStmt.init);
10832
10833          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
10834          {
10835             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
10836             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
10837             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
10838          }
10839
10840          if(stmt.forStmt.check)
10841             ProcessStatement(stmt.forStmt.check);
10842          if(stmt.forStmt.increment)
10843          {
10844             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
10845                ProcessExpressionType(exp);
10846          }
10847
10848          if(stmt.forStmt.stmt)
10849             ProcessStatement(stmt.forStmt.stmt);
10850          break;
10851       }
10852       case forEachStmt:
10853       {
10854          Identifier id = stmt.forEachStmt.id;
10855          OldList * exp = stmt.forEachStmt.exp;
10856          OldList * filter = stmt.forEachStmt.filter;
10857          Statement block = stmt.forEachStmt.stmt;
10858          char iteratorType[1024];
10859          Type source;
10860          Expression e;
10861          bool isBuiltin = exp && exp->last && 
10862             (((Expression)exp->last).type == ExpressionType::arrayExp || 
10863               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
10864          Expression arrayExp;
10865          char * typeString = null;
10866          int builtinCount = 0;
10867
10868          for(e = exp ? exp->first : null; e; e = e.next)
10869          {
10870             if(!e.next)
10871             {
10872                FreeType(e.destType);
10873                e.destType = ProcessTypeString("Container", false);
10874             }
10875             if(!isBuiltin || e.next)
10876                ProcessExpressionType(e);
10877          }
10878
10879          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
10880          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
10881             eClass_IsDerived(source._class.registered, containerClass)))
10882          {
10883             Class _class = source ? source._class.registered : null;
10884             Symbol symbol;
10885             Expression expIt = null;
10886             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
10887             Class arrayClass = eSystem_FindClass(privateModule, "Array");
10888             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
10889             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
10890             stmt.type = compoundStmt;
10891             
10892             stmt.compound.context = Context { };
10893             stmt.compound.context.parent = curContext;
10894             curContext = stmt.compound.context;
10895
10896             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
10897             {
10898                Class mapClass = eSystem_FindClass(privateModule, "Map");
10899                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
10900                isCustomAVLTree = true;
10901                if(eClass_IsDerived(source._class.registered, avlTreeClass))
10902                   isAVLTree = true;
10903                else if(eClass_IsDerived(source._class.registered, mapClass))
10904                   isMap = true;
10905             }
10906             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
10907             else if(source && eClass_IsDerived(source._class.registered, linkListClass)) 
10908             {
10909                Class listClass = eSystem_FindClass(privateModule, "List");
10910                isLinkList = true;
10911                isList = eClass_IsDerived(source._class.registered, listClass);
10912             }
10913
10914             if(isArray)
10915             {
10916                Declarator decl;
10917                OldList * specs = MkList();
10918                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
10919                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
10920                stmt.compound.declarations = MkListOne(
10921                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
10922                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
10923                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), 
10924                      MkInitializerAssignment(MkExpBrackets(exp))))));
10925             }
10926             else if(isBuiltin)
10927             {
10928                Type type = null;
10929                char typeStringBuf[1024];
10930                
10931                // TODO: Merge this code?
10932                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
10933                if(((Expression)exp->last).type == castExp)
10934                {
10935                   TypeName typeName = ((Expression)exp->last).cast.typeName;
10936                   if(typeName)
10937                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
10938                }
10939
10940                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
10941                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
10942                   arrayExp.destType._class.registered.templateArgs)
10943                {
10944                   Class templateClass = arrayExp.destType._class.registered;
10945                   typeString = templateClass.templateArgs[2].dataTypeString;
10946                }
10947                else if(arrayExp.list)
10948                {
10949                   // Guess type from expressions in the array
10950                   Expression e;
10951                   for(e = arrayExp.list->first; e; e = e.next)
10952                   {
10953                      ProcessExpressionType(e);
10954                      if(e.expType)
10955                      {
10956                         if(!type) { type = e.expType; type.refCount++; }
10957                         else
10958                         {
10959                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10960                            if(!MatchTypeExpression(e, type, null, false))
10961                            {
10962                               FreeType(type);
10963                               type = e.expType;
10964                               e.expType = null;
10965                               
10966                               e = arrayExp.list->first;
10967                               ProcessExpressionType(e);
10968                               if(e.expType)
10969                               {
10970                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
10971                                  if(!MatchTypeExpression(e, type, null, false))
10972                                  {
10973                                     FreeType(e.expType);
10974                                     e.expType = null;
10975                                     FreeType(type);
10976                                     type = null;
10977                                     break;
10978                                  }                           
10979                               }
10980                            }
10981                         }
10982                         if(e.expType)
10983                         {
10984                            FreeType(e.expType);
10985                            e.expType = null;
10986                         }
10987                      }
10988                   }
10989                   if(type)
10990                   {
10991                      typeStringBuf[0] = '\0';
10992                      PrintType(type, typeStringBuf, false, true);
10993                      typeString = typeStringBuf;
10994                      FreeType(type);
10995                   }
10996                }
10997                if(typeString)
10998                {
10999                   OldList * initializers = MkList();
11000                   Declarator decl;
11001                   OldList * specs = MkList();
11002                   if(arrayExp.list)
11003                   {
11004                      Expression e;
11005
11006                      builtinCount = arrayExp.list->count;
11007                      type = ProcessTypeString(typeString, false);
11008                      while(e = arrayExp.list->first)
11009                      {
11010                         arrayExp.list->Remove(e);
11011                         e.destType = type;
11012                         type.refCount++;
11013                         ProcessExpressionType(e);
11014                         ListAdd(initializers, MkInitializerAssignment(e));
11015                      }
11016                      FreeType(type);
11017                      delete arrayExp.list;
11018                   }
11019                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11020                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier), 
11021                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11022
11023                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorArray(PlugDeclarator(
11024                      /*CopyDeclarator(*/decl/*)*/, MkDeclaratorIdentifier(MkIdentifier("__internalArray"))), null), MkInitializerList(initializers)))));
11025                   
11026                   FreeList(exp, FreeExpression);
11027                }
11028                else
11029                {
11030                   arrayExp.expType = ProcessTypeString("Container", false);
11031                   Compiler_Error($"Couldn't determine type of array elements\n");
11032                }
11033
11034                /*
11035                Declarator decl;
11036                OldList * specs = MkList();
11037
11038                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
11039                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11040                stmt.compound.declarations = MkListOne(
11041                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11042                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11043                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))), 
11044                      MkInitializerAssignment(MkExpBrackets(exp))))));
11045                */
11046             }
11047             else if(isLinkList && !isList)
11048             {
11049                Declarator decl;
11050                OldList * specs = MkList();
11051                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11052                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11053                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11054                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")), 
11055                      MkInitializerAssignment(MkExpBrackets(exp))))));
11056             }
11057             /*else if(isCustomAVLTree)
11058             {
11059                Declarator decl;
11060                OldList * specs = MkList();
11061                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11062                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11063                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11064                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")), 
11065                      MkInitializerAssignment(MkExpBrackets(exp))))));
11066             }*/
11067             else if(_class.templateArgs)
11068             {
11069                if(isMap)
11070                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11071                else
11072                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11073
11074                stmt.compound.declarations = MkListOne(
11075                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11076                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null, 
11077                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11078             }
11079             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11080
11081             if(block && block.type == compoundStmt && block.compound.context)
11082             {
11083                block.compound.context.parent = stmt.compound.context;
11084             }
11085             if(filter)
11086             {
11087                block = MkIfStmt(filter, block, null);
11088             }
11089             if(isArray)
11090             {
11091                stmt.compound.statements = MkListOne(MkForStmt(
11092                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11093                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11094                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11095                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11096                   block));
11097               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11098               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11099               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11100             }
11101             else if(isBuiltin)
11102             {
11103                char count[128];
11104                //OldList * specs = MkList();
11105                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11106
11107                sprintf(count, "%d", builtinCount);
11108
11109                stmt.compound.statements = MkListOne(MkForStmt(
11110                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11111                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11112                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11113                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11114                   block));
11115
11116                /*
11117                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11118                stmt.compound.statements = MkListOne(MkForStmt(
11119                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11120                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11121                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11122                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11123                   block));
11124               */
11125               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11126               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11127               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11128             }
11129             else if(isLinkList && !isList)
11130             {
11131                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11132                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11133                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString && 
11134                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11135                {
11136                   stmt.compound.statements = MkListOne(MkForStmt(
11137                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11138                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11139                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11140                      block));
11141                }
11142                else
11143                {
11144                   OldList * specs = MkList();
11145                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11146                   stmt.compound.statements = MkListOne(MkForStmt(
11147                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11148                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11149                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11150                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11151                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11152                      block));
11153                }
11154                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11155                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11156                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11157             }
11158             /*else if(isCustomAVLTree)
11159             {
11160                stmt.compound.statements = MkListOne(MkForStmt(
11161                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11162                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11163                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11164                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11165                   block));
11166
11167                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11168                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11169                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11170             }*/
11171             else
11172             {
11173                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11174                   MkIdentifier("Next")), null)), block));
11175             }
11176             ProcessExpressionType(expIt);
11177             if(stmt.compound.declarations->first)
11178                ProcessDeclaration(stmt.compound.declarations->first);
11179
11180             if(symbol) 
11181                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11182
11183             ProcessStatement(stmt);
11184             curContext = stmt.compound.context.parent;
11185             break;
11186          }
11187          else
11188          {
11189             Compiler_Error($"Expression is not a container\n");
11190          }
11191          break;
11192       }
11193       case gotoStmt:
11194          break;
11195       case continueStmt:
11196          break;
11197       case breakStmt:
11198          break;
11199       case returnStmt:
11200       {
11201          Expression exp;
11202          if(stmt.expressions)
11203          {
11204             for(exp = stmt.expressions->first; exp; exp = exp.next)
11205             {
11206                if(!exp.next)
11207                {
11208                   if(curFunction && !curFunction.type)
11209                      curFunction.type = ProcessType(
11210                         curFunction.specifiers, curFunction.declarator);
11211                   FreeType(exp.destType);
11212                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11213                   if(exp.destType) exp.destType.refCount++;
11214                }
11215                ProcessExpressionType(exp);
11216             }
11217          }
11218          break;
11219       }
11220       case badDeclarationStmt:
11221       {
11222          ProcessDeclaration(stmt.decl);
11223          break;
11224       }
11225       case asmStmt:
11226       {
11227          AsmField field;
11228          if(stmt.asmStmt.inputFields)
11229          {
11230             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11231                if(field.expression)
11232                   ProcessExpressionType(field.expression);
11233          }
11234          if(stmt.asmStmt.outputFields)
11235          {
11236             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11237                if(field.expression)
11238                   ProcessExpressionType(field.expression);
11239          }
11240          if(stmt.asmStmt.clobberedFields)
11241          {
11242             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11243             {
11244                if(field.expression)
11245                   ProcessExpressionType(field.expression);
11246             }
11247          }
11248          break;
11249       }
11250       case watchStmt:
11251       {
11252          PropertyWatch propWatch;
11253          OldList * watches = stmt._watch.watches;
11254          Expression object = stmt._watch.object;
11255          Expression watcher = stmt._watch.watcher;
11256          if(watcher)
11257             ProcessExpressionType(watcher);
11258          if(object)
11259             ProcessExpressionType(object);
11260
11261          if(inCompiler)
11262          {
11263             if(watcher || thisClass)
11264             {
11265                External external = curExternal;
11266                Context context = curContext;
11267
11268                stmt.type = expressionStmt;
11269                stmt.expressions = MkList();
11270
11271                curExternal = external.prev;
11272
11273                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11274                {
11275                   ClassFunction func;
11276                   char watcherName[1024];
11277                   Class watcherClass = watcher ? 
11278                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11279                   External createdExternal;
11280
11281                   // Create a declaration above
11282                   External externalDecl = MkExternalDeclaration(null);
11283                   ast->Insert(curExternal.prev, externalDecl);
11284
11285                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11286                   if(propWatch.deleteWatch)
11287                      strcat(watcherName, "_delete");
11288                   else
11289                   {
11290                      Identifier propID;
11291                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11292                      {
11293                         strcat(watcherName, "_");
11294                         strcat(watcherName, propID.string);
11295                      }
11296                   }
11297
11298                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11299                   {
11300                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11301                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11302                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11303                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11304                      ProcessClassFunctionBody(func, propWatch.compound);
11305                      propWatch.compound = null;
11306
11307                      //afterExternal = afterExternal ? afterExternal : curExternal;
11308
11309                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11310                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11311                      // TESTING THIS...
11312                      createdExternal.symbol.idCode = external.symbol.idCode;
11313
11314                      curExternal = createdExternal;
11315                      ProcessFunction(createdExternal.function);
11316
11317
11318                      // Create a declaration above
11319                      {
11320                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
11321                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11322                         externalDecl.declaration = decl;
11323                         if(decl.symbol && !decl.symbol.pointerExternal)
11324                            decl.symbol.pointerExternal = externalDecl;
11325                      }
11326
11327                      if(propWatch.deleteWatch)
11328                      {
11329                         OldList * args = MkList();
11330                         ListAdd(args, CopyExpression(object));
11331                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11332                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11333                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11334                      }
11335                      else
11336                      {
11337                         Class _class = object.expType._class.registered;
11338                         Identifier propID;
11339
11340                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11341                         {
11342                            char propName[1024];
11343                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11344                            if(prop)
11345                            {
11346                               char getName[1024], setName[1024];
11347                               OldList * args = MkList();
11348
11349                               DeclareProperty(prop, setName, getName);                              
11350                               
11351                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11352                               strcpy(propName, "__ecereProp_");
11353                               FullClassNameCat(propName, prop._class.fullName, false);
11354                               strcat(propName, "_");
11355                               // strcat(propName, prop.name);
11356                               FullClassNameCat(propName, prop.name, true);
11357
11358                               ListAdd(args, CopyExpression(object));
11359                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11360                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11361                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11362
11363                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11364                            }
11365                            else
11366                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11367                         }
11368                      }
11369                   }
11370                   else
11371                      Compiler_Error($"Invalid watched object\n");
11372                }
11373
11374                curExternal = external;
11375                curContext = context;
11376
11377                if(watcher)
11378                   FreeExpression(watcher);
11379                if(object)
11380                   FreeExpression(object);
11381                FreeList(watches, FreePropertyWatch);
11382             }
11383             else
11384                Compiler_Error($"No observer specified and not inside a _class\n");
11385          }
11386          else
11387          {
11388             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11389             {
11390                ProcessStatement(propWatch.compound);
11391             }
11392
11393          }
11394          break;
11395       }
11396       case fireWatchersStmt:
11397       {
11398          OldList * watches = stmt._watch.watches;
11399          Expression object = stmt._watch.object;
11400          Class _class;
11401          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11402          // printf("%X\n", watches);
11403          // printf("%X\n", stmt._watch.watches);
11404          if(object)
11405             ProcessExpressionType(object);
11406
11407          if(inCompiler)
11408          {
11409             _class = object ? 
11410                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11411
11412             if(_class)
11413             {
11414                Identifier propID;
11415
11416                stmt.type = expressionStmt;
11417                stmt.expressions = MkList();
11418
11419                // Check if we're inside a property set
11420                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11421                {
11422                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11423                }
11424                else if(!watches)
11425                {
11426                   //Compiler_Error($"No property specified and not inside a property set\n");
11427                }
11428                if(watches)
11429                {
11430                   for(propID = watches->first; propID; propID = propID.next)
11431                   {
11432                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11433                      if(prop)
11434                      {
11435                         CreateFireWatcher(prop, object, stmt);
11436                      }
11437                      else
11438                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11439                   }
11440                }
11441                else
11442                {
11443                   // Fire all properties!
11444                   Property prop;
11445                   Class base;
11446                   for(base = _class; base; base = base.base)
11447                   {
11448                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11449                      {
11450                         if(prop.isProperty && prop.isWatchable)
11451                         {
11452                            CreateFireWatcher(prop, object, stmt);
11453                         }
11454                      }
11455                   }
11456                }
11457
11458                if(object)
11459                   FreeExpression(object);
11460                FreeList(watches, FreeIdentifier);
11461             }
11462             else
11463                Compiler_Error($"Invalid object specified and not inside a class\n");
11464          }
11465          break;
11466       }
11467       case stopWatchingStmt:
11468       {
11469          OldList * watches = stmt._watch.watches;
11470          Expression object = stmt._watch.object;
11471          Expression watcher = stmt._watch.watcher;
11472          Class _class;
11473          if(object)
11474             ProcessExpressionType(object);
11475          if(watcher)
11476             ProcessExpressionType(watcher);
11477          if(inCompiler)
11478          {
11479             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11480
11481             if(watcher || thisClass)
11482             {
11483                if(_class)
11484                {
11485                   Identifier propID;
11486
11487                   stmt.type = expressionStmt;
11488                   stmt.expressions = MkList();
11489
11490                   if(!watches)
11491                   {
11492                      OldList * args;
11493                      // eInstance_StopWatching(object, null, watcher); 
11494                      args = MkList();
11495                      ListAdd(args, CopyExpression(object));
11496                      ListAdd(args, MkExpConstant("0"));
11497                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11498                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11499                   }
11500                   else
11501                   {
11502                      for(propID = watches->first; propID; propID = propID.next)
11503                      {
11504                         char propName[1024];
11505                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11506                         if(prop)
11507                         {
11508                            char getName[1024], setName[1024];
11509                            OldList * args = MkList();
11510
11511                            DeclareProperty(prop, setName, getName);
11512          
11513                            // eInstance_StopWatching(object, prop, watcher); 
11514                            strcpy(propName, "__ecereProp_");
11515                            FullClassNameCat(propName, prop._class.fullName, false);
11516                            strcat(propName, "_");
11517                            // strcat(propName, prop.name);
11518                            FullClassNameCat(propName, prop.name, true);
11519                            MangleClassName(propName);
11520
11521                            ListAdd(args, CopyExpression(object));
11522                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11523                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11524                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11525                         }
11526                         else
11527                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11528                      }
11529                   }
11530
11531                   if(object)
11532                      FreeExpression(object);
11533                   if(watcher)
11534                      FreeExpression(watcher);
11535                   FreeList(watches, FreeIdentifier);
11536                }
11537                else
11538                   Compiler_Error($"Invalid object specified and not inside a class\n");
11539             }
11540             else
11541                Compiler_Error($"No observer specified and not inside a class\n");
11542          }
11543          break;
11544       }
11545    }
11546 }
11547
11548 static void ProcessFunction(FunctionDefinition function)
11549 {
11550    Identifier id = GetDeclId(function.declarator);
11551    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11552    Type type = symbol ? symbol.type : null;
11553    Class oldThisClass = thisClass;
11554    Context oldTopContext = topContext;
11555
11556    yylloc = function.loc;
11557    // Process thisClass
11558    
11559    if(type && type.thisClass)
11560    {
11561       Symbol classSym = type.thisClass;
11562       Class _class = type.thisClass.registered;
11563       char className[1024];
11564       char structName[1024];
11565       Declarator funcDecl;
11566       Symbol thisSymbol;
11567
11568       bool typedObject = false;
11569
11570       if(_class && !_class.base)
11571       {
11572          _class = currentClass;
11573          if(_class && !_class.symbol)
11574             _class.symbol = FindClass(_class.fullName);
11575          classSym = _class ? _class.symbol : null;
11576          typedObject = true;
11577       }
11578
11579       thisClass = _class;
11580
11581       if(inCompiler && _class)
11582       {
11583          if(type.kind == functionType)
11584          {
11585             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11586             {
11587                //TypeName param = symbol.type.params.first;
11588                Type param = symbol.type.params.first;
11589                symbol.type.params.Remove(param);
11590                //FreeTypeName(param);
11591                FreeType(param);
11592             }
11593             if(type.classObjectType != classPointer)
11594             {
11595                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11596                symbol.type.staticMethod = true;
11597                symbol.type.thisClass = null;
11598
11599                // HIGH DANGER: VERIFYING THIS...
11600                symbol.type.extraParam = false;
11601             }
11602          }
11603
11604          strcpy(className, "__ecereClass_");
11605          FullClassNameCat(className, _class.fullName, true);
11606
11607          MangleClassName(className);
11608
11609          structName[0] = 0;
11610          FullClassNameCat(structName, _class.fullName, false);
11611
11612          // [class] this
11613          
11614
11615          funcDecl = GetFuncDecl(function.declarator);
11616          if(funcDecl)
11617          {
11618             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11619             {
11620                TypeName param = funcDecl.function.parameters->first;
11621                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11622                {
11623                   funcDecl.function.parameters->Remove(param);
11624                   FreeTypeName(param);
11625                }
11626             }
11627
11628             // DANGER: Watch for this... Check if it's a Conversion?
11629             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11630             
11631             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
11632             if(!function.propertyNoThis)
11633             {
11634                TypeName thisParam;
11635                
11636                if(type.classObjectType != classPointer)
11637                {
11638                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11639                   if(!funcDecl.function.parameters)
11640                      funcDecl.function.parameters = MkList();
11641                   funcDecl.function.parameters->Insert(null, thisParam);
11642                }
11643
11644                if(typedObject)
11645                {
11646                   if(type.classObjectType != classPointer)
11647                   {
11648                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
11649                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
11650                   }
11651
11652                   thisParam = TypeName
11653                   {
11654                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11655                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11656                   };
11657                   funcDecl.function.parameters->Insert(null, thisParam);
11658                }
11659             }
11660          }
11661
11662          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11663          {
11664             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11665             funcDecl = GetFuncDecl(initDecl.declarator);
11666             if(funcDecl)
11667             {
11668                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11669                {
11670                   TypeName param = funcDecl.function.parameters->first;
11671                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11672                   {
11673                      funcDecl.function.parameters->Remove(param);
11674                      FreeTypeName(param);
11675                   }
11676                }
11677
11678                if(type.classObjectType != classPointer)
11679                {
11680                   // DANGER: Watch for this... Check if it's a Conversion?
11681                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11682                   {
11683                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11684
11685                      if(!funcDecl.function.parameters)
11686                         funcDecl.function.parameters = MkList();
11687                      funcDecl.function.parameters->Insert(null, thisParam);
11688                   }
11689                }
11690             }         
11691          }
11692       }
11693       
11694       // Add this to the context
11695       if(function.body)
11696       {
11697          if(type.classObjectType != classPointer)
11698          {
11699             thisSymbol = Symbol
11700             {
11701                string = CopyString("this");
11702                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
11703             };
11704             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
11705
11706             if(typedObject && thisSymbol.type)
11707             {
11708                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
11709                thisSymbol.type.byReference = type.byReference;
11710                /*
11711                thisSymbol = Symbol { string = CopyString("class") };
11712                function.body.compound.context.symbols.Add(thisSymbol);
11713                */
11714             }
11715          }
11716       }
11717
11718       // Pointer to class data
11719       
11720       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
11721       {
11722          DataMember member = null;
11723          {
11724             Class base;
11725             for(base = _class; base && base.type != systemClass; base = base.next)
11726             {
11727                for(member = base.membersAndProperties.first; member; member = member.next)
11728                   if(!member.isProperty)
11729                      break;
11730                if(member)
11731                   break;
11732             }
11733          }
11734          for(member = _class.membersAndProperties.first; member; member = member.next)
11735             if(!member.isProperty)
11736                break;
11737          if(member)
11738          {
11739             char pointerName[1024];
11740    
11741             Declaration decl;
11742             Initializer initializer;
11743             Expression exp, bytePtr;
11744    
11745             strcpy(pointerName, "__ecerePointer_");
11746             FullClassNameCat(pointerName, _class.fullName, false);
11747             {
11748                char className[1024];
11749                strcpy(className, "__ecereClass_");
11750                FullClassNameCat(className, classSym.string, true);
11751                MangleClassName(className);
11752
11753                // Testing This
11754                DeclareClass(classSym, className);
11755             }
11756
11757             // ((byte *) this)
11758             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
11759
11760             if(_class.fixed)
11761             {
11762                char string[256];
11763                sprintf(string, "%d", _class.offset);
11764                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
11765             }
11766             else
11767             {
11768                // ([bytePtr] + [className]->offset)
11769                exp = QBrackets(MkExpOp(bytePtr, '+',
11770                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
11771             }
11772
11773             // (this ? [exp] : 0)
11774             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
11775             exp.expType = Type
11776             {
11777                refCount = 1;
11778                kind = pointerType;
11779                type = Type { refCount = 1, kind = voidType };
11780             };
11781    
11782             if(function.body)
11783             {
11784                yylloc = function.body.loc;
11785                // ([structName] *) [exp]
11786                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
11787                initializer = MkInitializerAssignment(
11788                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
11789
11790                // [structName] * [pointerName] = [initializer];
11791                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
11792
11793                {
11794                   Context prevContext = curContext;
11795                   curContext = function.body.compound.context;
11796
11797                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
11798                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
11799
11800                   curContext = prevContext;
11801                }
11802
11803                // WHY?
11804                decl.symbol = null;
11805
11806                if(!function.body.compound.declarations)
11807                   function.body.compound.declarations = MkList();
11808                function.body.compound.declarations->Insert(null, decl);
11809             }
11810          }
11811       }
11812       
11813
11814       // Loop through the function and replace undeclared identifiers
11815       // which are a member of the class (methods, properties or data)
11816       // by "this.[member]"
11817    }
11818    else
11819       thisClass = null;
11820
11821    if(id)
11822    {
11823       FreeSpecifier(id._class);
11824       id._class = null;
11825
11826       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11827       {
11828          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11829          id = GetDeclId(initDecl.declarator);
11830
11831          FreeSpecifier(id._class);
11832          id._class = null;
11833       }
11834    }
11835    if(function.body)
11836       topContext = function.body.compound.context;
11837    {
11838       FunctionDefinition oldFunction = curFunction;
11839       curFunction = function;
11840       if(function.body)
11841          ProcessStatement(function.body);
11842
11843       // If this is a property set and no firewatchers has been done yet, add one here
11844       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
11845       {
11846          Statement prevCompound = curCompound;
11847          Context prevContext = curContext;
11848
11849          Statement fireWatchers = MkFireWatchersStmt(null, null);
11850          if(!function.body.compound.statements) function.body.compound.statements = MkList();
11851          ListAdd(function.body.compound.statements, fireWatchers);
11852
11853          curCompound = function.body;
11854          curContext = function.body.compound.context;
11855
11856          ProcessStatement(fireWatchers);
11857
11858          curContext = prevContext;
11859          curCompound = prevCompound;
11860
11861       }
11862
11863       curFunction = oldFunction;
11864    }
11865
11866    if(function.declarator)
11867    {
11868       ProcessDeclarator(function.declarator);
11869    }
11870
11871    topContext = oldTopContext;
11872    thisClass = oldThisClass;
11873 }
11874
11875 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
11876 static void ProcessClass(OldList definitions, Symbol symbol)
11877 {
11878    ClassDef def;
11879    External external = curExternal;
11880    Class regClass = symbol ? symbol.registered : null;
11881
11882    // Process all functions
11883    for(def = definitions.first; def; def = def.next)
11884    {
11885       if(def.type == functionClassDef)
11886       {
11887          if(def.function.declarator)
11888             curExternal = def.function.declarator.symbol.pointerExternal;
11889          else
11890             curExternal = external;
11891
11892          ProcessFunction((FunctionDefinition)def.function);
11893       }
11894       else if(def.type == declarationClassDef)
11895       {
11896          if(def.decl.type == instDeclaration)
11897          {
11898             thisClass = regClass;
11899             ProcessInstantiationType(def.decl.inst);
11900             thisClass = null;
11901          }
11902          // Testing this
11903          else
11904          {
11905             Class backThisClass = thisClass;
11906             if(regClass) thisClass = regClass;
11907             ProcessDeclaration(def.decl);
11908             thisClass = backThisClass;
11909          }
11910       }
11911       else if(def.type == defaultPropertiesClassDef && def.defProperties)
11912       {
11913          MemberInit defProperty;
11914
11915          // Add this to the context
11916          Symbol thisSymbol = Symbol
11917          {
11918             string = CopyString("this");
11919             type = regClass ? MkClassType(regClass.fullName) : null;
11920          };
11921          globalContext.symbols.Add((BTNode)thisSymbol);
11922          
11923          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
11924          {
11925             thisClass = regClass;
11926             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
11927             thisClass = null;
11928          }
11929
11930          globalContext.symbols.Remove((BTNode)thisSymbol);
11931          FreeSymbol(thisSymbol);
11932       }
11933       else if(def.type == propertyClassDef && def.propertyDef)
11934       {
11935          PropertyDef prop = def.propertyDef;
11936
11937          // Add this to the context
11938          /*
11939          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
11940          globalContext.symbols.Add(thisSymbol);
11941          */
11942          
11943          thisClass = regClass;
11944          if(prop.setStmt)
11945          {
11946             if(regClass)
11947             {
11948                Symbol thisSymbol
11949                {
11950                   string = CopyString("this");
11951                   type = MkClassType(regClass.fullName);
11952                };
11953                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
11954             }
11955
11956             curExternal = prop.symbol ? prop.symbol.externalSet : null;
11957             ProcessStatement(prop.setStmt);
11958          }
11959          if(prop.getStmt)
11960          {
11961             if(regClass)
11962             {
11963                Symbol thisSymbol
11964                {
11965                   string = CopyString("this");
11966                   type = MkClassType(regClass.fullName);
11967                };
11968                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
11969             }
11970
11971             curExternal = prop.symbol ? prop.symbol.externalGet : null;
11972             ProcessStatement(prop.getStmt);
11973          }
11974          if(prop.issetStmt)
11975          {
11976             if(regClass)
11977             {
11978                Symbol thisSymbol
11979                {
11980                   string = CopyString("this");
11981                   type = MkClassType(regClass.fullName);
11982                };
11983                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
11984             }
11985
11986             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
11987             ProcessStatement(prop.issetStmt);
11988          }
11989
11990          thisClass = null;
11991
11992          /*
11993          globalContext.symbols.Remove(thisSymbol);
11994          FreeSymbol(thisSymbol);
11995          */
11996       }
11997       else if(def.type == propertyWatchClassDef && def.propertyWatch)
11998       {
11999          PropertyWatch propertyWatch = def.propertyWatch;
12000         
12001          thisClass = regClass;
12002          if(propertyWatch.compound)
12003          {
12004             Symbol thisSymbol
12005             {
12006                string = CopyString("this");
12007                type = regClass ? MkClassType(regClass.fullName) : null;
12008             };
12009
12010             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12011
12012             curExternal = null;
12013             ProcessStatement(propertyWatch.compound);
12014          }
12015          thisClass = null;
12016       }
12017    }
12018 }
12019
12020 void ComputeDataTypes()
12021 {
12022    External external;
12023    External temp { };
12024    currentClass = null;
12025
12026    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12027
12028    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12029
12030    // WHERE SHOULD THIS GO?
12031    // curExternal = ast->first;
12032    ast->Insert(null, temp);
12033
12034    curExternal = temp;
12035
12036    DeclareStruct("ecere::com::Class", false);
12037    DeclareStruct("ecere::com::Instance", false);
12038    DeclareStruct("ecere::com::Property", false);
12039    DeclareStruct("ecere::com::DataMember", false);
12040    DeclareStruct("ecere::com::Method", false);
12041    DeclareStruct("ecere::com::SerialBuffer", false);
12042    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12043
12044    ast->Remove(temp);
12045
12046    for(external = ast->first; external; external = external.next)
12047    {
12048       afterExternal = curExternal = external;
12049       if(external.type == functionExternal)
12050       {
12051          currentClass = external.function._class;
12052          ProcessFunction(external.function);
12053       }
12054       // There shouldn't be any _class member access here anyways...
12055       else if(external.type == declarationExternal)
12056       {
12057          currentClass = null;
12058          ProcessDeclaration(external.declaration);
12059       }
12060       else if(external.type == classExternal)
12061       {
12062          ClassDefinition _class = external._class;
12063          currentClass = external.symbol.registered;
12064          if(_class.definitions)
12065          {
12066             ProcessClass(_class.definitions, _class.symbol);
12067          }
12068          if(inCompiler)
12069          {
12070             // Free class data...
12071             ast->Remove(external);
12072             delete external;
12073          }
12074       }
12075       else if(external.type == nameSpaceExternal)
12076       {
12077          thisNameSpace = external.id.string;
12078       }
12079    }
12080    currentClass = null;
12081    thisNameSpace = null;
12082
12083    delete temp.symbol;
12084    delete temp;
12085 }