compiler/libec; samples/openrider: (#839, #840) Fixed issues with typed_object method...
[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 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case charType:
92          case shortType:
93          case intType:
94          case int64Type:
95          case intPtrType:
96          case intSizeType:
97             if(type1.passAsTemplate && !type2.passAsTemplate)
98                return true;
99             return type1.isSigned != type2.isSigned;
100          case classType:
101             return type1._class != type2._class;
102          case pointerType:
103             return NeedCast(type1.type, type2.type);
104          default:
105             return true; //false; ????
106       }
107    }
108    return true;
109 }
110
111 static void ReplaceClassMembers(Expression exp, Class _class)
112 {
113    if(exp.type == identifierExp && exp.identifier)
114    {
115       Identifier id = exp.identifier;
116       Context ctx;
117       Symbol symbol = null;
118       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
119       {
120          // First, check if the identifier is declared inside the function
121          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
122          {
123             symbol = (Symbol)ctx.symbols.FindString(id.string);
124             if(symbol) break;
125          }
126       }
127
128       // If it is not, check if it is a member of the _class
129       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
130       {
131          Property prop = eClass_FindProperty(_class, id.string, privateModule);
132          Method method = null;
133          DataMember member = null;
134          ClassProperty classProp = null;
135          if(!prop)
136          {
137             method = eClass_FindMethod(_class, id.string, privateModule);
138          }
139          if(!prop && !method)
140             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
141          if(!prop && !method && !member)
142          {
143             classProp = eClass_FindClassProperty(_class, id.string);
144          }
145          if(prop || method || member || classProp)
146          {
147             // Replace by this.[member]
148             exp.type = memberExp;
149             exp.member.member = id;
150             exp.member.memberType = unresolvedMember;
151             exp.member.exp = QMkExpId("this");
152             //exp.member.exp.loc = exp.loc;
153             exp.addedThis = true;
154          }
155          else if(_class && _class.templateParams.first)
156          {
157             Class sClass;
158             for(sClass = _class; sClass; sClass = sClass.base)
159             {
160                if(sClass.templateParams.first)
161                {
162                   ClassTemplateParameter param;
163                   for(param = sClass.templateParams.first; param; param = param.next)
164                   {
165                      if(param.type == expression && !strcmp(param.name, id.string))
166                      {
167                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
168
169                         if(argExp)
170                         {
171                            Declarator decl;
172                            OldList * specs = MkList();
173
174                            FreeIdentifier(exp.member.member);
175
176                            ProcessExpressionType(argExp);
177
178                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
179
180                            exp.expType = ProcessType(specs, decl);
181
182                            // *[expType] *[argExp]
183                            exp.type = bracketsExp;
184                            exp.list = MkListOne(MkExpOp(null, '*',
185                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
186                         }
187                      }
188                   }
189                }
190             }
191          }
192       }
193    }
194 }
195
196 ////////////////////////////////////////////////////////////////////////
197 // PRINTING ////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////
199
200 public char * PrintInt(int64 result)
201 {
202    char temp[100];
203    if(result > MAXINT64)
204       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
205    else
206       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
207    return CopyString(temp);
208 }
209
210 public char * PrintUInt(uint64 result)
211 {
212    char temp[100];
213    if(result > MAXDWORD)
214       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
215    else if(result > MAXINT)
216       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
217    else
218       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
219    return CopyString(temp);
220 }
221
222 public char * PrintInt64(int64 result)
223 {
224    char temp[100];
225    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintUInt64(uint64 result)
230 {
231    char temp[100];
232    if(result > MAXINT64)
233       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
234    else
235       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintHexUInt(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
244    else
245       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
246    return CopyString(temp);
247 }
248
249 public char * PrintHexUInt64(uint64 result)
250 {
251    char temp[100];
252    if(result > MAXDWORD)
253       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
254    else
255       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
256    return CopyString(temp);
257 }
258
259 public char * PrintShort(short result)
260 {
261    char temp[100];
262    sprintf(temp, "%d", (unsigned short)result);
263    return CopyString(temp);
264 }
265
266 public char * PrintUShort(unsigned short result)
267 {
268    char temp[100];
269    if(result > 32767)
270       sprintf(temp, "0x%X", (int)result);
271    else
272       sprintf(temp, "%d", (int)result);
273    return CopyString(temp);
274 }
275
276 public char * PrintChar(char result)
277 {
278    char temp[100];
279    if(result > 0 && isprint(result))
280       sprintf(temp, "'%c'", result);
281    else if(result < 0)
282       sprintf(temp, "%d", (int)result);
283    else
284       //sprintf(temp, "%#X", result);
285       sprintf(temp, "0x%X", (unsigned char)result);
286    return CopyString(temp);
287 }
288
289 public char * PrintUChar(unsigned char result)
290 {
291    char temp[100];
292    sprintf(temp, "0x%X", result);
293    return CopyString(temp);
294 }
295
296 public char * PrintFloat(float result)
297 {
298    char temp[350];
299    sprintf(temp, "%.16ff", result);
300    return CopyString(temp);
301 }
302
303 public char * PrintDouble(double result)
304 {
305    char temp[350];
306    sprintf(temp, "%.16f", result);
307    return CopyString(temp);
308 }
309
310 ////////////////////////////////////////////////////////////////////////
311 ////////////////////////////////////////////////////////////////////////
312
313 //public Operand GetOperand(Expression exp);
314
315 #define GETVALUE(name, t) \
316    public bool Get##name(Expression exp, t * value2) \
317    {                                                        \
318       Operand op2 = GetOperand(exp);                        \
319       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
320       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
321       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
322       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
323       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
324       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
325       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
326       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
327       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
328       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
329       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
330       else if(op2.kind == charType) *value2 = (t) op2.uc;                         \
331       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
332       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
333       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
334       else                                                                          \
335          return false;                                                              \
336       return true;                                                                  \
337    }
338
339 // To help the deubugger currently not preprocessing...
340 #define HELP(x) x
341
342 GETVALUE(Int, HELP(int));
343 GETVALUE(UInt, HELP(unsigned int));
344 GETVALUE(Int64, HELP(int64));
345 GETVALUE(UInt64, HELP(uint64));
346 GETVALUE(IntPtr, HELP(intptr));
347 GETVALUE(UIntPtr, HELP(uintptr));
348 GETVALUE(IntSize, HELP(intsize));
349 GETVALUE(UIntSize, HELP(uintsize));
350 GETVALUE(Short, HELP(short));
351 GETVALUE(UShort, HELP(unsigned short));
352 GETVALUE(Char, HELP(char));
353 GETVALUE(UChar, HELP(unsigned char));
354 GETVALUE(Float, HELP(float));
355 GETVALUE(Double, HELP(double));
356
357 void ComputeExpression(Expression exp);
358
359 void ComputeClassMembers(Class _class, bool isMember)
360 {
361    DataMember member = isMember ? (DataMember) _class : null;
362    Context context = isMember ? null : SetupTemplatesContext(_class);
363    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) && 
364                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
365    {
366       int c;
367       int unionMemberOffset = 0;
368       int bitFields = 0;
369
370       /*
371       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
372          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
373       */
374
375       if(member)
376       {
377          member.memberOffset = 0;
378          if(targetBits < sizeof(void *) * 8)
379             member.structAlignment = 0;
380       }
381       else if(targetBits < sizeof(void *) * 8)
382          _class.structAlignment = 0;
383
384       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
385       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
386          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
387
388       if(!member && _class.destructionWatchOffset)
389          _class.memberOffset += sizeof(OldList);
390
391       // To avoid reentrancy...
392       //_class.structSize = -1;
393
394       {
395          DataMember dataMember;
396          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
397          {
398             if(!dataMember.isProperty)
399             {
400                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
401                {
402                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
403                   /*if(!dataMember.dataType)
404                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
405                      */
406                }
407             }
408          }
409       }
410
411       {
412          DataMember dataMember;
413          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
414          {
415             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
416             {
417                if(!isMember && _class.type == bitClass && dataMember.dataType)
418                {
419                   BitMember bitMember = (BitMember) dataMember;
420                   uint64 mask = 0;
421                   int d;
422
423                   ComputeTypeSize(dataMember.dataType);
424
425                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
426                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
427
428                   _class.memberOffset = bitMember.pos + bitMember.size;
429                   for(d = 0; d<bitMember.size; d++)
430                   {
431                      if(d)
432                         mask <<= 1;
433                      mask |= 1;
434                   }
435                   bitMember.mask = mask << bitMember.pos;
436                }
437                else if(dataMember.type == normalMember && dataMember.dataType)
438                {
439                   int size;
440                   int alignment = 0;
441
442                   // Prevent infinite recursion
443                   if(dataMember.dataType.kind != classType || 
444                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
445                      _class.type != structClass)))
446                      ComputeTypeSize(dataMember.dataType);
447
448                   if(dataMember.dataType.bitFieldCount)
449                   {
450                      bitFields += dataMember.dataType.bitFieldCount;
451                      size = 0;
452                   }
453                   else
454                   {
455                      if(bitFields)
456                      {
457                         int size = (bitFields + 7) / 8;
458
459                         if(isMember)
460                         {
461                            // TESTING THIS PADDING CODE
462                            if(alignment)
463                            {
464                               member.structAlignment = Max(member.structAlignment, alignment);
465
466                               if(member.memberOffset % alignment)
467                                  member.memberOffset += alignment - (member.memberOffset % alignment);
468                            }
469
470                            dataMember.offset = member.memberOffset;
471                            if(member.type == unionMember)
472                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
473                            else
474                            {
475                               member.memberOffset += size;
476                            }
477                         }
478                         else
479                         {
480                            // TESTING THIS PADDING CODE
481                            if(alignment)
482                            {
483                               _class.structAlignment = Max(_class.structAlignment, alignment);
484
485                               if(_class.memberOffset % alignment)
486                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
487                            }
488
489                            dataMember.offset = _class.memberOffset;
490                            _class.memberOffset += size;
491                         }
492                         bitFields = 0;
493                      }
494                      size = dataMember.dataType.size;
495                      alignment = dataMember.dataType.alignment;
496                   }
497
498                   if(isMember)
499                   {
500                      // TESTING THIS PADDING CODE
501                      if(alignment)
502                      {
503                         member.structAlignment = Max(member.structAlignment, alignment);
504
505                         if(member.memberOffset % alignment)
506                            member.memberOffset += alignment - (member.memberOffset % alignment);
507                      }
508
509                      dataMember.offset = member.memberOffset;
510                      if(member.type == unionMember)
511                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
512                      else
513                      {
514                         member.memberOffset += size;
515                      }
516                   }
517                   else
518                   {
519                      // TESTING THIS PADDING CODE
520                      if(alignment)
521                      {
522                         _class.structAlignment = Max(_class.structAlignment, alignment);
523
524                         if(_class.memberOffset % alignment)
525                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
526                      }
527
528                      dataMember.offset = _class.memberOffset;
529                      _class.memberOffset += size;
530                   }
531                }
532                else
533                {
534                   int alignment;
535
536                   ComputeClassMembers((Class)dataMember, true);
537                   alignment = dataMember.structAlignment;
538
539                   if(isMember)
540                   {
541                      if(alignment)
542                      {
543                         if(member.memberOffset % alignment)
544                            member.memberOffset += alignment - (member.memberOffset % alignment);
545
546                         member.structAlignment = Max(member.structAlignment, alignment);
547                      }
548                      dataMember.offset = member.memberOffset;
549                      if(member.type == unionMember)
550                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
551                      else
552                         member.memberOffset += dataMember.memberOffset;
553                   }
554                   else
555                   {
556                      if(alignment)
557                      {
558                         if(_class.memberOffset % alignment)
559                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
560                         _class.structAlignment = Max(_class.structAlignment, alignment);
561                      }
562                      dataMember.offset = _class.memberOffset;
563                      _class.memberOffset += dataMember.memberOffset;
564                   }
565                }
566             }
567          }
568          if(bitFields)
569          {
570             int alignment = 0;
571             int size = (bitFields + 7) / 8;
572
573             if(isMember)
574             {
575                // TESTING THIS PADDING CODE
576                if(alignment)
577                {
578                   member.structAlignment = Max(member.structAlignment, alignment);
579
580                   if(member.memberOffset % alignment)
581                      member.memberOffset += alignment - (member.memberOffset % alignment);
582                }
583
584                if(member.type == unionMember)
585                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
586                else
587                {
588                   member.memberOffset += size;
589                }
590             }
591             else
592             {
593                // TESTING THIS PADDING CODE
594                if(alignment)
595                {
596                   _class.structAlignment = Max(_class.structAlignment, alignment);
597
598                   if(_class.memberOffset % alignment)
599                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
600                }
601                _class.memberOffset += size;
602             }
603             bitFields = 0;
604          }
605       }
606       if(member && member.type == unionMember)
607       {
608          member.memberOffset = unionMemberOffset;
609       }
610       
611       if(!isMember)
612       {
613          /*if(_class.type == structClass)
614             _class.size = _class.memberOffset;
615          else
616          */
617
618          if(_class.type != bitClass)
619          {
620             int extra = 0;
621             if(_class.structAlignment)
622             {
623                if(_class.memberOffset % _class.structAlignment)
624                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
625             }
626             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
627             if(!member)
628             {
629                Property prop;
630                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
631                {
632                   if(prop.isProperty && prop.isWatchable)
633                   {
634                      prop.watcherOffset = _class.structSize;
635                      _class.structSize += sizeof(OldList);
636                   }
637                }
638             }
639
640             // Fix Derivatives
641             {
642                OldLink derivative;
643                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
644                {
645                   Class deriv = derivative.data;
646
647                   if(deriv.computeSize)
648                   {
649                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
650                      deriv.offset = /*_class.offset + */_class.structSize;
651                      deriv.memberOffset = 0;
652                      // ----------------------
653
654                      deriv.structSize = deriv.offset;
655
656                      ComputeClassMembers(deriv, false);
657                   }
658                }
659             }
660          }
661       }
662    }
663    if(context)
664       FinishTemplatesContext(context);
665 }
666
667 public void ComputeModuleClasses(Module module)
668 {
669    Class _class;
670    OldLink subModule;
671    
672    for(subModule = module.modules.first; subModule; subModule = subModule.next)
673       ComputeModuleClasses(subModule.data);
674    for(_class = module.classes.first; _class; _class = _class.next)
675       ComputeClassMembers(_class, false);
676 }
677
678
679 public int ComputeTypeSize(Type type)
680 {
681    uint size = type ? type.size : 0;
682    if(!size && type && !type.computing)
683    {
684       type.computing = true;
685       switch(type.kind)
686       {
687          case charType: type.alignment = size = sizeof(char); break;
688          case intType: type.alignment = size = sizeof(int); break;
689          case int64Type: type.alignment = size = sizeof(int64); break;
690          case intPtrType: type.alignment = size = targetBits / 8; break;
691          case intSizeType: type.alignment = size = targetBits / 8; break;
692          case longType: type.alignment = size = sizeof(long); break;
693          case shortType: type.alignment = size = sizeof(short); break;
694          case floatType: type.alignment = size = sizeof(float); break;
695          case doubleType: type.alignment = size = sizeof(double); break;
696          case classType:
697          {
698             Class _class = type._class ? type._class.registered : null;
699
700             if(_class && _class.type == structClass)
701             {
702                // Ensure all members are properly registered
703                ComputeClassMembers(_class, false);
704                type.alignment = _class.structAlignment;
705                size = _class.structSize;
706                if(type.alignment && size % type.alignment)
707                   size += type.alignment - (size % type.alignment);
708
709             }
710             else if(_class && (_class.type == unitClass || 
711                    _class.type == enumClass || 
712                    _class.type == bitClass))
713             {
714                if(!_class.dataType)
715                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
716                size = type.alignment = ComputeTypeSize(_class.dataType);
717             }
718             else
719                size = type.alignment = targetBits / 8; // sizeof(Instance *);
720             break;
721          }
722          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
723          case arrayType: 
724             if(type.arraySizeExp)
725             {
726                ProcessExpressionType(type.arraySizeExp);
727                ComputeExpression(type.arraySizeExp);
728                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType && 
729                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
730                {
731                   Location oldLoc = yylloc;
732                   // bool isConstant = type.arraySizeExp.isConstant;
733                   char expression[10240];
734                   expression[0] = '\0';
735                   type.arraySizeExp.expType = null;
736                   yylloc = type.arraySizeExp.loc;
737                   if(inCompiler)
738                      PrintExpression(type.arraySizeExp, expression);
739                   Compiler_Error($"Array size not constant int (%s)\n", expression);
740                   yylloc = oldLoc;
741                }
742                GetInt(type.arraySizeExp, &type.arraySize);
743             }
744             else if(type.enumClass)
745             {
746                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
747                {
748                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
749                }
750                else
751                   type.arraySize = 0;
752             }
753             else
754             {
755                // Unimplemented auto size
756                type.arraySize = 0;
757             }
758
759             size = ComputeTypeSize(type.type) * type.arraySize;
760             if(type.type)
761                type.alignment = type.type.alignment;
762             
763             break;
764          case structType:
765          {
766             Type member;
767             for(member = type.members.first; member; member = member.next)
768             {
769                uint addSize = ComputeTypeSize(member);
770
771                member.offset = size;
772                if(member.alignment && size % member.alignment)
773                   member.offset += member.alignment - (size % member.alignment);
774                size = member.offset;
775
776                type.alignment = Max(type.alignment, member.alignment);
777                size += addSize;
778             }
779             if(type.alignment && size % type.alignment)
780                size += type.alignment - (size % type.alignment);
781             break;
782          }
783          case unionType:
784          {
785             Type member;
786             for(member = type.members.first; member; member = member.next)
787             {
788                uint addSize = ComputeTypeSize(member);
789                
790                member.offset = size;
791                if(member.alignment && size % member.alignment)
792                   member.offset += member.alignment - (size % member.alignment);
793                size = member.offset;
794
795                type.alignment = Max(type.alignment, member.alignment);
796                size = Max(size, addSize);
797             }
798             if(type.alignment && size % type.alignment)
799                size += type.alignment - (size % type.alignment);
800             break;
801          }
802          case templateType:
803          {
804             TemplateParameter param = type.templateParameter;
805             Type baseType = ProcessTemplateParameterType(param);
806             if(baseType)
807             {
808                size = ComputeTypeSize(baseType);
809                type.alignment = baseType.alignment;
810             }
811             else
812                type.alignment = size = sizeof(uint64);
813             break;
814          }
815          case enumType:
816          {
817             type.alignment = size = sizeof(enum { test });
818             break;
819          }
820          case thisClassType:
821          {
822             type.alignment = size = targetBits / 8; //sizeof(void *);
823             break;
824          }
825       }
826       type.size = size;
827       type.computing = false;
828    }
829    return size;
830 }
831
832
833 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
834 {
835    // This function is in need of a major review when implementing private members etc.
836    DataMember topMember = isMember ? (DataMember) _class : null;
837    uint totalSize = 0;
838    uint maxSize = 0;
839    int alignment, size;
840    DataMember member;
841    Context context = isMember ? null : SetupTemplatesContext(_class);
842    if(addedPadding)
843       *addedPadding = false;
844
845    if(!isMember && _class.base)
846    {
847       maxSize = _class.structSize;
848       //if(_class.base.type != systemClass) // Commented out with new Instance _class
849       {
850          // DANGER: Testing this noHeadClass here...
851          if(_class.type == structClass || _class.type == noHeadClass)
852             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
853          else
854          {
855             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
856             if(maxSize > baseSize)
857                maxSize -= baseSize;
858             else
859                maxSize = 0;
860          }
861       }
862    }
863
864    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
865    {
866       if(!member.isProperty)
867       {
868          switch(member.type)
869          {
870             case normalMember:
871             {
872                if(member.dataTypeString)
873                {
874                   OldList * specs = MkList(), * decls = MkList();
875                   Declarator decl;
876
877                   decl = SpecDeclFromString(member.dataTypeString, specs, 
878                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
879                   ListAdd(decls, MkStructDeclarator(decl, null));
880                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
881
882                   if(!member.dataType)
883                      member.dataType = ProcessType(specs, decl);
884
885                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
886
887                   {
888                      Type type = ProcessType(specs, decl);
889                      DeclareType(member.dataType, false, false);
890                      FreeType(type);
891                   }
892                   /*
893                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
894                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
895                      DeclareStruct(member.dataType._class.string, false);
896                   */
897
898                   ComputeTypeSize(member.dataType);
899                   size = member.dataType.size;
900                   alignment = member.dataType.alignment;
901
902                   if(alignment)
903                   {
904                      if(totalSize % alignment)
905                         totalSize += alignment - (totalSize % alignment);
906                   }
907                   totalSize += size;
908                }
909                break;
910             }
911             case unionMember:
912             case structMember:
913             {
914                OldList * specs = MkList(), * list = MkList();
915                
916                size = 0;
917                AddMembers(list, (Class)member, true, &size, topClass, null);
918                ListAdd(specs, 
919                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
920                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
921                alignment = member.structAlignment;
922
923                if(alignment)
924                {
925                   if(totalSize % alignment)
926                      totalSize += alignment - (totalSize % alignment);
927                }
928                totalSize += size;
929                break;
930             }
931          }
932       }
933    }
934    if(retSize)
935    {
936       if(topMember && topMember.type == unionMember)
937          *retSize = Max(*retSize, totalSize);
938       else
939          *retSize += totalSize;
940    }
941    else if(totalSize < maxSize && _class.type != systemClass)
942    {
943       int autoPadding = 0;
944       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
945          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
946       if(totalSize + autoPadding < maxSize)
947       {
948          char sizeString[50];
949          sprintf(sizeString, "%d", maxSize - totalSize);
950          ListAdd(declarations, 
951             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)), 
952             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
953          if(addedPadding)
954             *addedPadding = true;
955       }
956    }
957    if(context)
958       FinishTemplatesContext(context);
959    return topMember ? topMember.memberID : _class.memberID;
960 }
961
962 static int DeclareMembers(Class _class, bool isMember)
963 {
964    DataMember topMember = isMember ? (DataMember) _class : null;
965    uint totalSize = 0;
966    DataMember member;
967    Context context = isMember ? null : SetupTemplatesContext(_class);
968    
969    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
970       DeclareMembers(_class.base, false);
971
972    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
973    {
974       if(!member.isProperty)
975       {
976          switch(member.type)
977          {
978             case normalMember:
979             {
980                /*
981                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
982                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
983                   DeclareStruct(member.dataType._class.string, false);
984                   */
985                if(!member.dataType && member.dataTypeString)
986                   member.dataType = ProcessTypeString(member.dataTypeString, false);
987                if(member.dataType)
988                   DeclareType(member.dataType, false, false);
989                break;
990             }
991             case unionMember:
992             case structMember:
993             {
994                DeclareMembers((Class)member, true);
995                break;
996             }
997          }
998       }
999    }
1000    if(context)
1001       FinishTemplatesContext(context);
1002
1003    return topMember ? topMember.memberID : _class.memberID;
1004 }
1005
1006 void DeclareStruct(char * name, bool skipNoHead)
1007 {
1008    External external = null;
1009    Symbol classSym = FindClass(name);
1010
1011    if(!inCompiler || !classSym) return;
1012
1013    // We don't need any declaration for bit classes...
1014    if(classSym.registered && 
1015       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1016       return;
1017
1018    /*if(classSym.registered.templateClass)
1019       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1020    */
1021
1022    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1023    {
1024       // Add typedef struct
1025       Declaration decl;
1026       OldList * specifiers, * declarators;
1027       OldList * declarations = null;
1028       char structName[1024];
1029       external = (classSym.registered && classSym.registered.type == structClass) ? 
1030          classSym.pointerExternal : classSym.structExternal;
1031
1032       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1033       // Moved this one up because DeclareClass done later will need it
1034
1035       classSym.declaring++;
1036
1037       if(strchr(classSym.string, '<'))
1038       {
1039          if(classSym.registered.templateClass)
1040          {
1041             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1042             classSym.declaring--;
1043          }
1044          return;
1045       }
1046       
1047       //if(!skipNoHead)
1048          DeclareMembers(classSym.registered, false);
1049
1050       structName[0] = 0;
1051       FullClassNameCat(structName, name, false);
1052
1053       /*if(!external)      
1054          external = MkExternalDeclaration(null);*/
1055
1056       if(!skipNoHead)
1057       {
1058          bool addedPadding = false;
1059          classSym.declaredStructSym = true;
1060
1061          declarations = MkList();
1062
1063          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1064
1065          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1066          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1067
1068          if(!declarations->count || (declarations->count == 1 && addedPadding))
1069          {
1070             FreeList(declarations, FreeClassDef);
1071             declarations = null;
1072          }
1073       }
1074       if(skipNoHead || declarations)
1075       {
1076          if(external && external.declaration)
1077          {
1078             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1079
1080             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1081             {
1082                // TODO: Fix this
1083                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1084
1085                // DANGER
1086                if(classSym.structExternal)
1087                   ast->Move(classSym.structExternal, curExternal.prev);
1088                ast->Move(classSym.pointerExternal, curExternal.prev);
1089
1090                classSym.id = curExternal.symbol.idCode;
1091                classSym.idCode = curExternal.symbol.idCode;
1092                // external = classSym.pointerExternal;
1093                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1094             }
1095          }
1096          else
1097          {
1098             if(!external)      
1099                external = MkExternalDeclaration(null);
1100
1101             specifiers = MkList();
1102             declarators = MkList();
1103             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1104
1105             /*
1106             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1107             ListAdd(declarators, MkInitDeclarator(d, null));
1108             */
1109             external.declaration = decl = MkDeclaration(specifiers, declarators);
1110             if(decl.symbol && !decl.symbol.pointerExternal)
1111                decl.symbol.pointerExternal = external;
1112
1113             // For simple classes, keep the declaration as the external to move around
1114             if(classSym.registered && classSym.registered.type == structClass)
1115             {
1116                char className[1024];
1117                strcpy(className, "__ecereClass_");
1118                FullClassNameCat(className, classSym.string, true);
1119                MangleClassName(className);
1120
1121                // Testing This
1122                DeclareClass(classSym, className);
1123
1124                external.symbol = classSym;
1125                classSym.pointerExternal = external;
1126                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1127                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1128             }
1129             else
1130             {
1131                char className[1024];
1132                strcpy(className, "__ecereClass_");
1133                FullClassNameCat(className, classSym.string, true);
1134                MangleClassName(className);
1135
1136                // TOFIX: TESTING THIS...
1137                classSym.structExternal = external;
1138                DeclareClass(classSym, className);
1139                external.symbol = classSym;
1140             }
1141
1142             //if(curExternal)
1143                ast->Insert(curExternal ? curExternal.prev : null, external);
1144          }
1145       }
1146
1147       classSym.declaring--;
1148    }
1149    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1150    {
1151       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1152       // Moved this one up because DeclareClass done later will need it
1153
1154       // TESTING THIS:
1155       classSym.declaring++;
1156
1157       //if(!skipNoHead)
1158       {
1159          if(classSym.registered)
1160             DeclareMembers(classSym.registered, false);
1161       }
1162
1163       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1164       {
1165          // TODO: Fix this
1166          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1167
1168          // DANGER
1169          if(classSym.structExternal)
1170             ast->Move(classSym.structExternal, curExternal.prev);
1171          ast->Move(classSym.pointerExternal, curExternal.prev);
1172
1173          classSym.id = curExternal.symbol.idCode;
1174          classSym.idCode = curExternal.symbol.idCode;
1175          // external = classSym.pointerExternal;
1176          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1177       }
1178
1179       classSym.declaring--;
1180    }
1181    //return external;
1182 }
1183
1184 void DeclareProperty(Property prop, char * setName, char * getName)
1185 {
1186    Symbol symbol = prop.symbol;
1187    char propName[1024];
1188
1189    strcpy(setName, "__ecereProp_");
1190    FullClassNameCat(setName, prop._class.fullName, false);
1191    strcat(setName, "_Set_");
1192    // strcat(setName, prop.name);
1193    FullClassNameCat(setName, prop.name, true);
1194
1195    strcpy(getName, "__ecereProp_");
1196    FullClassNameCat(getName, prop._class.fullName, false);
1197    strcat(getName, "_Get_");
1198    FullClassNameCat(getName, prop.name, true);
1199    // strcat(getName, prop.name);
1200
1201    strcpy(propName, "__ecereProp_");
1202    FullClassNameCat(propName, prop._class.fullName, false);
1203    strcat(propName, "_");
1204    FullClassNameCat(propName, prop.name, true);
1205    // strcat(propName, prop.name);
1206
1207    // To support "char *" property
1208    MangleClassName(getName);
1209    MangleClassName(setName);
1210    MangleClassName(propName);
1211
1212    if(prop._class.type == structClass)
1213       DeclareStruct(prop._class.fullName, false);
1214
1215    if(!symbol || curExternal.symbol.idCode < symbol.id)
1216    {
1217       bool imported = false;
1218       bool dllImport = false;
1219       if(!symbol || symbol._import)
1220       {
1221          if(!symbol)
1222          {
1223             Symbol classSym;
1224             if(!prop._class.symbol)
1225                prop._class.symbol = FindClass(prop._class.fullName);
1226             classSym = prop._class.symbol;
1227             if(classSym && !classSym._import)
1228             {
1229                ModuleImport module;
1230
1231                if(prop._class.module)
1232                   module = FindModule(prop._class.module);
1233                else
1234                   module = mainModule;
1235
1236                classSym._import = ClassImport
1237                {
1238                   name = CopyString(prop._class.fullName);
1239                   isRemote = prop._class.isRemote;
1240                };
1241                module.classes.Add(classSym._import);
1242             }
1243             symbol = prop.symbol = Symbol { };
1244             symbol._import = (ClassImport)PropertyImport
1245             {
1246                name = CopyString(prop.name);
1247                isVirtual = false; //prop.isVirtual;
1248                hasSet = prop.Set ? true : false;
1249                hasGet = prop.Get ? true : false;
1250             };
1251             if(classSym)
1252                classSym._import.properties.Add(symbol._import);
1253          }
1254          imported = true;
1255          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1256             dllImport = true;
1257       }
1258
1259       if(!symbol.type)
1260       {
1261          Context context = SetupTemplatesContext(prop._class);
1262          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1263          FinishTemplatesContext(context);
1264       }
1265
1266       // Get
1267       if(prop.Get)
1268       {
1269          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1270          {
1271             Declaration decl;
1272             OldList * specifiers, * declarators;
1273             Declarator d;
1274             OldList * params;
1275             Specifier spec;
1276             External external;
1277             Declarator typeDecl;
1278             bool simple = false;
1279
1280             specifiers = MkList();
1281             declarators = MkList();
1282             params = MkList();
1283
1284             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)), 
1285                MkDeclaratorIdentifier(MkIdentifier("this"))));
1286
1287             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1288             //if(imported)
1289             if(dllImport)
1290                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1291
1292             {
1293                Context context = SetupTemplatesContext(prop._class);
1294                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1295                FinishTemplatesContext(context);
1296             }
1297
1298             // Make sure the simple _class's type is declared
1299             for(spec = specifiers->first; spec; spec = spec.next)
1300             {
1301                if(spec.type == nameSpecifier /*SpecifierClass*/)
1302                {
1303                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1304                   {
1305                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1306                      symbol._class = classSym.registered;
1307                      if(classSym.registered && classSym.registered.type == structClass)
1308                      {
1309                         DeclareStruct(spec.name, false);
1310                         simple = true;
1311                      }
1312                   }
1313                }
1314             }
1315
1316             if(!simple)
1317                d = PlugDeclarator(typeDecl, d);
1318             else
1319             {
1320                ListAdd(params, MkTypeName(specifiers, 
1321                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1322                specifiers = MkList();
1323             }
1324
1325             d = MkDeclaratorFunction(d, params);
1326  
1327             //if(imported)
1328             if(dllImport)
1329                specifiers->Insert(null, MkSpecifier(EXTERN));
1330             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1331                specifiers->Insert(null, MkSpecifier(STATIC));
1332             if(simple)
1333                ListAdd(specifiers, MkSpecifier(VOID));
1334
1335             ListAdd(declarators, MkInitDeclarator(d, null));
1336
1337             decl = MkDeclaration(specifiers, declarators);
1338
1339             external = MkExternalDeclaration(decl);
1340             ast->Insert(curExternal.prev, external);
1341             external.symbol = symbol;
1342             symbol.externalGet = external;
1343
1344             ReplaceThisClassSpecifiers(specifiers, prop._class);
1345
1346             if(typeDecl)
1347                FreeDeclarator(typeDecl);
1348          }
1349          else
1350          {
1351             // Move declaration higher...
1352             ast->Move(symbol.externalGet, curExternal.prev);
1353          }
1354       }
1355
1356       // Set
1357       if(prop.Set)
1358       {
1359          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1360          {
1361             Declaration decl;
1362             OldList * specifiers, * declarators;
1363             Declarator d;
1364             OldList * params;
1365             Specifier spec;
1366             External external;
1367             Declarator typeDecl;
1368
1369             declarators = MkList();
1370             params = MkList();
1371
1372             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1373             if(!prop.conversion || prop._class.type == structClass)
1374             {
1375                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)), 
1376                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1377             }
1378
1379             specifiers = MkList();
1380
1381             {
1382                Context context = SetupTemplatesContext(prop._class);
1383                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1384                   MkDeclaratorIdentifier(MkIdentifier("value")));
1385                FinishTemplatesContext(context);
1386             }
1387             ListAdd(params, MkTypeName(specifiers, d));
1388
1389             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1390             //if(imported)
1391             if(dllImport)
1392                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1393             d = MkDeclaratorFunction(d, params);
1394
1395             // Make sure the simple _class's type is declared
1396             for(spec = specifiers->first; spec; spec = spec.next)
1397             {
1398                if(spec.type == nameSpecifier /*SpecifierClass*/)
1399                {
1400                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1401                   {
1402                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1403                      symbol._class = classSym.registered;
1404                      if(classSym.registered && classSym.registered.type == structClass)
1405                         DeclareStruct(spec.name, false);
1406                   }
1407                }
1408             }
1409
1410             ListAdd(declarators, MkInitDeclarator(d, null));
1411
1412             specifiers = MkList();
1413             //if(imported)
1414             if(dllImport)
1415                specifiers->Insert(null, MkSpecifier(EXTERN));
1416             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1417                specifiers->Insert(null, MkSpecifier(STATIC));
1418
1419             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1420             if(!prop.conversion || prop._class.type == structClass)
1421                ListAdd(specifiers, MkSpecifier(VOID));
1422             else
1423                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1424
1425             decl = MkDeclaration(specifiers, declarators);
1426
1427             external = MkExternalDeclaration(decl);
1428             ast->Insert(curExternal.prev, external);
1429             external.symbol = symbol;
1430             symbol.externalSet = external;
1431
1432             ReplaceThisClassSpecifiers(specifiers, prop._class);
1433          }
1434          else
1435          {
1436             // Move declaration higher...
1437             ast->Move(symbol.externalSet, curExternal.prev);
1438          }
1439       }
1440
1441       // Property (for Watchers)
1442       if(!symbol.externalPtr)
1443       {
1444          Declaration decl;
1445          External external;
1446          OldList * specifiers = MkList();
1447
1448          if(imported)
1449             specifiers->Insert(null, MkSpecifier(EXTERN));
1450          else
1451             specifiers->Insert(null, MkSpecifier(STATIC));
1452
1453          ListAdd(specifiers, MkSpecifierName("Property"));
1454
1455          {
1456             OldList * list = MkList();
1457             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1458                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1459
1460             if(!imported)
1461             {
1462                strcpy(propName, "__ecerePropM_");
1463                FullClassNameCat(propName, prop._class.fullName, false);
1464                strcat(propName, "_");
1465                // strcat(propName, prop.name);
1466                FullClassNameCat(propName, prop.name, true);
1467
1468                MangleClassName(propName);
1469
1470                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1471                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1472             }
1473             decl = MkDeclaration(specifiers, list);
1474          }
1475
1476          external = MkExternalDeclaration(decl);
1477          ast->Insert(curExternal.prev, external);
1478          external.symbol = symbol;
1479          symbol.externalPtr = external;
1480       }
1481       else
1482       {
1483          // Move declaration higher...
1484          ast->Move(symbol.externalPtr, curExternal.prev);
1485       }
1486
1487       symbol.id = curExternal.symbol.idCode;
1488    }
1489 }
1490
1491 // ***************** EXPRESSION PROCESSING ***************************
1492 public Type Dereference(Type source)
1493 {
1494    Type type = null;
1495    if(source)
1496    {
1497       if(source.kind == pointerType || source.kind == arrayType)
1498       {
1499          type = source.type;
1500          source.type.refCount++;
1501       }
1502       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1503       {
1504          type = Type
1505          {
1506             kind = charType;
1507             refCount = 1;
1508          };
1509       }
1510       // Support dereferencing of no head classes for now...
1511       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1512       {
1513          type = source;
1514          source.refCount++;
1515       }
1516       else
1517          Compiler_Error($"cannot dereference type\n");
1518    }
1519    return type;
1520 }
1521
1522 static Type Reference(Type source)
1523 {
1524    Type type = null;
1525    if(source)
1526    {
1527       type = Type
1528       {
1529          kind = pointerType;
1530          type = source;
1531          refCount = 1;
1532       };
1533       source.refCount++;
1534    }
1535    return type;
1536 }
1537
1538 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1539 {
1540    Identifier ident = member.identifiers ? member.identifiers->first : null;
1541    bool found = false;
1542    DataMember dataMember = null;
1543    Method method = null;
1544    bool freeType = false;
1545
1546    yylloc = member.loc;
1547
1548    if(!ident)
1549    {
1550       if(curMember)
1551       {
1552          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1553          if(*curMember)
1554          {
1555             found = true;
1556             dataMember = *curMember;
1557          }
1558       }
1559    }
1560    else
1561    {
1562       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1563       DataMember _subMemberStack[256];
1564       int _subMemberStackPos = 0;
1565
1566       // FILL MEMBER STACK
1567       if(!thisMember)
1568          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1569       if(thisMember)
1570       {
1571          dataMember = thisMember;
1572          if(curMember && thisMember.memberAccess == publicAccess)
1573          {
1574             *curMember = thisMember;
1575             *curClass = thisMember._class;
1576             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1577             *subMemberStackPos = _subMemberStackPos;
1578          }
1579          found = true;
1580       }
1581       else
1582       {
1583          // Setting a method
1584          method = eClass_FindMethod(_class, ident.string, privateModule);
1585          if(method && method.type == virtualMethod)
1586             found = true;
1587          else
1588             method = null;
1589       }
1590    }
1591
1592    if(found)
1593    {
1594       Type type = null;
1595       if(dataMember)
1596       {
1597          if(!dataMember.dataType && dataMember.dataTypeString)
1598          {
1599             //Context context = SetupTemplatesContext(dataMember._class);
1600             Context context = SetupTemplatesContext(_class);
1601             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1602             FinishTemplatesContext(context);
1603          }
1604          type = dataMember.dataType;
1605       }
1606       else if(method)
1607       {
1608          // This is for destination type...
1609          if(!method.dataType)
1610             ProcessMethodType(method);
1611          //DeclareMethod(method);
1612          // method.dataType = ((Symbol)method.symbol)->type;
1613          type = method.dataType;
1614       }
1615
1616       if(ident && ident.next)
1617       {
1618          for(ident = ident.next; ident && type; ident = ident.next)
1619          {
1620             if(type.kind == classType)
1621             {
1622                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1623                if(!dataMember)
1624                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1625                if(dataMember)
1626                   type = dataMember.dataType;
1627             }
1628             else if(type.kind == structType || type.kind == unionType)
1629             {
1630                Type memberType;
1631                for(memberType = type.members.first; memberType; memberType = memberType.next)
1632                {
1633                   if(!strcmp(memberType.name, ident.string))
1634                   {
1635                      type = memberType;
1636                      break;
1637                   }
1638                }
1639             }
1640          }
1641       }
1642
1643       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1644       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1645       {
1646          int id = 0;
1647          ClassTemplateParameter curParam = null;
1648          Class sClass;
1649          for(sClass = _class; sClass; sClass = sClass.base)
1650          {
1651             id = 0;
1652             if(sClass.templateClass) sClass = sClass.templateClass;
1653             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1654             {
1655                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1656                {
1657                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1658                   {
1659                      if(sClass.templateClass) sClass = sClass.templateClass;
1660                      id += sClass.templateParams.count;
1661                   }
1662                   break;
1663                }
1664                id++;
1665             }
1666             if(curParam) break;
1667          }
1668
1669          if(curParam)
1670          {
1671             ClassTemplateArgument arg = _class.templateArgs[id];
1672             if(arg.dataTypeString)
1673             {
1674                // FreeType(type);
1675                type = ProcessTypeString(arg.dataTypeString, false);
1676                freeType = true;
1677                if(type && _class.templateClass)
1678                   type.passAsTemplate = true;
1679                if(type)
1680                {
1681                   // type.refCount++;
1682                   /*if(!exp.destType)
1683                   {
1684                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1685                      exp.destType.refCount++;
1686                   }*/
1687                }
1688             }
1689          }
1690       }
1691       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1692       {
1693          Class expClass = type._class.registered;
1694          Class cClass = null;
1695          int c;
1696          int paramCount = 0;
1697          int lastParam = -1;
1698          
1699          char templateString[1024];
1700          ClassTemplateParameter param;
1701          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1702          for(cClass = expClass; cClass; cClass = cClass.base)
1703          {
1704             int p = 0;
1705             if(cClass.templateClass) cClass = cClass.templateClass;
1706             for(param = cClass.templateParams.first; param; param = param.next)
1707             {
1708                int id = p;
1709                Class sClass;
1710                ClassTemplateArgument arg;
1711                for(sClass = cClass.base; sClass; sClass = sClass.base) 
1712                {
1713                   if(sClass.templateClass) sClass = sClass.templateClass;
1714                   id += sClass.templateParams.count;
1715                }
1716                arg = expClass.templateArgs[id];
1717
1718                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1719                {
1720                   ClassTemplateParameter cParam;
1721                   //int p = numParams - sClass.templateParams.count;
1722                   int p = 0;
1723                   Class nextClass;
1724                   if(sClass.templateClass) sClass = sClass.templateClass;
1725
1726                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) 
1727                   {
1728                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1729                      p += nextClass.templateParams.count;
1730                   }
1731                   
1732                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1733                   {
1734                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1735                      {
1736                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1737                         {
1738                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1739                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1740                            break;
1741                         }
1742                      }
1743                   }
1744                }
1745
1746                {
1747                   char argument[256];
1748                   argument[0] = '\0';
1749                   /*if(arg.name)
1750                   {
1751                      strcat(argument, arg.name.string);
1752                      strcat(argument, " = ");
1753                   }*/
1754                   switch(param.type)
1755                   {
1756                      case expression:
1757                      {
1758                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1759                         char expString[1024];
1760                         OldList * specs = MkList();
1761                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1762                         Expression exp;
1763                         char * string = PrintHexUInt64(arg.expression.ui64);
1764                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1765
1766                         ProcessExpressionType(exp);
1767                         ComputeExpression(exp);
1768                         expString[0] = '\0';
1769                         PrintExpression(exp, expString);
1770                         strcat(argument, expString);
1771                         //delete exp;
1772                         FreeExpression(exp);
1773                         break;
1774                      }
1775                      case identifier:
1776                      {
1777                         strcat(argument, arg.member.name);
1778                         break;
1779                      }
1780                      case TemplateParameterType::type:
1781                      {
1782                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1783                            strcat(argument, arg.dataTypeString);
1784                         break;
1785                      }
1786                   }
1787                   if(argument[0])
1788                   {
1789                      if(paramCount) strcat(templateString, ", ");
1790                      if(lastParam != p - 1)
1791                      {
1792                         strcat(templateString, param.name);
1793                         strcat(templateString, " = ");
1794                      }
1795                      strcat(templateString, argument);
1796                      paramCount++;
1797                      lastParam = p;
1798                   }
1799                   p++;
1800                }               
1801             }
1802          }
1803          {
1804             int len = strlen(templateString);
1805             if(templateString[len-1] == '<')
1806                len--;
1807             else
1808             {
1809                if(templateString[len-1] == '>')
1810                   templateString[len++] = ' ';
1811                templateString[len++] = '>';
1812             }
1813             templateString[len++] = '\0';
1814          }
1815          {
1816             Context context = SetupTemplatesContext(_class);
1817             if(freeType) FreeType(type);
1818             type = ProcessTypeString(templateString, false);
1819             freeType = true;
1820             FinishTemplatesContext(context);
1821          }
1822       }
1823
1824       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1825       {
1826          ProcessExpressionType(member.initializer.exp);
1827          if(!member.initializer.exp.expType)
1828          {
1829             if(inCompiler)
1830             {
1831                char expString[10240];
1832                expString[0] = '\0';
1833                PrintExpression(member.initializer.exp, expString);
1834                ChangeCh(expString, '\n', ' ');
1835                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1836             }
1837          }
1838          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1839          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1840          {
1841             Compiler_Error($"incompatible instance method %s\n", ident.string);
1842          }
1843       }
1844       else if(member.initializer)
1845       {
1846          /*
1847          FreeType(member.exp.destType);
1848          member.exp.destType = type;
1849          if(member.exp.destType)
1850             member.exp.destType.refCount++;
1851          ProcessExpressionType(member.exp);
1852          */
1853
1854          ProcessInitializer(member.initializer, type);
1855       }
1856       if(freeType) FreeType(type);
1857    }
1858    else
1859    {
1860       if(_class && _class.type == unitClass)
1861       {
1862          if(member.initializer)
1863          {
1864             /*
1865             FreeType(member.exp.destType);
1866             member.exp.destType = MkClassType(_class.fullName);
1867             ProcessExpressionType(member.initializer, type);
1868             */
1869             Type type = MkClassType(_class.fullName);
1870             ProcessInitializer(member.initializer, type);
1871             FreeType(type);
1872          }
1873       }
1874       else
1875       {
1876          if(member.initializer)
1877          {
1878             //ProcessExpressionType(member.exp);
1879             ProcessInitializer(member.initializer, null);
1880          }
1881          if(ident)
1882          {
1883             if(method)
1884             {
1885                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1886             }
1887             else if(_class)
1888             {
1889                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1890                if(inCompiler)
1891                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1892             }
1893          }
1894          else if(_class)
1895             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1896       }
1897    }
1898 }
1899
1900 void ProcessInstantiationType(Instantiation inst)
1901 {
1902    yylloc = inst.loc;
1903    if(inst._class)
1904    {
1905       MembersInit members;
1906       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1907       Class _class;
1908       
1909       /*if(!inst._class.symbol)
1910          inst._class.symbol = FindClass(inst._class.name);*/
1911       classSym = inst._class.symbol;
1912       _class = classSym ? classSym.registered : null;
1913
1914       // DANGER: Patch for mutex not declaring its struct when not needed
1915       if(!_class || _class.type != noHeadClass)
1916          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1917
1918       afterExternal = afterExternal ? afterExternal : curExternal;
1919
1920       if(inst.exp)
1921          ProcessExpressionType(inst.exp);
1922
1923       inst.isConstant = true;
1924       if(inst.members)
1925       {
1926          DataMember curMember = null;
1927          Class curClass = null;
1928          DataMember subMemberStack[256];
1929          int subMemberStackPos = 0;
1930
1931          for(members = inst.members->first; members; members = members.next)
1932          {
1933             switch(members.type)
1934             {
1935                case methodMembersInit:
1936                {
1937                   char name[1024];
1938                   static uint instMethodID = 0;
1939                   External external = curExternal;
1940                   Context context = curContext;
1941                   Declarator declarator = members.function.declarator;
1942                   Identifier nameID = GetDeclId(declarator);
1943                   char * unmangled = nameID ? nameID.string : null;
1944                   Expression exp;
1945                   External createdExternal = null;
1946
1947                   if(inCompiler)
1948                   {
1949                      char number[16];
1950                      //members.function.dontMangle = true;
1951                      strcpy(name, "__ecereInstMeth_");
1952                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1953                      strcat(name, "_");
1954                      strcat(name, nameID.string);
1955                      strcat(name, "_");
1956                      sprintf(number, "_%08d", instMethodID++);
1957                      strcat(name, number);                     
1958                      nameID.string = CopyString(name);
1959                   }
1960
1961                   // Do modifications here...
1962                   if(declarator)
1963                   {
1964                      Symbol symbol = declarator.symbol;
1965                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1966                                     
1967                      if(method && method.type == virtualMethod)
1968                      {
1969                         symbol.method = method;
1970                         ProcessMethodType(method);
1971
1972                         if(!symbol.type.thisClass)
1973                         {
1974                            if(method.dataType.thisClass && currentClass && 
1975                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1976                            {
1977                               if(!currentClass.symbol)
1978                                  currentClass.symbol = FindClass(currentClass.fullName);
1979                               symbol.type.thisClass = currentClass.symbol;
1980                            }
1981                            else
1982                            {
1983                               if(!_class.symbol)
1984                                  _class.symbol = FindClass(_class.fullName);
1985                               symbol.type.thisClass = _class.symbol;
1986                            }
1987                         }
1988                         // TESTING THIS HERE:
1989                         DeclareType(symbol.type, true, true);
1990
1991                      }
1992                      else if(classSym)
1993                      {
1994                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1995                            unmangled, classSym.string);
1996                      }
1997                   }
1998
1999                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2000                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2001
2002                   if(nameID)
2003                   {
2004                      FreeSpecifier(nameID._class);
2005                      nameID._class = null;
2006                   }
2007
2008                   if(inCompiler)
2009                   {
2010
2011                      Type type = declarator.symbol.type;
2012                      External oldExternal = curExternal;
2013
2014                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2015                      // *** It was commented out for problems such as
2016                      /*
2017                            class VirtualDesktop : Window
2018                            {
2019                               clientSize = Size { };
2020                               Timer timer
2021                               {
2022                                  bool DelayExpired()
2023                                  {
2024                                     clientSize.w;
2025                                     return true;
2026                                  }
2027                               };
2028                            }
2029                      */
2030                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2031
2032                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2033
2034                      /*
2035                      if(strcmp(declarator.symbol.string, name))
2036                      {
2037                         printf("TOCHECK: Look out for this\n");
2038                         delete declarator.symbol.string;
2039                         declarator.symbol.string = CopyString(name);
2040                      }
2041                      
2042                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2043                      {
2044                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2045                         excludedSymbols->Remove(declarator.symbol);
2046                         globalContext.symbols.Add((BTNode)declarator.symbol);
2047                         if(strstr(declarator.symbol.string), "::")
2048                            globalContext.hasNameSpace = true;
2049
2050                      }
2051                      */
2052                   
2053                      //curExternal = curExternal.prev;
2054                      //afterExternal = afterExternal->next;
2055
2056                      //ProcessFunction(afterExternal->function);
2057
2058                      //curExternal = afterExternal;
2059                      {
2060                         External externalDecl;
2061                         externalDecl = MkExternalDeclaration(null);
2062                         ast->Insert(oldExternal.prev, externalDecl);
2063
2064                         // Which function does this process?
2065                         if(createdExternal.function)
2066                         {
2067                            ProcessFunction(createdExternal.function);
2068
2069                            //curExternal = oldExternal;
2070
2071                            {
2072                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2073
2074                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
2075                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076                      
2077                               //externalDecl = MkExternalDeclaration(decl);
2078                         
2079                               //***** ast->Insert(external.prev, externalDecl);
2080                               //ast->Insert(curExternal.prev, externalDecl);
2081                               externalDecl.declaration = decl;
2082                               if(decl.symbol && !decl.symbol.pointerExternal)
2083                                  decl.symbol.pointerExternal = externalDecl;
2084
2085                               // Trying this out...
2086                               declarator.symbol.pointerExternal = externalDecl;
2087                            }
2088                         }
2089                      }
2090                   }
2091                   else if(declarator)
2092                   {
2093                      curExternal = declarator.symbol.pointerExternal;
2094                      ProcessFunction((FunctionDefinition)members.function);
2095                   }
2096                   curExternal = external;
2097                   curContext = context;
2098
2099                   if(inCompiler)
2100                   {
2101                      FreeClassFunction(members.function);
2102
2103                      // In this pass, turn this into a MemberInitData
2104                      exp = QMkExpId(name);
2105                      members.type = dataMembersInit;
2106                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2107
2108                      delete unmangled;
2109                   }
2110                   break;
2111                }
2112                case dataMembersInit:
2113                {
2114                   if(members.dataMembers && classSym)
2115                   {
2116                      MemberInit member;
2117                      Location oldyyloc = yylloc;
2118                      for(member = members.dataMembers->first; member; member = member.next)
2119                      {
2120                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2121                         if(member.initializer && !member.initializer.isConstant)
2122                            inst.isConstant = false;
2123                      }
2124                      yylloc = oldyyloc;
2125                   }
2126                   break;
2127                }
2128             }
2129          }
2130       }
2131    }
2132 }
2133
2134 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2135 {
2136    // OPTIMIZATIONS: TESTING THIS...
2137    if(inCompiler)
2138    {
2139       if(type.kind == functionType)
2140       {
2141          Type param;
2142          if(declareParams)
2143          {
2144             for(param = type.params.first; param; param = param.next)
2145                DeclareType(param, declarePointers, true);
2146          }
2147          DeclareType(type.returnType, declarePointers, true);
2148       }
2149       else if(type.kind == pointerType && declarePointers)
2150          DeclareType(type.type, declarePointers, false);
2151       else if(type.kind == classType)
2152       {
2153          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2154             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2155       }
2156       else if(type.kind == structType || type.kind == unionType)
2157       {
2158          Type member;
2159          for(member = type.members.first; member; member = member.next)
2160             DeclareType(member, false, false);
2161       }
2162       else if(type.kind == arrayType)
2163          DeclareType(type.arrayType, declarePointers, false);
2164    }
2165 }
2166
2167 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2168 {
2169    ClassTemplateArgument * arg = null;
2170    int id = 0;
2171    ClassTemplateParameter curParam = null;
2172    Class sClass;
2173    for(sClass = _class; sClass; sClass = sClass.base)
2174    {
2175       id = 0;
2176       if(sClass.templateClass) sClass = sClass.templateClass;
2177       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2178       {
2179          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2180          {
2181             for(sClass = sClass.base; sClass; sClass = sClass.base)
2182             {
2183                if(sClass.templateClass) sClass = sClass.templateClass;
2184                id += sClass.templateParams.count;
2185             }
2186             break;
2187          }
2188          id++;
2189       }
2190       if(curParam) break;
2191    }
2192    if(curParam)
2193    {
2194       arg = &_class.templateArgs[id];
2195       if(arg && param.type == type)
2196          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2197    }
2198    return arg;
2199 }
2200
2201 public Context SetupTemplatesContext(Class _class)
2202 {
2203    Context context = PushContext();
2204    context.templateTypesOnly = true;
2205    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2206    {
2207       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2208       for(; param; param = param.next)
2209       {
2210          if(param.type == type && param.identifier)
2211          {
2212             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2213             curContext.templateTypes.Add((BTNode)type);
2214          }
2215       }
2216    }
2217    else if(_class)
2218    {
2219       Class sClass;
2220       for(sClass = _class; sClass; sClass = sClass.base)
2221       {
2222          ClassTemplateParameter p;
2223          for(p = sClass.templateParams.first; p; p = p.next)
2224          {
2225             //OldList * specs = MkList();
2226             //Declarator decl = null;
2227             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2228             if(p.type == type)
2229             {
2230                TemplateParameter param = p.param;
2231                TemplatedType type;
2232                if(!param)
2233                {
2234                   // ADD DATA TYPE HERE...
2235                   p.param = param = TemplateParameter
2236                   {
2237                      identifier = MkIdentifier(p.name), type = p.type, 
2238                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2239                   };
2240                }
2241                type = TemplatedType { key = (uintptr)p.name, param = param };
2242                curContext.templateTypes.Add((BTNode)type);
2243             }
2244          }
2245       }
2246    }
2247    return context;
2248 }
2249
2250 public void FinishTemplatesContext(Context context)
2251 {
2252    PopContext(context);
2253    FreeContext(context);
2254    delete context;
2255 }
2256
2257 public void ProcessMethodType(Method method)
2258 {
2259    if(!method.dataType)
2260    {
2261       Context context = SetupTemplatesContext(method._class);
2262
2263       method.dataType = ProcessTypeString(method.dataTypeString, false);
2264
2265       FinishTemplatesContext(context);
2266
2267       if(method.type != virtualMethod && method.dataType)
2268       {
2269          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2270          {
2271             if(!method._class.symbol)
2272                method._class.symbol = FindClass(method._class.fullName);
2273             method.dataType.thisClass = method._class.symbol;
2274          }
2275       }
2276
2277       // Why was this commented out? Working fine without now...
2278
2279       /*
2280       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2281          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2282          */
2283    }
2284
2285    /*
2286    if(type)
2287    {
2288       char * par = strstr(type, "(");
2289       char * classOp = null;
2290       int classOpLen = 0;
2291       if(par)
2292       {
2293          int c;
2294          for(c = par-type-1; c >= 0; c++)
2295          {
2296             if(type[c] == ':' && type[c+1] == ':')
2297             {
2298                classOp = type + c - 1;
2299                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2300                {
2301                   classOp--;
2302                   classOpLen++;
2303                }
2304                break;
2305             }
2306             else if(!isspace(type[c]))
2307                break;
2308          }
2309       }
2310       if(classOp)
2311       {
2312          char temp[1024];
2313          int typeLen = strlen(type);
2314          memcpy(temp, classOp, classOpLen);
2315          temp[classOpLen] = '\0';
2316          if(temp[0])
2317             _class = eSystem_FindClass(module, temp);
2318          else
2319             _class = null;
2320          method.dataTypeString = new char[typeLen - classOpLen + 1];
2321          memcpy(method.dataTypeString, type, classOp - type);
2322          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2323       }
2324       else
2325          method.dataTypeString = type;
2326    }
2327    */
2328 }
2329
2330
2331 public void ProcessPropertyType(Property prop)
2332 {
2333    if(!prop.dataType)
2334    {
2335       Context context = SetupTemplatesContext(prop._class);
2336       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2337       FinishTemplatesContext(context);
2338    }
2339 }
2340
2341 public void DeclareMethod(Method method, char * name)
2342 {
2343    Symbol symbol = method.symbol;
2344    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2345    {
2346       bool imported = false;
2347       bool dllImport = false;
2348
2349       if(!method.dataType)
2350          method.dataType = ProcessTypeString(method.dataTypeString, false);
2351
2352       if(!symbol || symbol._import || method.type == virtualMethod)
2353       {
2354          if(!symbol || method.type == virtualMethod)
2355          {
2356             Symbol classSym;
2357             if(!method._class.symbol)
2358                method._class.symbol = FindClass(method._class.fullName);
2359             classSym = method._class.symbol;
2360             if(!classSym._import)
2361             {
2362                ModuleImport module;
2363                
2364                if(method._class.module && method._class.module.name)
2365                   module = FindModule(method._class.module);
2366                else
2367                   module = mainModule;
2368                classSym._import = ClassImport
2369                {
2370                   name = CopyString(method._class.fullName);
2371                   isRemote = method._class.isRemote;
2372                };
2373                module.classes.Add(classSym._import);
2374             }
2375             if(!symbol)
2376             {
2377                symbol = method.symbol = Symbol { };
2378             }
2379             if(!symbol._import)
2380             {
2381                symbol._import = (ClassImport)MethodImport
2382                {
2383                   name = CopyString(method.name);
2384                   isVirtual = method.type == virtualMethod;
2385                };
2386                classSym._import.methods.Add(symbol._import);
2387             }
2388             if(!symbol)
2389             {
2390                // Set the symbol type
2391                /*
2392                if(!type.thisClass)
2393                {
2394                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2395                }
2396                else if(type.thisClass == (void *)-1)
2397                {
2398                   type.thisClass = null;
2399                }
2400                */
2401                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2402                symbol.type = method.dataType;
2403                if(symbol.type) symbol.type.refCount++;
2404             }
2405             /*
2406             if(!method.thisClass || strcmp(method.thisClass, "void"))
2407                symbol.type.params.Insert(null, 
2408                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2409             */
2410          }
2411          if(!method.dataType.dllExport)
2412          {
2413             imported = true;
2414             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2415                dllImport = true;
2416          }
2417       }
2418
2419       /* MOVING THIS UP
2420       if(!method.dataType)
2421          method.dataType = ((Symbol)method.symbol).type;
2422          //ProcessMethodType(method);
2423       */
2424
2425       if(method.type != virtualMethod && method.dataType)
2426          DeclareType(method.dataType, true, true);
2427
2428       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2429       {
2430          // We need a declaration here :)
2431          Declaration decl;
2432          OldList * specifiers, * declarators;
2433          Declarator d;
2434          Declarator funcDecl;
2435          External external;
2436
2437          specifiers = MkList();
2438          declarators = MkList();
2439
2440          //if(imported)
2441          if(dllImport)
2442             ListAdd(specifiers, MkSpecifier(EXTERN));
2443          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2444             ListAdd(specifiers, MkSpecifier(STATIC));
2445
2446          if(method.type == virtualMethod)
2447          {
2448             ListAdd(specifiers, MkSpecifier(INT));
2449             d = MkDeclaratorIdentifier(MkIdentifier(name));
2450          }
2451          else
2452          {
2453             d = MkDeclaratorIdentifier(MkIdentifier(name));
2454             //if(imported)
2455             if(dllImport)
2456                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2457             {
2458                Context context = SetupTemplatesContext(method._class);
2459                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2460                FinishTemplatesContext(context);
2461             }
2462             funcDecl = GetFuncDecl(d);
2463
2464             if(dllImport)
2465             {
2466                Specifier spec, next;
2467                for(spec = specifiers->first; spec; spec = next)
2468                {
2469                   next = spec.next;
2470                   if(spec.type == extendedSpecifier)
2471                   {
2472                      specifiers->Remove(spec);
2473                      FreeSpecifier(spec);
2474                   }
2475                }
2476             }
2477
2478             // Add this parameter if not a static method
2479             if(method.dataType && !method.dataType.staticMethod)
2480             {
2481                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2482                {
2483                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2484                   TypeName thisParam = MkTypeName(MkListOne(
2485                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)), 
2486                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2487                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2488                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2489
2490                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2491                   {
2492                      TypeName param = funcDecl.function.parameters->first;
2493                      funcDecl.function.parameters->Remove(param);
2494                      FreeTypeName(param);
2495                   }
2496
2497                   if(!funcDecl.function.parameters)
2498                      funcDecl.function.parameters = MkList();
2499                   funcDecl.function.parameters->Insert(null, thisParam);
2500                }
2501             }
2502             // Make sure we don't have empty parameter declarations for static methods...
2503             /*
2504             else if(!funcDecl.function.parameters)
2505             {
2506                funcDecl.function.parameters = MkList();
2507                funcDecl.function.parameters->Insert(null, 
2508                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2509             }*/
2510          }
2511          // TESTING THIS:
2512          ProcessDeclarator(d);
2513
2514          ListAdd(declarators, MkInitDeclarator(d, null));
2515
2516          decl = MkDeclaration(specifiers, declarators);
2517
2518          ReplaceThisClassSpecifiers(specifiers, method._class);
2519
2520          // Keep a different symbol for the function definition than the declaration...
2521          if(symbol.pointerExternal)
2522          {
2523             Symbol functionSymbol { };
2524
2525             // Copy symbol
2526             {
2527                *functionSymbol = *symbol;
2528                functionSymbol.string = CopyString(symbol.string);
2529                if(functionSymbol.type)
2530                   functionSymbol.type.refCount++;
2531             }
2532
2533             excludedSymbols->Add(functionSymbol);
2534             symbol.pointerExternal.symbol = functionSymbol;
2535          }
2536          external = MkExternalDeclaration(decl);
2537          if(curExternal)
2538             ast->Insert(curExternal ? curExternal.prev : null, external);
2539          external.symbol = symbol;
2540          symbol.pointerExternal = external;
2541       }
2542       else if(ast)
2543       {
2544          // Move declaration higher...
2545          ast->Move(symbol.pointerExternal, curExternal.prev);
2546       }
2547
2548       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2549    }
2550 }
2551
2552 char * ReplaceThisClass(Class _class)
2553 {
2554    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2555    {
2556       bool first = true;
2557       int p = 0;
2558       ClassTemplateParameter param;
2559       int lastParam = -1;
2560       
2561       char className[1024];
2562       strcpy(className, _class.fullName);
2563       for(param = _class.templateParams.first; param; param = param.next)
2564       {
2565          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2566          {
2567             if(first) strcat(className, "<");
2568             if(!first) strcat(className, ", ");
2569             if(lastParam + 1 != p)
2570             {
2571                strcat(className, param.name);
2572                strcat(className, " = ");
2573             }
2574             strcat(className, param.name);
2575             first = false;
2576             lastParam = p;
2577          }
2578          p++;
2579       }
2580       if(!first)
2581       {
2582          int len = strlen(className);
2583          if(className[len-1] == '>') className[len++] = ' ';
2584          className[len++] = '>';
2585          className[len++] = '\0';
2586       }
2587       return CopyString(className);
2588    }
2589    else
2590       return CopyString(_class.fullName);   
2591 }
2592
2593 Type ReplaceThisClassType(Class _class)
2594 {
2595    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2596    {
2597       bool first = true;
2598       int p = 0;
2599       ClassTemplateParameter param;
2600       int lastParam = -1;
2601       char className[1024];
2602       strcpy(className, _class.fullName);
2603       
2604       for(param = _class.templateParams.first; param; param = param.next)
2605       {
2606          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2607          {
2608             if(first) strcat(className, "<");
2609             if(!first) strcat(className, ", ");
2610             if(lastParam + 1 != p)
2611             {
2612                strcat(className, param.name);
2613                strcat(className, " = ");
2614             }
2615             strcat(className, param.name);
2616             first = false;
2617             lastParam = p;
2618          }
2619          p++;
2620       }
2621       if(!first)
2622       {
2623          int len = strlen(className);
2624          if(className[len-1] == '>') className[len++] = ' ';
2625          className[len++] = '>';
2626          className[len++] = '\0';
2627       }
2628       return MkClassType(className);
2629       //return ProcessTypeString(className, false);
2630    }
2631    else
2632    {
2633       return MkClassType(_class.fullName);
2634       //return ProcessTypeString(_class.fullName, false);
2635    }
2636 }
2637
2638 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2639 {
2640    if(specs != null && _class)
2641    {
2642       Specifier spec;
2643       for(spec = specs.first; spec; spec = spec.next)
2644       {
2645          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2646          {
2647             spec.type = nameSpecifier;
2648             spec.name = ReplaceThisClass(_class);
2649             spec.symbol = FindClass(spec.name); //_class.symbol;
2650          }
2651       }
2652    }
2653 }
2654
2655 // Returns imported or not
2656 bool DeclareFunction(GlobalFunction function, char * name)
2657 {
2658    Symbol symbol = function.symbol;
2659    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2660    {
2661       bool imported = false;
2662       bool dllImport = false;
2663
2664       if(!function.dataType)
2665       {
2666          function.dataType = ProcessTypeString(function.dataTypeString, false);
2667          if(!function.dataType.thisClass)
2668             function.dataType.staticMethod = true;
2669       }
2670
2671       if(inCompiler)
2672       {
2673          if(!symbol)
2674          {
2675             ModuleImport module = FindModule(function.module);
2676             // WARNING: This is not added anywhere...
2677             symbol = function.symbol = Symbol {  };
2678
2679             if(module.name)
2680             {
2681                if(!function.dataType.dllExport)
2682                {
2683                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2684                   module.functions.Add(symbol._import);
2685                }
2686             }
2687             // Set the symbol type
2688             {
2689                symbol.type = ProcessTypeString(function.dataTypeString, false);
2690                if(!symbol.type.thisClass)
2691                   symbol.type.staticMethod = true;
2692             }
2693          }
2694          imported = symbol._import ? true : false;
2695          if(imported && function.module != privateModule && function.module.importType != staticImport)
2696             dllImport = true;
2697       }
2698
2699       DeclareType(function.dataType, true, true);
2700
2701       if(inCompiler)
2702       {
2703          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2704          {
2705             // We need a declaration here :)
2706             Declaration decl;
2707             OldList * specifiers, * declarators;
2708             Declarator d;
2709             Declarator funcDecl;
2710             External external;
2711
2712             specifiers = MkList();
2713             declarators = MkList();
2714
2715             //if(imported)
2716                ListAdd(specifiers, MkSpecifier(EXTERN));
2717             /*
2718             else
2719                ListAdd(specifiers, MkSpecifier(STATIC));
2720             */
2721
2722             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2723             //if(imported)
2724             if(dllImport)
2725                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2726
2727             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2728             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2729             if(function.module.importType == staticImport)
2730             {
2731                Specifier spec;
2732                for(spec = specifiers->first; spec; spec = spec.next)
2733                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2734                   {
2735                      specifiers->Remove(spec);
2736                      FreeSpecifier(spec);
2737                      break;
2738                   }
2739             }
2740
2741             funcDecl = GetFuncDecl(d);
2742
2743             // Make sure we don't have empty parameter declarations for static methods...
2744             if(funcDecl && !funcDecl.function.parameters)
2745             {
2746                funcDecl.function.parameters = MkList();
2747                funcDecl.function.parameters->Insert(null, 
2748                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2749             }
2750
2751             ListAdd(declarators, MkInitDeclarator(d, null));
2752
2753             {
2754                Context oldCtx = curContext;
2755                curContext = globalContext;
2756                decl = MkDeclaration(specifiers, declarators);
2757                curContext = oldCtx;
2758             }
2759
2760             // Keep a different symbol for the function definition than the declaration...
2761             if(symbol.pointerExternal)
2762             {
2763                Symbol functionSymbol { };
2764                // Copy symbol
2765                {
2766                   *functionSymbol = *symbol;
2767                   functionSymbol.string = CopyString(symbol.string);
2768                   if(functionSymbol.type)
2769                      functionSymbol.type.refCount++;
2770                }
2771
2772                excludedSymbols->Add(functionSymbol);
2773
2774                symbol.pointerExternal.symbol = functionSymbol;
2775             }
2776             external = MkExternalDeclaration(decl);
2777             if(curExternal)
2778                ast->Insert(curExternal.prev, external);
2779             external.symbol = symbol;
2780             symbol.pointerExternal = external;
2781          }
2782          else
2783          {
2784             // Move declaration higher...
2785             ast->Move(symbol.pointerExternal, curExternal.prev);
2786          }
2787
2788          if(curExternal)
2789             symbol.id = curExternal.symbol.idCode;
2790       }
2791    }
2792    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2793 }
2794
2795 void DeclareGlobalData(GlobalData data)
2796 {
2797    Symbol symbol = data.symbol;
2798    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2799    {
2800       if(inCompiler)
2801       {
2802          if(!symbol)
2803             symbol = data.symbol = Symbol { };
2804       }
2805       if(!data.dataType)
2806          data.dataType = ProcessTypeString(data.dataTypeString, false);
2807       DeclareType(data.dataType, true, true);
2808       if(inCompiler)
2809       {
2810          if(!symbol.pointerExternal)
2811          {
2812             // We need a declaration here :)
2813             Declaration decl;
2814             OldList * specifiers, * declarators;
2815             Declarator d;
2816             External external;
2817
2818             specifiers = MkList();
2819             declarators = MkList();
2820
2821             ListAdd(specifiers, MkSpecifier(EXTERN));
2822             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2823             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2824
2825             ListAdd(declarators, MkInitDeclarator(d, null));
2826
2827             decl = MkDeclaration(specifiers, declarators);
2828             external = MkExternalDeclaration(decl);
2829             if(curExternal)
2830                ast->Insert(curExternal.prev, external);
2831             external.symbol = symbol;
2832             symbol.pointerExternal = external;
2833          }
2834          else
2835          {
2836             // Move declaration higher...
2837             ast->Move(symbol.pointerExternal, curExternal.prev);
2838          }
2839
2840          if(curExternal)
2841             symbol.id = curExternal.symbol.idCode;
2842       }
2843    }
2844 }
2845
2846 class Conversion : struct
2847 {
2848    Conversion prev, next;
2849    Property convert;
2850    bool isGet;
2851    Type resultType;
2852 };
2853
2854 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2855 {
2856    if(source && dest)
2857    {
2858       // Property convert;
2859
2860       if(source.kind == templateType && dest.kind != templateType)
2861       {
2862          Type type = ProcessTemplateParameterType(source.templateParameter);
2863          if(type) source = type;
2864       }
2865
2866       if(dest.kind == templateType && source.kind != templateType)
2867       {
2868          Type type = ProcessTemplateParameterType(dest.templateParameter);
2869          if(type) dest = type;
2870       }
2871
2872       if(dest.classObjectType == typedObject)
2873       {
2874          if(source.classObjectType != anyObject)
2875             return true;
2876          else
2877          {
2878             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2879             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2880             {
2881                return true;
2882             }
2883          }
2884       }
2885       else
2886       {
2887          if(source.classObjectType == anyObject)
2888             return true;
2889          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2890             return true;
2891       }
2892       
2893       if((dest.kind == structType && source.kind == structType) ||
2894          (dest.kind == unionType && source.kind == unionType))
2895       {
2896          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2897              (source.members.first && source.members.first == dest.members.first))
2898             return true;
2899       }
2900
2901       if(dest.kind == ellipsisType && source.kind != voidType)
2902          return true;
2903
2904       if(dest.kind == pointerType && dest.type.kind == voidType &&
2905          ((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))
2906          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2907
2908          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2909       
2910          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2911          return true;
2912       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2913          ((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))
2914          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2915
2916          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2917
2918          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2919          return true;
2920
2921       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2922       {
2923          if(source._class.registered && source._class.registered.type == unitClass)
2924          {
2925             if(conversions != null)
2926             {
2927                if(source._class.registered == dest._class.registered)
2928                   return true;
2929             }
2930             else
2931             {
2932                Class sourceBase, destBase;
2933                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2934                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2935                if(sourceBase == destBase)
2936                   return true;
2937             }
2938          }
2939          // Don't match enum inheriting from other enum if resolving enumeration values
2940          // TESTING: !dest.classObjectType
2941          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2942             (enumBaseType || 
2943                (!source._class.registered || source._class.registered.type != enumClass) || 
2944                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2945             return true;
2946          else
2947          {
2948             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2949             if(enumBaseType && 
2950                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2951                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
2952             {
2953                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2954                {
2955                   return true;
2956                }
2957             }
2958          }
2959       }
2960
2961       // JUST ADDED THIS...
2962       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2963          return true;
2964
2965       if(doConversion)
2966       {
2967          // Just added this for Straight conversion of ColorAlpha => Color
2968          if(source.kind == classType)
2969          {
2970             Class _class;
2971             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2972             {
2973                Property convert;
2974                for(convert = _class.conversions.first; convert; convert = convert.next)
2975                {
2976                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2977                   {
2978                      Conversion after = (conversions != null) ? conversions.last : null;
2979
2980                      if(!convert.dataType)
2981                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2982                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2983                      {
2984                         if(!conversions && !convert.Get)
2985                            return true;
2986                         else if(conversions != null)
2987                         {
2988                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
2989                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
2990                               (dest.kind != classType || dest._class.registered != _class.base))
2991                               return true;
2992                            else
2993                            {
2994                               Conversion conv { convert = convert, isGet = true };
2995                               // conversions.Add(conv);
2996                               conversions.Insert(after, conv);
2997                               return true;
2998                            }
2999                         }
3000                      }
3001                   }
3002                }
3003             }
3004          }
3005
3006          // MOVING THIS??
3007
3008          if(dest.kind == classType)
3009          {
3010             Class _class;
3011             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3012             {
3013                Property convert;
3014                for(convert = _class.conversions.first; convert; convert = convert.next)
3015                {
3016                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3017                   {
3018                      // Conversion after = (conversions != null) ? conversions.last : null;
3019
3020                      if(!convert.dataType)
3021                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3022                      // Just added this equality check to prevent recursion.... Make it safer?
3023                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3024                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3025                      {
3026                         if(!conversions && !convert.Set)
3027                            return true;
3028                         else if(conversions != null)
3029                         {
3030                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3031                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3032                               (source.kind != classType || source._class.registered != _class.base))
3033                               return true;
3034                            else
3035                            {
3036                               // *** Testing this! ***
3037                               Conversion conv { convert = convert };
3038                               conversions.Add(conv);
3039                               //conversions.Insert(after, conv);
3040                               return true;
3041                            }
3042                         }
3043                      }
3044                   }
3045                }
3046             }
3047             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3048             {
3049                if(source.kind != voidType && source.kind != structType && source.kind != unionType && 
3050                   (source.kind != classType || source._class.registered.type != structClass))
3051                   return true;
3052             }*/
3053
3054             // TESTING THIS... IS THIS OK??
3055             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3056             {
3057                if(!dest._class.registered.dataType)
3058                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3059                // Only support this for classes...
3060                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3061                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3062                {
3063                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3064                   {
3065                      return true;
3066                   }
3067                }
3068             }
3069          }
3070
3071          // Moved this lower
3072          if(source.kind == classType)
3073          {
3074             Class _class;
3075             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3076             {
3077                Property convert;
3078                for(convert = _class.conversions.first; convert; convert = convert.next)
3079                {
3080                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3081                   {
3082                      Conversion after = (conversions != null) ? conversions.last : null;
3083
3084                      if(!convert.dataType)
3085                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3086                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3087                      {
3088                         if(!conversions && !convert.Get)
3089                            return true;
3090                         else if(conversions != null)
3091                         {
3092                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3093                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3094                               (dest.kind != classType || dest._class.registered != _class.base))
3095                               return true;
3096                            else
3097                            {
3098                               Conversion conv { convert = convert, isGet = true };
3099
3100                               // conversions.Add(conv);
3101                               conversions.Insert(after, conv);
3102                               return true;
3103                            }
3104                         }
3105                      }
3106                   }
3107                }
3108             }
3109
3110             // TESTING THIS... IS THIS OK??
3111             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3112             {
3113                if(!source._class.registered.dataType)
3114                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3115                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3116                {
3117                   return true;
3118                }
3119             }
3120          }
3121       }
3122
3123       if(source.kind == classType || source.kind == subClassType)
3124          ;
3125       else if(dest.kind == source.kind && 
3126          (dest.kind != structType && dest.kind != unionType &&
3127           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3128           return true;
3129       // RECENTLY ADDED THESE
3130       else if(dest.kind == doubleType && source.kind == floatType)
3131          return true;
3132       else if(dest.kind == shortType && source.kind == charType)
3133          return true;
3134       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == intSizeType /* Exception here for size_t */))
3135          return true;
3136       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3137          return true;
3138       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3139          return true;
3140       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3141          return true;
3142       else if(source.kind == enumType &&
3143          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3144           return true;
3145       else if(dest.kind == enumType &&
3146          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3147           return true;
3148       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && 
3149               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3150       {
3151          Type paramSource, paramDest;
3152
3153          if(dest.kind == methodType)     
3154             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3155          if(source.kind == methodType)   
3156             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3157
3158          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3159          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3160          if(dest.kind == methodType) 
3161             dest = dest.method.dataType;
3162          if(source.kind == methodType) 
3163             source = source.method.dataType;
3164
3165          paramSource = source.params.first;
3166          if(paramSource && paramSource.kind == voidType) paramSource = null;
3167          paramDest = dest.params.first;
3168          if(paramDest && paramDest.kind == voidType) paramDest = null;
3169
3170      
3171          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) && 
3172             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3173          {
3174             // Source thisClass must be derived from destination thisClass
3175             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3176                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3177             {
3178                if(paramDest && paramDest.kind == classType)
3179                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3180                else
3181                   Compiler_Error($"method class should not take an object\n");
3182                return false;
3183             }
3184             paramDest = paramDest.next;
3185          }
3186          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3187          {
3188             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3189             {
3190                if(dest.thisClass)
3191                {
3192                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3193                   {
3194                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3195                      return false;
3196                   }
3197                }
3198                else
3199                {
3200                   // THIS WAS BACKWARDS:
3201                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3202                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3203                   {
3204                      if(owningClassDest)
3205                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3206                      else
3207                         Compiler_Error($"overriding class expected to be derived from method class\n");      
3208                      return false;
3209                   }
3210                }
3211                paramSource = paramSource.next;
3212             }
3213             else
3214             {
3215                if(dest.thisClass)
3216                {
3217                   // Source thisClass must be derived from destination thisClass
3218                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3219                   {
3220                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3221                      return false;
3222                   }
3223                }
3224                else
3225                {
3226                   // THIS WAS BACKWARDS TOO??
3227                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3228                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3229                   {
3230                      //if(owningClass)
3231                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3232                      //else
3233                         //Compiler_Error($"overriding class expected to be derived from method class\n");      
3234                      return false;
3235                   }
3236                }
3237             }
3238          }
3239
3240
3241          // Source return type must be derived from destination return type
3242          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3243          {
3244             Compiler_Warning($"incompatible return type for function\n");
3245             return false;
3246          }
3247
3248          // Check parameters
3249       
3250          for(; paramDest; paramDest = paramDest.next)
3251          {
3252             if(!paramSource)
3253             {
3254                //Compiler_Warning($"not enough parameters\n");
3255                Compiler_Error($"not enough parameters\n");
3256                return false;
3257             }
3258             {
3259                Type paramDestType = paramDest;
3260                Type paramSourceType = paramSource;
3261                Type type = paramDestType;
3262
3263                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3264                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3265                   paramSource.kind != templateType)
3266                {
3267                   int id = 0;
3268                   ClassTemplateParameter curParam = null;
3269                   Class sClass;
3270                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3271                   {
3272                      id = 0;
3273                      if(sClass.templateClass) sClass = sClass.templateClass;
3274                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3275                      {
3276                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3277                         {
3278                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3279                            {
3280                               if(sClass.templateClass) sClass = sClass.templateClass;
3281                               id += sClass.templateParams.count;
3282                            }
3283                            break;
3284                         }
3285                         id++;
3286                      }
3287                      if(curParam) break;
3288                   }
3289
3290                   if(curParam)
3291                   {
3292                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3293                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3294                   }
3295                }
3296
3297                // paramDest must be derived from paramSource
3298                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) && 
3299                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3300                {
3301                   char type[1024];
3302                   type[0] = 0;
3303                   PrintType(paramDest, type, false, true);
3304                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3305                   
3306                   if(paramDestType != paramDest)
3307                      FreeType(paramDestType);
3308                   return false;
3309                }
3310                if(paramDestType != paramDest)
3311                   FreeType(paramDestType);
3312             }
3313          
3314             paramSource = paramSource.next;
3315          }
3316          if(paramSource)
3317          {
3318             Compiler_Error($"too many parameters\n");
3319             return false;
3320          }
3321          return true;
3322       }
3323       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3324       {
3325          return true;
3326       }
3327       else if((dest.kind == pointerType || dest.kind == arrayType) && 
3328          (source.kind == arrayType || source.kind == pointerType))
3329       {
3330          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3331             return true;
3332       }
3333    }
3334    return false;
3335 }
3336
3337 static void FreeConvert(Conversion convert)
3338 {
3339    if(convert.resultType)
3340       FreeType(convert.resultType);
3341 }
3342
3343 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest, 
3344                               char * string, OldList conversions)
3345 {
3346    BTNamedLink link;
3347
3348    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3349    {
3350       Class _class = link.data;
3351       if(_class.type == enumClass)
3352       {
3353          OldList converts { };
3354          Type type { };
3355          type.kind = classType;
3356
3357          if(!_class.symbol)
3358             _class.symbol = FindClass(_class.fullName);
3359          type._class = _class.symbol;
3360
3361          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3362          {
3363             NamedLink value;
3364             Class enumClass = eSystem_FindClass(privateModule, "enum");
3365             if(enumClass)
3366             {
3367                Class baseClass;
3368                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3369                {
3370                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3371                   for(value = e.values.first; value; value = value.next)
3372                   {
3373                      if(!strcmp(value.name, string))
3374                         break;
3375                   }
3376                   if(value)
3377                   {
3378                      FreeExpContents(sourceExp);
3379                      FreeType(sourceExp.expType);
3380
3381                      sourceExp.isConstant = true;
3382                      sourceExp.expType = MkClassType(baseClass.fullName);
3383                      //if(inCompiler)
3384                      {
3385                         char constant[256];
3386                         sourceExp.type = constantExp;
3387                         if(!strcmp(baseClass.dataTypeString, "int"))
3388                            sprintf(constant, "%d",(int)value.data);
3389                         else
3390                            sprintf(constant, "0x%X",(int)value.data);
3391                         sourceExp.constant = CopyString(constant);
3392                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3393                      }
3394                   
3395                      while(converts.first)
3396                      {
3397                         Conversion convert = converts.first;
3398                         converts.Remove(convert);
3399                         conversions.Add(convert);
3400                      }
3401                      delete type;
3402                      return true;
3403                   }
3404                }
3405             }
3406          }
3407          if(converts.first)
3408             converts.Free(FreeConvert);
3409          delete type;
3410       }
3411    }
3412    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3413       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3414          return true;
3415    return false;
3416 }
3417
3418 public bool ModuleVisibility(Module searchIn, Module searchFor)
3419 {
3420    SubModule subModule;
3421    
3422    if(searchFor == searchIn)
3423       return true;
3424
3425    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3426    {
3427       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3428       {
3429          if(ModuleVisibility(subModule.module, searchFor))
3430             return true;
3431       }
3432    }
3433    return false;
3434 }
3435
3436 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3437 {
3438    Module module;
3439
3440    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3441       return true;
3442    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3443       return true;
3444    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3445       return true;
3446
3447    for(module = mainModule.application.allModules.first; module; module = module.next)
3448    {
3449       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3450          return true;
3451    }
3452    return false;
3453 }
3454
3455 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3456 {
3457    Type source = sourceExp.expType;
3458    Type realDest = dest;
3459    Type backupSourceExpType = null;
3460
3461    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3462       return true;
3463
3464    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3465    {
3466        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3467        {
3468           Class sourceBase, destBase;
3469           for(sourceBase = source._class.registered; 
3470               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3471               sourceBase = sourceBase.base);
3472           for(destBase = dest._class.registered; 
3473               destBase && destBase.base && destBase.base.type != systemClass;
3474               destBase = destBase.base);
3475           //if(source._class.registered == dest._class.registered)
3476           if(sourceBase == destBase)
3477              return true;
3478        }
3479    }
3480
3481    if(source)
3482    {
3483       OldList * specs;
3484       bool flag = false;
3485       int64 value = MAXINT;
3486
3487       source.refCount++;
3488       dest.refCount++;
3489
3490       if(sourceExp.type == constantExp)
3491       {
3492          if(source.isSigned)
3493             value = strtoll(sourceExp.constant, null, 0);
3494          else
3495             value = strtoull(sourceExp.constant, null, 0);
3496       }
3497       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3498       {
3499          if(source.isSigned)
3500             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3501          else
3502             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3503       }
3504
3505       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered && 
3506          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3507       {
3508          FreeType(source);
3509          source = Type { kind = intType, isSigned = false, refCount = 1 };
3510       }
3511
3512       if(dest.kind == classType)
3513       {
3514          Class _class = dest._class ? dest._class.registered : null;
3515
3516          if(_class && _class.type == unitClass)
3517          {
3518             if(source.kind != classType)
3519             {
3520                Type tempType { };
3521                Type tempDest, tempSource;
3522
3523                for(; _class.base.type != systemClass; _class = _class.base);
3524                tempSource = dest;
3525                tempDest = tempType;
3526
3527                tempType.kind = classType;
3528                if(!_class.symbol)
3529                   _class.symbol = FindClass(_class.fullName);
3530
3531                tempType._class = _class.symbol;
3532                tempType.truth = dest.truth;
3533                if(tempType._class)
3534                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3535
3536                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3537                backupSourceExpType = sourceExp.expType;
3538                sourceExp.expType = dest; dest.refCount++;
3539                //sourceExp.expType = MkClassType(_class.fullName);
3540                flag = true;            
3541
3542                delete tempType;
3543             }
3544          }
3545
3546
3547          // Why wasn't there something like this?
3548          if(_class && _class.type == bitClass && source.kind != classType)
3549          {
3550             if(!dest._class.registered.dataType)
3551                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3552             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3553             {
3554                FreeType(source);
3555                FreeType(sourceExp.expType);
3556                source = sourceExp.expType = MkClassType(dest._class.string);
3557                source.refCount++;
3558                
3559                //source.kind = classType;
3560                //source._class = dest._class;
3561             }
3562          }
3563
3564          // Adding two enumerations
3565          /*
3566          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3567          {
3568             if(!source._class.registered.dataType)
3569                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3570             if(!dest._class.registered.dataType)
3571                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3572
3573             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3574             {
3575                FreeType(source);
3576                source = sourceExp.expType = MkClassType(dest._class.string);
3577                source.refCount++;
3578                
3579                //source.kind = classType;
3580                //source._class = dest._class;
3581             }
3582          }*/
3583
3584          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3585          {
3586             OldList * specs = MkList();
3587             Declarator decl;
3588             char string[1024];
3589
3590             ReadString(string, sourceExp.string);
3591             decl = SpecDeclFromString(string, specs, null);
3592
3593             FreeExpContents(sourceExp);
3594             FreeType(sourceExp.expType);
3595
3596             sourceExp.type = classExp;
3597             sourceExp._classExp.specifiers = specs;
3598             sourceExp._classExp.decl = decl;
3599             sourceExp.expType = dest;
3600             dest.refCount++;
3601
3602             FreeType(source);
3603             FreeType(dest);
3604             if(backupSourceExpType) FreeType(backupSourceExpType);
3605             return true;
3606          }
3607       }
3608       else if(source.kind == classType)
3609       {
3610          Class _class = source._class ? source._class.registered : null;
3611
3612          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3613          {
3614             /*
3615             if(dest.kind != classType)
3616             {
3617                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3618                if(!source._class.registered.dataType)
3619                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3620                
3621                FreeType(dest);
3622                dest = MkClassType(source._class.string);
3623                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3624                //   dest = MkClassType(source._class.string);
3625             }
3626             */
3627             
3628             if(dest.kind != classType)
3629             {
3630                Type tempType { };
3631                Type tempDest, tempSource;
3632
3633                if(!source._class.registered.dataType)
3634                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3635
3636                for(; _class.base.type != systemClass; _class = _class.base);
3637                tempDest = source;
3638                tempSource = tempType;
3639                tempType.kind = classType;
3640                tempType._class = FindClass(_class.fullName);
3641                tempType.truth = source.truth;
3642                tempType.classObjectType = source.classObjectType;
3643
3644                if(tempType._class)
3645                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3646                
3647                // PUT THIS BACK TESTING UNITS?
3648                if(conversions.last)
3649                {
3650                   ((Conversion)(conversions.last)).resultType = dest;
3651                   dest.refCount++;
3652                }
3653                
3654                FreeType(sourceExp.expType);
3655                sourceExp.expType = MkClassType(_class.fullName);
3656                sourceExp.expType.truth = source.truth;
3657                sourceExp.expType.classObjectType = source.classObjectType;
3658
3659                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3660
3661                if(!sourceExp.destType)
3662                {
3663                   FreeType(sourceExp.destType);
3664                   sourceExp.destType = sourceExp.expType;
3665                   if(sourceExp.expType)
3666                      sourceExp.expType.refCount++;
3667                }
3668                //flag = true;
3669                //source = _class.dataType;
3670
3671
3672                // TOCHECK: TESTING THIS NEW CODE
3673                if(!_class.dataType)
3674                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3675                FreeType(dest);
3676                dest = MkClassType(source._class.string);
3677                dest.truth = source.truth;
3678                dest.classObjectType = source.classObjectType;
3679                
3680                FreeType(source);
3681                source = _class.dataType;
3682                source.refCount++;
3683
3684                delete tempType;
3685             }
3686          }
3687       }
3688
3689       if(!flag)
3690       {
3691          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3692          {
3693             FreeType(source);
3694             FreeType(dest);
3695             return true;
3696          }
3697       }
3698
3699       // Implicit Casts
3700       /*
3701       if(source.kind == classType)
3702       {
3703          Class _class = source._class.registered;
3704          if(_class.type == unitClass)
3705          {
3706             if(!_class.dataType)
3707                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3708             source = _class.dataType;
3709          }
3710       }*/
3711
3712       if(dest.kind == classType)
3713       {
3714          Class _class = dest._class ? dest._class.registered : null;
3715          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || 
3716             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3717          {
3718             if(_class.type == normalClass || _class.type == noHeadClass)
3719             {
3720                Expression newExp { };
3721                *newExp = *sourceExp;
3722                if(sourceExp.destType) sourceExp.destType.refCount++;
3723                if(sourceExp.expType)  sourceExp.expType.refCount++;
3724                sourceExp.type = castExp;
3725                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3726                sourceExp.cast.exp = newExp;
3727                FreeType(sourceExp.expType);
3728                sourceExp.expType = null;
3729                ProcessExpressionType(sourceExp);
3730
3731                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3732                if(!inCompiler)
3733                {
3734                   FreeType(sourceExp.expType);
3735                   sourceExp.expType = dest;
3736                }
3737
3738                FreeType(source);
3739                if(inCompiler) FreeType(dest);
3740
3741                if(backupSourceExpType) FreeType(backupSourceExpType);
3742                return true;
3743             }
3744
3745             if(!_class.dataType)
3746                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3747             FreeType(dest);
3748             dest = _class.dataType;
3749             dest.refCount++;
3750          }
3751
3752          // Accept lower precision types for units, since we want to keep the unit type
3753          if(dest.kind == doubleType && 
3754             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3755              source.kind == charType))
3756          {
3757             specs = MkListOne(MkSpecifier(DOUBLE));
3758          }
3759          else if(dest.kind == floatType && 
3760             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3761             source.kind == doubleType))
3762          {
3763             specs = MkListOne(MkSpecifier(FLOAT));
3764          }
3765          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3766             source.kind == floatType || source.kind == doubleType))
3767          {
3768             specs = MkList();
3769             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3770             ListAdd(specs, MkSpecifier(INT64));
3771          }
3772          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3773             source.kind == floatType || source.kind == doubleType))
3774          {
3775             specs = MkList();
3776             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3777             ListAdd(specs, MkSpecifier(INT));
3778          }
3779          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == intType ||
3780             source.kind == floatType || source.kind == doubleType))
3781          {
3782             specs = MkList();
3783             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3784             ListAdd(specs, MkSpecifier(SHORT));
3785          }
3786          else if(dest.kind == charType && (source.kind == charType || source.kind == shortType || source.kind == intType ||
3787             source.kind == floatType || source.kind == doubleType))
3788          {
3789             specs = MkList();
3790             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3791             ListAdd(specs, MkSpecifier(CHAR));
3792          }
3793          else
3794          {
3795             FreeType(source);
3796             FreeType(dest);
3797             if(backupSourceExpType)
3798             {
3799                // Failed to convert: revert previous exp type
3800                if(sourceExp.expType) FreeType(sourceExp.expType);
3801                sourceExp.expType = backupSourceExpType;
3802             }
3803             return false;
3804          }
3805       }
3806       else if(dest.kind == doubleType && 
3807          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3808           source.kind == charType))
3809       {
3810          specs = MkListOne(MkSpecifier(DOUBLE));
3811       }
3812       else if(dest.kind == floatType && 
3813          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3814       {
3815          specs = MkListOne(MkSpecifier(FLOAT));
3816       }
3817       else if(dest.kind == charType && (source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) && 
3818          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3819       {
3820          specs = MkList();
3821          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3822          ListAdd(specs, MkSpecifier(CHAR));
3823       }
3824       else if(dest.kind == shortType && (source.kind == enumType || source.kind == charType || source.kind == shortType || 
3825          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3826       {
3827          specs = MkList();
3828          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3829          ListAdd(specs, MkSpecifier(SHORT));
3830       }
3831       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType))
3832       {
3833          specs = MkList();
3834          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3835          ListAdd(specs, MkSpecifier(INT));
3836       }
3837       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3838       {
3839          specs = MkList();
3840          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3841          ListAdd(specs, MkSpecifier(INT64));
3842       }
3843       else if(dest.kind == enumType && 
3844          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3845       {
3846          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3847       }
3848       else
3849       {
3850          FreeType(source);
3851          FreeType(dest);
3852          if(backupSourceExpType)
3853          {
3854             // Failed to convert: revert previous exp type
3855             if(sourceExp.expType) FreeType(sourceExp.expType);
3856             sourceExp.expType = backupSourceExpType;
3857          }
3858          return false;
3859       }
3860
3861       if(!flag)
3862       {
3863          Expression newExp { };
3864          *newExp = *sourceExp;
3865          newExp.prev = null;
3866          newExp.next = null;
3867          if(sourceExp.destType) sourceExp.destType.refCount++;
3868          if(sourceExp.expType)  sourceExp.expType.refCount++;
3869
3870          sourceExp.type = castExp;
3871          if(realDest.kind == classType)
3872          {
3873             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3874             FreeList(specs, FreeSpecifier);
3875          }
3876          else
3877             sourceExp.cast.typeName = MkTypeName(specs, null);
3878          if(newExp.type == opExp)
3879          {
3880             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3881          }
3882          else
3883             sourceExp.cast.exp = newExp;
3884          
3885          FreeType(sourceExp.expType);
3886          sourceExp.expType = null;
3887          ProcessExpressionType(sourceExp);
3888       }
3889       else
3890          FreeList(specs, FreeSpecifier);
3891
3892       FreeType(dest);
3893       FreeType(source);
3894       if(backupSourceExpType) FreeType(backupSourceExpType);
3895
3896       return true;
3897    }
3898    else
3899    {
3900       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3901       if(sourceExp.type == identifierExp)
3902       {
3903          Identifier id = sourceExp.identifier;
3904          if(dest.kind == classType)
3905          {
3906             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3907             {
3908                Class _class = dest._class.registered;
3909                Class enumClass = eSystem_FindClass(privateModule, "enum");
3910                if(enumClass)
3911                {
3912                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3913                   {
3914                      NamedLink value;
3915                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3916                      for(value = e.values.first; value; value = value.next)
3917                      {
3918                         if(!strcmp(value.name, id.string))
3919                            break;
3920                      }
3921                      if(value)
3922                      {
3923                         FreeExpContents(sourceExp);
3924                         FreeType(sourceExp.expType);
3925
3926                         sourceExp.isConstant = true;
3927                         sourceExp.expType = MkClassType(_class.fullName);
3928                         //if(inCompiler)
3929                         {
3930                            char constant[256];
3931                            sourceExp.type = constantExp;
3932                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3933                               sprintf(constant, "%d", (int) value.data);
3934                            else
3935                               sprintf(constant, "0x%X", (int) value.data);
3936                            sourceExp.constant = CopyString(constant);
3937                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3938                         }
3939                         return true;
3940                      }
3941                   }
3942                }
3943             }
3944          }
3945
3946          // Loop through all enum classes
3947          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3948             return true;
3949       }
3950    }
3951    return false;
3952 }
3953
3954 #define TERTIARY(o, name, m, t, p) \
3955    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3956    {                                                              \
3957       exp.type = constantExp;                                    \
3958       exp.string = p(op1.m ? op2.m : op3.m);                     \
3959       if(!exp.expType) \
3960          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3961       return true;                                                \
3962    }
3963
3964 #define BINARY(o, name, m, t, p) \
3965    static bool name(Expression exp, Operand op1, Operand op2)   \
3966    {                                                              \
3967       t value2 = op2.m;                                           \
3968       exp.type = constantExp;                                    \
3969       exp.string = p(op1.m o value2);                     \
3970       if(!exp.expType) \
3971          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3972       return true;                                                \
3973    }
3974
3975 #define BINARY_DIVIDE(o, name, m, t, p) \
3976    static bool name(Expression exp, Operand op1, Operand op2)   \
3977    {                                                              \
3978       t value2 = op2.m;                                           \
3979       exp.type = constantExp;                                    \
3980       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3981       if(!exp.expType) \
3982          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3983       return true;                                                \
3984    }
3985
3986 #define UNARY(o, name, m, t, p) \
3987    static bool name(Expression exp, Operand op1)                \
3988    {                                                              \
3989       exp.type = constantExp;                                    \
3990       exp.string = p((t)(o op1.m));                                   \
3991       if(!exp.expType) \
3992          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3993       return true;                                                \
3994    }
3995
3996 #define OPERATOR_ALL(macro, o, name) \
3997    macro(o, Int##name, i, int, PrintInt) \
3998    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
3999    macro(o, Short##name, s, short, PrintShort) \
4000    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4001    macro(o, Char##name, c, char, PrintChar) \
4002    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4003    macro(o, Float##name, f, float, PrintFloat) \
4004    macro(o, Double##name, d, double, PrintDouble)
4005
4006 #define OPERATOR_INTTYPES(macro, o, name) \
4007    macro(o, Int##name, i, int, PrintInt) \
4008    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4009    macro(o, Short##name, s, short, PrintShort) \
4010    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4011    macro(o, Char##name, c, char, PrintChar) \
4012    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4013
4014
4015 // binary arithmetic
4016 OPERATOR_ALL(BINARY, +, Add)
4017 OPERATOR_ALL(BINARY, -, Sub)
4018 OPERATOR_ALL(BINARY, *, Mul)
4019 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4020 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4021
4022 // unary arithmetic
4023 OPERATOR_ALL(UNARY, -, Neg)
4024
4025 // unary arithmetic increment and decrement
4026 OPERATOR_ALL(UNARY, ++, Inc)
4027 OPERATOR_ALL(UNARY, --, Dec)
4028
4029 // binary arithmetic assignment
4030 OPERATOR_ALL(BINARY, =, Asign)
4031 OPERATOR_ALL(BINARY, +=, AddAsign)
4032 OPERATOR_ALL(BINARY, -=, SubAsign)
4033 OPERATOR_ALL(BINARY, *=, MulAsign)
4034 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4035 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4036
4037 // binary bitwise
4038 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4039 OPERATOR_INTTYPES(BINARY, |, BitOr)
4040 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4041 OPERATOR_INTTYPES(BINARY, <<, LShift)
4042 OPERATOR_INTTYPES(BINARY, >>, RShift)
4043
4044 // unary bitwise
4045 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4046
4047 // binary bitwise assignment
4048 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4049 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4050 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4051 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4052 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4053
4054 // unary logical negation
4055 OPERATOR_INTTYPES(UNARY, !, Not)
4056
4057 // binary logical equality
4058 OPERATOR_ALL(BINARY, ==, Equ)
4059 OPERATOR_ALL(BINARY, !=, Nqu)
4060
4061 // binary logical
4062 OPERATOR_ALL(BINARY, &&, And)
4063 OPERATOR_ALL(BINARY, ||, Or)
4064
4065 // binary logical relational
4066 OPERATOR_ALL(BINARY, >, Grt)
4067 OPERATOR_ALL(BINARY, <, Sma)
4068 OPERATOR_ALL(BINARY, >=, GrtEqu)
4069 OPERATOR_ALL(BINARY, <=, SmaEqu)
4070
4071 // tertiary condition operator
4072 OPERATOR_ALL(TERTIARY, ?, Cond)
4073
4074 //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
4075 #define OPERATOR_TABLE_ALL(name, type) \
4076     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4077                           type##Neg, \
4078                           type##Inc, type##Dec, \
4079                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4080                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4081                           type##BitNot, \
4082                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4083                           type##Not, \
4084                           type##Equ, type##Nqu, \
4085                           type##And, type##Or, \
4086                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4087                         }; \
4088
4089 #define OPERATOR_TABLE_INTTYPES(name, type) \
4090     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4091                           type##Neg, \
4092                           type##Inc, type##Dec, \
4093                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4094                           null, null, null, null, null, \
4095                           null, \
4096                           null, null, null, null, null, \
4097                           null, \
4098                           type##Equ, type##Nqu, \
4099                           type##And, type##Or, \
4100                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4101                         }; \
4102
4103 OPERATOR_TABLE_ALL(int, Int)
4104 OPERATOR_TABLE_ALL(uint, UInt)
4105 OPERATOR_TABLE_ALL(short, Short)
4106 OPERATOR_TABLE_ALL(ushort, UShort)
4107 OPERATOR_TABLE_INTTYPES(float, Float)
4108 OPERATOR_TABLE_INTTYPES(double, Double)
4109 OPERATOR_TABLE_ALL(char, Char)
4110 OPERATOR_TABLE_ALL(uchar, UChar)
4111
4112 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4113 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4114 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4115 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4116 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4117 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4118 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4119 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4120
4121 public void ReadString(char * output,  char * string)
4122 {
4123    int len = strlen(string);
4124    int c,d = 0;
4125    bool quoted = false, escaped = false;
4126    for(c = 0; c<len; c++)
4127    {
4128       char ch = string[c];
4129       if(escaped)
4130       {
4131          switch(ch)
4132          {
4133             case 'n': output[d] = '\n'; break;
4134             case 't': output[d] = '\t'; break;
4135             case 'a': output[d] = '\a'; break;
4136             case 'b': output[d] = '\b'; break;
4137             case 'f': output[d] = '\f'; break;
4138             case 'r': output[d] = '\r'; break;
4139             case 'v': output[d] = '\v'; break;
4140             case '\\': output[d] = '\\'; break;
4141             case '\"': output[d] = '\"'; break;
4142             default: output[d++] = '\\'; output[d] = ch;
4143             //default: output[d] = ch;
4144          }
4145          d++;
4146          escaped = false;
4147       }
4148       else 
4149       {
4150          if(ch == '\"') 
4151             quoted ^= true;
4152          else if(quoted)
4153          {
4154             if(ch == '\\')
4155                escaped = true;
4156             else
4157                output[d++] = ch;
4158          }
4159       }
4160    }
4161    output[d] = '\0';
4162 }
4163
4164 public Operand GetOperand(Expression exp)
4165 {
4166    Operand op { };
4167    Type type = exp.expType;
4168    if(type)
4169    {
4170       while(type.kind == classType && 
4171          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4172       {
4173          if(!type._class.registered.dataType)
4174             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4175          type = type._class.registered.dataType;
4176          
4177       }
4178       op.kind = type.kind;
4179       op.type = exp.expType;
4180       if(exp.isConstant && exp.type == constantExp)
4181       {
4182          switch(op.kind)
4183          {
4184             case charType:
4185             {
4186                if(exp.constant[0] == '\'')
4187                   op.c = exp.constant[1];
4188                else if(type.isSigned)
4189                {
4190                   op.c = (char)strtol(exp.constant, null, 0);
4191                   op.ops = charOps;
4192                }
4193                else
4194                {
4195                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4196                   op.ops = ucharOps;
4197                }
4198                break;
4199             }
4200             case shortType:
4201                if(type.isSigned)
4202                {
4203                   op.s = (short)strtol(exp.constant, null, 0);
4204                   op.ops = shortOps;
4205                }
4206                else
4207                {
4208                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4209                   op.ops = ushortOps;
4210                }
4211                break;
4212             case intType:
4213             case longType:
4214                if(type.isSigned)
4215                {
4216                   op.i = (int)strtol(exp.constant, null, 0);
4217                   op.ops = intOps;
4218                }
4219                else
4220                {
4221                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4222                   op.ops = uintOps;
4223                }
4224                op.kind = intType;
4225                break;
4226             case int64Type:
4227                if(type.isSigned)
4228                {
4229                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4230                   op.ops = intOps;
4231                }
4232                else
4233                {
4234                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4235                   op.ops = uintOps;
4236                }
4237                op.kind = intType;
4238                break;
4239             case intPtrType:
4240                if(type.isSigned)
4241                {
4242                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4243                   op.ops = intOps;
4244                }
4245                else
4246                {
4247                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4248                   op.ops = uintOps;
4249                }
4250                op.kind = intType;
4251                break;
4252             case intSizeType:
4253                if(type.isSigned)
4254                {
4255                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4256                   op.ops = intOps;
4257                }
4258                else
4259                {
4260                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4261                   op.ops = uintOps;
4262                }
4263                op.kind = intType;
4264                break;
4265             case floatType:
4266                op.f = (float)strtod(exp.constant, null);
4267                op.ops = floatOps;
4268                break;
4269             case doubleType:
4270                op.d = (double)strtod(exp.constant, null);
4271                op.ops = doubleOps;
4272                break;
4273             //case classType:    For when we have operator overloading...
4274             // Pointer additions
4275             //case functionType:
4276             case arrayType:
4277             case pointerType:
4278             case classType:
4279                op.ui64 = _strtoui64(exp.constant, null, 0);
4280                op.kind = pointerType;
4281                op.ops = uintOps;
4282                // op.ptrSize = 
4283                break;
4284          }
4285       }
4286    }
4287    return op;
4288 }
4289
4290 static void UnusedFunction()
4291 {
4292    int a;
4293    a.OnGetString(0,0,0);
4294 }
4295 default:
4296 extern int __ecereVMethodID_class_OnGetString;
4297 public:
4298
4299 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4300 {
4301    DataMember dataMember;
4302    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4303    {
4304       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4305          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4306       else
4307       {
4308          Expression exp { };
4309          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4310          Type type;
4311          void * ptr = inst.data + dataMember.offset + offset;
4312          char * result = null;
4313          exp.loc = member.loc = inst.loc;
4314          ((Identifier)member.identifiers->first).loc = inst.loc;
4315
4316          if(!dataMember.dataType)
4317             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4318          type = dataMember.dataType;
4319          if(type.kind == classType)
4320          {
4321             Class _class = type._class.registered;
4322             if(_class.type == enumClass)
4323             {
4324                Class enumClass = eSystem_FindClass(privateModule, "enum");
4325                if(enumClass)
4326                {
4327                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4328                   NamedLink item;
4329                   for(item = e.values.first; item; item = item.next)
4330                   {
4331                      if((int)item.data == *(int *)ptr)
4332                      {
4333                         result = item.name;
4334                         break;
4335                      }
4336                   }
4337                   if(result)
4338                   {
4339                      exp.identifier = MkIdentifier(result);
4340                      exp.type = identifierExp;
4341                      exp.destType = MkClassType(_class.fullName);
4342                      ProcessExpressionType(exp);
4343                   }
4344                }
4345             }
4346             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4347             {
4348                if(!_class.dataType)
4349                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4350                type = _class.dataType;
4351             }
4352          }
4353          if(!result)
4354          {
4355             switch(type.kind)
4356             {
4357                case floatType:
4358                {
4359                   FreeExpContents(exp);
4360
4361                   exp.constant = PrintFloat(*(float*)ptr);
4362                   exp.type = constantExp;
4363                   break;
4364                }
4365                case doubleType:
4366                {
4367                   FreeExpContents(exp);
4368
4369                   exp.constant = PrintDouble(*(double*)ptr);
4370                   exp.type = constantExp;
4371                   break;
4372                }
4373                case intType:
4374                {
4375                   FreeExpContents(exp);
4376
4377                   exp.constant = PrintInt(*(int*)ptr);
4378                   exp.type = constantExp;
4379                   break;
4380                }
4381                case int64Type:
4382                {
4383                   FreeExpContents(exp);
4384
4385                   exp.constant = PrintInt64(*(int64*)ptr);
4386                   exp.type = constantExp;
4387                   break;
4388                }
4389                case intPtrType:
4390                {
4391                   FreeExpContents(exp);
4392                   // TODO: This should probably use proper type
4393                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4394                   exp.type = constantExp;
4395                   break;
4396                }
4397                case intSizeType:
4398                {
4399                   FreeExpContents(exp);
4400                   // TODO: This should probably use proper type
4401                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4402                   exp.type = constantExp;
4403                   break;
4404                }
4405                default:
4406                   Compiler_Error($"Unhandled type populating instance\n");
4407             }
4408          }
4409          ListAdd(memberList, member);
4410       }
4411
4412       if(parentDataMember.type == unionMember)
4413          break;
4414    }
4415 }
4416
4417 void PopulateInstance(Instantiation inst)
4418 {
4419    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4420    Class _class = classSym.registered;
4421    DataMember dataMember;
4422    OldList * memberList = MkList();
4423    // Added this check and ->Add to prevent memory leaks on bad code
4424    if(!inst.members)
4425       inst.members = MkListOne(MkMembersInitList(memberList));
4426    else
4427       inst.members->Add(MkMembersInitList(memberList));
4428    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4429    {
4430       if(!dataMember.isProperty)
4431       {
4432          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4433             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4434          else
4435          {
4436             Expression exp { };
4437             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4438             Type type;
4439             void * ptr = inst.data + dataMember.offset;
4440             char * result = null;
4441
4442             exp.loc = member.loc = inst.loc;
4443             ((Identifier)member.identifiers->first).loc = inst.loc;
4444
4445             if(!dataMember.dataType)
4446                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4447             type = dataMember.dataType;
4448             if(type.kind == classType)
4449             {
4450                Class _class = type._class.registered;
4451                if(_class.type == enumClass)
4452                {
4453                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4454                   if(enumClass)
4455                   {
4456                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4457                      NamedLink item;
4458                      for(item = e.values.first; item; item = item.next)
4459                      {
4460                         if((int)item.data == *(int *)ptr)
4461                         {
4462                            result = item.name;
4463                            break;
4464                         }
4465                      }
4466                   }
4467                   if(result)
4468                   {
4469                      exp.identifier = MkIdentifier(result);
4470                      exp.type = identifierExp;
4471                      exp.destType = MkClassType(_class.fullName);
4472                      ProcessExpressionType(exp);
4473                   }                     
4474                }
4475                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4476                {
4477                   if(!_class.dataType)
4478                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4479                   type = _class.dataType;
4480                }
4481             }
4482             if(!result)
4483             {
4484                switch(type.kind)
4485                {
4486                   case floatType:
4487                   {
4488                      exp.constant = PrintFloat(*(float*)ptr);
4489                      exp.type = constantExp;
4490                      break;
4491                   }
4492                   case doubleType:
4493                   {
4494                      exp.constant = PrintDouble(*(double*)ptr);
4495                      exp.type = constantExp;
4496                      break;
4497                   }
4498                   case intType:
4499                   {
4500                      exp.constant = PrintInt(*(int*)ptr);
4501                      exp.type = constantExp;
4502                      break;
4503                   }
4504                   case int64Type:
4505                   {
4506                      exp.constant = PrintInt64(*(int64*)ptr);
4507                      exp.type = constantExp;
4508                      break;
4509                   }
4510                   case intPtrType:
4511                   {
4512                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4513                      exp.type = constantExp;
4514                      break;
4515                   }
4516                   default:
4517                      Compiler_Error($"Unhandled type populating instance\n");
4518                }
4519             }
4520             ListAdd(memberList, member);
4521          }
4522       }
4523    }
4524 }
4525
4526 void ComputeInstantiation(Expression exp)
4527 {
4528    Instantiation inst = exp.instance;
4529    MembersInit members;
4530    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4531    Class _class = classSym ? classSym.registered : null;
4532    DataMember curMember = null;
4533    Class curClass = null;
4534    DataMember subMemberStack[256];
4535    int subMemberStackPos = 0;
4536    uint64 bits = 0;
4537
4538    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4539    {
4540       // Don't recompute the instantiation... 
4541       // Non Simple classes will have become constants by now
4542       if(inst.data) 
4543          return;
4544
4545       if(_class.type == normalClass || _class.type == noHeadClass)
4546          inst.data = (byte *)eInstance_New(_class);
4547       else
4548          inst.data = new0 byte[_class.structSize];
4549    }
4550
4551    if(inst.members)
4552    {
4553       for(members = inst.members->first; members; members = members.next)
4554       {
4555          switch(members.type)
4556          {
4557             case dataMembersInit:
4558             {
4559                if(members.dataMembers)
4560                {
4561                   MemberInit member;
4562                   for(member = members.dataMembers->first; member; member = member.next)
4563                   {
4564                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4565                      bool found = false;
4566
4567                      Property prop = null;
4568                      DataMember dataMember = null;
4569                      Method method = null;
4570                      uint dataMemberOffset;
4571
4572                      if(!ident)
4573                      {
4574                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4575                         if(curMember)
4576                         {
4577                            if(curMember.isProperty)
4578                               prop = (Property)curMember;
4579                            else
4580                            {
4581                               dataMember = curMember;
4582                               
4583                               // CHANGED THIS HERE
4584                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4585
4586                               // 2013/17/29 -- It seems that this was missing here!
4587                               if(_class.type == normalClass)
4588                                  dataMemberOffset += _class.base.structSize;
4589                               // dataMemberOffset = dataMember.offset;
4590                            }
4591                            found = true;
4592                         }
4593                      }
4594                      else
4595                      {
4596                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4597                         if(prop)
4598                         {
4599                            found = true;
4600                            if(prop.memberAccess == publicAccess)
4601                            {
4602                               curMember = (DataMember)prop;
4603                               curClass = prop._class;
4604                            }
4605                         }
4606                         else
4607                         {
4608                            DataMember _subMemberStack[256];
4609                            int _subMemberStackPos = 0;
4610
4611                            // FILL MEMBER STACK
4612                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4613
4614                            if(dataMember)
4615                            {
4616                               found = true;
4617                               if(dataMember.memberAccess == publicAccess)
4618                               {
4619                                  curMember = dataMember;
4620                                  curClass = dataMember._class;
4621                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4622                                  subMemberStackPos = _subMemberStackPos;
4623                               }
4624                            }
4625                         }
4626                      }
4627
4628                      if(found && member.initializer && member.initializer.type == expInitializer)
4629                      {
4630                         Expression value = member.initializer.exp;
4631                         Type type = null;
4632                         bool deepMember = false;
4633                         if(prop)
4634                         {
4635                            type = prop.dataType;
4636                         }
4637                         else if(dataMember)
4638                         {
4639                            if(!dataMember.dataType)
4640                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4641                            
4642                            type = dataMember.dataType;
4643                         }
4644
4645                         if(ident && ident.next)
4646                         {
4647                            deepMember = true;
4648
4649                            // for(; ident && type; ident = ident.next)
4650                            for(ident = ident.next; ident && type; ident = ident.next)
4651                            {
4652                               if(type.kind == classType)
4653                               {
4654                                  prop = eClass_FindProperty(type._class.registered,
4655                                     ident.string, privateModule);
4656                                  if(prop)
4657                                     type = prop.dataType;
4658                                  else
4659                                  {
4660                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered, 
4661                                        ident.string, &dataMemberOffset, privateModule, null, null);
4662                                     if(dataMember)
4663                                        type = dataMember.dataType;
4664                                  }
4665                               }
4666                               else if(type.kind == structType || type.kind == unionType)
4667                               {
4668                                  Type memberType;
4669                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4670                                  {
4671                                     if(!strcmp(memberType.name, ident.string))
4672                                     {
4673                                        type = memberType;
4674                                        break;
4675                                     }
4676                                  }
4677                               }
4678                            }
4679                         }
4680                         if(value)
4681                         {
4682                            FreeType(value.destType);
4683                            value.destType = type;
4684                            if(type) type.refCount++;
4685                            ComputeExpression(value);
4686                         }
4687                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4688                         {
4689                            if(type.kind == classType)
4690                            {
4691                               Class _class = type._class.registered;
4692                               if(_class.type == bitClass || _class.type == unitClass ||
4693                                  _class.type == enumClass)
4694                               {
4695                                  if(!_class.dataType)
4696                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4697                                  type = _class.dataType;
4698                               }
4699                            }
4700
4701                            if(dataMember)
4702                            {
4703                               void * ptr = inst.data + dataMemberOffset;
4704                               
4705                               if(value.type == constantExp)
4706                               {
4707                                  switch(type.kind)
4708                                  {
4709                                     case intType:
4710                                     {
4711                                        GetInt(value, (int*)ptr);
4712                                        break;
4713                                     }
4714                                     case int64Type:
4715                                     {
4716                                        GetInt64(value, (int64*)ptr);
4717                                        break;
4718                                     }
4719                                     case intPtrType:
4720                                     {
4721                                        GetIntPtr(value, (intptr*)ptr);
4722                                        break;
4723                                     }
4724                                     case intSizeType:
4725                                     {
4726                                        GetIntSize(value, (intsize*)ptr);
4727                                        break;
4728                                     }
4729                                     case floatType:
4730                                     {
4731                                        GetFloat(value, (float*)ptr);
4732                                        break;
4733                                     }
4734                                     case doubleType:
4735                                     {
4736                                        GetDouble(value, (double *)ptr);
4737                                        break;
4738                                     }
4739                                  }
4740                               }
4741                               else if(value.type == instanceExp)
4742                               {
4743                                  if(type.kind == classType)
4744                                  {
4745                                     Class _class = type._class.registered;
4746                                     if(_class.type == structClass)
4747                                     {
4748                                        ComputeTypeSize(type);
4749                                        if(value.instance.data)
4750                                           memcpy(ptr, value.instance.data, type.size);
4751                                     }
4752                                  }
4753                               }
4754                            }
4755                            else if(prop)
4756                            {
4757                               if(value.type == instanceExp && value.instance.data)
4758                               {
4759                                  if(type.kind == classType)
4760                                  {
4761                                     Class _class = type._class.registered;
4762                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4763                                     {
4764                                        void (*Set)(void *, void *) = (void *)prop.Set;
4765                                        Set(inst.data, value.instance.data);
4766                                        PopulateInstance(inst);
4767                                     }
4768                                  }
4769                               }
4770                               else if(value.type == constantExp)
4771                               {
4772                                  switch(type.kind)
4773                                  {
4774                                     case doubleType:
4775                                     {
4776                                        void (*Set)(void *, double) = (void *)prop.Set;
4777                                        Set(inst.data, strtod(value.constant, null) );
4778                                        break;
4779                                     }
4780                                     case floatType:
4781                                     {
4782                                        void (*Set)(void *, float) = (void *)prop.Set;
4783                                        Set(inst.data, (float)(strtod(value.constant, null)));
4784                                        break;
4785                                     }
4786                                     case intType:
4787                                     {
4788                                        void (*Set)(void *, int) = (void *)prop.Set;
4789                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4790                                        break;
4791                                     }
4792                                     case int64Type:
4793                                     {
4794                                        void (*Set)(void *, int64) = (void *)prop.Set;
4795                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4796                                        break;
4797                                     }
4798                                     case intPtrType:
4799                                     {
4800                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4801                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4802                                        break;
4803                                     }
4804                                     case intSizeType:
4805                                     {
4806                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4807                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4808                                        break;
4809                                     }
4810                                  }
4811                               }
4812                               else if(value.type == stringExp)
4813                               {
4814                                  char temp[1024];
4815                                  ReadString(temp, value.string);
4816                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4817                               }
4818                            }
4819                         }
4820                         else if(!deepMember && type && _class.type == unitClass)
4821                         {
4822                            if(prop)
4823                            {
4824                               // Only support converting units to units for now...
4825                               if(value.type == constantExp)
4826                               {
4827                                  if(type.kind == classType)
4828                                  {
4829                                     Class _class = type._class.registered;
4830                                     if(_class.type == unitClass)
4831                                     {
4832                                        if(!_class.dataType)
4833                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4834                                        type = _class.dataType;
4835                                     }
4836                                  }
4837                                  // TODO: Assuming same base type for units...
4838                                  switch(type.kind)
4839                                  {
4840                                     case floatType:
4841                                     {
4842                                        float fValue;
4843                                        float (*Set)(float) = (void *)prop.Set;
4844                                        GetFloat(member.initializer.exp, &fValue);
4845                                        exp.constant = PrintFloat(Set(fValue));
4846                                        exp.type = constantExp;
4847                                        break;
4848                                     }
4849                                     case doubleType:
4850                                     {
4851                                        double dValue;
4852                                        double (*Set)(double) = (void *)prop.Set;
4853                                        GetDouble(member.initializer.exp, &dValue);
4854                                        exp.constant = PrintDouble(Set(dValue));
4855                                        exp.type = constantExp;
4856                                        break;
4857                                     }
4858                                  }
4859                               }
4860                            }
4861                         }
4862                         else if(!deepMember && type && _class.type == bitClass)
4863                         {
4864                            if(prop)
4865                            {
4866                               if(value.type == instanceExp && value.instance.data)
4867                               {
4868                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4869                                  bits = Set(value.instance.data);
4870                               }
4871                               else if(value.type == constantExp)
4872                               {
4873                               }
4874                            }
4875                            else if(dataMember)
4876                            {
4877                               BitMember bitMember = (BitMember) dataMember;
4878                               Type type;
4879                               int part = 0;
4880                               GetInt(value, &part);
4881                               bits = (bits & ~bitMember.mask);
4882                               if(!bitMember.dataType)
4883                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4884
4885                               type = bitMember.dataType;
4886
4887                               if(type.kind == classType && type._class && type._class.registered)
4888                               {
4889                                  if(!type._class.registered.dataType)
4890                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);                                    
4891                                  type = type._class.registered.dataType;
4892                               }
4893
4894                               switch(type.kind)
4895                               {
4896                                  case charType:
4897                                     if(type.isSigned)
4898                                        bits |= ((char)part << bitMember.pos);
4899                                     else
4900                                        bits |= ((unsigned char)part << bitMember.pos);
4901                                     break;
4902                                  case shortType:
4903                                     if(type.isSigned)
4904                                        bits |= ((short)part << bitMember.pos);
4905                                     else
4906                                        bits |= ((unsigned short)part << bitMember.pos);
4907                                     break;
4908                                  case intType:
4909                                  case longType:
4910                                     if(type.isSigned)
4911                                        bits |= ((int)part << bitMember.pos);
4912                                     else
4913                                        bits |= ((unsigned int)part << bitMember.pos);
4914                                     break;
4915                                  case int64Type:
4916                                     if(type.isSigned)
4917                                        bits |= ((int64)part << bitMember.pos);
4918                                     else
4919                                        bits |= ((uint64)part << bitMember.pos);
4920                                     break;
4921                                  case intPtrType:
4922                                     if(type.isSigned)
4923                                     {
4924                                        bits |= ((intptr)part << bitMember.pos);
4925                                     }
4926                                     else
4927                                     {
4928                                        bits |= ((uintptr)part << bitMember.pos);
4929                                     }
4930                                     break;
4931                                  case intSizeType:
4932                                     if(type.isSigned)
4933                                     {
4934                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4935                                     }
4936                                     else
4937                                     {
4938                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4939                                     }
4940                                     break;
4941                               }
4942                            }
4943                         }
4944                      }
4945                      else
4946                      {
4947                         if(_class && _class.type == unitClass)
4948                         {
4949                            ComputeExpression(member.initializer.exp);
4950                            exp.constant = member.initializer.exp.constant;
4951                            exp.type = constantExp;
4952                            
4953                            member.initializer.exp.constant = null;
4954                         }
4955                      }
4956                   }
4957                }
4958                break;
4959             }
4960          }
4961       }
4962    }
4963    if(_class && _class.type == bitClass)
4964    {
4965       exp.constant = PrintHexUInt(bits);
4966       exp.type = constantExp;
4967    }
4968    if(exp.type != instanceExp)
4969    {
4970       FreeInstance(inst);
4971    }
4972 }
4973
4974 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4975 {
4976    if(exp.op.op == SIZEOF)
4977    {
4978       FreeExpContents(exp);
4979       exp.type = constantExp;
4980       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
4981    }
4982    else
4983    {
4984       if(!exp.op.exp1)
4985       {
4986          switch(exp.op.op)
4987          {
4988             // unary arithmetic
4989             case '+':
4990             {
4991                // Provide default unary +
4992                Expression exp2 = exp.op.exp2;
4993                exp.op.exp2 = null;
4994                FreeExpContents(exp);
4995                FreeType(exp.expType);
4996                FreeType(exp.destType);
4997                *exp = *exp2;
4998                delete exp2;
4999                break;
5000             }
5001             case '-':
5002                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5003                break;
5004             // unary arithmetic increment and decrement
5005                   //OPERATOR_ALL(UNARY, ++, Inc)
5006                   //OPERATOR_ALL(UNARY, --, Dec)
5007             // unary bitwise
5008             case '~':
5009                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5010                break;
5011             // unary logical negation
5012             case '!':
5013                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5014                break;
5015          }
5016       }
5017       else
5018       {
5019          switch(exp.op.op)
5020          {
5021             // binary arithmetic
5022             case '+':
5023                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5024                break;
5025             case '-':
5026                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5027                break;
5028             case '*':
5029                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5030                break;
5031             case '/':
5032                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5033                break;
5034             case '%':
5035                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5036                break;
5037             // binary arithmetic assignment
5038                   //OPERATOR_ALL(BINARY, =, Asign)
5039                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5040                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5041                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5042                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5043                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5044             // binary bitwise
5045             case '&':
5046                if(exp.op.exp2)
5047                {
5048                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5049                }
5050                break;
5051             case '|':
5052                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5053                break;
5054             case '^':
5055                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5056                break;
5057             case LEFT_OP:
5058                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5059                break;
5060             case RIGHT_OP:
5061                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5062                break;
5063             // binary bitwise assignment
5064                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5065                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5066                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5067                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5068                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5069             // binary logical equality
5070             case EQ_OP:
5071                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5072                break;
5073             case NE_OP:
5074                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5075                break;
5076             // binary logical
5077             case AND_OP:
5078                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5079                break;
5080             case OR_OP:
5081                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5082                break;
5083             // binary logical relational
5084             case '>':
5085                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5086                break;
5087             case '<':
5088                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5089                break;
5090             case GE_OP:
5091                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5092                break;
5093             case LE_OP:
5094                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5095                break;
5096          }
5097       }
5098    }
5099 }
5100
5101 void ComputeExpression(Expression exp)
5102 {
5103    char expString[10240];
5104    expString[0] = '\0';
5105 #ifdef _DEBUG
5106    PrintExpression(exp, expString);
5107 #endif
5108
5109    switch(exp.type)
5110    {
5111       case instanceExp:
5112       {
5113          ComputeInstantiation(exp);
5114          break;
5115       }
5116       /*
5117       case constantExp:
5118          break;
5119       */
5120       case opExp:
5121       {
5122          Expression exp1, exp2 = null;
5123          Operand op1 { };
5124          Operand op2 { };
5125
5126          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5127          if(exp.op.exp2)
5128             ComputeExpression(exp.op.exp2);
5129          if(exp.op.exp1)
5130          {
5131             ComputeExpression(exp.op.exp1);
5132             exp1 = exp.op.exp1;
5133             exp2 = exp.op.exp2;
5134             op1 = GetOperand(exp1);
5135             if(op1.type) op1.type.refCount++;
5136             if(exp2)
5137             {
5138                op2 = GetOperand(exp2);
5139                if(op2.type) op2.type.refCount++;
5140             }
5141          }
5142          else 
5143          {
5144             exp1 = exp.op.exp2;
5145             op1 = GetOperand(exp1);
5146             if(op1.type) op1.type.refCount++;
5147          }
5148
5149          CallOperator(exp, exp1, exp2, op1, op2);
5150          /*
5151          switch(exp.op.op)
5152          {
5153             // Unary operators
5154             case '&':
5155                // Also binary
5156                if(exp.op.exp1 && exp.op.exp2)
5157                {
5158                   // Binary And
5159                   if(op1.ops.BitAnd)
5160                   {
5161                      FreeExpContents(exp);
5162                      op1.ops.BitAnd(exp, op1, op2);
5163                   }
5164                }
5165                break;
5166             case '*':
5167                if(exp.op.exp1)
5168                {
5169                   if(op1.ops.Mul)
5170                   {
5171                      FreeExpContents(exp);
5172                      op1.ops.Mul(exp, op1, op2);
5173                   }
5174                }
5175                break;
5176             case '+':
5177                if(exp.op.exp1)
5178                {
5179                   if(op1.ops.Add)
5180                   {
5181                      FreeExpContents(exp);
5182                      op1.ops.Add(exp, op1, op2);
5183                   }
5184                }
5185                else
5186                {
5187                   // Provide default unary +
5188                   Expression exp2 = exp.op.exp2;
5189                   exp.op.exp2 = null;
5190                   FreeExpContents(exp);
5191                   FreeType(exp.expType);
5192                   FreeType(exp.destType);
5193
5194                   *exp = *exp2;
5195                   delete exp2;
5196                }
5197                break;
5198             case '-':
5199                if(exp.op.exp1)
5200                {
5201                   if(op1.ops.Sub) 
5202                   {
5203                      FreeExpContents(exp);
5204                      op1.ops.Sub(exp, op1, op2);
5205                   }
5206                }
5207                else
5208                {
5209                   if(op1.ops.Neg) 
5210                   {
5211                      FreeExpContents(exp);
5212                      op1.ops.Neg(exp, op1);
5213                   }
5214                }
5215                break;
5216             case '~':
5217                if(op1.ops.BitNot)
5218                {
5219                   FreeExpContents(exp);
5220                   op1.ops.BitNot(exp, op1);
5221                }
5222                break;
5223             case '!':
5224                if(op1.ops.Not)
5225                {
5226                   FreeExpContents(exp);
5227                   op1.ops.Not(exp, op1);
5228                }
5229                break;
5230             // Binary only operators
5231             case '/':
5232                if(op1.ops.Div) 
5233                {
5234                   FreeExpContents(exp);
5235                   op1.ops.Div(exp, op1, op2);
5236                }
5237                break;
5238             case '%':
5239                if(op1.ops.Mod)
5240                {
5241                   FreeExpContents(exp);
5242                   op1.ops.Mod(exp, op1, op2);
5243                }
5244                break;
5245             case LEFT_OP:
5246                break;
5247             case RIGHT_OP:
5248                break;
5249             case '<':
5250                if(exp.op.exp1)
5251                {
5252                   if(op1.ops.Sma)
5253                   {
5254                      FreeExpContents(exp);
5255                      op1.ops.Sma(exp, op1, op2);
5256                   }
5257                }
5258                break;
5259             case '>':
5260                if(exp.op.exp1)
5261                {
5262                   if(op1.ops.Grt)
5263                   {
5264                      FreeExpContents(exp);
5265                      op1.ops.Grt(exp, op1, op2);
5266                   }
5267                }
5268                break;
5269             case LE_OP:
5270                if(exp.op.exp1)
5271                {
5272                   if(op1.ops.SmaEqu)
5273                   {
5274                      FreeExpContents(exp);
5275                      op1.ops.SmaEqu(exp, op1, op2);
5276                   }
5277                }
5278                break;
5279             case GE_OP:
5280                if(exp.op.exp1)
5281                {
5282                   if(op1.ops.GrtEqu)
5283                   {
5284                      FreeExpContents(exp);
5285                      op1.ops.GrtEqu(exp, op1, op2);
5286                   }
5287                }
5288                break;
5289             case EQ_OP:
5290                if(exp.op.exp1)
5291                {
5292                   if(op1.ops.Equ)
5293                   {
5294                      FreeExpContents(exp);
5295                      op1.ops.Equ(exp, op1, op2);
5296                   }
5297                }
5298                break;
5299             case NE_OP:
5300                if(exp.op.exp1)
5301                {
5302                   if(op1.ops.Nqu)
5303                   {
5304                      FreeExpContents(exp);
5305                      op1.ops.Nqu(exp, op1, op2);
5306                   }
5307                }
5308                break;
5309             case '|':
5310                if(op1.ops.BitOr) 
5311                {
5312                   FreeExpContents(exp);
5313                   op1.ops.BitOr(exp, op1, op2);
5314                }
5315                break;
5316             case '^':
5317                if(op1.ops.BitXor) 
5318                {
5319                   FreeExpContents(exp);
5320                   op1.ops.BitXor(exp, op1, op2);
5321                }
5322                break;
5323             case AND_OP:
5324                break;
5325             case OR_OP:
5326                break;
5327             case SIZEOF:
5328                FreeExpContents(exp);
5329                exp.type = constantExp;
5330                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5331                break;
5332          }
5333          */
5334          if(op1.type) FreeType(op1.type);
5335          if(op2.type) FreeType(op2.type);
5336          break;
5337       }
5338       case bracketsExp:
5339       case extensionExpressionExp:
5340       {
5341          Expression e, n;
5342          for(e = exp.list->first; e; e = n)
5343          {
5344             n = e.next;
5345             if(!n)
5346             {
5347                OldList * list = exp.list;
5348                ComputeExpression(e);
5349                //FreeExpContents(exp);
5350                FreeType(exp.expType);
5351                FreeType(exp.destType);
5352                *exp = *e;
5353                delete e;
5354                delete list;
5355             }
5356             else
5357             {
5358                FreeExpression(e);
5359             }
5360          }
5361          break;
5362       }
5363       /*
5364
5365       case ExpIndex:
5366       {
5367          Expression e;
5368          exp.isConstant = true;
5369
5370          ComputeExpression(exp.index.exp);
5371          if(!exp.index.exp.isConstant)
5372             exp.isConstant = false;
5373
5374          for(e = exp.index.index->first; e; e = e.next)
5375          {
5376             ComputeExpression(e);
5377             if(!e.next)
5378             {
5379                // Check if this type is int
5380             }
5381             if(!e.isConstant)
5382                exp.isConstant = false;
5383          }
5384          exp.expType = Dereference(exp.index.exp.expType);
5385          break;
5386       }
5387       */
5388       case memberExp:
5389       {
5390          Expression memberExp = exp.member.exp;
5391          Identifier memberID = exp.member.member;
5392
5393          Type type;
5394          ComputeExpression(exp.member.exp);
5395          type = exp.member.exp.expType;
5396          if(type)
5397          {
5398             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);
5399             Property prop = null;
5400             DataMember member = null;
5401             Class convertTo = null;
5402             if(type.kind == subClassType && exp.member.exp.type == classExp)
5403                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5404
5405             if(!_class)
5406             {
5407                char string[256];
5408                Symbol classSym;
5409                string[0] = '\0';
5410                PrintTypeNoConst(type, string, false, true);
5411                classSym = FindClass(string);
5412                _class = classSym ? classSym.registered : null;
5413             }
5414
5415             if(exp.member.member)
5416             {
5417                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5418                if(!prop)
5419                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5420             }
5421             if(!prop && !member && _class && exp.member.member)
5422             {
5423                Symbol classSym = FindClass(exp.member.member.string);
5424                convertTo = _class;
5425                _class = classSym ? classSym.registered : null;
5426                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5427             }
5428       
5429             if(prop)
5430             {
5431                if(prop.compiled)
5432                {
5433                   Type type = prop.dataType;
5434                   // TODO: Assuming same base type for units...
5435                   if(_class.type == unitClass)
5436                   {
5437                      if(type.kind == classType)
5438                      {
5439                         Class _class = type._class.registered;
5440                         if(_class.type == unitClass)
5441                         {
5442                            if(!_class.dataType)
5443                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5444                            type = _class.dataType;
5445                         }
5446                      }
5447                      switch(type.kind)
5448                      {
5449                         case floatType:
5450                         {
5451                            float value;
5452                            float (*Get)(float) = (void *)prop.Get;
5453                            GetFloat(exp.member.exp, &value);
5454                            exp.constant = PrintFloat(Get ? Get(value) : value);
5455                            exp.type = constantExp;
5456                            break;
5457                         }
5458                         case doubleType:
5459                         {
5460                            double value;
5461                            double (*Get)(double);
5462                            GetDouble(exp.member.exp, &value);
5463                      
5464                            if(convertTo)
5465                               Get = (void *)prop.Set;
5466                            else
5467                               Get = (void *)prop.Get;
5468                            exp.constant = PrintDouble(Get ? Get(value) : value);
5469                            exp.type = constantExp;
5470                            break;
5471                         }
5472                      }
5473                   }
5474                   else
5475                   {
5476                      if(convertTo)
5477                      {
5478                         Expression value = exp.member.exp;
5479                         Type type;
5480                         if(!prop.dataType)
5481                            ProcessPropertyType(prop);
5482
5483                         type = prop.dataType;
5484                         if(!type)
5485                         {
5486                             // printf("Investigate this\n");
5487                         }
5488                         else if(_class.type == structClass)
5489                         {
5490                            switch(type.kind)
5491                            {
5492                               case classType:
5493                               {
5494                                  Class propertyClass = type._class.registered;
5495                                  if(propertyClass.type == structClass && value.type == instanceExp)
5496                                  {
5497                                     void (*Set)(void *, void *) = (void *)prop.Set;
5498                                     exp.instance = Instantiation { };
5499                                     exp.instance.data = new0 byte[_class.structSize];
5500                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5501                                     exp.instance.loc = exp.loc;
5502                                     exp.type = instanceExp;
5503                                     Set(exp.instance.data, value.instance.data);
5504                                     PopulateInstance(exp.instance);
5505                                  }
5506                                  break;
5507                               }
5508                               case intType:
5509                               {
5510                                  int intValue;
5511                                  void (*Set)(void *, int) = (void *)prop.Set;
5512
5513                                  exp.instance = Instantiation { };
5514                                  exp.instance.data = new0 byte[_class.structSize];
5515                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5516                                  exp.instance.loc = exp.loc;
5517                                  exp.type = instanceExp;
5518                               
5519                                  GetInt(value, &intValue);
5520
5521                                  Set(exp.instance.data, intValue);
5522                                  PopulateInstance(exp.instance);
5523                                  break;
5524                               }
5525                               case int64Type:
5526                               {
5527                                  int64 intValue;
5528                                  void (*Set)(void *, int64) = (void *)prop.Set;
5529
5530                                  exp.instance = Instantiation { };
5531                                  exp.instance.data = new0 byte[_class.structSize];
5532                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5533                                  exp.instance.loc = exp.loc;
5534                                  exp.type = instanceExp;
5535                               
5536                                  GetInt64(value, &intValue);
5537
5538                                  Set(exp.instance.data, intValue);
5539                                  PopulateInstance(exp.instance);
5540                                  break;
5541                               }
5542                               case intPtrType:
5543                               {
5544                                  // TOFIX:
5545                                  intptr intValue;
5546                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5547
5548                                  exp.instance = Instantiation { };
5549                                  exp.instance.data = new0 byte[_class.structSize];
5550                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5551                                  exp.instance.loc = exp.loc;
5552                                  exp.type = instanceExp;
5553                               
5554                                  GetIntPtr(value, &intValue);
5555
5556                                  Set(exp.instance.data, intValue);
5557                                  PopulateInstance(exp.instance);
5558                                  break;
5559                               }
5560                               case intSizeType:
5561                               {
5562                                  // TOFIX:
5563                                  intsize intValue;
5564                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5565
5566                                  exp.instance = Instantiation { };
5567                                  exp.instance.data = new0 byte[_class.structSize];
5568                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5569                                  exp.instance.loc = exp.loc;
5570                                  exp.type = instanceExp;
5571
5572                                  GetIntSize(value, &intValue);
5573
5574                                  Set(exp.instance.data, intValue);
5575                                  PopulateInstance(exp.instance);
5576                                  break;
5577                               }
5578                               case doubleType:
5579                               {
5580                                  double doubleValue;
5581                                  void (*Set)(void *, double) = (void *)prop.Set;
5582
5583                                  exp.instance = Instantiation { };
5584                                  exp.instance.data = new0 byte[_class.structSize];
5585                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5586                                  exp.instance.loc = exp.loc;
5587                                  exp.type = instanceExp;
5588                               
5589                                  GetDouble(value, &doubleValue);
5590
5591                                  Set(exp.instance.data, doubleValue);
5592                                  PopulateInstance(exp.instance);
5593                                  break;
5594                               }
5595                            }
5596                         }
5597                         else if(_class.type == bitClass)
5598                         {
5599                            switch(type.kind)
5600                            {
5601                               case classType:
5602                               {
5603                                  Class propertyClass = type._class.registered;
5604                                  if(propertyClass.type == structClass && value.instance.data)
5605                                  {
5606                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5607                                     unsigned int bits = Set(value.instance.data);
5608                                     exp.constant = PrintHexUInt(bits);
5609                                     exp.type = constantExp;
5610                                     break;
5611                                  }
5612                                  else if(_class.type == bitClass)
5613                                  {
5614                                     unsigned int value;
5615                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5616                                     unsigned int bits;
5617
5618                                     GetUInt(exp.member.exp, &value);
5619                                     bits = Set(value);
5620                                     exp.constant = PrintHexUInt(bits);
5621                                     exp.type = constantExp;
5622                                  }
5623                               }
5624                            }
5625                         }
5626                      }
5627                      else
5628                      {
5629                         if(_class.type == bitClass)
5630                         {
5631                            unsigned int value;
5632                            GetUInt(exp.member.exp, &value);
5633
5634                            switch(type.kind)
5635                            {
5636                               case classType:
5637                               {
5638                                  Class _class = type._class.registered;
5639                                  if(_class.type == structClass)
5640                                  {
5641                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5642
5643                                     exp.instance = Instantiation { };
5644                                     exp.instance.data = new0 byte[_class.structSize];
5645                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5646                                     exp.instance.loc = exp.loc;
5647                                     //exp.instance.fullSet = true;
5648                                     exp.type = instanceExp;
5649                                     Get(value, exp.instance.data);
5650                                     PopulateInstance(exp.instance);
5651                                  }
5652                                  else if(_class.type == bitClass)
5653                                  {
5654                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5655                                     uint64 bits = Get(value);
5656                                     exp.constant = PrintHexUInt64(bits);
5657                                     exp.type = constantExp;
5658                                  }
5659                                  break;
5660                               }
5661                            }
5662                         }
5663                         else if(_class.type == structClass)
5664                         {
5665                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5666                            switch(type.kind)
5667                            {
5668                               case classType:
5669                               {
5670                                  Class _class = type._class.registered;
5671                                  if(_class.type == structClass && value)
5672                                  {
5673                                     void (*Get)(void *, void *) = (void *)prop.Get;
5674
5675                                     exp.instance = Instantiation { };
5676                                     exp.instance.data = new0 byte[_class.structSize];
5677                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5678                                     exp.instance.loc = exp.loc;
5679                                     //exp.instance.fullSet = true;
5680                                     exp.type = instanceExp;
5681                                     Get(value, exp.instance.data);
5682                                     PopulateInstance(exp.instance);
5683                                  }
5684                                  break;
5685                               }
5686                            }
5687                         }
5688                         /*else
5689                         {
5690                            char * value = exp.member.exp.instance.data;
5691                            switch(type.kind)
5692                            {
5693                               case classType:
5694                               {
5695                                  Class _class = type._class.registered;
5696                                  if(_class.type == normalClass)
5697                                  {
5698                                     void *(*Get)(void *) = (void *)prop.Get;
5699
5700                                     exp.instance = Instantiation { };
5701                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5702                                     exp.type = instanceExp;
5703                                     exp.instance.data = Get(value, exp.instance.data);
5704                                  }
5705                                  break;
5706                               }
5707                            }
5708                         }
5709                         */
5710                      }
5711                   }
5712                }
5713                else
5714                {
5715                   exp.isConstant = false;
5716                }
5717             }
5718             else if(member)
5719             {
5720             }
5721          }
5722
5723          if(exp.type != ExpressionType::memberExp)
5724          {
5725             FreeExpression(memberExp);
5726             FreeIdentifier(memberID);
5727          }
5728          break;
5729       }
5730       case typeSizeExp:
5731       {
5732          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5733          FreeExpContents(exp);
5734          exp.constant = PrintUInt(ComputeTypeSize(type));
5735          exp.type = constantExp;         
5736          FreeType(type);
5737          break;
5738       }
5739       case classSizeExp:
5740       {
5741          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5742          if(classSym && classSym.registered)
5743          {
5744             if(classSym.registered.fixed)
5745             {
5746                FreeSpecifier(exp._class);
5747                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5748                exp.type = constantExp;
5749             }
5750             else
5751             {
5752                char className[1024];
5753                strcpy(className, "__ecereClass_");
5754                FullClassNameCat(className, classSym.string, true);
5755                MangleClassName(className);
5756
5757                FreeExpContents(exp);
5758                exp.type = pointerExp;
5759                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5760                exp.member.member = MkIdentifier("structSize");
5761             }
5762          }
5763          break;
5764       }
5765       case castExp:
5766       //case constantExp:
5767       {
5768          Type type;
5769          Expression e = exp;
5770          if(exp.type == castExp)
5771          {
5772             if(exp.cast.exp)
5773                ComputeExpression(exp.cast.exp);
5774             e = exp.cast.exp;
5775          }
5776          if(e && exp.expType)
5777          {
5778             /*if(exp.destType)
5779                type = exp.destType;
5780             else*/
5781                type = exp.expType;
5782             if(type.kind == classType)
5783             {
5784                Class _class = type._class.registered;
5785                if(_class && (_class.type == unitClass || _class.type == bitClass))
5786                {
5787                   if(!_class.dataType)
5788                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5789                   type = _class.dataType;
5790                }
5791             }
5792             
5793             switch(type.kind)
5794             {
5795                case charType:
5796                   if(type.isSigned)
5797                   {
5798                      char value;
5799                      GetChar(e, &value);
5800                      FreeExpContents(exp);
5801                      exp.constant = PrintChar(value);
5802                      exp.type = constantExp;
5803                   }
5804                   else
5805                   {
5806                      unsigned char value;
5807                      GetUChar(e, &value);
5808                      FreeExpContents(exp);
5809                      exp.constant = PrintUChar(value);
5810                      exp.type = constantExp;
5811                   }
5812                   break;
5813                case shortType:
5814                   if(type.isSigned)
5815                   {
5816                      short value;
5817                      GetShort(e, &value);
5818                      FreeExpContents(exp);
5819                      exp.constant = PrintShort(value);
5820                      exp.type = constantExp;
5821                   }
5822                   else
5823                   {
5824                      unsigned short value;
5825                      GetUShort(e, &value);
5826                      FreeExpContents(exp);
5827                      exp.constant = PrintUShort(value);
5828                      exp.type = constantExp;
5829                   }
5830                   break;
5831                case intType:
5832                   if(type.isSigned)
5833                   {
5834                      int value;
5835                      GetInt(e, &value);
5836                      FreeExpContents(exp);
5837                      exp.constant = PrintInt(value);
5838                      exp.type = constantExp;
5839                   }
5840                   else
5841                   {
5842                      unsigned int value;
5843                      GetUInt(e, &value);
5844                      FreeExpContents(exp);
5845                      exp.constant = PrintUInt(value);
5846                      exp.type = constantExp;
5847                   }
5848                   break;
5849                case int64Type:
5850                   if(type.isSigned)
5851                   {
5852                      int64 value;
5853                      GetInt64(e, &value);
5854                      FreeExpContents(exp);
5855                      exp.constant = PrintInt64(value);
5856                      exp.type = constantExp;
5857                   }
5858                   else
5859                   {
5860                      uint64 value;
5861                      GetUInt64(e, &value);
5862                      FreeExpContents(exp);
5863                      exp.constant = PrintUInt64(value);
5864                      exp.type = constantExp;
5865                   }
5866                   break;
5867                case intPtrType:
5868                   if(type.isSigned)
5869                   {
5870                      intptr value;
5871                      GetIntPtr(e, &value);
5872                      FreeExpContents(exp);
5873                      exp.constant = PrintInt64((int64)value);
5874                      exp.type = constantExp;
5875                   }
5876                   else
5877                   {
5878                      uintptr value;
5879                      GetUIntPtr(e, &value);
5880                      FreeExpContents(exp);
5881                      exp.constant = PrintUInt64((uint64)value);
5882                      exp.type = constantExp;
5883                   }
5884                   break;
5885                case intSizeType:
5886                   if(type.isSigned)
5887                   {
5888                      intsize value;
5889                      GetIntSize(e, &value);
5890                      FreeExpContents(exp);
5891                      exp.constant = PrintInt64((int64)value);
5892                      exp.type = constantExp;
5893                   }
5894                   else
5895                   {
5896                      uintsize value;
5897                      GetUIntSize(e, &value);
5898                      FreeExpContents(exp);
5899                      exp.constant = PrintUInt64((uint64)value);
5900                      exp.type = constantExp;
5901                   }
5902                   break;
5903                case floatType:
5904                {
5905                   float value;
5906                   GetFloat(e, &value);
5907                   FreeExpContents(exp);
5908                   exp.constant = PrintFloat(value);
5909                   exp.type = constantExp;
5910                   break;
5911                }
5912                case doubleType:
5913                {  
5914                   double value;
5915                   GetDouble(e, &value);
5916                   FreeExpContents(exp);
5917                   exp.constant = PrintDouble(value);
5918                   exp.type = constantExp;
5919                   break;
5920                }
5921             }
5922          }
5923          break;
5924       }
5925       case conditionExp:
5926       {
5927          Operand op1 { };
5928          Operand op2 { };
5929          Operand op3 { };
5930
5931          if(exp.cond.exp)
5932             // Caring only about last expression for now...
5933             ComputeExpression(exp.cond.exp->last);
5934          if(exp.cond.elseExp)
5935             ComputeExpression(exp.cond.elseExp);
5936          if(exp.cond.cond)
5937             ComputeExpression(exp.cond.cond);
5938
5939          op1 = GetOperand(exp.cond.cond);
5940          if(op1.type) op1.type.refCount++;
5941          op2 = GetOperand(exp.cond.exp->last);
5942          if(op2.type) op2.type.refCount++;
5943          op3 = GetOperand(exp.cond.elseExp);
5944          if(op3.type) op3.type.refCount++;
5945
5946          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5947          if(op1.type) FreeType(op1.type);
5948          if(op2.type) FreeType(op2.type);
5949          if(op3.type) FreeType(op3.type);
5950          break;
5951       }  
5952    }
5953 }
5954
5955 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5956 {
5957    bool result = true;
5958    if(destType)
5959    {
5960       OldList converts { };
5961       Conversion convert;
5962
5963       if(destType.kind == voidType)
5964          return false;
5965
5966       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5967          result = false;
5968       if(converts.count)
5969       {
5970          // for(convert = converts.last; convert; convert = convert.prev)
5971          for(convert = converts.first; convert; convert = convert.next)
5972          {
5973             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5974             if(!empty)
5975             {
5976                Expression newExp { };
5977                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
5978
5979                // TODO: Check this...
5980                *newExp = *exp;
5981                newExp.destType = null;
5982
5983                if(convert.isGet)
5984                {
5985                   // [exp].ColorRGB
5986                   exp.type = memberExp;
5987                   exp.addedThis = true;
5988                   exp.member.exp = newExp;
5989                   FreeType(exp.member.exp.expType);
5990
5991                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
5992                   exp.member.exp.expType.classObjectType = objectType;
5993                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
5994                   exp.member.memberType = propertyMember;
5995                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
5996                   // TESTING THIS... for (int)degrees
5997                   exp.needCast = true;
5998                   if(exp.expType) exp.expType.refCount++;
5999                   ApplyAnyObjectLogic(exp.member.exp);
6000                }
6001                else
6002                {
6003                
6004                   /*if(exp.isConstant)
6005                   {
6006                      // Color { ColorRGB = [exp] };
6007                      exp.type = instanceExp;
6008                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6009                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6010                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6011                   }
6012                   else*/
6013                   {
6014                      // If not constant, don't turn it yet into an instantiation
6015                      // (Go through the deep members system first)
6016                      exp.type = memberExp;
6017                      exp.addedThis = true;
6018                      exp.member.exp = newExp;
6019
6020                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6021                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6022                         newExp.expType._class.registered.type == noHeadClass)
6023                      {
6024                         newExp.byReference = true;
6025                      }
6026
6027                      FreeType(exp.member.exp.expType);
6028                      /*exp.member.exp.expType = convert.convert.dataType;
6029                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6030                      exp.member.exp.expType = null;
6031                      if(convert.convert.dataType)
6032                      {
6033                         exp.member.exp.expType = { };
6034                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6035                         exp.member.exp.expType.refCount = 1;
6036                         exp.member.exp.expType.classObjectType = objectType;
6037                         ApplyAnyObjectLogic(exp.member.exp);
6038                      }
6039
6040                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6041                      exp.member.memberType = reverseConversionMember;
6042                      exp.expType = convert.resultType ? convert.resultType :
6043                         MkClassType(convert.convert._class.fullName);
6044                      exp.needCast = true;
6045                      if(convert.resultType) convert.resultType.refCount++;
6046                   }
6047                }
6048             }
6049             else
6050             {
6051                FreeType(exp.expType);
6052                if(convert.isGet)
6053                {
6054                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6055                   exp.needCast = true;
6056                   if(exp.expType) exp.expType.refCount++;
6057                }
6058                else
6059                {
6060                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6061                   exp.needCast = true;
6062                   if(convert.resultType)
6063                      convert.resultType.refCount++;
6064                }
6065             }
6066          }
6067          if(exp.isConstant && inCompiler)
6068             ComputeExpression(exp);
6069
6070          converts.Free(FreeConvert);
6071       }
6072
6073       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6074       {
6075          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6076       }
6077       if(!result && exp.expType && exp.destType)
6078       {
6079          if((exp.destType.kind == classType && exp.expType.kind == pointerType && 
6080              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6081             (exp.expType.kind == classType && exp.destType.kind == pointerType && 
6082             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6083             result = true;
6084       }
6085    }
6086    // if(result) CheckTemplateTypes(exp);
6087    return result;
6088 }
6089
6090 void CheckTemplateTypes(Expression exp)
6091 {
6092    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6093    {
6094       Expression newExp { };
6095       Statement compound;
6096       Context context;
6097       *newExp = *exp;
6098       if(exp.destType) exp.destType.refCount++;
6099       if(exp.expType)  exp.expType.refCount++;
6100       newExp.prev = null;
6101       newExp.next = null;
6102
6103       switch(exp.expType.kind)
6104       {
6105          case doubleType:
6106             if(exp.destType.classObjectType)
6107             {
6108                // We need to pass the address, just pass it along (Undo what was done above)
6109                if(exp.destType) exp.destType.refCount--;
6110                if(exp.expType)  exp.expType.refCount--;
6111                delete newExp;
6112             }
6113             else
6114             {
6115                // If we're looking for value:
6116                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6117                OldList * specs;
6118                OldList * unionDefs = MkList();
6119                OldList * statements = MkList();
6120                context = PushContext();
6121                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null))); 
6122                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6123                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6124                exp.type = extensionCompoundExp;
6125                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6126                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6127                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6128                exp.compound.compound.context = context;
6129                PopContext(context);
6130             }
6131             break;
6132          default:
6133             exp.type = castExp;
6134             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6135             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6136             break;
6137       }
6138    }
6139    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6140    {
6141       Expression newExp { };
6142       Statement compound;
6143       Context context;
6144       *newExp = *exp;
6145       if(exp.destType) exp.destType.refCount++;
6146       if(exp.expType)  exp.expType.refCount++;
6147       newExp.prev = null;
6148       newExp.next = null;
6149
6150       switch(exp.expType.kind)
6151       {
6152          case doubleType:
6153             if(exp.destType.classObjectType)
6154             {
6155                // We need to pass the address, just pass it along (Undo what was done above)
6156                if(exp.destType) exp.destType.refCount--;
6157                if(exp.expType)  exp.expType.refCount--;
6158                delete newExp;
6159             }
6160             else
6161             {
6162                // If we're looking for value:
6163                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6164                OldList * specs;
6165                OldList * unionDefs = MkList();
6166                OldList * statements = MkList();
6167                context = PushContext();
6168                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null))); 
6169                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6170                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6171                exp.type = extensionCompoundExp;
6172                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6173                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6174                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6175                exp.compound.compound.context = context;
6176                PopContext(context);
6177             }
6178             break;
6179          case classType:
6180          {
6181             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6182             {
6183                exp.type = bracketsExp;
6184                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6185                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6186                ProcessExpressionType(exp.list->first);
6187                break;
6188             }
6189             else
6190             {
6191                exp.type = bracketsExp;
6192                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6193                newExp.needCast = true;
6194                ProcessExpressionType(exp.list->first);
6195                break;
6196             }
6197          }
6198          default:
6199          {
6200             if(exp.expType.kind == templateType)
6201             {
6202                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6203                if(type)
6204                {
6205                   FreeType(exp.destType);
6206                   FreeType(exp.expType);
6207                   delete newExp;
6208                   break;
6209                }
6210             }
6211             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6212             {
6213                exp.type = opExp;
6214                exp.op.op = '*';
6215                exp.op.exp1 = null;
6216                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6217                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6218             }
6219             else
6220             {
6221                char typeString[1024];
6222                Declarator decl;
6223                OldList * specs = MkList();
6224                typeString[0] = '\0';
6225                PrintType(exp.expType, typeString, false, false);
6226                decl = SpecDeclFromString(typeString, specs, null);
6227                
6228                exp.type = castExp;
6229                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6230                exp.cast.typeName = MkTypeName(specs, decl);
6231                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6232                exp.cast.exp.needCast = true;
6233             }
6234             break;
6235          }
6236       }
6237    }
6238 }
6239 // TODO: The Symbol tree should be reorganized by namespaces
6240 // Name Space:
6241 //    - Tree of all symbols within (stored without namespace)
6242 //    - Tree of sub-namespaces
6243
6244 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6245 {
6246    int nsLen = strlen(nameSpace);
6247    Symbol symbol;
6248    // Start at the name space prefix
6249    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6250    {
6251       char * s = symbol.string;
6252       if(!strncmp(s, nameSpace, nsLen))
6253       {
6254          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6255          int c;
6256          char * namePart;
6257          for(c = strlen(s)-1; c >= 0; c--)
6258             if(s[c] == ':')
6259                break;
6260
6261          namePart = s+c+1;
6262          if(!strcmp(namePart, name))
6263          {
6264             // TODO: Error on ambiguity
6265             return symbol;
6266          }
6267       }
6268       else
6269          break;
6270    }
6271    return null;
6272 }
6273
6274 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6275 {
6276    int c;
6277    char nameSpace[1024];
6278    char * namePart;
6279    bool gotColon = false;
6280
6281    nameSpace[0] = '\0';
6282    for(c = strlen(name)-1; c >= 0; c--)
6283       if(name[c] == ':')
6284       {
6285          gotColon = true;
6286          break;
6287       }
6288
6289    namePart = name+c+1;
6290    while(c >= 0 && name[c] == ':') c--;
6291    if(c >= 0)
6292    {
6293       // Try an exact match first
6294       Symbol symbol = (Symbol)tree.FindString(name);
6295       if(symbol)
6296          return symbol;
6297
6298       // Namespace specified
6299       memcpy(nameSpace, name, c + 1);
6300       nameSpace[c+1] = 0;
6301
6302       return ScanWithNameSpace(tree, nameSpace, namePart);
6303    }
6304    else if(gotColon)
6305    {
6306       // Looking for a global symbol, e.g. ::Sleep()
6307       Symbol symbol = (Symbol)tree.FindString(namePart);
6308       return symbol;
6309    }
6310    else
6311    {
6312       // Name only (no namespace specified)
6313       Symbol symbol = (Symbol)tree.FindString(namePart);
6314       if(symbol)
6315          return symbol;
6316       return ScanWithNameSpace(tree, "", namePart);
6317    }
6318    return null;
6319 }
6320
6321 static void ProcessDeclaration(Declaration decl);
6322
6323 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6324 {
6325 #ifdef _DEBUG
6326    //Time startTime = GetTime();
6327 #endif
6328    // Optimize this later? Do this before/less?
6329    Context ctx;
6330    Symbol symbol = null;
6331    // First, check if the identifier is declared inside the function
6332    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6333
6334    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6335    {
6336       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6337       {
6338          symbol = null;
6339          if(thisNameSpace)
6340          {
6341             char curName[1024];
6342             strcpy(curName, thisNameSpace);
6343             strcat(curName, "::");
6344             strcat(curName, name);
6345             // Try to resolve in current namespace first
6346             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6347          }
6348          if(!symbol)
6349             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6350       }
6351       else
6352          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6353
6354       if(symbol || ctx == endContext) break;
6355    }
6356    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6357    {
6358       if(symbol.pointerExternal.type == functionExternal)
6359       {
6360          FunctionDefinition function = symbol.pointerExternal.function;
6361
6362          // Modified this recently...
6363          Context tmpContext = curContext;
6364          curContext = null;         
6365          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6366          curContext = tmpContext;
6367
6368          symbol.pointerExternal.symbol = symbol;
6369
6370          // TESTING THIS:
6371          DeclareType(symbol.type, true, true);
6372
6373          ast->Insert(curExternal.prev, symbol.pointerExternal);
6374
6375          symbol.id = curExternal.symbol.idCode;
6376
6377       }
6378       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6379       {
6380          ast->Move(symbol.pointerExternal, curExternal.prev);
6381          symbol.id = curExternal.symbol.idCode;
6382       }
6383    }
6384 #ifdef _DEBUG
6385    //findSymbolTotalTime += GetTime() - startTime;
6386 #endif
6387    return symbol;
6388 }
6389
6390 static void GetTypeSpecs(Type type, OldList * specs)
6391 {
6392    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6393    switch(type.kind)
6394    {
6395       case classType: 
6396       {
6397          if(type._class.registered)
6398          {
6399             if(!type._class.registered.dataType)
6400                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6401             GetTypeSpecs(type._class.registered.dataType, specs);
6402          }
6403          break;
6404       }
6405       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6406       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6407       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6408       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6409       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6410       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6411       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6412       case intType: 
6413       default:
6414          ListAdd(specs, MkSpecifier(INT)); break;
6415    }
6416 }
6417
6418 static void PrintArraySize(Type arrayType, char * string)
6419 {
6420    char size[256];
6421    size[0] = '\0';
6422    strcat(size, "[");
6423    if(arrayType.enumClass)
6424       strcat(size, arrayType.enumClass.string);
6425    else if(arrayType.arraySizeExp)
6426       PrintExpression(arrayType.arraySizeExp, size);
6427    strcat(size, "]");
6428    strcat(string, size);
6429 }
6430
6431 // WARNING : This function expects a null terminated string since it recursively concatenate...
6432 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6433 {
6434    if(type)
6435    {
6436       if(printConst && type.constant)
6437          strcat(string, "const ");
6438       switch(type.kind)
6439       {
6440          case classType:
6441          {
6442             Symbol c = type._class;
6443             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6444             //       look into merging with thisclass ?
6445             if(type.classObjectType == typedObject)
6446                strcat(string, "typed_object");
6447             else if(type.classObjectType == anyObject)
6448                strcat(string, "any_object");
6449             else
6450             {
6451                if(c && c.string)
6452                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6453             }
6454             if(type.byReference)
6455                strcat(string, " &");
6456             break;
6457          }
6458          case voidType: strcat(string, "void"); break;
6459          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6460          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6461          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6462          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6463          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6464          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6465          case floatType: strcat(string, "float"); break;
6466          case doubleType: strcat(string, "double"); break;
6467          case structType:
6468             if(type.enumName)
6469             {
6470                strcat(string, "struct ");
6471                strcat(string, type.enumName);
6472             }
6473             else if(type.typeName)
6474                strcat(string, type.typeName);
6475             else
6476             {
6477                Type member;
6478                strcat(string, "struct { ");
6479                for(member = type.members.first; member; member = member.next)
6480                {
6481                   PrintType(member, string, true, fullName);
6482                   strcat(string,"; ");
6483                }
6484                strcat(string,"}");
6485             }
6486             break;
6487          case unionType:
6488             if(type.enumName)
6489             {
6490                strcat(string, "union ");
6491                strcat(string, type.enumName);
6492             }
6493             else if(type.typeName)
6494                strcat(string, type.typeName);
6495             else
6496             {
6497                strcat(string, "union ");
6498                strcat(string,"(unnamed)");
6499             }
6500             break;
6501          case enumType:
6502             if(type.enumName)
6503             {
6504                strcat(string, "enum ");
6505                strcat(string, type.enumName);
6506             }
6507             else if(type.typeName)
6508                strcat(string, type.typeName);
6509             else
6510                strcat(string, "int"); // "enum");
6511             break;
6512          case ellipsisType:
6513             strcat(string, "...");
6514             break;
6515          case subClassType:
6516             strcat(string, "subclass(");
6517             strcat(string, type._class ? type._class.string : "int");
6518             strcat(string, ")");                  
6519             break;
6520          case templateType:
6521             strcat(string, type.templateParameter.identifier.string);
6522             break;
6523          case thisClassType:
6524             strcat(string, "thisclass");
6525             break;
6526          case vaListType:
6527             strcat(string, "__builtin_va_list");
6528             break;
6529       }
6530    }
6531 }
6532
6533 static void PrintName(Type type, char * string, bool fullName)
6534 {
6535    if(type.name && type.name[0])
6536    {
6537       if(fullName)
6538          strcat(string, type.name);
6539       else
6540       {
6541          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6542          if(name) name += 2; else name = type.name;
6543          strcat(string, name);
6544       }
6545    }
6546 }
6547
6548 static void PrintAttribs(Type type, char * string)
6549 {
6550    if(type)
6551    {
6552       if(type.dllExport)   strcat(string, "dllexport ");
6553       if(type.attrStdcall) strcat(string, "stdcall ");
6554    }
6555 }
6556
6557 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6558 {
6559    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6560    {
6561       Type attrType = null;
6562       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6563          PrintAttribs(type, string);
6564       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6565          strcat(string, " const");
6566       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6567       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6568          strcat(string, " (");
6569       if(type.kind == pointerType)
6570       {
6571          if(type.type.kind == functionType || type.type.kind == methodType)
6572             PrintAttribs(type.type, string);
6573       }
6574       if(type.kind == pointerType)
6575       {
6576          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6577             strcat(string, "*");
6578          else
6579             strcat(string, " *");
6580       }
6581       if(printConst && type.constant && type.kind == pointerType)
6582          strcat(string, " const");
6583    }
6584    else
6585       PrintTypeSpecs(type, string, fullName, printConst);
6586 }
6587
6588 static void PostPrintType(Type type, char * string, bool fullName)
6589 {
6590    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6591       strcat(string, ")");
6592    if(type.kind == arrayType)
6593       PrintArraySize(type, string);
6594    else if(type.kind == functionType)
6595    {
6596       Type param;
6597       strcat(string, "(");
6598       for(param = type.params.first; param; param = param.next)
6599       {
6600          PrintType(param, string, true, fullName);
6601          if(param.next) strcat(string, ", ");
6602       }
6603       strcat(string, ")");
6604    }
6605    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6606       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6607 }
6608
6609 // *****
6610 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6611 // *****
6612 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6613 {
6614    PrePrintType(type, string, fullName, null, printConst);
6615
6616    if(type.thisClass || (printName && type.name && type.name[0]))
6617       strcat(string, " ");
6618    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6619    {
6620       Symbol _class = type.thisClass;
6621       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6622       {
6623          if(type.classObjectType == classPointer)
6624             strcat(string, "class");
6625          else
6626             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6627       }
6628       else if(_class && _class.string)
6629       {
6630          String s = _class.string;
6631          if(fullName)
6632             strcat(string, s);
6633          else
6634          {
6635             char * name = RSearchString(s, "::", strlen(s), true, false);
6636             if(name) name += 2; else name = s;
6637             strcat(string, name);
6638          }
6639       }
6640       strcat(string, "::");
6641    }
6642
6643    if(printName && type.name)
6644       PrintName(type, string, fullName);
6645    PostPrintType(type, string, fullName);
6646    if(type.bitFieldCount)
6647    {
6648       char count[100];
6649       sprintf(count, ":%d", type.bitFieldCount);
6650       strcat(string, count);
6651    }
6652 }
6653
6654 void PrintType(Type type, char * string, bool printName, bool fullName)
6655 {
6656    _PrintType(type, string, printName, fullName, true);
6657 }
6658
6659 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6660 {
6661    _PrintType(type, string, printName, fullName, false);
6662 }
6663
6664 static Type FindMember(Type type, char * string)
6665 {
6666    Type memberType;
6667    for(memberType = type.members.first; memberType; memberType = memberType.next)
6668    {
6669       if(!memberType.name)
6670       {
6671          Type subType = FindMember(memberType, string);
6672          if(subType)
6673             return subType;
6674       }
6675       else if(!strcmp(memberType.name, string))
6676          return memberType;
6677    }
6678    return null;
6679 }
6680
6681 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6682 {
6683    Type memberType;
6684    for(memberType = type.members.first; memberType; memberType = memberType.next)
6685    {
6686       if(!memberType.name)
6687       {
6688          Type subType = FindMember(memberType, string);
6689          if(subType)
6690          {
6691             *offset += memberType.offset;
6692             return subType;
6693          }
6694       }
6695       else if(!strcmp(memberType.name, string))
6696       {
6697          *offset += memberType.offset;
6698          return memberType;
6699       }
6700    }
6701    return null;
6702 }
6703
6704 Expression ParseExpressionString(char * expression)
6705 {
6706    fileInput = TempFile { };
6707    fileInput.Write(expression, 1, strlen(expression));
6708    fileInput.Seek(0, start);
6709
6710    echoOn = false;
6711    parsedExpression = null;
6712    resetScanner();
6713    expression_yyparse();
6714    delete fileInput;
6715
6716    return parsedExpression;
6717 }
6718
6719 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6720 {
6721    Identifier id = exp.identifier;
6722    Method method = null;
6723    Property prop = null;
6724    DataMember member = null;
6725    ClassProperty classProp = null;
6726
6727    if(_class && _class.type == enumClass)
6728    {
6729       NamedLink value = null;
6730       Class enumClass = eSystem_FindClass(privateModule, "enum");
6731       if(enumClass)
6732       {
6733          Class baseClass;
6734          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6735          {
6736             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6737             for(value = e.values.first; value; value = value.next)
6738             {
6739                if(!strcmp(value.name, id.string))
6740                   break;
6741             }
6742             if(value)
6743             {
6744                char constant[256];
6745
6746                FreeExpContents(exp);
6747
6748                exp.type = constantExp;
6749                exp.isConstant = true;
6750                if(!strcmp(baseClass.dataTypeString, "int"))
6751                   sprintf(constant, "%d",(int)value.data);
6752                else
6753                   sprintf(constant, "0x%X",(int)value.data);
6754                exp.constant = CopyString(constant);
6755                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6756                exp.expType = MkClassType(baseClass.fullName);
6757                break;
6758             }
6759          }
6760       }
6761       if(value)
6762          return true;
6763    }
6764    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6765    {
6766       ProcessMethodType(method);
6767       exp.expType = Type
6768       {
6769          refCount = 1;
6770          kind = methodType;
6771          method = method;
6772          // Crash here?
6773          // TOCHECK: Put it back to what it was...
6774          // methodClass = _class;
6775          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6776       };
6777       //id._class = null;
6778       return true;
6779    }
6780    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6781    {
6782       if(!prop.dataType)
6783          ProcessPropertyType(prop);
6784       exp.expType = prop.dataType;
6785       if(prop.dataType) prop.dataType.refCount++;
6786       return true;
6787    }
6788    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6789    {
6790       if(!member.dataType)
6791          member.dataType = ProcessTypeString(member.dataTypeString, false);
6792       exp.expType = member.dataType;
6793       if(member.dataType) member.dataType.refCount++;
6794       return true;
6795    }
6796    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6797    {
6798       if(!classProp.dataType)
6799          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6800
6801       if(classProp.constant)
6802       {
6803          FreeExpContents(exp);
6804
6805          exp.isConstant = true;
6806          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6807          {
6808             //char constant[256];
6809             exp.type = stringExp;
6810             exp.constant = QMkString((char *)classProp.Get(_class));
6811          }
6812          else
6813          {
6814             char constant[256];
6815             exp.type = constantExp;
6816             sprintf(constant, "%d", (int)classProp.Get(_class));
6817             exp.constant = CopyString(constant);
6818          }
6819       }
6820       else
6821       {
6822          // TO IMPLEMENT...
6823       }
6824
6825       exp.expType = classProp.dataType;
6826       if(classProp.dataType) classProp.dataType.refCount++;
6827       return true;
6828    }
6829    return false;
6830 }
6831
6832 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6833 {
6834    BinaryTree * tree = &nameSpace.functions;
6835    GlobalData data = (GlobalData)tree->FindString(name);
6836    NameSpace * child;
6837    if(!data)
6838    {
6839       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6840       {
6841          data = ScanGlobalData(child, name);
6842          if(data)
6843             break;
6844       }
6845    }
6846    return data;
6847 }
6848
6849 static GlobalData FindGlobalData(char * name)
6850 {
6851    int start = 0, c;
6852    NameSpace * nameSpace;
6853    nameSpace = globalData;
6854    for(c = 0; name[c]; c++)
6855    {
6856       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6857       {
6858          NameSpace * newSpace;
6859          char * spaceName = new char[c - start + 1];
6860          strncpy(spaceName, name + start, c - start);
6861          spaceName[c-start] = '\0';
6862          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6863          delete spaceName;
6864          if(!newSpace)
6865             return null;
6866          nameSpace = newSpace;
6867          if(name[c] == ':') c++;
6868          start = c+1;
6869       }
6870    }
6871    if(c - start)
6872    {
6873       return ScanGlobalData(nameSpace, name + start);
6874    }
6875    return null;
6876 }
6877
6878 static int definedExpStackPos;
6879 static void * definedExpStack[512];
6880
6881 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6882 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6883 {
6884    Expression prev = checkedExp.prev, next = checkedExp.next;
6885
6886    FreeExpContents(checkedExp);
6887    FreeType(checkedExp.expType);
6888    FreeType(checkedExp.destType);
6889
6890    *checkedExp = *newExp;
6891
6892    delete newExp;
6893
6894    checkedExp.prev = prev;
6895    checkedExp.next = next;
6896 }
6897
6898 void ApplyAnyObjectLogic(Expression e)
6899 {
6900    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6901 #ifdef _DEBUG
6902    char debugExpString[4096];
6903    debugExpString[0] = '\0';
6904    PrintExpression(e, debugExpString);
6905 #endif
6906
6907    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6908    {
6909       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6910       //ellipsisDestType = destType;
6911       if(e && e.expType)
6912       {
6913          Type type = e.expType;
6914          Class _class = null;
6915          //Type destType = e.destType;
6916
6917          if(type.kind == classType && type._class && type._class.registered)
6918          {
6919             _class = type._class.registered;
6920          }
6921          else if(type.kind == subClassType)
6922          {
6923             _class = FindClass("ecere::com::Class").registered;
6924          }
6925          else
6926          {
6927             char string[1024] = "";
6928             Symbol classSym;
6929
6930             PrintTypeNoConst(type, string, false, true);
6931             classSym = FindClass(string);
6932             if(classSym) _class = classSym.registered;
6933          }
6934
6935          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "uintptr") && strcmp(_class.fullName, "intptr")) || // Patched so that class isn't considered SYSTEM...
6936             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != intPtrType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
6937             destType.byReference)))
6938          {
6939             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6940             {
6941                Expression checkedExp = e, newExp;
6942
6943                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6944                {
6945                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6946                   {
6947                      if(checkedExp.type == extensionCompoundExp)
6948                      {
6949                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6950                      }
6951                      else
6952                         checkedExp = checkedExp.list->last;
6953                   }
6954                   else if(checkedExp.type == castExp)
6955                      checkedExp = checkedExp.cast.exp;
6956                }
6957
6958                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6959                {
6960                   newExp = checkedExp.op.exp2;
6961                   checkedExp.op.exp2 = null;
6962                   FreeExpContents(checkedExp);
6963                   
6964                   if(e.expType && e.expType.passAsTemplate)
6965                   {
6966                      char size[100];
6967                      ComputeTypeSize(e.expType);
6968                      sprintf(size, "%d", e.expType.size);
6969                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
6970                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
6971                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
6972                   }
6973
6974                   ReplaceExpContents(checkedExp, newExp);
6975                   e.byReference = true;
6976                }
6977                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
6978                {
6979                   Expression checkedExp, newExp;
6980
6981                   {
6982                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
6983                      bool hasAddress =
6984                         e.type == identifierExp ||
6985                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
6986                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
6987                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
6988                         e.type == indexExp;
6989
6990                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
6991                      {
6992                         Context context = PushContext();
6993                         Declarator decl;
6994                         OldList * specs = MkList();
6995                         char typeString[1024];
6996                         Expression newExp { };
6997
6998                         typeString[0] = '\0';
6999                         *newExp = *e;
7000
7001                         //if(e.destType) e.destType.refCount++;
7002                         // if(exp.expType) exp.expType.refCount++;
7003                         newExp.prev = null;
7004                         newExp.next = null;
7005                         newExp.expType = null;
7006
7007                         PrintTypeNoConst(e.expType, typeString, false, true);
7008                         decl = SpecDeclFromString(typeString, specs, null);
7009                         newExp.destType = ProcessType(specs, decl);
7010
7011                         curContext = context;
7012
7013                         // We need a current compound for this
7014                         if(curCompound)
7015                         {
7016                            char name[100];
7017                            OldList * stmts = MkList();
7018                            e.type = extensionCompoundExp;
7019                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7020                            if(!curCompound.compound.declarations)
7021                               curCompound.compound.declarations = MkList();
7022                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7023                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7024                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7025                            e.compound = MkCompoundStmt(null, stmts);
7026                         }
7027                         else
7028                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7029
7030                         /*
7031                         e.compound = MkCompoundStmt(
7032                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7033                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))), 
7034
7035                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7036                         */
7037                         
7038                         {
7039                            Type type = e.destType;
7040                            e.destType = { };
7041                            CopyTypeInto(e.destType, type);
7042                            e.destType.refCount = 1;
7043                            e.destType.classObjectType = none;
7044                            FreeType(type);
7045                         }
7046
7047                         e.compound.compound.context = context;
7048                         PopContext(context);
7049                         curContext = context.parent;
7050                      }
7051                   }
7052
7053                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7054                   checkedExp = e;
7055                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7056                   {
7057                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7058                      {
7059                         if(checkedExp.type == extensionCompoundExp)
7060                         {
7061                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7062                         }
7063                         else
7064                            checkedExp = checkedExp.list->last;
7065                      }
7066                      else if(checkedExp.type == castExp)
7067                         checkedExp = checkedExp.cast.exp;
7068                   }
7069                   {
7070                      Expression operand { };
7071                      operand = *checkedExp;
7072                      checkedExp.destType = null;
7073                      checkedExp.expType = null;
7074                      checkedExp.Clear();
7075                      checkedExp.type = opExp;
7076                      checkedExp.op.op = '&';
7077                      checkedExp.op.exp1 = null;
7078                      checkedExp.op.exp2 = operand;
7079
7080                      //newExp = MkExpOp(null, '&', checkedExp);
7081                   }
7082                   //ReplaceExpContents(checkedExp, newExp);
7083                }
7084             }
7085          }
7086       }
7087    }
7088    {
7089       // If expression type is a simple class, make it an address
7090       // FixReference(e, true);
7091    }
7092 //#if 0
7093    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
7094       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7095          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7096    {
7097       if(e.expType.classObjectType && destType && destType.classObjectType) //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
7098       {
7099          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7100       }
7101       else
7102       {
7103          Expression thisExp { };
7104
7105          *thisExp = *e;
7106          thisExp.prev = null;
7107          thisExp.next = null;
7108          e.Clear();
7109
7110          e.type = bracketsExp;
7111          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7112          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7113             ((Expression)e.list->first).byReference = true;
7114
7115          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7116          {
7117             e.expType = thisExp.expType;
7118             e.expType.refCount++;
7119          }
7120          else*/
7121          {
7122             e.expType = { };
7123             CopyTypeInto(e.expType, thisExp.expType);
7124             e.expType.byReference = false;
7125             e.expType.refCount = 1;
7126
7127             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7128                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7129             {
7130                e.expType.classObjectType = none;
7131             }
7132          }
7133       }
7134    }
7135 // TOFIX: Try this for a nice IDE crash!
7136 //#endif
7137    // The other way around
7138    else 
7139 //#endif
7140    if(destType && e.expType && 
7141          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7142          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
7143          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7144    {
7145       if(destType.kind == ellipsisType)
7146       {
7147          Compiler_Error($"Unspecified type\n");
7148       }
7149       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7150       {
7151          bool byReference = e.expType.byReference;
7152          Expression thisExp { };
7153          Declarator decl;
7154          OldList * specs = MkList();
7155          char typeString[1024]; // Watch buffer overruns
7156          Type type;
7157          ClassObjectType backupClassObjectType;
7158          bool backupByReference;
7159
7160          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7161             type = e.expType;
7162          else
7163             type = destType;            
7164
7165          backupClassObjectType = type.classObjectType;
7166          backupByReference = type.byReference;
7167
7168          type.classObjectType = none;
7169          type.byReference = false;
7170
7171          typeString[0] = '\0';
7172          PrintType(type, typeString, false, true);
7173          decl = SpecDeclFromString(typeString, specs, null);
7174
7175          type.classObjectType = backupClassObjectType;
7176          type.byReference = backupByReference;
7177
7178          *thisExp = *e;
7179          thisExp.prev = null;
7180          thisExp.next = null;
7181          e.Clear();
7182
7183          if( ( type.kind == classType && type._class && type._class.registered && 
7184                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass || 
7185                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7186              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7187              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7188          {
7189             e.type = opExp;
7190             e.op.op = '*';
7191             e.op.exp1 = null;
7192             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7193
7194             e.expType = { };
7195             CopyTypeInto(e.expType, type);
7196             e.expType.byReference = false;
7197             e.expType.refCount = 1;
7198          }
7199          else
7200          {
7201             e.type = castExp;
7202             e.cast.typeName = MkTypeName(specs, decl);
7203             e.cast.exp = thisExp;
7204             e.byReference = true;
7205             e.expType = type;
7206             type.refCount++;
7207          }
7208          e.destType = destType;
7209          destType.refCount++;
7210       }
7211    }
7212 }
7213
7214 void ProcessExpressionType(Expression exp)
7215 {
7216    bool unresolved = false;
7217    Location oldyylloc = yylloc;
7218    bool notByReference = false;
7219 #ifdef _DEBUG   
7220    char debugExpString[4096];
7221    debugExpString[0] = '\0';
7222    PrintExpression(exp, debugExpString);
7223 #endif
7224    if(!exp || exp.expType) 
7225       return;
7226
7227    //eSystem_Logf("%s\n", expString);
7228    
7229    // Testing this here
7230    yylloc = exp.loc;
7231    switch(exp.type)
7232    {
7233       case identifierExp:
7234       {
7235          Identifier id = exp.identifier;
7236          if(!id) return;
7237
7238          // DOING THIS LATER NOW...
7239          if(id._class && id._class.name)
7240          {
7241             id.classSym = id._class.symbol; // FindClass(id._class.name);
7242             /* TODO: Name Space Fix ups
7243             if(!id.classSym)
7244                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7245             */
7246          }
7247
7248          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7249          {
7250             exp.expType = ProcessTypeString("Module", true);
7251             break;
7252          }
7253          else */if(strstr(id.string, "__ecereClass") == id.string)
7254          {
7255             exp.expType = ProcessTypeString("ecere::com::Class", true);
7256             break;
7257          }
7258          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7259          {
7260             // Added this here as well
7261             ReplaceClassMembers(exp, thisClass);
7262             if(exp.type != identifierExp)
7263             {
7264                ProcessExpressionType(exp);
7265                break;
7266             }
7267
7268             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7269                break;
7270          }
7271          else
7272          {
7273             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7274             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7275             if(!symbol/* && exp.destType*/)
7276             {
7277                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7278                   break;
7279                else
7280                {
7281                   if(thisClass)
7282                   {
7283                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7284                      if(exp.type != identifierExp)
7285                      {
7286                         ProcessExpressionType(exp);
7287                         break;
7288                      }
7289                   }
7290                   // Static methods called from inside the _class
7291                   else if(currentClass && !id._class)
7292                   {
7293                      if(ResolveIdWithClass(exp, currentClass, true))
7294                         break;
7295                   }
7296                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7297                }
7298             }
7299
7300             // If we manage to resolve this symbol
7301             if(symbol)
7302             {
7303                Type type = symbol.type;
7304                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7305
7306                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7307                {
7308                   Context context = SetupTemplatesContext(_class);
7309                   type = ReplaceThisClassType(_class);
7310                   FinishTemplatesContext(context);
7311                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7312                }
7313
7314                FreeSpecifier(id._class);
7315                id._class = null;
7316                delete id.string;
7317                id.string = CopyString(symbol.string);
7318
7319                id.classSym = null;
7320                exp.expType = type;
7321                if(type)
7322                   type.refCount++;
7323                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7324                   // Add missing cases here... enum Classes...
7325                   exp.isConstant = true;
7326
7327                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7328                if(symbol.isParam || !strcmp(id.string, "this"))
7329                {
7330                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7331                      exp.byReference = true;
7332                   
7333                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7334                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) && 
7335                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) || 
7336                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7337                   {
7338                      Identifier id = exp.identifier;
7339                      exp.type = bracketsExp;
7340                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7341                   }*/
7342                }
7343
7344                if(symbol.isIterator)
7345                {
7346                   if(symbol.isIterator == 3)
7347                   {
7348                      exp.type = bracketsExp;
7349                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7350                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7351                      exp.expType = null;
7352                      ProcessExpressionType(exp);                     
7353                   }
7354                   else if(symbol.isIterator != 4)
7355                   {
7356                      exp.type = memberExp;
7357                      exp.member.exp = MkExpIdentifier(exp.identifier);
7358                      exp.member.exp.expType = exp.expType;
7359                      /*if(symbol.isIterator == 6)
7360                         exp.member.member = MkIdentifier("key");
7361                      else*/
7362                         exp.member.member = MkIdentifier("data");
7363                      exp.expType = null;
7364                      ProcessExpressionType(exp);
7365                   }
7366                }
7367                break;
7368             }
7369             else
7370             {
7371                DefinedExpression definedExp = null;
7372                if(thisNameSpace && !(id._class && !id._class.name))
7373                {
7374                   char name[1024];
7375                   strcpy(name, thisNameSpace);
7376                   strcat(name, "::");
7377                   strcat(name, id.string);
7378                   definedExp = eSystem_FindDefine(privateModule, name);
7379                }
7380                if(!definedExp)
7381                   definedExp = eSystem_FindDefine(privateModule, id.string);
7382                if(definedExp)
7383                {
7384                   int c;
7385                   for(c = 0; c<definedExpStackPos; c++)
7386                      if(definedExpStack[c] == definedExp)
7387                         break;
7388                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7389                   {
7390                      Location backupYylloc = yylloc;
7391                      definedExpStack[definedExpStackPos++] = definedExp;
7392                      fileInput = TempFile { };
7393                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7394                      fileInput.Seek(0, start);
7395
7396                      echoOn = false;
7397                      parsedExpression = null;
7398                      resetScanner();
7399                      expression_yyparse();
7400                      delete fileInput;
7401
7402                      yylloc = backupYylloc;
7403
7404                      if(parsedExpression)
7405                      {
7406                         FreeIdentifier(id);
7407                         exp.type = bracketsExp;
7408                         exp.list = MkListOne(parsedExpression);
7409                         parsedExpression.loc = yylloc;
7410                         ProcessExpressionType(exp);
7411                         definedExpStackPos--;
7412                         return;
7413                      }
7414                      definedExpStackPos--;
7415                   }
7416                   else
7417                   {
7418                      if(inCompiler)
7419                      {
7420                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7421                      }
7422                   }
7423                }
7424                else
7425                {
7426                   GlobalData data = null;
7427                   if(thisNameSpace && !(id._class && !id._class.name))
7428                   {
7429                      char name[1024];
7430                      strcpy(name, thisNameSpace);
7431                      strcat(name, "::");
7432                      strcat(name, id.string);
7433                      data = FindGlobalData(name);
7434                   }
7435                   if(!data)
7436                      data = FindGlobalData(id.string);
7437                   if(data)
7438                   {
7439                      DeclareGlobalData(data);
7440                      exp.expType = data.dataType;
7441                      if(data.dataType) data.dataType.refCount++;
7442
7443                      delete id.string;
7444                      id.string = CopyString(data.fullName);
7445                      FreeSpecifier(id._class);
7446                      id._class = null;
7447
7448                      break;
7449                   }
7450                   else
7451                   {
7452                      GlobalFunction function = null;
7453                      if(thisNameSpace && !(id._class && !id._class.name))
7454                      {
7455                         char name[1024];
7456                         strcpy(name, thisNameSpace);
7457                         strcat(name, "::");
7458                         strcat(name, id.string);
7459                         function = eSystem_FindFunction(privateModule, name);
7460                      }
7461                      if(!function)
7462                         function = eSystem_FindFunction(privateModule, id.string);
7463                      if(function)
7464                      {
7465                         char name[1024];
7466                         delete id.string;
7467                         id.string = CopyString(function.name);
7468                         name[0] = 0;
7469
7470                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7471                            strcpy(name, "__ecereFunction_");
7472                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7473                         if(DeclareFunction(function, name))
7474                         {
7475                            delete id.string;
7476                            id.string = CopyString(name);
7477                         }
7478                         exp.expType = function.dataType;
7479                         if(function.dataType) function.dataType.refCount++;
7480
7481                         FreeSpecifier(id._class);
7482                         id._class = null;
7483
7484                         break;
7485                      }
7486                   }
7487                }
7488             }
7489          }
7490          unresolved = true;
7491          break;
7492       }
7493       case instanceExp:
7494       {
7495          Class _class;
7496          // Symbol classSym;
7497
7498          if(!exp.instance._class)
7499          {
7500             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7501             {
7502                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7503             }
7504          }
7505
7506          //classSym = FindClass(exp.instance._class.fullName);
7507          //_class = classSym ? classSym.registered : null;
7508
7509          ProcessInstantiationType(exp.instance);
7510          exp.isConstant = exp.instance.isConstant;
7511
7512          /*
7513          if(_class.type == unitClass && _class.base.type != systemClass)
7514          {
7515             {
7516                Type destType = exp.destType;
7517
7518                exp.destType = MkClassType(_class.base.fullName);
7519                exp.expType = MkClassType(_class.fullName);
7520                CheckExpressionType(exp, exp.destType, true);
7521
7522                exp.destType = destType;
7523             }
7524             exp.expType = MkClassType(_class.fullName);
7525          }
7526          else*/
7527          if(exp.instance._class)
7528          {
7529             exp.expType = MkClassType(exp.instance._class.name);
7530             /*if(exp.expType._class && exp.expType._class.registered && 
7531                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7532                exp.expType.byReference = true;*/
7533          }         
7534          break;
7535       }
7536       case constantExp:
7537       {
7538          if(!exp.expType)
7539          {
7540             Type type
7541             {
7542                refCount = 1;
7543                constant = true;
7544             };
7545             exp.expType = type;
7546
7547             if(exp.constant[0] == '\'')
7548             {
7549                if((int)((byte *)exp.constant)[1] > 127)
7550                {
7551                   int nb;
7552                   unichar ch = UTF8GetChar(exp.constant + 1, &nb);
7553                   if(nb < 2) ch = exp.constant[1];
7554                   delete exp.constant;
7555                   exp.constant = PrintUInt(ch);
7556                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7557                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7558                   type._class = FindClass("unichar");
7559
7560                   type.isSigned = false;
7561                }
7562                else
7563                {
7564                   type.kind = charType;
7565                   type.isSigned = true;
7566                }
7567             }
7568             else if(strchr(exp.constant, '.'))
7569             {
7570                char ch = exp.constant[strlen(exp.constant)-1];
7571                if(ch == 'f')
7572                   type.kind = floatType;
7573                else
7574                   type.kind = doubleType;
7575                type.isSigned = true;
7576             }
7577             else
7578             {
7579                if(exp.constant[0] == '0' && exp.constant[1])
7580                   type.isSigned = false;
7581                else if(strchr(exp.constant, 'L') || strchr(exp.constant, 'l'))
7582                   type.isSigned = false;
7583                else if(strtoll(exp.constant, null, 0) > MAXINT)
7584                   type.isSigned = false;
7585                else
7586                   type.isSigned = true;
7587                type.kind = intType;
7588             }
7589             exp.isConstant = true;
7590             if(exp.destType && exp.destType.kind == doubleType)
7591                type.kind = doubleType;
7592             else if(exp.destType && exp.destType.kind == floatType)
7593                type.kind = floatType;
7594             else if(exp.destType && exp.destType.kind == int64Type)
7595                type.kind = int64Type;
7596          }
7597          break;
7598       }
7599       case stringExp:
7600       {
7601          exp.isConstant = true;      // Why wasn't this constant?
7602          exp.expType = Type
7603          {
7604             refCount = 1;
7605             kind = pointerType;
7606             type = Type
7607             {
7608                refCount = 1;
7609                kind = charType;
7610                constant = true;
7611             }
7612          };
7613          break;
7614       }
7615       case newExp:
7616       case new0Exp:
7617          ProcessExpressionType(exp._new.size);
7618          exp.expType = Type
7619          {
7620             refCount = 1;
7621             kind = pointerType;
7622             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7623          };
7624          DeclareType(exp.expType.type, false, false);
7625          break;
7626       case renewExp:
7627       case renew0Exp:
7628          ProcessExpressionType(exp._renew.size);
7629          ProcessExpressionType(exp._renew.exp);
7630          exp.expType = Type
7631          {
7632             refCount = 1;
7633             kind = pointerType;
7634             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7635          };
7636          DeclareType(exp.expType.type, false, false);
7637          break;
7638       case opExp:
7639       {
7640          bool assign = false, boolResult = false, boolOps = false;
7641          Type type1 = null, type2 = null;
7642          bool useDestType = false, useSideType = false;
7643          Location oldyylloc = yylloc;
7644          bool useSideUnit = false;
7645
7646          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7647          Type dummy
7648          {
7649             count = 1;
7650             refCount = 1;
7651          };
7652
7653          switch(exp.op.op)
7654          {
7655             // Assignment Operators
7656             case '=': 
7657             case MUL_ASSIGN:
7658             case DIV_ASSIGN:
7659             case MOD_ASSIGN:
7660             case ADD_ASSIGN:
7661             case SUB_ASSIGN:
7662             case LEFT_ASSIGN:
7663             case RIGHT_ASSIGN:
7664             case AND_ASSIGN:
7665             case XOR_ASSIGN:
7666             case OR_ASSIGN:
7667                assign = true;
7668                break;
7669             // boolean Operators
7670             case '!':
7671                // Expect boolean operators
7672                //boolOps = true;
7673                //boolResult = true;
7674                break;
7675             case AND_OP:
7676             case OR_OP:
7677                // Expect boolean operands
7678                boolOps = true;
7679                boolResult = true;
7680                break;
7681             // Comparisons
7682             case EQ_OP:
7683             case '<':
7684             case '>':
7685             case LE_OP:
7686             case GE_OP:
7687             case NE_OP:
7688                // Gives boolean result
7689                boolResult = true;
7690                useSideType = true;
7691                break;
7692             case '+':
7693             case '-':
7694                useSideUnit = true;
7695
7696                // Just added these... testing
7697             case '|':
7698             case '&':
7699             case '^':
7700
7701             // DANGER: Verify units
7702             case '/':
7703             case '%':
7704             case '*':
7705                
7706                if(exp.op.op != '*' || exp.op.exp1)
7707                {
7708                   useSideType = true;
7709                   useDestType = true;
7710                }
7711                break;
7712
7713             /*// Implement speed etc.
7714             case '*':
7715             case '/':
7716                break;
7717             */
7718          }
7719          if(exp.op.op == '&')
7720          {
7721             // Added this here earlier for Iterator address as key
7722             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7723             {
7724                Identifier id = exp.op.exp2.identifier;
7725                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7726                if(symbol && symbol.isIterator == 2)
7727                {
7728                   exp.type = memberExp;
7729                   exp.member.exp = exp.op.exp2;
7730                   exp.member.member = MkIdentifier("key");
7731                   exp.expType = null;
7732                   exp.op.exp2.expType = symbol.type;
7733                   symbol.type.refCount++;
7734                   ProcessExpressionType(exp);
7735                   FreeType(dummy);
7736                   break;
7737                }
7738                // exp.op.exp2.usage.usageRef = true;
7739             }
7740          }
7741
7742          //dummy.kind = TypeDummy;
7743
7744          if(exp.op.exp1)
7745          {
7746             if(exp.destType && exp.destType.kind == classType &&
7747                exp.destType._class && exp.destType._class.registered && useDestType &&
7748                
7749               ((exp.destType._class.registered.type == unitClass && useSideUnit) || 
7750                exp.destType._class.registered.type == enumClass ||
7751                exp.destType._class.registered.type == bitClass
7752                )) 
7753
7754               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7755             {
7756                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7757                exp.op.exp1.destType = exp.destType;
7758                if(exp.destType)
7759                   exp.destType.refCount++;
7760             }
7761             else if(!assign)
7762             {
7763                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7764                exp.op.exp1.destType = dummy;
7765                dummy.refCount++;               
7766             }
7767
7768             // TESTING THIS HERE...
7769             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7770             ProcessExpressionType(exp.op.exp1);
7771             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7772
7773             if(exp.op.exp1.destType == dummy)
7774             {
7775                FreeType(dummy);
7776                exp.op.exp1.destType = null;
7777             }
7778             type1 = exp.op.exp1.expType;
7779          }
7780
7781          if(exp.op.exp2)
7782          {
7783             char expString[10240];
7784             expString[0] = '\0';
7785             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7786             {
7787                if(exp.op.exp1)
7788                {
7789                   exp.op.exp2.destType = exp.op.exp1.expType;
7790                   if(exp.op.exp1.expType)
7791                      exp.op.exp1.expType.refCount++;
7792                }
7793                else
7794                {
7795                   exp.op.exp2.destType = exp.destType;
7796                   if(exp.destType)
7797                      exp.destType.refCount++;
7798                }
7799
7800                if(type1) type1.refCount++;
7801                exp.expType = type1;
7802             }
7803             else if(assign)
7804             {
7805                if(inCompiler)
7806                   PrintExpression(exp.op.exp2, expString);
7807
7808                if(type1 && type1.kind == pointerType)
7809                {
7810                   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 ||
7811                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7812                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7813                   else if(exp.op.op == '=')
7814                   {
7815                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7816                      exp.op.exp2.destType = type1;
7817                      if(type1)
7818                         type1.refCount++;
7819                   }
7820                }
7821                else
7822                {
7823                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;) 
7824                   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/* ||
7825                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7826                   else
7827                   {
7828                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7829                      exp.op.exp2.destType = type1;
7830                      if(type1)
7831                         type1.refCount++;
7832                   }
7833                }
7834                if(type1) type1.refCount++;
7835                exp.expType = type1;
7836             }
7837             else if(exp.destType && exp.destType.kind == classType &&
7838                exp.destType._class && exp.destType._class.registered && 
7839                
7840                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) || 
7841                   (exp.destType._class.registered.type == enumClass && useDestType)) 
7842                   )
7843             {
7844                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7845                exp.op.exp2.destType = exp.destType;
7846                if(exp.destType)
7847                   exp.destType.refCount++;
7848             }
7849             else
7850             {
7851                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7852                exp.op.exp2.destType = dummy;
7853                dummy.refCount++;
7854             }
7855
7856             // TESTING THIS HERE... (DANGEROUS)
7857             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered && 
7858                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7859             {
7860                FreeType(exp.op.exp2.destType);
7861                exp.op.exp2.destType = type1;
7862                type1.refCount++;
7863             }
7864             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7865             ProcessExpressionType(exp.op.exp2);
7866             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7867
7868             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7869             {
7870                if(exp.op.exp2.expType.kind == intSizeType || exp.op.exp2.expType.kind == intPtrType || exp.op.exp2.expType.kind == int64Type || exp.op.exp2.expType.kind == intType || exp.op.exp2.expType.kind == shortType || exp.op.exp2.expType.kind == charType)
7871                {
7872                   if(exp.op.op != '=' && type1.type.kind == voidType) 
7873                      Compiler_Error($"void *: unknown size\n");
7874                }
7875                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|| 
7876                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7877                               (exp.op.exp2.expType._class.registered.type == normalClass || 
7878                               exp.op.exp2.expType._class.registered.type == structClass ||
7879                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7880                {
7881                   if(exp.op.op == ADD_ASSIGN)
7882                      Compiler_Error($"cannot add two pointers\n");                   
7883                }
7884                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType && 
7885                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7886                {
7887                   if(exp.op.op == ADD_ASSIGN)
7888                      Compiler_Error($"cannot add two pointers\n");                   
7889                }
7890                else if(inCompiler)
7891                {
7892                   char type1String[1024];
7893                   char type2String[1024];
7894                   type1String[0] = '\0';
7895                   type2String[0] = '\0';
7896                   
7897                   PrintType(exp.op.exp2.expType, type1String, false, true);
7898                   PrintType(type1, type2String, false, true);
7899                   ChangeCh(expString, '\n', ' ');
7900                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7901                }
7902             }
7903
7904             if(exp.op.exp2.destType == dummy)
7905             {
7906                FreeType(dummy);
7907                exp.op.exp2.destType = null;
7908             }
7909
7910             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
7911             {
7912                type2 = { };
7913                type2.refCount = 1;
7914                CopyTypeInto(type2, exp.op.exp2.expType);
7915                type2.isSigned = true;
7916             }
7917             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
7918             {
7919                type2 = { kind = intType };
7920                type2.refCount = 1;
7921                type2.isSigned = true;
7922             }
7923             else
7924                type2 = exp.op.exp2.expType;
7925          }
7926
7927          dummy.kind = voidType;
7928
7929          if(exp.op.op == SIZEOF)
7930          {
7931             exp.expType = Type
7932             {
7933                refCount = 1;
7934                kind = intType;
7935             };
7936             exp.isConstant = true;
7937          }
7938          // Get type of dereferenced pointer
7939          else if(exp.op.op == '*' && !exp.op.exp1)
7940          {
7941             exp.expType = Dereference(type2);
7942             if(type2 && type2.kind == classType)
7943                notByReference = true;
7944          }
7945          else if(exp.op.op == '&' && !exp.op.exp1)
7946             exp.expType = Reference(type2);
7947          else if(!assign)
7948          {
7949             if(boolOps)
7950             {
7951                if(exp.op.exp1) 
7952                {
7953                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7954                   exp.op.exp1.destType = MkClassType("bool");
7955                   exp.op.exp1.destType.truth = true;
7956                   if(!exp.op.exp1.expType)
7957                      ProcessExpressionType(exp.op.exp1);
7958                   else
7959                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
7960                   FreeType(exp.op.exp1.expType);
7961                   exp.op.exp1.expType = MkClassType("bool");
7962                   exp.op.exp1.expType.truth = true;
7963                }
7964                if(exp.op.exp2) 
7965                {
7966                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7967                   exp.op.exp2.destType = MkClassType("bool");
7968                   exp.op.exp2.destType.truth = true;
7969                   if(!exp.op.exp2.expType)
7970                      ProcessExpressionType(exp.op.exp2);
7971                   else
7972                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
7973                   FreeType(exp.op.exp2.expType);
7974                   exp.op.exp2.expType = MkClassType("bool");
7975                   exp.op.exp2.expType.truth = true;
7976                }
7977             }
7978             else if(exp.op.exp1 && exp.op.exp2 && 
7979                ((useSideType /*&& 
7980                      (useSideUnit || 
7981                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
7982                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
7983                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) && 
7984                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
7985             {
7986                if(type1 && type2 &&
7987                   // If either both are class or both are not class
7988                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
7989                {
7990                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7991                   exp.op.exp2.destType = type1;
7992                   type1.refCount++;
7993                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7994                   exp.op.exp1.destType = type2;
7995                   type2.refCount++;
7996                   // Warning here for adding Radians + Degrees with no destination type
7997                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) && 
7998                      type1._class.registered && type1._class.registered.type == unitClass && 
7999                      type2._class.registered && type2._class.registered.type == unitClass && 
8000                      type1._class.registered != type2._class.registered)
8001                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8002                         type1._class.string, type2._class.string, type1._class.string);
8003
8004                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8005                   {
8006                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8007                      if(argExp)
8008                      {
8009                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8010
8011                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8012                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), 
8013                            exp.op.exp1)));
8014
8015                         ProcessExpressionType(exp.op.exp1);
8016
8017                         if(type2.kind != pointerType)
8018                         {
8019                            ProcessExpressionType(classExp);
8020
8021                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', 
8022                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8023                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8024                                  // noHeadClass
8025                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8026                                     OR_OP, 
8027                                  // normalClass
8028                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8029                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8030                                        MkPointer(null, null), null)))),                                  
8031                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8032
8033                            if(!exp.op.exp2.expType)
8034                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8035
8036                            ProcessExpressionType(exp.op.exp2);
8037                         }
8038                      }
8039                   }
8040                   
8041                   if(!boolResult && ((type1.kind == pointerType || type1.kind == arrayType || (type1.kind == classType && !strcmp(type1._class.string, "String"))) && (type2.kind == intSizeType || type2.kind == intPtrType || type2.kind == int64Type || type2.kind == intType || type2.kind == shortType || type2.kind == charType)))
8042                   {
8043                      if(type1.kind != classType && type1.type.kind == voidType) 
8044                         Compiler_Error($"void *: unknown size\n");
8045                      exp.expType = type1;
8046                      if(type1) type1.refCount++;
8047                   }
8048                   else if(!boolResult && ((type2.kind == pointerType || type2.kind == arrayType || (type2.kind == classType && !strcmp(type2._class.string, "String"))) && (type1.kind == intSizeType || type1.kind == intPtrType || type1.kind == int64Type || type1.kind == intType || type1.kind == shortType || type1.kind == charType)))
8049                   {
8050                      if(type2.kind != classType && type2.type.kind == voidType) 
8051                         Compiler_Error($"void *: unknown size\n");
8052                      exp.expType = type2;
8053                      if(type2) type2.refCount++;
8054                   }
8055                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8056                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8057                   {
8058                      Compiler_Warning($"different levels of indirection\n");
8059                   }
8060                   else 
8061                   {
8062                      bool success = false;
8063                      if(type1.kind == pointerType && type2.kind == pointerType)
8064                      {
8065                         if(exp.op.op == '+')
8066                            Compiler_Error($"cannot add two pointers\n");
8067                         else if(exp.op.op == '-')
8068                         {
8069                            // Pointer Subtraction gives integer
8070                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8071                            {
8072                               exp.expType = Type
8073                               {
8074                                  kind = intType;
8075                                  refCount = 1;
8076                               };
8077                               success = true;
8078
8079                               if(type1.type.kind == templateType)
8080                               {
8081                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8082                                  if(argExp)
8083                                  {
8084                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8085
8086                                     ProcessExpressionType(classExp);
8087
8088                                     exp.type = bracketsExp;
8089                                     exp.list = MkListOne(MkExpOp(
8090                                        MkExpBrackets(MkListOne(MkExpOp(
8091                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8092                                              , exp.op.op, 
8093                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/', 
8094                                           
8095                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8096
8097                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8098                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8099                                                 // noHeadClass
8100                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8101                                                    OR_OP, 
8102                                                 // normalClass
8103                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8104                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8105                                                       MkPointer(null, null), null)))),                                  
8106                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8107
8108                                              
8109                                              ));
8110                                     
8111                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8112                                     FreeType(dummy);
8113                                     return;                                       
8114                                  }
8115                               }
8116                            }
8117                         }
8118                      }
8119
8120                      if(!success && exp.op.exp1.type == constantExp)
8121                      {
8122                         // If first expression is constant, try to match that first
8123                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8124                         {
8125                            if(exp.expType) FreeType(exp.expType);
8126                            exp.expType = exp.op.exp1.destType;
8127                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8128                            success = true;
8129                         }
8130                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8131                         {
8132                            if(exp.expType) FreeType(exp.expType);
8133                            exp.expType = exp.op.exp2.destType;
8134                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8135                            success = true;
8136                         }
8137                      }
8138                      else if(!success)
8139                      {
8140                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8141                         {
8142                            if(exp.expType) FreeType(exp.expType);
8143                            exp.expType = exp.op.exp2.destType;
8144                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8145                            success = true;
8146                         }
8147                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8148                         {
8149                            if(exp.expType) FreeType(exp.expType);
8150                            exp.expType = exp.op.exp1.destType;
8151                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8152                            success = true;
8153                         }
8154                      }
8155                      if(!success)
8156                      {
8157                         char expString1[10240];
8158                         char expString2[10240];
8159                         char type1[1024];
8160                         char type2[1024];
8161                         expString1[0] = '\0';
8162                         expString2[0] = '\0';
8163                         type1[0] = '\0';
8164                         type2[0] = '\0';
8165                         if(inCompiler)
8166                         {
8167                            PrintExpression(exp.op.exp1, expString1);
8168                            ChangeCh(expString1, '\n', ' ');
8169                            PrintExpression(exp.op.exp2, expString2);
8170                            ChangeCh(expString2, '\n', ' ');
8171                            PrintType(exp.op.exp1.expType, type1, false, true);
8172                            PrintType(exp.op.exp2.expType, type2, false, true);
8173                         }
8174
8175                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8176                      }
8177                   }
8178                }
8179                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8180                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8181                {
8182                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8183                   // Convert e.g. / 4 into / 4.0
8184                   exp.op.exp1.destType = type2._class.registered.dataType;
8185                   if(type2._class.registered.dataType)
8186                      type2._class.registered.dataType.refCount++;
8187                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8188                   exp.expType = type2;
8189                   if(type2) type2.refCount++;
8190                }
8191                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8192                {
8193                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8194                   // Convert e.g. / 4 into / 4.0
8195                   exp.op.exp2.destType = type1._class.registered.dataType;
8196                   if(type1._class.registered.dataType)
8197                      type1._class.registered.dataType.refCount++;
8198                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8199                   exp.expType = type1;
8200                   if(type1) type1.refCount++;
8201                }
8202                else if(type1)
8203                {
8204                   bool valid = false;
8205
8206                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8207                   {
8208                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8209
8210                      if(!type1._class.registered.dataType)
8211                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8212                      exp.op.exp2.destType = type1._class.registered.dataType;
8213                      exp.op.exp2.destType.refCount++;
8214
8215                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8216                      type2 = exp.op.exp2.destType;
8217
8218                      exp.expType = type2;
8219                      type2.refCount++;
8220                   }
8221                   
8222                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8223                   {
8224                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8225
8226                      if(!type2._class.registered.dataType)
8227                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8228                      exp.op.exp1.destType = type2._class.registered.dataType;
8229                      exp.op.exp1.destType.refCount++;
8230
8231                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8232                      type1 = exp.op.exp1.destType;
8233                      exp.expType = type1;
8234                      type1.refCount++;
8235                   }
8236
8237                   // TESTING THIS NEW CODE
8238                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8239                   {
8240                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8241                      {
8242                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8243                         {
8244                            if(exp.expType) FreeType(exp.expType);
8245                            exp.expType = exp.op.exp1.expType;
8246                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8247                            valid = true;
8248                         }
8249                      }
8250
8251                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8252                      {
8253                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8254                         {
8255                            if(exp.expType) FreeType(exp.expType);
8256                            exp.expType = exp.op.exp2.expType;
8257                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8258                            valid = true;
8259                         }
8260                      }
8261                   }
8262
8263                   if(!valid)
8264                   {
8265                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8266                      exp.op.exp2.destType = type1;
8267                      type1.refCount++;
8268
8269                      /*
8270                      // Maybe this was meant to be an enum...
8271                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8272                      {
8273                         Type oldType = exp.op.exp2.expType;
8274                         exp.op.exp2.expType = null;
8275                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8276                            FreeType(oldType);
8277                         else
8278                            exp.op.exp2.expType = oldType;
8279                      }
8280                      */
8281
8282                      /*
8283                      // TESTING THIS HERE... LATEST ADDITION
8284                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8285                      {
8286                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8287                         exp.op.exp2.destType = type2._class.registered.dataType;
8288                         if(type2._class.registered.dataType)
8289                            type2._class.registered.dataType.refCount++;
8290                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8291                         
8292                         //exp.expType = type2._class.registered.dataType; //type2;
8293                         //if(type2) type2.refCount++;
8294                      }
8295
8296                      // TESTING THIS HERE... LATEST ADDITION
8297                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8298                      {
8299                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8300                         exp.op.exp1.destType = type1._class.registered.dataType;
8301                         if(type1._class.registered.dataType)
8302                            type1._class.registered.dataType.refCount++;
8303                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8304                         exp.expType = type1._class.registered.dataType; //type1;
8305                         if(type1) type1.refCount++;
8306                      }
8307                      */
8308
8309                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8310                      {
8311                         if(exp.expType) FreeType(exp.expType);
8312                         exp.expType = exp.op.exp2.destType;
8313                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8314                      }
8315                      else if(type1 && type2)
8316                      {
8317                         char expString1[10240];
8318                         char expString2[10240];
8319                         char type1String[1024];
8320                         char type2String[1024];
8321                         expString1[0] = '\0';
8322                         expString2[0] = '\0';
8323                         type1String[0] = '\0';
8324                         type2String[0] = '\0';
8325                         if(inCompiler)
8326                         {
8327                            PrintExpression(exp.op.exp1, expString1);
8328                            ChangeCh(expString1, '\n', ' ');
8329                            PrintExpression(exp.op.exp2, expString2);
8330                            ChangeCh(expString2, '\n', ' ');
8331                            PrintType(exp.op.exp1.expType, type1String, false, true);
8332                            PrintType(exp.op.exp2.expType, type2String, false, true);
8333                         }
8334
8335                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8336
8337                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8338                         {
8339                            exp.expType = exp.op.exp1.expType;
8340                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8341                         }
8342                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8343                         {
8344                            exp.expType = exp.op.exp2.expType;
8345                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8346                         }
8347                      }
8348                   }
8349                }
8350                else if(type2)
8351                {
8352                   // Maybe this was meant to be an enum...
8353                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8354                   {
8355                      Type oldType = exp.op.exp1.expType;
8356                      exp.op.exp1.expType = null;
8357                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8358                         FreeType(oldType);
8359                      else
8360                         exp.op.exp1.expType = oldType;
8361                   }
8362
8363                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8364                   exp.op.exp1.destType = type2;
8365                   type2.refCount++;
8366                   /*
8367                   // TESTING THIS HERE... LATEST ADDITION
8368                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8369                   {
8370                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8371                      exp.op.exp1.destType = type1._class.registered.dataType;
8372                      if(type1._class.registered.dataType)
8373                         type1._class.registered.dataType.refCount++;
8374                   }
8375
8376                   // TESTING THIS HERE... LATEST ADDITION
8377                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8378                   {
8379                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8380                      exp.op.exp2.destType = type2._class.registered.dataType;
8381                      if(type2._class.registered.dataType)
8382                         type2._class.registered.dataType.refCount++;
8383                   }
8384                   */
8385
8386                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8387                   {
8388                      if(exp.expType) FreeType(exp.expType);
8389                      exp.expType = exp.op.exp1.destType;
8390                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8391                   }
8392                }
8393             }
8394             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8395             {
8396                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8397                {
8398                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8399                   // Convert e.g. / 4 into / 4.0
8400                   exp.op.exp1.destType = type2._class.registered.dataType;
8401                   if(type2._class.registered.dataType)
8402                      type2._class.registered.dataType.refCount++;
8403                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8404                }
8405                if(exp.op.op == '!')
8406                {
8407                   exp.expType = MkClassType("bool");
8408                   exp.expType.truth = true;
8409                }
8410                else
8411                {
8412                   exp.expType = type2;
8413                   if(type2) type2.refCount++;
8414                }
8415             }
8416             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8417             {
8418                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8419                {
8420                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8421                   // Convert e.g. / 4 into / 4.0
8422                   exp.op.exp2.destType = type1._class.registered.dataType;
8423                   if(type1._class.registered.dataType)
8424                      type1._class.registered.dataType.refCount++;
8425                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8426                }
8427                exp.expType = type1;
8428                if(type1) type1.refCount++;
8429             }
8430          }
8431          
8432          yylloc = exp.loc;
8433          if(exp.op.exp1 && !exp.op.exp1.expType)
8434          {
8435             char expString[10000];
8436             expString[0] = '\0';
8437             if(inCompiler)
8438             {
8439                PrintExpression(exp.op.exp1, expString);
8440                ChangeCh(expString, '\n', ' ');
8441             }
8442             if(expString[0])
8443                Compiler_Error($"couldn't determine type of %s\n", expString);
8444          }
8445          if(exp.op.exp2 && !exp.op.exp2.expType)
8446          {
8447             char expString[10240];
8448             expString[0] = '\0';
8449             if(inCompiler)
8450             {
8451                PrintExpression(exp.op.exp2, expString);
8452                ChangeCh(expString, '\n', ' ');
8453             }
8454             if(expString[0])
8455                Compiler_Error($"couldn't determine type of %s\n", expString);
8456          }
8457
8458          if(boolResult)
8459          {
8460             FreeType(exp.expType);
8461             exp.expType = MkClassType("bool");
8462             exp.expType.truth = true;
8463          }
8464
8465          if(exp.op.op != SIZEOF)
8466             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8467                (!exp.op.exp2 || exp.op.exp2.isConstant);
8468
8469          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8470          {
8471             DeclareType(exp.op.exp2.expType, false, false);
8472          }
8473
8474          yylloc = oldyylloc;
8475
8476          FreeType(dummy);
8477          break;
8478       }
8479       case bracketsExp:
8480       case extensionExpressionExp:
8481       {
8482          Expression e;
8483          exp.isConstant = true;
8484          for(e = exp.list->first; e; e = e.next)
8485          {
8486             bool inced = false;
8487             if(!e.next)
8488             {
8489                FreeType(e.destType);
8490                e.destType = exp.destType;
8491                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8492             }
8493             ProcessExpressionType(e);
8494             if(inced)
8495                exp.destType.count--;
8496             if(!exp.expType && !e.next)
8497             {
8498                exp.expType = e.expType;
8499                if(e.expType) e.expType.refCount++;
8500             }
8501             if(!e.isConstant)
8502                exp.isConstant = false;
8503          }
8504
8505          // In case a cast became a member...
8506          e = exp.list->first;
8507          if(!e.next && e.type == memberExp)
8508          {
8509             // Preserve prev, next
8510             Expression next = exp.next, prev = exp.prev;
8511
8512
8513             FreeType(exp.expType);
8514             FreeType(exp.destType);
8515             delete exp.list;
8516             
8517             *exp = *e;
8518
8519             exp.prev = prev;
8520             exp.next = next;
8521
8522             delete e;
8523
8524             ProcessExpressionType(exp);
8525          }
8526          break;
8527       }
8528       case indexExp:
8529       {
8530          Expression e;
8531          exp.isConstant = true;
8532
8533          ProcessExpressionType(exp.index.exp);
8534          if(!exp.index.exp.isConstant)
8535             exp.isConstant = false;
8536
8537          if(exp.index.exp.expType)
8538          {
8539             Type source = exp.index.exp.expType;
8540             if(source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
8541                eClass_IsDerived(source._class.registered, containerClass) && 
8542                source._class.registered.templateArgs)
8543             {
8544                Class _class = source._class.registered;
8545                exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8546
8547                if(exp.index.index && exp.index.index->last)
8548                {
8549                   ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8550                }
8551             }
8552          }
8553
8554          for(e = exp.index.index->first; e; e = e.next)
8555          {
8556             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8557             {
8558                if(e.destType) FreeType(e.destType);
8559                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8560             }
8561             ProcessExpressionType(e);
8562             if(!e.next)
8563             {
8564                // Check if this type is int
8565             }
8566             if(!e.isConstant)
8567                exp.isConstant = false;
8568          }
8569
8570          if(!exp.expType)
8571             exp.expType = Dereference(exp.index.exp.expType);
8572          if(exp.expType)
8573             DeclareType(exp.expType, false, false);
8574          break;
8575       }
8576       case callExp:
8577       {
8578          Expression e;
8579          Type functionType;
8580          Type methodType = null;
8581          char name[1024];
8582          name[0] = '\0';
8583
8584          if(inCompiler)
8585          {
8586             PrintExpression(exp.call.exp,  name);
8587             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8588             {
8589                //exp.call.exp.expType = null;
8590                PrintExpression(exp.call.exp,  name);
8591             }
8592          }
8593          if(exp.call.exp.type == identifierExp)
8594          {
8595             Expression idExp = exp.call.exp;
8596             Identifier id = idExp.identifier;
8597             if(!strcmp(id.string, "__builtin_frame_address"))
8598             {
8599                exp.expType = ProcessTypeString("void *", true);
8600                if(exp.call.arguments && exp.call.arguments->first)
8601                   ProcessExpressionType(exp.call.arguments->first);
8602                break;
8603             }
8604             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8605             {
8606                exp.expType = ProcessTypeString("int", true);
8607                if(exp.call.arguments && exp.call.arguments->first)
8608                   ProcessExpressionType(exp.call.arguments->first);
8609                break;
8610             }
8611             else if(!strcmp(id.string, "Max") ||
8612                !strcmp(id.string, "Min") ||
8613                !strcmp(id.string, "Sgn") ||
8614                !strcmp(id.string, "Abs"))
8615             {
8616                Expression a = null;
8617                Expression b = null;
8618                Expression tempExp1 = null, tempExp2 = null;
8619                if((!strcmp(id.string, "Max") ||
8620                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8621                {
8622                   a = exp.call.arguments->first;
8623                   b = exp.call.arguments->last;
8624                   tempExp1 = a;
8625                   tempExp2 = b;
8626                }
8627                else if(exp.call.arguments->count == 1)
8628                {
8629                   a = exp.call.arguments->first;
8630                   tempExp1 = a;
8631                }
8632                
8633                if(a)
8634                {
8635                   exp.call.arguments->Clear();
8636                   idExp.identifier = null;
8637
8638                   FreeExpContents(exp);
8639
8640                   ProcessExpressionType(a);
8641                   if(b)
8642                      ProcessExpressionType(b);
8643
8644                   exp.type = bracketsExp;
8645                   exp.list = MkList();
8646
8647                   if(a.expType && (!b || b.expType))
8648                   {
8649                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8650                      {
8651                         // Use the simpleStruct name/ids for now...
8652                         if(inCompiler)
8653                         {
8654                            OldList * specs = MkList();
8655                            OldList * decls = MkList();
8656                            Declaration decl;
8657                            char temp1[1024], temp2[1024];
8658
8659                            GetTypeSpecs(a.expType, specs);
8660
8661                            if(a && !a.isConstant && a.type != identifierExp)
8662                            {
8663                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8664                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8665                               tempExp1 = QMkExpId(temp1);
8666                               tempExp1.expType = a.expType;
8667                               if(a.expType)
8668                                  a.expType.refCount++;
8669                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8670                            }
8671                            if(b && !b.isConstant && b.type != identifierExp)
8672                            {
8673                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8674                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8675                               tempExp2 = QMkExpId(temp2);
8676                               tempExp2.expType = b.expType;
8677                               if(b.expType)
8678                                  b.expType.refCount++;
8679                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8680                            }                        
8681
8682                            decl = MkDeclaration(specs, decls);
8683                            if(!curCompound.compound.declarations)
8684                               curCompound.compound.declarations = MkList();
8685                            curCompound.compound.declarations->Insert(null, decl);
8686                         }
8687                      }
8688                   }
8689
8690                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8691                   {
8692                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8693                      ListAdd(exp.list, 
8694                         MkExpCondition(MkExpBrackets(MkListOne(
8695                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8696                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8697                      exp.expType = a.expType;
8698                      if(a.expType)
8699                         a.expType.refCount++;
8700                   }
8701                   else if(!strcmp(id.string, "Abs"))
8702                   {
8703                      ListAdd(exp.list, 
8704                         MkExpCondition(MkExpBrackets(MkListOne(
8705                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8706                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8707                      exp.expType = a.expType;
8708                      if(a.expType)
8709                         a.expType.refCount++;
8710                   }
8711                   else if(!strcmp(id.string, "Sgn"))
8712                   {
8713                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8714                      ListAdd(exp.list, 
8715                         MkExpCondition(MkExpBrackets(MkListOne(
8716                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8717                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8718                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8719                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8720                      exp.expType = ProcessTypeString("int", false);
8721                   }
8722
8723                   FreeExpression(tempExp1);
8724                   if(tempExp2) FreeExpression(tempExp2);
8725
8726                   FreeIdentifier(id);
8727                   break;
8728                }
8729             }
8730          }
8731
8732          {
8733             Type dummy
8734             {
8735                count = 1;
8736                refCount = 1;
8737             };
8738             if(!exp.call.exp.destType)
8739             {
8740                exp.call.exp.destType = dummy;
8741                dummy.refCount++;
8742             }
8743             ProcessExpressionType(exp.call.exp);
8744             if(exp.call.exp.destType == dummy)
8745             {
8746                FreeType(dummy);
8747                exp.call.exp.destType = null;
8748             }
8749             FreeType(dummy);
8750          }
8751
8752          // Check argument types against parameter types
8753          functionType = exp.call.exp.expType;
8754
8755          if(functionType && functionType.kind == TypeKind::methodType)
8756          {
8757             methodType = functionType;
8758             functionType = methodType.method.dataType;
8759             
8760             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8761             // TOCHECK: Instead of doing this here could this be done per param?
8762             if(exp.call.exp.expType.usedClass)
8763             {
8764                char typeString[1024];
8765                typeString[0] = '\0';
8766                {
8767                   Symbol back = functionType.thisClass;
8768                   // Do not output class specifier here (thisclass was added to this)
8769                   functionType.thisClass = null;
8770                   PrintType(functionType, typeString, true, true);
8771                   functionType.thisClass = back;
8772                }
8773                if(strstr(typeString, "thisclass"))
8774                {
8775                   OldList * specs = MkList();
8776                   Declarator decl;
8777                   {
8778                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8779
8780                      decl = SpecDeclFromString(typeString, specs, null);
8781                      
8782                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8783                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8784                         exp.call.exp.expType.usedClass))
8785                         thisClassParams = false;
8786                      
8787                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8788                      {
8789                         Class backupThisClass = thisClass;
8790                         thisClass = exp.call.exp.expType.usedClass;
8791                         ProcessDeclarator(decl);
8792                         thisClass = backupThisClass;
8793                      }
8794
8795                      thisClassParams = true;
8796
8797                      functionType = ProcessType(specs, decl);
8798                      functionType.refCount = 0;
8799                      FinishTemplatesContext(context);
8800                   }
8801
8802                   FreeList(specs, FreeSpecifier);
8803                   FreeDeclarator(decl);
8804                 }
8805             }
8806          }
8807          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8808          {
8809             Type type = functionType.type;
8810             if(!functionType.refCount)
8811             {
8812                functionType.type = null;
8813                FreeType(functionType);
8814             }
8815             //methodType = functionType;
8816             functionType = type;
8817          }
8818          if(functionType && functionType.kind != TypeKind::functionType)
8819          {
8820             Compiler_Error($"called object %s is not a function\n", name);
8821          }
8822          else if(functionType)
8823          {
8824             bool emptyParams = false, noParams = false;
8825             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8826             Type type = functionType.params.first;
8827             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8828             int extra = 0;
8829             Location oldyylloc = yylloc;
8830
8831             if(!type) emptyParams = true;
8832
8833             // WORKING ON THIS:
8834             if(functionType.extraParam && e && functionType.thisClass)
8835             {
8836                e.destType = MkClassType(functionType.thisClass.string);
8837                e = e.next;
8838             }
8839
8840             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8841             // Fixed #141 by adding '&& !functionType.extraParam'
8842             if(!functionType.staticMethod && !functionType.extraParam)
8843             {
8844                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType && 
8845                   memberExp.member.exp.expType._class)
8846                {
8847                   type = MkClassType(memberExp.member.exp.expType._class.string);
8848                   if(e)
8849                   {
8850                      e.destType = type;
8851                      e = e.next;
8852                      type = functionType.params.first;
8853                   }
8854                   else
8855                      type.refCount = 0;
8856                }
8857                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8858                {
8859                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8860                   type.byReference = functionType.byReference;
8861                   type.typedByReference = functionType.typedByReference;
8862                   if(e)
8863                   {
8864                      // Allow manually passing a class for typed object
8865                      if(type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
8866                         e = e.next;
8867                      e.destType = type;
8868                      e = e.next;
8869                      type = functionType.params.first;
8870                   }
8871                   else
8872                      type.refCount = 0;
8873                   //extra = 1;
8874                }
8875             }
8876
8877             if(type && type.kind == voidType)
8878             {
8879                noParams = true;
8880                if(!type.refCount) FreeType(type);
8881                type = null;
8882             }
8883
8884             for( ; e; e = e.next)
8885             {
8886                if(!type && !emptyParams)
8887                {
8888                   yylloc = e.loc;
8889                   if(methodType && methodType.methodClass)
8890                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8891                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8892                         noParams ? 0 : functionType.params.count);
8893                   else
8894                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8895                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8896                         noParams ? 0 : functionType.params.count);
8897                   break;
8898                }
8899
8900                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8901                {
8902                   Type templatedType = null;
8903                   Class _class = methodType.usedClass;
8904                   ClassTemplateParameter curParam = null;
8905                   int id = 0;
8906                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8907                   {
8908                      Class sClass;
8909                      for(sClass = _class; sClass; sClass = sClass.base)
8910                      {
8911                         if(sClass.templateClass) sClass = sClass.templateClass;
8912                         id = 0;
8913                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8914                         {
8915                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8916                            {
8917                               Class nextClass;
8918                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
8919                               {
8920                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
8921                                  id += nextClass.templateParams.count;
8922                               }
8923                               break;
8924                            }
8925                            id++;
8926                         }
8927                         if(curParam) break;
8928                      }
8929                   }
8930                   if(curParam && _class.templateArgs[id].dataTypeString)
8931                   {
8932                      ClassTemplateArgument arg = _class.templateArgs[id];
8933                      {
8934                         Context context = SetupTemplatesContext(_class);
8935                      
8936                         /*if(!arg.dataType)
8937                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
8938                         templatedType = ProcessTypeString(arg.dataTypeString, false);
8939                         FinishTemplatesContext(context);
8940                      }
8941                      e.destType = templatedType;
8942                      if(templatedType)
8943                      {
8944                         templatedType.passAsTemplate = true;
8945                         // templatedType.refCount++;
8946                      }
8947                   }
8948                   else
8949                   {
8950                      e.destType = type;
8951                      if(type) type.refCount++;
8952                   }
8953                }
8954                else
8955                {
8956                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
8957                   {
8958                      e.destType = type.prev;
8959                      e.destType.refCount++;
8960                   }
8961                   else
8962                   {
8963                      e.destType = type;
8964                      if(type) type.refCount++;
8965                   }
8966                }
8967                // Don't reach the end for the ellipsis
8968                if(type && type.kind != ellipsisType)
8969                {
8970                   Type next = type.next;
8971                   if(!type.refCount) FreeType(type);
8972                   type = next;
8973                }
8974             }
8975
8976             if(type && type.kind != ellipsisType)
8977             {
8978                if(methodType && methodType.methodClass)
8979                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
8980                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
8981                      functionType.params.count + extra);
8982                else
8983                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
8984                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
8985                      functionType.params.count + extra);
8986             }
8987             yylloc = oldyylloc;
8988             if(type && !type.refCount) FreeType(type);
8989          }
8990          else
8991          {
8992             functionType = Type
8993             {
8994                refCount = 0;
8995                kind = TypeKind::functionType;
8996             };
8997
8998             if(exp.call.exp.type == identifierExp)
8999             {
9000                char * string = exp.call.exp.identifier.string;
9001                if(inCompiler)
9002                {
9003                   Symbol symbol;
9004                   Location oldyylloc = yylloc;
9005
9006                   yylloc = exp.call.exp.identifier.loc;
9007                   if(strstr(string, "__builtin_") == string)
9008                   {
9009                      if(exp.destType)
9010                      {
9011                         functionType.returnType = exp.destType;
9012                         exp.destType.refCount++;
9013                      }
9014                   }
9015                   else
9016                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9017                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9018                   globalContext.symbols.Add((BTNode)symbol);
9019                   if(strstr(symbol.string, "::"))
9020                      globalContext.hasNameSpace = true;
9021
9022                   yylloc = oldyylloc;
9023                }
9024             }
9025             else if(exp.call.exp.type == memberExp)
9026             {
9027                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9028                   exp.call.exp.member.member.string);*/
9029             }
9030             else
9031                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9032
9033             if(!functionType.returnType)
9034             {
9035                functionType.returnType = Type
9036                {
9037                   refCount = 1;
9038                   kind = intType;
9039                };
9040             }
9041          }
9042          if(functionType && functionType.kind == TypeKind::functionType)
9043          {
9044             exp.expType = functionType.returnType;
9045
9046             if(functionType.returnType)
9047                functionType.returnType.refCount++;
9048
9049             if(!functionType.refCount)
9050                FreeType(functionType);
9051          }
9052
9053          if(exp.call.arguments)
9054          {
9055             for(e = exp.call.arguments->first; e; e = e.next)
9056             {
9057                Type destType = e.destType;
9058                ProcessExpressionType(e);
9059             }
9060          }
9061          break;
9062       }
9063       case memberExp:
9064       {
9065          Type type;
9066          Location oldyylloc = yylloc;
9067          bool thisPtr;
9068          Expression checkExp = exp.member.exp;
9069          while(checkExp)
9070          {
9071             if(checkExp.type == castExp)
9072                checkExp = checkExp.cast.exp;
9073             else if(checkExp.type == bracketsExp)
9074                checkExp = checkExp.list ? checkExp.list->first : null;
9075             else
9076                break;
9077          }
9078
9079          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9080          exp.thisPtr = thisPtr;
9081
9082          // DOING THIS LATER NOW...
9083          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9084          {
9085             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9086             /* TODO: Name Space Fix ups
9087             if(!exp.member.member.classSym)
9088                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9089             */
9090          }
9091
9092          ProcessExpressionType(exp.member.exp);
9093          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class && 
9094             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9095          {
9096             exp.isConstant = false;
9097          }
9098          else
9099             exp.isConstant = exp.member.exp.isConstant;
9100          type = exp.member.exp.expType;
9101
9102          yylloc = exp.loc;
9103
9104          if(type && (type.kind == templateType))
9105          {
9106             Class _class = thisClass ? thisClass : currentClass;
9107             ClassTemplateParameter param = null;
9108             if(_class)
9109             {
9110                for(param = _class.templateParams.first; param; param = param.next)
9111                {
9112                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9113                      break;
9114                }
9115             }
9116             if(param && param.defaultArg.member)
9117             {
9118                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9119                if(argExp)
9120                {
9121                   Expression expMember = exp.member.exp;
9122                   Declarator decl;
9123                   OldList * specs = MkList();
9124                   char thisClassTypeString[1024];
9125
9126                   FreeIdentifier(exp.member.member);
9127
9128                   ProcessExpressionType(argExp);
9129
9130                   {
9131                      char * colon = strstr(param.defaultArg.memberString, "::");
9132                      if(colon)
9133                      {
9134                         char className[1024];
9135                         Class sClass;
9136
9137                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9138                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9139                      }
9140                      else
9141                         strcpy(thisClassTypeString, _class.fullName);
9142                   }
9143
9144                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9145
9146                   exp.expType = ProcessType(specs, decl);
9147                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9148                   {
9149                      Class expClass = exp.expType._class.registered;
9150                      Class cClass = null;
9151                      int c;
9152                      int paramCount = 0;
9153                      int lastParam = -1;
9154                      
9155                      char templateString[1024];
9156                      ClassTemplateParameter param;
9157                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9158                      for(cClass = expClass; cClass; cClass = cClass.base)
9159                      {
9160                         int p = 0;
9161                         for(param = cClass.templateParams.first; param; param = param.next)
9162                         {
9163                            int id = p;
9164                            Class sClass;
9165                            ClassTemplateArgument arg;
9166                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9167                            arg = expClass.templateArgs[id];
9168
9169                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9170                            {
9171                               ClassTemplateParameter cParam;
9172                               //int p = numParams - sClass.templateParams.count;
9173                               int p = 0;
9174                               Class nextClass;
9175                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9176                               
9177                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9178                               {
9179                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9180                                  {
9181                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9182                                     {
9183                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9184                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9185                                        break;
9186                                     }
9187                                  }
9188                               }
9189                            }
9190
9191                            {
9192                               char argument[256];
9193                               argument[0] = '\0';
9194                               /*if(arg.name)
9195                               {
9196                                  strcat(argument, arg.name.string);
9197                                  strcat(argument, " = ");
9198                               }*/
9199                               switch(param.type)
9200                               {
9201                                  case expression:
9202                                  {
9203                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9204                                     char expString[1024];
9205                                     OldList * specs = MkList();
9206                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9207                                     Expression exp;
9208                                     char * string = PrintHexUInt64(arg.expression.ui64);
9209                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9210
9211                                     ProcessExpressionType(exp);
9212                                     ComputeExpression(exp);
9213                                     expString[0] = '\0';
9214                                     PrintExpression(exp, expString);
9215                                     strcat(argument, expString);
9216                                     // delete exp;
9217                                     FreeExpression(exp);
9218                                     break;
9219                                  }
9220                                  case identifier:
9221                                  {
9222                                     strcat(argument, arg.member.name);
9223                                     break;
9224                                  }
9225                                  case TemplateParameterType::type:
9226                                  {
9227                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9228                                     {
9229                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9230                                           strcat(argument, thisClassTypeString);
9231                                        else
9232                                           strcat(argument, arg.dataTypeString);
9233                                     }
9234                                     break;
9235                                  }
9236                               }
9237                               if(argument[0])
9238                               {
9239                                  if(paramCount) strcat(templateString, ", ");
9240                                  if(lastParam != p - 1)
9241                                  {
9242                                     strcat(templateString, param.name);
9243                                     strcat(templateString, " = ");
9244                                  }
9245                                  strcat(templateString, argument);
9246                                  paramCount++;
9247                                  lastParam = p;
9248                               }
9249                               p++;
9250                            }               
9251                         }
9252                      }
9253                      {
9254                         int len = strlen(templateString);
9255                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9256                         templateString[len++] = '>';
9257                         templateString[len++] = '\0';
9258                      }
9259                      {
9260                         Context context = SetupTemplatesContext(_class);
9261                         FreeType(exp.expType);
9262                         exp.expType = ProcessTypeString(templateString, false);
9263                         FinishTemplatesContext(context);
9264                      }                     
9265                   }
9266
9267                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9268                   exp.type = bracketsExp;
9269                   exp.list = MkListOne(MkExpOp(null, '*',
9270                   /*opExp;
9271                   exp.op.op = '*';
9272                   exp.op.exp1 = null;
9273                   exp.op.exp2 = */
9274                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9275                      MkExpBrackets(MkListOne(
9276                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9277                            '+',  
9278                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")), 
9279                            '+',
9280                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9281                            
9282                            ));
9283                }
9284             }
9285             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type && 
9286                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9287             {
9288                type = ProcessTemplateParameterType(type.templateParameter);
9289             }
9290          }
9291          // TODO: *** This seems to be where we should add method support for all basic types ***
9292          if(type && (type.kind == templateType));
9293          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9294                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType ||
9295                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9296                           (type.kind == pointerType && type.type.kind == charType)))
9297          {
9298             Identifier id = exp.member.member;
9299             TypeKind typeKind = type.kind;
9300             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9301             if(typeKind == subClassType && exp.member.exp.type == classExp)
9302             {
9303                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9304                typeKind = classType;
9305             }
9306
9307             if(id)
9308             {
9309                if(typeKind == intType || typeKind == enumType)
9310                   _class = eSystem_FindClass(privateModule, "int");
9311                else if(!_class)
9312                {
9313                   if(type.kind == classType && type._class && type._class.registered)
9314                   {
9315                      _class = type._class.registered;
9316                   }
9317                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9318                   {
9319                      _class = FindClass("char *").registered;
9320                   }
9321                   else if(type.kind == pointerType)
9322                   {
9323                      _class = eSystem_FindClass(privateModule, "uintptr");
9324                      FreeType(exp.expType);
9325                      exp.expType = ProcessTypeString("uintptr", false);
9326                      exp.byReference = true;
9327                   }
9328                   else
9329                   {
9330                      char string[1024] = "";
9331                      Symbol classSym;
9332                      PrintTypeNoConst(type, string, false, true);
9333                      classSym = FindClass(string);
9334                      if(classSym) _class = classSym.registered;
9335                   }
9336                }
9337             }
9338
9339             if(_class && id)
9340             {
9341                /*bool thisPtr = 
9342                   (exp.member.exp.type == identifierExp && 
9343                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9344                Property prop = null;
9345                Method method = null;
9346                DataMember member = null;
9347                Property revConvert = null;
9348                ClassProperty classProp = null;
9349
9350                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9351                   exp.member.memberType = propertyMember;
9352
9353                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9354                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9355
9356                if(typeKind != subClassType)
9357                {
9358                   // Prioritize data members over properties for "this"
9359                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9360                   {
9361                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9362                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9363                      {
9364                         prop = eClass_FindProperty(_class, id.string, privateModule);
9365                         if(prop)
9366                            member = null;
9367                      }
9368                      if(!member && !prop)
9369                         prop = eClass_FindProperty(_class, id.string, privateModule);
9370                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9371                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9372                         exp.member.thisPtr = true;
9373                   }
9374                   // Prioritize properties over data members otherwise
9375                   else
9376                   {
9377                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9378                      if(!id.classSym)
9379                      {
9380                         prop = eClass_FindProperty(_class, id.string, null);
9381                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9382                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9383                      }
9384
9385                      if(!prop && !member)
9386                      {
9387                         method = eClass_FindMethod(_class, id.string, null);
9388                         if(!method)
9389                         {
9390                            prop = eClass_FindProperty(_class, id.string, privateModule);
9391                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9392                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9393                         }
9394                      }
9395
9396                      if(member && prop)
9397                      {
9398                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9399                            prop = null;
9400                         else
9401                            member = null;
9402                      }
9403                   }
9404                }
9405                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9406                   method = eClass_FindMethod(_class, id.string, privateModule);
9407                if(!prop && !member && !method)
9408                {
9409                   if(typeKind == subClassType)
9410                   {
9411                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9412                      if(classProp)
9413                      {
9414                         exp.member.memberType = classPropertyMember;
9415                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9416                      }
9417                      else
9418                      {
9419                         // Assume this is a class_data member
9420                         char structName[1024];
9421                         Identifier id = exp.member.member;
9422                         Expression classExp = exp.member.exp;
9423                         type.refCount++;
9424
9425                         FreeType(classExp.expType);
9426                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9427                      
9428                         strcpy(structName, "__ecereClassData_");
9429                         FullClassNameCat(structName, type._class.string, false);
9430                         exp.type = pointerExp;
9431                         exp.member.member = id;
9432
9433                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9434                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
9435                               MkExpBrackets(MkListOne(MkExpOp(
9436                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
9437                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9438                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9439                                  )));
9440
9441                         FreeType(type);
9442
9443                         ProcessExpressionType(exp);
9444                         return;
9445                      }
9446                   }
9447                   else
9448                   {
9449                      // Check for reverse conversion
9450                      // (Convert in an instantiation later, so that we can use
9451                      //  deep properties system)
9452                      Symbol classSym = FindClass(id.string);
9453                      if(classSym)
9454                      {
9455                         Class convertClass = classSym.registered;
9456                         if(convertClass)
9457                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9458                      }
9459                   }
9460                }
9461       
9462                if(prop)
9463                {
9464                   exp.member.memberType = propertyMember;
9465                   if(!prop.dataType)
9466                      ProcessPropertyType(prop);
9467                   exp.expType = prop.dataType;                     
9468                   if(prop.dataType) prop.dataType.refCount++;
9469                }
9470                else if(member)
9471                {
9472                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9473                   {
9474                      FreeExpContents(exp);
9475                      exp.type = identifierExp;
9476                      exp.identifier = MkIdentifier("class");
9477                      ProcessExpressionType(exp);
9478                      return;
9479                   }
9480
9481                   exp.member.memberType = dataMember;
9482                   DeclareStruct(_class.fullName, false);
9483                   if(!member.dataType)
9484                   {
9485                      Context context = SetupTemplatesContext(_class);
9486                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9487                      FinishTemplatesContext(context);
9488                   }
9489                   exp.expType = member.dataType;
9490                   if(member.dataType) member.dataType.refCount++;
9491                }
9492                else if(revConvert)
9493                {
9494                   exp.member.memberType = reverseConversionMember;
9495                   exp.expType = MkClassType(revConvert._class.fullName);
9496                }
9497                else if(method)
9498                {
9499                   //if(inCompiler)
9500                   {
9501                      /*if(id._class)
9502                      {
9503                         exp.type = identifierExp;
9504                         exp.identifier = exp.member.member;
9505                      }
9506                      else*/
9507                         exp.member.memberType = methodMember;
9508                   }
9509                   if(!method.dataType)
9510                      ProcessMethodType(method);
9511                   exp.expType = Type
9512                   {
9513                      refCount = 1;
9514                      kind = methodType;
9515                      method = method;
9516                   };
9517
9518                   // Tricky spot here... To use instance versus class virtual table
9519                   // Put it back to what it was... What did we break?
9520
9521                   // Had to put it back for overriding Main of Thread global instance
9522
9523                   //exp.expType.methodClass = _class;
9524                   exp.expType.methodClass = (id && id._class) ? _class : null;
9525
9526                   // Need the actual class used for templated classes
9527                   exp.expType.usedClass = _class;
9528                }
9529                else if(!classProp)
9530                {
9531                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9532                   {
9533                      FreeExpContents(exp);
9534                      exp.type = identifierExp;
9535                      exp.identifier = MkIdentifier("class");
9536                      ProcessExpressionType(exp);
9537                      return;
9538                   }
9539                   yylloc = exp.member.member.loc;
9540                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9541                   if(inCompiler)
9542                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9543                }
9544
9545                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9546                {
9547                   Class tClass;
9548
9549                   tClass = _class;
9550                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9551
9552                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9553                   {
9554                      int id = 0;
9555                      ClassTemplateParameter curParam = null;
9556                      Class sClass;
9557
9558                      for(sClass = tClass; sClass; sClass = sClass.base)
9559                      {
9560                         id = 0;
9561                         if(sClass.templateClass) sClass = sClass.templateClass;
9562                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9563                         {
9564                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9565                            {
9566                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9567                                  id += sClass.templateParams.count;
9568                               break;
9569                            }
9570                            id++;
9571                         }
9572                         if(curParam) break;
9573                      }
9574
9575                      if(curParam && tClass.templateArgs[id].dataTypeString)
9576                      {
9577                         ClassTemplateArgument arg = tClass.templateArgs[id];
9578                         Context context = SetupTemplatesContext(tClass);
9579                         /*if(!arg.dataType)
9580                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9581                         FreeType(exp.expType);
9582                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9583                         if(exp.expType)
9584                         {
9585                            if(exp.expType.kind == thisClassType)
9586                            {
9587                               FreeType(exp.expType);
9588                               exp.expType = ReplaceThisClassType(_class);
9589                            }
9590
9591                            if(tClass.templateClass)
9592                               exp.expType.passAsTemplate = true;
9593                            //exp.expType.refCount++;
9594                            if(!exp.destType)
9595                            {
9596                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9597                               //exp.destType.refCount++;
9598
9599                               if(exp.destType.kind == thisClassType)
9600                               {
9601                                  FreeType(exp.destType);
9602                                  exp.destType = ReplaceThisClassType(_class);
9603                               }
9604                            }
9605                         }
9606                         FinishTemplatesContext(context);
9607                      }
9608                   }
9609                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9610                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9611                   {
9612                      int id = 0;
9613                      ClassTemplateParameter curParam = null;
9614                      Class sClass;
9615
9616                      for(sClass = tClass; sClass; sClass = sClass.base)
9617                      {
9618                         id = 0;
9619                         if(sClass.templateClass) sClass = sClass.templateClass;
9620                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9621                         {
9622                            if(curParam.type == TemplateParameterType::type && 
9623                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9624                            {
9625                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9626                                  id += sClass.templateParams.count;
9627                               break;
9628                            }
9629                            id++;
9630                         }
9631                         if(curParam) break;
9632                      }
9633
9634                      if(curParam)
9635                      {
9636                         ClassTemplateArgument arg = tClass.templateArgs[id];
9637                         Context context = SetupTemplatesContext(tClass);
9638                         Type basicType;
9639                         /*if(!arg.dataType)
9640                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9641                         
9642                         basicType = ProcessTypeString(arg.dataTypeString, false);
9643                         if(basicType)
9644                         {
9645                            if(basicType.kind == thisClassType)
9646                            {
9647                               FreeType(basicType);
9648                               basicType = ReplaceThisClassType(_class);
9649                            }
9650
9651                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9652                            if(tClass.templateClass)
9653                               basicType.passAsTemplate = true;
9654                            */
9655                            
9656                            FreeType(exp.expType);
9657
9658                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9659                            //exp.expType.refCount++;
9660                            if(!exp.destType)
9661                            {
9662                               exp.destType = exp.expType;
9663                               exp.destType.refCount++;
9664                            }
9665
9666                            {
9667                               Expression newExp { };
9668                               OldList * specs = MkList();
9669                               Declarator decl;
9670                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9671                               *newExp = *exp;
9672                               if(exp.destType) exp.destType.refCount++;
9673                               if(exp.expType)  exp.expType.refCount++;
9674                               exp.type = castExp;
9675                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9676                               exp.cast.exp = newExp;
9677                               //FreeType(exp.expType);
9678                               //exp.expType = null;
9679                               //ProcessExpressionType(sourceExp);
9680                            }
9681                         }
9682                         FinishTemplatesContext(context);
9683                      }
9684                   }
9685                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9686                   {
9687                      Class expClass = exp.expType._class.registered;
9688                      if(expClass)
9689                      {
9690                         Class cClass = null;
9691                         int c;
9692                         int p = 0;
9693                         int paramCount = 0;
9694                         int lastParam = -1;
9695                         char templateString[1024];
9696                         ClassTemplateParameter param;
9697                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9698                         while(cClass != expClass)
9699                         {
9700                            Class sClass;
9701                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9702                            cClass = sClass;
9703
9704                            for(param = cClass.templateParams.first; param; param = param.next)
9705                            {
9706                               Class cClassCur = null;
9707                               int c;
9708                               int cp = 0;
9709                               ClassTemplateParameter paramCur = null;
9710                               ClassTemplateArgument arg;
9711                               while(cClassCur != tClass && !paramCur)
9712                               {
9713                                  Class sClassCur;
9714                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9715                                  cClassCur = sClassCur;
9716
9717                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9718                                  {
9719                                     if(!strcmp(paramCur.name, param.name))
9720                                     {
9721                                        
9722                                        break;
9723                                     }
9724                                     cp++;
9725                                  }
9726                               }
9727                               if(paramCur && paramCur.type == TemplateParameterType::type)
9728                                  arg = tClass.templateArgs[cp];
9729                               else
9730                                  arg = expClass.templateArgs[p];
9731
9732                               {
9733                                  char argument[256];
9734                                  argument[0] = '\0';
9735                                  /*if(arg.name)
9736                                  {
9737                                     strcat(argument, arg.name.string);
9738                                     strcat(argument, " = ");
9739                                  }*/
9740                                  switch(param.type)
9741                                  {
9742                                     case expression:
9743                                     {
9744                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9745                                        char expString[1024];
9746                                        OldList * specs = MkList();
9747                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9748                                        Expression exp;
9749                                        char * string = PrintHexUInt64(arg.expression.ui64);
9750                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9751
9752                                        ProcessExpressionType(exp);
9753                                        ComputeExpression(exp);
9754                                        expString[0] = '\0';
9755                                        PrintExpression(exp, expString);
9756                                        strcat(argument, expString);
9757                                        // delete exp;
9758                                        FreeExpression(exp);
9759                                        break;
9760                                     }
9761                                     case identifier:
9762                                     {
9763                                        strcat(argument, arg.member.name);
9764                                        break;
9765                                     }
9766                                     case TemplateParameterType::type:
9767                                     {
9768                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9769                                           strcat(argument, arg.dataTypeString);
9770                                        break;
9771                                     }
9772                                  }
9773                                  if(argument[0])
9774                                  {
9775                                     if(paramCount) strcat(templateString, ", ");
9776                                     if(lastParam != p - 1)
9777                                     {
9778                                        strcat(templateString, param.name);
9779                                        strcat(templateString, " = ");
9780                                     }                                       
9781                                     strcat(templateString, argument);
9782                                     paramCount++;
9783                                     lastParam = p;
9784                                  }
9785                               }
9786                               p++;
9787                            }
9788                         }
9789                         {
9790                            int len = strlen(templateString);
9791                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9792                            templateString[len++] = '>';
9793                            templateString[len++] = '\0';
9794                         }
9795
9796                         FreeType(exp.expType);
9797                         {
9798                            Context context = SetupTemplatesContext(tClass);
9799                            exp.expType = ProcessTypeString(templateString, false);
9800                            FinishTemplatesContext(context);
9801                         }
9802                      }
9803                   }
9804                }
9805             }
9806             else
9807                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9808          }
9809          else if(type && (type.kind == structType || type.kind == unionType))
9810          {
9811             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9812             if(memberType)
9813             {
9814                exp.expType = memberType;
9815                if(memberType)
9816                   memberType.refCount++;
9817             }
9818          }
9819          else 
9820          {
9821             char expString[10240];
9822             expString[0] = '\0';
9823             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9824             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9825          }
9826
9827          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9828          {
9829             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9830             {
9831                Identifier id = exp.member.member;
9832                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9833                if(_class)
9834                {
9835                   FreeType(exp.expType);
9836                   exp.expType = ReplaceThisClassType(_class);
9837                }
9838             }
9839          }         
9840          yylloc = oldyylloc;
9841          break;
9842       }
9843       // Convert x->y into (*x).y
9844       case pointerExp:
9845       {
9846          Type destType = exp.destType;
9847
9848          // DOING THIS LATER NOW...
9849          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9850          {
9851             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9852             /* TODO: Name Space Fix ups
9853             if(!exp.member.member.classSym)
9854                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9855             */
9856          }
9857
9858          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9859          exp.type = memberExp;
9860          if(destType)
9861             destType.count++;
9862          ProcessExpressionType(exp);
9863          if(destType)
9864             destType.count--;
9865          break;
9866       }
9867       case classSizeExp:
9868       {
9869          //ComputeExpression(exp);
9870
9871          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9872          if(classSym && classSym.registered)
9873          {
9874             if(classSym.registered.type == noHeadClass)
9875             {
9876                char name[1024];
9877                name[0] = '\0';
9878                DeclareStruct(classSym.string, false);
9879                FreeSpecifier(exp._class);
9880                exp.type = typeSizeExp;
9881                FullClassNameCat(name, classSym.string, false);
9882                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9883             }
9884             else
9885             {
9886                if(classSym.registered.fixed)
9887                {
9888                   FreeSpecifier(exp._class);
9889                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9890                   exp.type = constantExp;
9891                }
9892                else
9893                {
9894                   char className[1024];
9895                   strcpy(className, "__ecereClass_");
9896                   FullClassNameCat(className, classSym.string, true);
9897                   MangleClassName(className);
9898
9899                   DeclareClass(classSym, className);
9900
9901                   FreeExpContents(exp);
9902                   exp.type = pointerExp;
9903                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9904                   exp.member.member = MkIdentifier("structSize");
9905                }
9906             }
9907          }
9908
9909          exp.expType = Type
9910          {
9911             refCount = 1;
9912             kind = intType;
9913          };
9914          // exp.isConstant = true;
9915          break;
9916       }
9917       case typeSizeExp:
9918       {
9919          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
9920
9921          exp.expType = Type
9922          {
9923             refCount = 1;
9924             kind = intType;
9925          };
9926          exp.isConstant = true;
9927
9928          DeclareType(type, false, false);
9929          FreeType(type);
9930          break;
9931       }
9932       case castExp:
9933       {
9934          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
9935          type.count = 1;
9936          FreeType(exp.cast.exp.destType);
9937          exp.cast.exp.destType = type;
9938          type.refCount++;
9939          ProcessExpressionType(exp.cast.exp);
9940          type.count = 0;
9941          exp.expType = type;
9942          //type.refCount++;
9943          
9944          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
9945          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
9946          {
9947             void * prev = exp.prev, * next = exp.next;
9948             Type expType = exp.cast.exp.destType;
9949             Expression castExp = exp.cast.exp;
9950             Type destType = exp.destType;
9951
9952             if(expType) expType.refCount++;
9953
9954             //FreeType(exp.destType);
9955             FreeType(exp.expType);
9956             FreeTypeName(exp.cast.typeName);
9957             
9958             *exp = *castExp;
9959             FreeType(exp.expType);
9960             FreeType(exp.destType);
9961
9962             exp.expType = expType;
9963             exp.destType = destType;
9964
9965             delete castExp;
9966
9967             exp.prev = prev;
9968             exp.next = next;
9969
9970          }
9971          else
9972          {
9973             exp.isConstant = exp.cast.exp.isConstant;
9974          }
9975          //FreeType(type);
9976          break;
9977       }
9978       case extensionInitializerExp:
9979       {
9980          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
9981          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
9982          // ProcessInitializer(exp.initializer.initializer, type);
9983          exp.expType = type;
9984          break;
9985       }
9986       case vaArgExp:
9987       {
9988          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
9989          ProcessExpressionType(exp.vaArg.exp);
9990          exp.expType = type;
9991          break;
9992       }
9993       case conditionExp:
9994       {
9995          Expression e;
9996          exp.isConstant = true;
9997
9998          FreeType(exp.cond.cond.destType);
9999          exp.cond.cond.destType = MkClassType("bool");
10000          exp.cond.cond.destType.truth = true;
10001          ProcessExpressionType(exp.cond.cond);
10002          if(!exp.cond.cond.isConstant)
10003             exp.isConstant = false;
10004          for(e = exp.cond.exp->first; e; e = e.next)
10005          {
10006             if(!e.next)
10007             {
10008                FreeType(e.destType);
10009                e.destType = exp.destType;
10010                if(e.destType) e.destType.refCount++;
10011             }
10012             ProcessExpressionType(e);
10013             if(!e.next)
10014             {
10015                exp.expType = e.expType;
10016                if(e.expType) e.expType.refCount++;
10017             }
10018             if(!e.isConstant)
10019                exp.isConstant = false;
10020          }
10021
10022          FreeType(exp.cond.elseExp.destType);
10023          // Added this check if we failed to find an expType
10024          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10025
10026          // Reversed it...
10027          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10028
10029          if(exp.cond.elseExp.destType)
10030             exp.cond.elseExp.destType.refCount++;
10031          ProcessExpressionType(exp.cond.elseExp);
10032
10033          // FIXED THIS: Was done before calling process on elseExp
10034          if(!exp.cond.elseExp.isConstant)
10035             exp.isConstant = false;
10036          break;
10037       }
10038       case extensionCompoundExp:
10039       {
10040          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10041          {
10042             Statement last = exp.compound.compound.statements->last;
10043             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10044             {
10045                ((Expression)last.expressions->last).destType = exp.destType;
10046                if(exp.destType)
10047                   exp.destType.refCount++;
10048             }
10049             ProcessStatement(exp.compound);
10050             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10051             if(exp.expType)
10052                exp.expType.refCount++;
10053          }
10054          break;
10055       }
10056       case classExp:
10057       {
10058          Specifier spec = exp._classExp.specifiers->first;
10059          if(spec && spec.type == nameSpecifier)
10060          {
10061             exp.expType = MkClassType(spec.name);
10062             exp.expType.kind = subClassType;
10063             exp.byReference = true;
10064          }
10065          else
10066          {
10067             exp.expType = MkClassType("ecere::com::Class");
10068             exp.byReference = true;
10069          }
10070          break;
10071       }
10072       case classDataExp:
10073       {
10074          Class _class = thisClass ? thisClass : currentClass;
10075          if(_class)
10076          {
10077             Identifier id = exp.classData.id;
10078             char structName[1024];
10079             Expression classExp;
10080             strcpy(structName, "__ecereClassData_");
10081             FullClassNameCat(structName, _class.fullName, false);
10082             exp.type = pointerExp;
10083             exp.member.member = id;
10084             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10085                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10086             else
10087                classExp = MkExpIdentifier(MkIdentifier("class"));
10088
10089             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10090                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
10091                   MkExpBrackets(MkListOne(MkExpOp(
10092                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
10093                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10094                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10095                      )));
10096
10097             ProcessExpressionType(exp);
10098             return;
10099          }
10100          break;
10101       }
10102       case arrayExp:
10103       {
10104          Type type = null;
10105          char * typeString = null;
10106          char typeStringBuf[1024];
10107          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10108             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10109          {
10110             Class templateClass = exp.destType._class.registered;
10111             typeString = templateClass.templateArgs[2].dataTypeString;
10112          }
10113          else if(exp.list)
10114          {
10115             // Guess type from expressions in the array
10116             Expression e;
10117             for(e = exp.list->first; e; e = e.next)
10118             {
10119                ProcessExpressionType(e);
10120                if(e.expType)
10121                {
10122                   if(!type) { type = e.expType; type.refCount++; }
10123                   else
10124                   {
10125                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10126                      if(!MatchTypeExpression(e, type, null, false))
10127                      {
10128                         FreeType(type);
10129                         type = e.expType;
10130                         e.expType = null;
10131                         
10132                         e = exp.list->first;
10133                         ProcessExpressionType(e);
10134                         if(e.expType)
10135                         {
10136                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10137                            if(!MatchTypeExpression(e, type, null, false))
10138                            {
10139                               FreeType(e.expType);
10140                               e.expType = null;
10141                               FreeType(type);
10142                               type = null;
10143                               break;
10144                            }                           
10145                         }
10146                      }
10147                   }
10148                   if(e.expType)
10149                   {
10150                      FreeType(e.expType);
10151                      e.expType = null;
10152                   }
10153                }
10154             }
10155             if(type)
10156             {
10157                typeStringBuf[0] = '\0';
10158                PrintTypeNoConst(type, typeStringBuf, false, true);
10159                typeString = typeStringBuf;
10160                FreeType(type);
10161                type = null;
10162             }
10163          }
10164          if(typeString)
10165          {
10166             /*
10167             (Container)& (struct BuiltInContainer)
10168             {
10169                ._vTbl = class(BuiltInContainer)._vTbl,
10170                ._class = class(BuiltInContainer),
10171                .refCount = 0,
10172                .data = (int[]){ 1, 7, 3, 4, 5 },
10173                .count = 5,
10174                .type = class(int),
10175             }
10176             */
10177             char templateString[1024];
10178             OldList * initializers = MkList();
10179             OldList * structInitializers = MkList();
10180             OldList * specs = MkList();
10181             Expression expExt;
10182             Declarator decl = SpecDeclFromString(typeString, specs, null);
10183             sprintf(templateString, "Container<%s>", typeString);
10184
10185             if(exp.list)
10186             {
10187                Expression e;
10188                type = ProcessTypeString(typeString, false);
10189                while(e = exp.list->first)
10190                {
10191                   exp.list->Remove(e);
10192                   e.destType = type;
10193                   type.refCount++;
10194                   ProcessExpressionType(e);
10195                   ListAdd(initializers, MkInitializerAssignment(e));
10196                }
10197                FreeType(type);
10198                delete exp.list;
10199             }
10200             
10201             DeclareStruct("ecere::com::BuiltInContainer", false);
10202
10203             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10204                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10205             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10206                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10207             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10208                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10209             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10210                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10211                MkInitializerList(initializers))));
10212                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10213             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10214                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10215             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10216                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10217             exp.expType = ProcessTypeString(templateString, false);
10218             exp.type = bracketsExp;
10219             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10220                MkExpOp(null, '&',
10221                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10222                   MkInitializerList(structInitializers)))));
10223             ProcessExpressionType(expExt);
10224          }
10225          else
10226          {
10227             exp.expType = ProcessTypeString("Container", false);
10228             Compiler_Error($"Couldn't determine type of array elements\n");
10229          }
10230          break;
10231       }
10232    }
10233
10234    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10235    {
10236       FreeType(exp.expType);
10237       exp.expType = ReplaceThisClassType(thisClass);
10238    }
10239
10240    // Resolve structures here
10241    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10242    {
10243       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10244       // TODO: Fix members reference...
10245       if(symbol)
10246       {
10247          if(exp.expType.kind != enumType)
10248          {
10249             Type member;
10250             String enumName = CopyString(exp.expType.enumName);
10251
10252             // Fixed a memory leak on self-referencing C structs typedefs
10253             // by instantiating a new type rather than simply copying members
10254             // into exp.expType
10255             FreeType(exp.expType);
10256             exp.expType = Type { };
10257             exp.expType.kind = symbol.type.kind;
10258             exp.expType.refCount++;
10259             exp.expType.enumName = enumName;
10260
10261             exp.expType.members = symbol.type.members;
10262             for(member = symbol.type.members.first; member; member = member.next)
10263                member.refCount++;
10264          }
10265          else
10266          {
10267             NamedLink member;
10268             for(member = symbol.type.members.first; member; member = member.next)
10269             {
10270                NamedLink value { name = CopyString(member.name) };
10271                exp.expType.members.Add(value);
10272             }
10273          }
10274       }
10275    }
10276
10277    yylloc = exp.loc;
10278    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10279    else if(exp.destType && !exp.destType.keepCast)
10280    {
10281       if(!CheckExpressionType(exp, exp.destType, false))
10282       {
10283          if(!exp.destType.count || unresolved)
10284          {
10285             if(!exp.expType)
10286             {
10287                yylloc = exp.loc;
10288                if(exp.destType.kind != ellipsisType)
10289                {
10290                   char type2[1024];
10291                   type2[0] = '\0';
10292                   if(inCompiler)
10293                   {
10294                      char expString[10240];
10295                      expString[0] = '\0';
10296
10297                      PrintType(exp.destType, type2, false, true);
10298
10299                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10300                      if(unresolved)
10301                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10302                      else if(exp.type != dummyExp)
10303                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10304                   }
10305                }
10306                else
10307                {
10308                   char expString[10240] ;
10309                   expString[0] = '\0';
10310                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10311
10312                   if(unresolved)
10313                      Compiler_Error($"unresolved identifier %s\n", expString);
10314                   else if(exp.type != dummyExp)
10315                      Compiler_Error($"couldn't determine type of %s\n", expString);
10316                }
10317             }
10318             else
10319             {
10320                char type1[1024];
10321                char type2[1024];
10322                type1[0] = '\0';
10323                type2[0] = '\0';
10324                if(inCompiler)
10325                {
10326                   PrintType(exp.expType, type1, false, true);
10327                   PrintType(exp.destType, type2, false, true);
10328                }
10329
10330                //CheckExpressionType(exp, exp.destType, false);
10331
10332                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10333                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType && 
10334                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10335                else
10336                {
10337                   char expString[10240];
10338                   expString[0] = '\0';
10339                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10340
10341 #ifdef _DEBUG
10342                   CheckExpressionType(exp, exp.destType, false);
10343 #endif
10344                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10345                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10346                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10347
10348                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10349                   FreeType(exp.expType);
10350                   exp.destType.refCount++;
10351                   exp.expType = exp.destType;
10352                }
10353             }
10354          }
10355       }
10356       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10357       {
10358          Expression newExp { };
10359          char typeString[1024];
10360          OldList * specs = MkList();
10361          Declarator decl;
10362
10363          typeString[0] = '\0';
10364
10365          *newExp = *exp;
10366
10367          if(exp.expType)  exp.expType.refCount++;
10368          if(exp.expType)  exp.expType.refCount++;
10369          exp.type = castExp;
10370          newExp.destType = exp.expType;
10371
10372          PrintType(exp.expType, typeString, false, false);
10373          decl = SpecDeclFromString(typeString, specs, null);
10374          
10375          exp.cast.typeName = MkTypeName(specs, decl);
10376          exp.cast.exp = newExp;
10377       }
10378    }
10379    else if(unresolved)
10380    {
10381       if(exp.identifier._class && exp.identifier._class.name)
10382          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10383       else if(exp.identifier.string && exp.identifier.string[0])
10384          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10385    }
10386    else if(!exp.expType && exp.type != dummyExp)
10387    {
10388       char expString[10240];
10389       expString[0] = '\0';
10390       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10391       Compiler_Error($"couldn't determine type of %s\n", expString);
10392    }
10393
10394    // Let's try to support any_object & typed_object here:
10395    if(inCompiler)
10396       ApplyAnyObjectLogic(exp);
10397
10398    // Mark nohead classes as by reference, unless we're casting them to an integral type
10399    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10400       exp.expType._class.registered.type == noHeadClass && (!exp.destType || 
10401          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType && 
10402           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType)))
10403    {
10404       exp.byReference = true;
10405    }
10406    yylloc = oldyylloc;
10407 }
10408
10409 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10410 {
10411    // THIS CODE WILL FIND NEXT MEMBER...
10412    if(*curMember) 
10413    {
10414       *curMember = (*curMember).next;
10415
10416       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10417       {
10418          *curMember = subMemberStack[--(*subMemberStackPos)];
10419          *curMember = (*curMember).next;
10420       }
10421
10422       // SKIP ALL PROPERTIES HERE...
10423       while((*curMember) && (*curMember).isProperty)
10424          *curMember = (*curMember).next;
10425
10426       if(subMemberStackPos)
10427       {
10428          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10429          {
10430             subMemberStack[(*subMemberStackPos)++] = *curMember;
10431
10432             *curMember = (*curMember).members.first;
10433             while(*curMember && (*curMember).isProperty)
10434                *curMember = (*curMember).next;                     
10435          }
10436       }
10437    }
10438    while(!*curMember)
10439    {
10440       if(!*curMember)
10441       {
10442          if(subMemberStackPos && *subMemberStackPos)
10443          {
10444             *curMember = subMemberStack[--(*subMemberStackPos)];
10445             *curMember = (*curMember).next;
10446          }
10447          else
10448          {
10449             Class lastCurClass = *curClass;
10450
10451             if(*curClass == _class) break;     // REACHED THE END
10452
10453             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10454             *curMember = (*curClass).membersAndProperties.first;
10455          }
10456
10457          while((*curMember) && (*curMember).isProperty)
10458             *curMember = (*curMember).next;
10459          if(subMemberStackPos)
10460          {
10461             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10462             {
10463                subMemberStack[(*subMemberStackPos)++] = *curMember;
10464
10465                *curMember = (*curMember).members.first;
10466                while(*curMember && (*curMember).isProperty)
10467                   *curMember = (*curMember).next;                     
10468             }
10469          }
10470       }
10471    }
10472 }
10473
10474
10475 static void ProcessInitializer(Initializer init, Type type)
10476 {
10477    switch(init.type)
10478    {
10479       case expInitializer:
10480          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10481          {
10482             // TESTING THIS FOR SHUTTING = 0 WARNING
10483             if(init.exp && !init.exp.destType)
10484             {
10485                FreeType(init.exp.destType);
10486                init.exp.destType = type;
10487                if(type) type.refCount++;
10488             }
10489             if(init.exp)
10490             {
10491                ProcessExpressionType(init.exp);
10492                init.isConstant = init.exp.isConstant;
10493             }
10494             break;
10495          }
10496          else
10497          {
10498             Expression exp = init.exp;
10499             Instantiation inst = exp.instance;
10500             MembersInit members;
10501
10502             init.type = listInitializer;
10503             init.list = MkList();
10504
10505             if(inst.members)
10506             {
10507                for(members = inst.members->first; members; members = members.next)
10508                {
10509                   if(members.type == dataMembersInit)
10510                   {
10511                      MemberInit member;
10512                      for(member = members.dataMembers->first; member; member = member.next)
10513                      {
10514                         ListAdd(init.list, member.initializer);
10515                         member.initializer = null;
10516                      }
10517                   }
10518                   // Discard all MembersInitMethod
10519                }
10520             }
10521             FreeExpression(exp);
10522          }
10523       case listInitializer:
10524       {
10525          Initializer i;
10526          Type initializerType = null;
10527          Class curClass = null;
10528          DataMember curMember = null;
10529          DataMember subMemberStack[256];
10530          int subMemberStackPos = 0;
10531
10532          if(type && type.kind == arrayType)
10533             initializerType = Dereference(type);
10534          else if(type && (type.kind == structType || type.kind == unionType))
10535             initializerType = type.members.first;
10536
10537          for(i = init.list->first; i; i = i.next)
10538          {
10539             if(type && type.kind == classType && type._class && type._class.registered)
10540             {
10541                // 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)
10542                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10543                // TODO: Generate error on initializing a private data member this way from another module...
10544                if(curMember)
10545                {
10546                   if(!curMember.dataType)
10547                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10548                   initializerType = curMember.dataType;
10549                }
10550             }
10551             ProcessInitializer(i, initializerType);
10552             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10553                initializerType = initializerType.next;
10554             if(!i.isConstant)
10555                init.isConstant = false;
10556          }
10557
10558          if(type && type.kind == arrayType)
10559             FreeType(initializerType);
10560
10561          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10562          {
10563             Compiler_Error($"Assigning list initializer to non list\n");
10564          }
10565          break;
10566       }
10567    }
10568 }
10569
10570 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10571 {
10572    switch(spec.type)
10573    {
10574       case baseSpecifier:
10575       {
10576          if(spec.specifier == THISCLASS)
10577          {
10578             if(thisClass)
10579             {
10580                spec.type = nameSpecifier;
10581                spec.name = ReplaceThisClass(thisClass);
10582                spec.symbol = FindClass(spec.name);
10583                ProcessSpecifier(spec, declareStruct);
10584             }
10585          }
10586          break;
10587       }
10588       case nameSpecifier:
10589       {
10590          Symbol symbol = FindType(curContext, spec.name);
10591          if(symbol)
10592             DeclareType(symbol.type, true, true);
10593          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10594             DeclareStruct(spec.name, false);
10595          break;
10596       }
10597       case enumSpecifier:
10598       {
10599          Enumerator e;
10600          if(spec.list)
10601          {
10602             for(e = spec.list->first; e; e = e.next)
10603             {
10604                if(e.exp)
10605                   ProcessExpressionType(e.exp);
10606             }
10607          }
10608          break;
10609       }
10610       case structSpecifier:
10611       case unionSpecifier:
10612       {
10613          if(spec.definitions)
10614          {
10615             ClassDef def;
10616             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10617             //if(symbol)
10618                ProcessClass(spec.definitions, symbol);
10619             /*else
10620             {
10621                for(def = spec.definitions->first; def; def = def.next)
10622                {
10623                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10624                      ProcessDeclaration(def.decl);
10625                }
10626             }*/
10627          }
10628          break;
10629       }
10630       /*
10631       case classSpecifier:
10632       {
10633          Symbol classSym = FindClass(spec.name);
10634          if(classSym && classSym.registered && classSym.registered.type == structClass)
10635             DeclareStruct(spec.name, false);
10636          break;
10637       }
10638       */
10639    }
10640 }
10641
10642
10643 static void ProcessDeclarator(Declarator decl)
10644 {
10645    switch(decl.type)
10646    {
10647       case identifierDeclarator:
10648          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10649          {
10650             FreeSpecifier(decl.identifier._class);
10651             decl.identifier._class = null;
10652          }
10653          break;
10654       case arrayDeclarator:
10655          if(decl.array.exp)
10656             ProcessExpressionType(decl.array.exp);
10657       case structDeclarator:
10658       case bracketsDeclarator:
10659       case functionDeclarator:
10660       case pointerDeclarator:
10661       case extendedDeclarator:
10662       case extendedDeclaratorEnd:
10663          if(decl.declarator)
10664             ProcessDeclarator(decl.declarator);
10665          if(decl.type == functionDeclarator)
10666          {
10667             Identifier id = GetDeclId(decl);
10668             if(id && id._class)
10669             {
10670                TypeName param
10671                {
10672                   qualifiers = MkListOne(id._class);
10673                   declarator = null;
10674                };
10675                if(!decl.function.parameters)
10676                   decl.function.parameters = MkList();               
10677                decl.function.parameters->Insert(null, param);
10678                id._class = null;
10679             }
10680             if(decl.function.parameters)
10681             {
10682                TypeName param;
10683                
10684                for(param = decl.function.parameters->first; param; param = param.next)
10685                {
10686                   if(param.qualifiers && param.qualifiers->first)
10687                   {
10688                      Specifier spec = param.qualifiers->first;
10689                      if(spec && spec.specifier == TYPED_OBJECT)
10690                      {
10691                         Declarator d = param.declarator;
10692                         TypeName newParam
10693                         {
10694                            qualifiers = MkListOne(MkSpecifier(VOID));
10695                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10696                         };
10697                         
10698                         FreeList(param.qualifiers, FreeSpecifier);
10699
10700                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10701                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10702
10703                         decl.function.parameters->Insert(param, newParam);
10704                         param = newParam;
10705                      }
10706                      else if(spec && spec.specifier == ANY_OBJECT)
10707                      {
10708                         Declarator d = param.declarator;
10709                         
10710                         FreeList(param.qualifiers, FreeSpecifier);
10711
10712                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10713                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);                        
10714                      }
10715                      else if(spec.specifier == THISCLASS)
10716                      {
10717                         if(thisClass)
10718                         {
10719                            spec.type = nameSpecifier;
10720                            spec.name = ReplaceThisClass(thisClass);
10721                            spec.symbol = FindClass(spec.name);
10722                            ProcessSpecifier(spec, false);
10723                         }
10724                      }
10725                   }
10726
10727                   if(param.declarator)
10728                      ProcessDeclarator(param.declarator);
10729                }
10730             }
10731          }
10732          break;
10733    }
10734 }
10735
10736 static void ProcessDeclaration(Declaration decl)
10737 {
10738    yylloc = decl.loc;
10739    switch(decl.type)
10740    {
10741       case initDeclaration:
10742       {
10743          bool declareStruct = false;
10744          /*
10745          lineNum = decl.pos.line;
10746          column = decl.pos.col;
10747          */
10748
10749          if(decl.declarators)
10750          {
10751             InitDeclarator d;
10752          
10753             for(d = decl.declarators->first; d; d = d.next)
10754             {
10755                Type type, subType;
10756                ProcessDeclarator(d.declarator);
10757
10758                type = ProcessType(decl.specifiers, d.declarator);
10759
10760                if(d.initializer)
10761                {
10762                   ProcessInitializer(d.initializer, type);
10763
10764                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }                  
10765                   
10766                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10767                      d.initializer.exp.type == instanceExp)
10768                   {
10769                      if(type.kind == classType && type._class == 
10770                         d.initializer.exp.expType._class)
10771                      {
10772                         Instantiation inst = d.initializer.exp.instance;
10773                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10774                         
10775                         d.initializer.exp.instance = null;
10776                         if(decl.specifiers)
10777                            FreeList(decl.specifiers, FreeSpecifier);
10778                         FreeList(decl.declarators, FreeInitDeclarator);
10779
10780                         d = null;
10781
10782                         decl.type = instDeclaration;
10783                         decl.inst = inst;
10784                      }
10785                   }
10786                }
10787                for(subType = type; subType;)
10788                {
10789                   if(subType.kind == classType)
10790                   {
10791                      declareStruct = true;
10792                      break;
10793                   }
10794                   else if(subType.kind == pointerType)
10795                      break;
10796                   else if(subType.kind == arrayType)
10797                      subType = subType.arrayType;
10798                   else
10799                      break;
10800                }
10801
10802                FreeType(type);
10803                if(!d) break;
10804             }
10805          }
10806
10807          if(decl.specifiers)
10808          {
10809             Specifier s;
10810             for(s = decl.specifiers->first; s; s = s.next)
10811             {
10812                ProcessSpecifier(s, declareStruct);
10813             }
10814          }
10815          break;
10816       }
10817       case instDeclaration:
10818       {
10819          ProcessInstantiationType(decl.inst);
10820          break;
10821       }
10822       case structDeclaration:
10823       {
10824          Specifier spec;
10825          Declarator d;
10826          bool declareStruct = false;
10827
10828          if(decl.declarators)
10829          {
10830             for(d = decl.declarators->first; d; d = d.next)
10831             {
10832                Type type = ProcessType(decl.specifiers, d.declarator);
10833                Type subType;
10834                ProcessDeclarator(d);
10835                for(subType = type; subType;)
10836                {
10837                   if(subType.kind == classType)
10838                   {
10839                      declareStruct = true;
10840                      break;
10841                   }
10842                   else if(subType.kind == pointerType)
10843                      break;
10844                   else if(subType.kind == arrayType)
10845                      subType = subType.arrayType;
10846                   else
10847                      break;
10848                }
10849                FreeType(type);
10850             }
10851          }
10852          if(decl.specifiers)
10853          {
10854             for(spec = decl.specifiers->first; spec; spec = spec.next)
10855                ProcessSpecifier(spec, declareStruct);
10856          }
10857          break;
10858       }
10859    }
10860 }
10861
10862 static FunctionDefinition curFunction;
10863
10864 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10865 {
10866    char propName[1024], propNameM[1024];
10867    char getName[1024], setName[1024];
10868    OldList * args;
10869
10870    DeclareProperty(prop, setName, getName);
10871
10872    // eInstance_FireWatchers(object, prop);
10873    strcpy(propName, "__ecereProp_");
10874    FullClassNameCat(propName, prop._class.fullName, false);
10875    strcat(propName, "_");
10876    // strcat(propName, prop.name);
10877    FullClassNameCat(propName, prop.name, true);
10878    MangleClassName(propName);
10879
10880    strcpy(propNameM, "__ecerePropM_");
10881    FullClassNameCat(propNameM, prop._class.fullName, false);
10882    strcat(propNameM, "_");
10883    // strcat(propNameM, prop.name);
10884    FullClassNameCat(propNameM, prop.name, true);
10885    MangleClassName(propNameM);
10886
10887    if(prop.isWatchable)
10888    {
10889       args = MkList();
10890       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10891       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10892       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10893
10894       args = MkList();
10895       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10896       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10897       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10898    }
10899
10900    
10901    {
10902       args = MkList();
10903       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10904       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10905       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10906
10907       args = MkList();
10908       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10909       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10910       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10911    }
10912    
10913    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) && 
10914       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
10915       curFunction.propSet.fireWatchersDone = true;
10916 }
10917
10918 static void ProcessStatement(Statement stmt)
10919 {
10920    yylloc = stmt.loc;
10921    /*
10922    lineNum = stmt.pos.line;
10923    column = stmt.pos.col;
10924    */
10925    switch(stmt.type)
10926    {
10927       case labeledStmt:
10928          ProcessStatement(stmt.labeled.stmt);
10929          break;
10930       case caseStmt:
10931          // This expression should be constant...
10932          if(stmt.caseStmt.exp)
10933          {
10934             FreeType(stmt.caseStmt.exp.destType);
10935             stmt.caseStmt.exp.destType = curSwitchType;
10936             if(curSwitchType) curSwitchType.refCount++;
10937             ProcessExpressionType(stmt.caseStmt.exp);
10938             ComputeExpression(stmt.caseStmt.exp);
10939          }
10940          if(stmt.caseStmt.stmt)
10941             ProcessStatement(stmt.caseStmt.stmt);
10942          break;
10943       case compoundStmt:
10944       {
10945          if(stmt.compound.context)
10946          {
10947             Declaration decl;
10948             Statement s;
10949
10950             Statement prevCompound = curCompound;
10951             Context prevContext = curContext;
10952
10953             if(!stmt.compound.isSwitch)
10954             {
10955                curCompound = stmt;
10956                curContext = stmt.compound.context;
10957             }
10958
10959             if(stmt.compound.declarations)
10960             {
10961                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
10962                   ProcessDeclaration(decl);
10963             }
10964             if(stmt.compound.statements)
10965             {
10966                for(s = stmt.compound.statements->first; s; s = s.next)
10967                   ProcessStatement(s);
10968             }
10969
10970             curContext = prevContext;
10971             curCompound = prevCompound;
10972          }
10973          break;
10974       }
10975       case expressionStmt:
10976       {
10977          Expression exp;
10978          if(stmt.expressions)
10979          {
10980             for(exp = stmt.expressions->first; exp; exp = exp.next)
10981                ProcessExpressionType(exp);
10982          }
10983          break;
10984       }
10985       case ifStmt:
10986       {
10987          Expression exp;
10988
10989          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
10990          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
10991          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
10992          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
10993          {
10994             ProcessExpressionType(exp);
10995          }
10996          if(stmt.ifStmt.stmt)
10997             ProcessStatement(stmt.ifStmt.stmt);
10998          if(stmt.ifStmt.elseStmt)
10999             ProcessStatement(stmt.ifStmt.elseStmt);
11000          break;
11001       }
11002       case switchStmt:
11003       {
11004          Type oldSwitchType = curSwitchType;
11005          if(stmt.switchStmt.exp)
11006          {
11007             Expression exp;
11008             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11009             {
11010                if(!exp.next)
11011                {
11012                   /*
11013                   Type destType
11014                   {
11015                      kind = intType;
11016                      refCount = 1;
11017                   };
11018                   e.exp.destType = destType;
11019                   */
11020
11021                   ProcessExpressionType(exp);
11022                }
11023                if(!exp.next)
11024                   curSwitchType = exp.expType;
11025             }
11026          }
11027          ProcessStatement(stmt.switchStmt.stmt);
11028          curSwitchType = oldSwitchType;
11029          break;
11030       }
11031       case whileStmt:
11032       {
11033          if(stmt.whileStmt.exp)
11034          {
11035             Expression exp;
11036
11037             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11038             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11039             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11040             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11041             {
11042                ProcessExpressionType(exp);
11043             }
11044          }
11045          if(stmt.whileStmt.stmt)
11046             ProcessStatement(stmt.whileStmt.stmt);
11047          break;
11048       }
11049       case doWhileStmt:
11050       {
11051          if(stmt.doWhile.exp)
11052          {
11053             Expression exp;
11054
11055             if(stmt.doWhile.exp->last)
11056             {
11057                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11058                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11059                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11060             }
11061             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11062             {
11063                ProcessExpressionType(exp);
11064             }
11065          }
11066          if(stmt.doWhile.stmt)
11067             ProcessStatement(stmt.doWhile.stmt);
11068          break;
11069       }
11070       case forStmt:
11071       {
11072          Expression exp;
11073          if(stmt.forStmt.init)
11074             ProcessStatement(stmt.forStmt.init);
11075
11076          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11077          {
11078             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11079             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11080             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11081          }
11082
11083          if(stmt.forStmt.check)
11084             ProcessStatement(stmt.forStmt.check);
11085          if(stmt.forStmt.increment)
11086          {
11087             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11088                ProcessExpressionType(exp);
11089          }
11090
11091          if(stmt.forStmt.stmt)
11092             ProcessStatement(stmt.forStmt.stmt);
11093          break;
11094       }
11095       case forEachStmt:
11096       {
11097          Identifier id = stmt.forEachStmt.id;
11098          OldList * exp = stmt.forEachStmt.exp;
11099          OldList * filter = stmt.forEachStmt.filter;
11100          Statement block = stmt.forEachStmt.stmt;
11101          char iteratorType[1024];
11102          Type source;
11103          Expression e;
11104          bool isBuiltin = exp && exp->last && 
11105             (((Expression)exp->last).type == ExpressionType::arrayExp || 
11106               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11107          Expression arrayExp;
11108          char * typeString = null;
11109          int builtinCount = 0;
11110
11111          for(e = exp ? exp->first : null; e; e = e.next)
11112          {
11113             if(!e.next)
11114             {
11115                FreeType(e.destType);
11116                e.destType = ProcessTypeString("Container", false);
11117             }
11118             if(!isBuiltin || e.next)
11119                ProcessExpressionType(e);
11120          }
11121
11122          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11123          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11124             eClass_IsDerived(source._class.registered, containerClass)))
11125          {
11126             Class _class = source ? source._class.registered : null;
11127             Symbol symbol;
11128             Expression expIt = null;
11129             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11130             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11131             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11132             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11133             stmt.type = compoundStmt;
11134             
11135             stmt.compound.context = Context { };
11136             stmt.compound.context.parent = curContext;
11137             curContext = stmt.compound.context;
11138
11139             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11140             {
11141                Class mapClass = eSystem_FindClass(privateModule, "Map");
11142                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11143                isCustomAVLTree = true;
11144                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11145                   isAVLTree = true;
11146                else if(eClass_IsDerived(source._class.registered, mapClass))
11147                   isMap = true;
11148             }
11149             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11150             else if(source && eClass_IsDerived(source._class.registered, linkListClass)) 
11151             {
11152                Class listClass = eSystem_FindClass(privateModule, "List");
11153                isLinkList = true;
11154                isList = eClass_IsDerived(source._class.registered, listClass);
11155             }
11156
11157             if(isArray)
11158             {
11159                Declarator decl;
11160                OldList * specs = MkList();
11161                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
11162                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11163                stmt.compound.declarations = MkListOne(
11164                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11165                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11166                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), 
11167                      MkInitializerAssignment(MkExpBrackets(exp))))));
11168             }
11169             else if(isBuiltin)
11170             {
11171                Type type = null;
11172                char typeStringBuf[1024];
11173                
11174                // TODO: Merge this code?
11175                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11176                if(((Expression)exp->last).type == castExp)
11177                {
11178                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11179                   if(typeName)
11180                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11181                }
11182
11183                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11184                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11185                   arrayExp.destType._class.registered.templateArgs)
11186                {
11187                   Class templateClass = arrayExp.destType._class.registered;
11188                   typeString = templateClass.templateArgs[2].dataTypeString;
11189                }
11190                else if(arrayExp.list)
11191                {
11192                   // Guess type from expressions in the array
11193                   Expression e;
11194                   for(e = arrayExp.list->first; e; e = e.next)
11195                   {
11196                      ProcessExpressionType(e);
11197                      if(e.expType)
11198                      {
11199                         if(!type) { type = e.expType; type.refCount++; }
11200                         else
11201                         {
11202                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11203                            if(!MatchTypeExpression(e, type, null, false))
11204                            {
11205                               FreeType(type);
11206                               type = e.expType;
11207                               e.expType = null;
11208                               
11209                               e = arrayExp.list->first;
11210                               ProcessExpressionType(e);
11211                               if(e.expType)
11212                               {
11213                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11214                                  if(!MatchTypeExpression(e, type, null, false))
11215                                  {
11216                                     FreeType(e.expType);
11217                                     e.expType = null;
11218                                     FreeType(type);
11219                                     type = null;
11220                                     break;
11221                                  }                           
11222                               }
11223                            }
11224                         }
11225                         if(e.expType)
11226                         {
11227                            FreeType(e.expType);
11228                            e.expType = null;
11229                         }
11230                      }
11231                   }
11232                   if(type)
11233                   {
11234                      typeStringBuf[0] = '\0';
11235                      PrintType(type, typeStringBuf, false, true);
11236                      typeString = typeStringBuf;
11237                      FreeType(type);
11238                   }
11239                }
11240                if(typeString)
11241                {
11242                   OldList * initializers = MkList();
11243                   Declarator decl;
11244                   OldList * specs = MkList();
11245                   if(arrayExp.list)
11246                   {
11247                      Expression e;
11248
11249                      builtinCount = arrayExp.list->count;
11250                      type = ProcessTypeString(typeString, false);
11251                      while(e = arrayExp.list->first)
11252                      {
11253                         arrayExp.list->Remove(e);
11254                         e.destType = type;
11255                         type.refCount++;
11256                         ProcessExpressionType(e);
11257                         ListAdd(initializers, MkInitializerAssignment(e));
11258                      }
11259                      FreeType(type);
11260                      delete arrayExp.list;
11261                   }
11262                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11263                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier), 
11264                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11265
11266                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11267                      PlugDeclarator(
11268                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11269                         ), MkInitializerList(initializers)))));
11270                   FreeList(exp, FreeExpression);
11271                }
11272                else
11273                {
11274                   arrayExp.expType = ProcessTypeString("Container", false);
11275                   Compiler_Error($"Couldn't determine type of array elements\n");
11276                }
11277
11278                /*
11279                Declarator decl;
11280                OldList * specs = MkList();
11281
11282                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
11283                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11284                stmt.compound.declarations = MkListOne(
11285                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11286                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11287                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))), 
11288                      MkInitializerAssignment(MkExpBrackets(exp))))));
11289                */
11290             }
11291             else if(isLinkList && !isList)
11292             {
11293                Declarator decl;
11294                OldList * specs = MkList();
11295                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11296                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11297                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11298                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")), 
11299                      MkInitializerAssignment(MkExpBrackets(exp))))));
11300             }
11301             /*else if(isCustomAVLTree)
11302             {
11303                Declarator decl;
11304                OldList * specs = MkList();
11305                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11306                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11307                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11308                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")), 
11309                      MkInitializerAssignment(MkExpBrackets(exp))))));
11310             }*/
11311             else if(_class.templateArgs)
11312             {
11313                if(isMap)
11314                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11315                else
11316                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11317
11318                stmt.compound.declarations = MkListOne(
11319                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11320                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null, 
11321                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11322             }
11323             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11324
11325             if(block && block.type == compoundStmt && block.compound.context)
11326             {
11327                block.compound.context.parent = stmt.compound.context;
11328             }
11329             if(filter)
11330             {
11331                block = MkIfStmt(filter, block, null);
11332             }
11333             if(isArray)
11334             {
11335                stmt.compound.statements = MkListOne(MkForStmt(
11336                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11337                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11338                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11339                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11340                   block));
11341               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11342               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11343               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11344             }
11345             else if(isBuiltin)
11346             {
11347                char count[128];
11348                //OldList * specs = MkList();
11349                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11350
11351                sprintf(count, "%d", builtinCount);
11352
11353                stmt.compound.statements = MkListOne(MkForStmt(
11354                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11355                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11356                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11357                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11358                   block));
11359
11360                /*
11361                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11362                stmt.compound.statements = MkListOne(MkForStmt(
11363                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11364                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11365                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11366                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11367                   block));
11368               */
11369               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11370               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11371               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11372             }
11373             else if(isLinkList && !isList)
11374             {
11375                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11376                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11377                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString && 
11378                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11379                {
11380                   stmt.compound.statements = MkListOne(MkForStmt(
11381                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11382                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11383                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11384                      block));
11385                }
11386                else
11387                {
11388                   OldList * specs = MkList();
11389                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11390                   stmt.compound.statements = MkListOne(MkForStmt(
11391                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11392                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11393                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11394                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11395                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11396                      block));
11397                }
11398                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11399                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11400                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11401             }
11402             /*else if(isCustomAVLTree)
11403             {
11404                stmt.compound.statements = MkListOne(MkForStmt(
11405                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11406                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11407                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11408                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11409                   block));
11410
11411                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11412                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11413                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11414             }*/
11415             else
11416             {
11417                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11418                   MkIdentifier("Next")), null)), block));
11419             }
11420             ProcessExpressionType(expIt);
11421             if(stmt.compound.declarations->first)
11422                ProcessDeclaration(stmt.compound.declarations->first);
11423
11424             if(symbol) 
11425                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11426
11427             ProcessStatement(stmt);
11428             curContext = stmt.compound.context.parent;
11429             break;
11430          }
11431          else
11432          {
11433             Compiler_Error($"Expression is not a container\n");
11434          }
11435          break;
11436       }
11437       case gotoStmt:
11438          break;
11439       case continueStmt:
11440          break;
11441       case breakStmt:
11442          break;
11443       case returnStmt:
11444       {
11445          Expression exp;
11446          if(stmt.expressions)
11447          {
11448             for(exp = stmt.expressions->first; exp; exp = exp.next)
11449             {
11450                if(!exp.next)
11451                {
11452                   if(curFunction && !curFunction.type)
11453                      curFunction.type = ProcessType(
11454                         curFunction.specifiers, curFunction.declarator);
11455                   FreeType(exp.destType);
11456                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11457                   if(exp.destType) exp.destType.refCount++;
11458                }
11459                ProcessExpressionType(exp);
11460             }
11461          }
11462          break;
11463       }
11464       case badDeclarationStmt:
11465       {
11466          ProcessDeclaration(stmt.decl);
11467          break;
11468       }
11469       case asmStmt:
11470       {
11471          AsmField field;
11472          if(stmt.asmStmt.inputFields)
11473          {
11474             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11475                if(field.expression)
11476                   ProcessExpressionType(field.expression);
11477          }
11478          if(stmt.asmStmt.outputFields)
11479          {
11480             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11481                if(field.expression)
11482                   ProcessExpressionType(field.expression);
11483          }
11484          if(stmt.asmStmt.clobberedFields)
11485          {
11486             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11487             {
11488                if(field.expression)
11489                   ProcessExpressionType(field.expression);
11490             }
11491          }
11492          break;
11493       }
11494       case watchStmt:
11495       {
11496          PropertyWatch propWatch;
11497          OldList * watches = stmt._watch.watches;
11498          Expression object = stmt._watch.object;
11499          Expression watcher = stmt._watch.watcher;
11500          if(watcher)
11501             ProcessExpressionType(watcher);
11502          if(object)
11503             ProcessExpressionType(object);
11504
11505          if(inCompiler)
11506          {
11507             if(watcher || thisClass)
11508             {
11509                External external = curExternal;
11510                Context context = curContext;
11511
11512                stmt.type = expressionStmt;
11513                stmt.expressions = MkList();
11514
11515                curExternal = external.prev;
11516
11517                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11518                {
11519                   ClassFunction func;
11520                   char watcherName[1024];
11521                   Class watcherClass = watcher ? 
11522                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11523                   External createdExternal;
11524
11525                   // Create a declaration above
11526                   External externalDecl = MkExternalDeclaration(null);
11527                   ast->Insert(curExternal.prev, externalDecl);
11528
11529                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11530                   if(propWatch.deleteWatch)
11531                      strcat(watcherName, "_delete");
11532                   else
11533                   {
11534                      Identifier propID;
11535                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11536                      {
11537                         strcat(watcherName, "_");
11538                         strcat(watcherName, propID.string);
11539                      }
11540                   }
11541
11542                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11543                   {
11544                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11545                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11546                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11547                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11548                      ProcessClassFunctionBody(func, propWatch.compound);
11549                      propWatch.compound = null;
11550
11551                      //afterExternal = afterExternal ? afterExternal : curExternal;
11552
11553                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11554                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11555                      // TESTING THIS...
11556                      createdExternal.symbol.idCode = external.symbol.idCode;
11557
11558                      curExternal = createdExternal;
11559                      ProcessFunction(createdExternal.function);
11560
11561
11562                      // Create a declaration above
11563                      {
11564                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
11565                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11566                         externalDecl.declaration = decl;
11567                         if(decl.symbol && !decl.symbol.pointerExternal)
11568                            decl.symbol.pointerExternal = externalDecl;
11569                      }
11570
11571                      if(propWatch.deleteWatch)
11572                      {
11573                         OldList * args = MkList();
11574                         ListAdd(args, CopyExpression(object));
11575                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11576                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11577                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11578                      }
11579                      else
11580                      {
11581                         Class _class = object.expType._class.registered;
11582                         Identifier propID;
11583
11584                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11585                         {
11586                            char propName[1024];
11587                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11588                            if(prop)
11589                            {
11590                               char getName[1024], setName[1024];
11591                               OldList * args = MkList();
11592
11593                               DeclareProperty(prop, setName, getName);                              
11594                               
11595                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11596                               strcpy(propName, "__ecereProp_");
11597                               FullClassNameCat(propName, prop._class.fullName, false);
11598                               strcat(propName, "_");
11599                               // strcat(propName, prop.name);
11600                               FullClassNameCat(propName, prop.name, true);
11601
11602                               ListAdd(args, CopyExpression(object));
11603                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11604                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11605                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11606
11607                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11608                            }
11609                            else
11610                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11611                         }
11612                      }
11613                   }
11614                   else
11615                      Compiler_Error($"Invalid watched object\n");
11616                }
11617
11618                curExternal = external;
11619                curContext = context;
11620
11621                if(watcher)
11622                   FreeExpression(watcher);
11623                if(object)
11624                   FreeExpression(object);
11625                FreeList(watches, FreePropertyWatch);
11626             }
11627             else
11628                Compiler_Error($"No observer specified and not inside a _class\n");
11629          }
11630          else
11631          {
11632             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11633             {
11634                ProcessStatement(propWatch.compound);
11635             }
11636
11637          }
11638          break;
11639       }
11640       case fireWatchersStmt:
11641       {
11642          OldList * watches = stmt._watch.watches;
11643          Expression object = stmt._watch.object;
11644          Class _class;
11645          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11646          // printf("%X\n", watches);
11647          // printf("%X\n", stmt._watch.watches);
11648          if(object)
11649             ProcessExpressionType(object);
11650
11651          if(inCompiler)
11652          {
11653             _class = object ? 
11654                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11655
11656             if(_class)
11657             {
11658                Identifier propID;
11659
11660                stmt.type = expressionStmt;
11661                stmt.expressions = MkList();
11662
11663                // Check if we're inside a property set
11664                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11665                {
11666                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11667                }
11668                else if(!watches)
11669                {
11670                   //Compiler_Error($"No property specified and not inside a property set\n");
11671                }
11672                if(watches)
11673                {
11674                   for(propID = watches->first; propID; propID = propID.next)
11675                   {
11676                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11677                      if(prop)
11678                      {
11679                         CreateFireWatcher(prop, object, stmt);
11680                      }
11681                      else
11682                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11683                   }
11684                }
11685                else
11686                {
11687                   // Fire all properties!
11688                   Property prop;
11689                   Class base;
11690                   for(base = _class; base; base = base.base)
11691                   {
11692                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11693                      {
11694                         if(prop.isProperty && prop.isWatchable)
11695                         {
11696                            CreateFireWatcher(prop, object, stmt);
11697                         }
11698                      }
11699                   }
11700                }
11701
11702                if(object)
11703                   FreeExpression(object);
11704                FreeList(watches, FreeIdentifier);
11705             }
11706             else
11707                Compiler_Error($"Invalid object specified and not inside a class\n");
11708          }
11709          break;
11710       }
11711       case stopWatchingStmt:
11712       {
11713          OldList * watches = stmt._watch.watches;
11714          Expression object = stmt._watch.object;
11715          Expression watcher = stmt._watch.watcher;
11716          Class _class;
11717          if(object)
11718             ProcessExpressionType(object);
11719          if(watcher)
11720             ProcessExpressionType(watcher);
11721          if(inCompiler)
11722          {
11723             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11724
11725             if(watcher || thisClass)
11726             {
11727                if(_class)
11728                {
11729                   Identifier propID;
11730
11731                   stmt.type = expressionStmt;
11732                   stmt.expressions = MkList();
11733
11734                   if(!watches)
11735                   {
11736                      OldList * args;
11737                      // eInstance_StopWatching(object, null, watcher); 
11738                      args = MkList();
11739                      ListAdd(args, CopyExpression(object));
11740                      ListAdd(args, MkExpConstant("0"));
11741                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11742                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11743                   }
11744                   else
11745                   {
11746                      for(propID = watches->first; propID; propID = propID.next)
11747                      {
11748                         char propName[1024];
11749                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11750                         if(prop)
11751                         {
11752                            char getName[1024], setName[1024];
11753                            OldList * args = MkList();
11754
11755                            DeclareProperty(prop, setName, getName);
11756          
11757                            // eInstance_StopWatching(object, prop, watcher); 
11758                            strcpy(propName, "__ecereProp_");
11759                            FullClassNameCat(propName, prop._class.fullName, false);
11760                            strcat(propName, "_");
11761                            // strcat(propName, prop.name);
11762                            FullClassNameCat(propName, prop.name, true);
11763                            MangleClassName(propName);
11764
11765                            ListAdd(args, CopyExpression(object));
11766                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11767                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11768                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11769                         }
11770                         else
11771                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11772                      }
11773                   }
11774
11775                   if(object)
11776                      FreeExpression(object);
11777                   if(watcher)
11778                      FreeExpression(watcher);
11779                   FreeList(watches, FreeIdentifier);
11780                }
11781                else
11782                   Compiler_Error($"Invalid object specified and not inside a class\n");
11783             }
11784             else
11785                Compiler_Error($"No observer specified and not inside a class\n");
11786          }
11787          break;
11788       }
11789    }
11790 }
11791
11792 static void ProcessFunction(FunctionDefinition function)
11793 {
11794    Identifier id = GetDeclId(function.declarator);
11795    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11796    Type type = symbol ? symbol.type : null;
11797    Class oldThisClass = thisClass;
11798    Context oldTopContext = topContext;
11799
11800    yylloc = function.loc;
11801    // Process thisClass
11802    
11803    if(type && type.thisClass)
11804    {
11805       Symbol classSym = type.thisClass;
11806       Class _class = type.thisClass.registered;
11807       char className[1024];
11808       char structName[1024];
11809       Declarator funcDecl;
11810       Symbol thisSymbol;
11811
11812       bool typedObject = false;
11813
11814       if(_class && !_class.base)
11815       {
11816          _class = currentClass;
11817          if(_class && !_class.symbol)
11818             _class.symbol = FindClass(_class.fullName);
11819          classSym = _class ? _class.symbol : null;
11820          typedObject = true;
11821       }
11822
11823       thisClass = _class;
11824
11825       if(inCompiler && _class)
11826       {
11827          if(type.kind == functionType)
11828          {
11829             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11830             {
11831                //TypeName param = symbol.type.params.first;
11832                Type param = symbol.type.params.first;
11833                symbol.type.params.Remove(param);
11834                //FreeTypeName(param);
11835                FreeType(param);
11836             }
11837             if(type.classObjectType != classPointer)
11838             {
11839                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11840                symbol.type.staticMethod = true;
11841                symbol.type.thisClass = null;
11842
11843                // HIGH DANGER: VERIFYING THIS...
11844                symbol.type.extraParam = false;
11845             }
11846          }
11847
11848          strcpy(className, "__ecereClass_");
11849          FullClassNameCat(className, _class.fullName, true);
11850
11851          MangleClassName(className);
11852
11853          structName[0] = 0;
11854          FullClassNameCat(structName, _class.fullName, false);
11855
11856          // [class] this
11857          
11858
11859          funcDecl = GetFuncDecl(function.declarator);
11860          if(funcDecl)
11861          {
11862             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11863             {
11864                TypeName param = funcDecl.function.parameters->first;
11865                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11866                {
11867                   funcDecl.function.parameters->Remove(param);
11868                   FreeTypeName(param);
11869                }
11870             }
11871
11872             // DANGER: Watch for this... Check if it's a Conversion?
11873             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11874             
11875             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
11876             if(!function.propertyNoThis)
11877             {
11878                TypeName thisParam;
11879                
11880                if(type.classObjectType != classPointer)
11881                {
11882                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11883                   if(!funcDecl.function.parameters)
11884                      funcDecl.function.parameters = MkList();
11885                   funcDecl.function.parameters->Insert(null, thisParam);
11886                }
11887
11888                if(typedObject)
11889                {
11890                   if(type.classObjectType != classPointer)
11891                   {
11892                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
11893                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
11894                   }
11895
11896                   thisParam = TypeName
11897                   {
11898                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11899                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11900                   };
11901                   funcDecl.function.parameters->Insert(null, thisParam);
11902                }
11903             }
11904          }
11905
11906          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11907          {
11908             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11909             funcDecl = GetFuncDecl(initDecl.declarator);
11910             if(funcDecl)
11911             {
11912                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11913                {
11914                   TypeName param = funcDecl.function.parameters->first;
11915                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11916                   {
11917                      funcDecl.function.parameters->Remove(param);
11918                      FreeTypeName(param);
11919                   }
11920                }
11921
11922                if(type.classObjectType != classPointer)
11923                {
11924                   // DANGER: Watch for this... Check if it's a Conversion?
11925                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11926                   {
11927                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11928
11929                      if(!funcDecl.function.parameters)
11930                         funcDecl.function.parameters = MkList();
11931                      funcDecl.function.parameters->Insert(null, thisParam);
11932                   }
11933                }
11934             }         
11935          }
11936       }
11937       
11938       // Add this to the context
11939       if(function.body)
11940       {
11941          if(type.classObjectType != classPointer)
11942          {
11943             thisSymbol = Symbol
11944             {
11945                string = CopyString("this");
11946                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
11947             };
11948             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
11949
11950             if(typedObject && thisSymbol.type)
11951             {
11952                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
11953                thisSymbol.type.byReference = type.byReference;
11954                thisSymbol.type.typedByReference = type.byReference;
11955                /*
11956                thisSymbol = Symbol { string = CopyString("class") };
11957                function.body.compound.context.symbols.Add(thisSymbol);
11958                */
11959             }
11960          }
11961       }
11962
11963       // Pointer to class data
11964       
11965       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
11966       {
11967          DataMember member = null;
11968          {
11969             Class base;
11970             for(base = _class; base && base.type != systemClass; base = base.next)
11971             {
11972                for(member = base.membersAndProperties.first; member; member = member.next)
11973                   if(!member.isProperty)
11974                      break;
11975                if(member)
11976                   break;
11977             }
11978          }
11979          for(member = _class.membersAndProperties.first; member; member = member.next)
11980             if(!member.isProperty)
11981                break;
11982          if(member)
11983          {
11984             char pointerName[1024];
11985    
11986             Declaration decl;
11987             Initializer initializer;
11988             Expression exp, bytePtr;
11989    
11990             strcpy(pointerName, "__ecerePointer_");
11991             FullClassNameCat(pointerName, _class.fullName, false);
11992             {
11993                char className[1024];
11994                strcpy(className, "__ecereClass_");
11995                FullClassNameCat(className, classSym.string, true);
11996                MangleClassName(className);
11997
11998                // Testing This
11999                DeclareClass(classSym, className);
12000             }
12001
12002             // ((byte *) this)
12003             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12004
12005             if(_class.fixed)
12006             {
12007                char string[256];
12008                sprintf(string, "%d", _class.offset);
12009                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12010             }
12011             else
12012             {
12013                // ([bytePtr] + [className]->offset)
12014                exp = QBrackets(MkExpOp(bytePtr, '+',
12015                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12016             }
12017
12018             // (this ? [exp] : 0)
12019             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12020             exp.expType = Type
12021             {
12022                refCount = 1;
12023                kind = pointerType;
12024                type = Type { refCount = 1, kind = voidType };
12025             };
12026    
12027             if(function.body)
12028             {
12029                yylloc = function.body.loc;
12030                // ([structName] *) [exp]
12031                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12032                initializer = MkInitializerAssignment(
12033                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12034
12035                // [structName] * [pointerName] = [initializer];
12036                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12037
12038                {
12039                   Context prevContext = curContext;
12040                   curContext = function.body.compound.context;
12041
12042                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12043                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12044
12045                   curContext = prevContext;
12046                }
12047
12048                // WHY?
12049                decl.symbol = null;
12050
12051                if(!function.body.compound.declarations)
12052                   function.body.compound.declarations = MkList();
12053                function.body.compound.declarations->Insert(null, decl);
12054             }
12055          }
12056       }
12057       
12058
12059       // Loop through the function and replace undeclared identifiers
12060       // which are a member of the class (methods, properties or data)
12061       // by "this.[member]"
12062    }
12063    else
12064       thisClass = null;
12065
12066    if(id)
12067    {
12068       FreeSpecifier(id._class);
12069       id._class = null;
12070
12071       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12072       {
12073          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12074          id = GetDeclId(initDecl.declarator);
12075
12076          FreeSpecifier(id._class);
12077          id._class = null;
12078       }
12079    }
12080    if(function.body)
12081       topContext = function.body.compound.context;
12082    {
12083       FunctionDefinition oldFunction = curFunction;
12084       curFunction = function;
12085       if(function.body)
12086          ProcessStatement(function.body);
12087
12088       // If this is a property set and no firewatchers has been done yet, add one here
12089       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12090       {
12091          Statement prevCompound = curCompound;
12092          Context prevContext = curContext;
12093
12094          Statement fireWatchers = MkFireWatchersStmt(null, null);
12095          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12096          ListAdd(function.body.compound.statements, fireWatchers);
12097
12098          curCompound = function.body;
12099          curContext = function.body.compound.context;
12100
12101          ProcessStatement(fireWatchers);
12102
12103          curContext = prevContext;
12104          curCompound = prevCompound;
12105
12106       }
12107
12108       curFunction = oldFunction;
12109    }
12110
12111    if(function.declarator)
12112    {
12113       ProcessDeclarator(function.declarator);
12114    }
12115
12116    topContext = oldTopContext;
12117    thisClass = oldThisClass;
12118 }
12119
12120 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12121 static void ProcessClass(OldList definitions, Symbol symbol)
12122 {
12123    ClassDef def;
12124    External external = curExternal;
12125    Class regClass = symbol ? symbol.registered : null;
12126
12127    // Process all functions
12128    for(def = definitions.first; def; def = def.next)
12129    {
12130       if(def.type == functionClassDef)
12131       {
12132          if(def.function.declarator)
12133             curExternal = def.function.declarator.symbol.pointerExternal;
12134          else
12135             curExternal = external;
12136
12137          ProcessFunction((FunctionDefinition)def.function);
12138       }
12139       else if(def.type == declarationClassDef)
12140       {
12141          if(def.decl.type == instDeclaration)
12142          {
12143             thisClass = regClass;
12144             ProcessInstantiationType(def.decl.inst);
12145             thisClass = null;
12146          }
12147          // Testing this
12148          else
12149          {
12150             Class backThisClass = thisClass;
12151             if(regClass) thisClass = regClass;
12152             ProcessDeclaration(def.decl);
12153             thisClass = backThisClass;
12154          }
12155       }
12156       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12157       {
12158          MemberInit defProperty;
12159
12160          // Add this to the context
12161          Symbol thisSymbol = Symbol
12162          {
12163             string = CopyString("this");
12164             type = regClass ? MkClassType(regClass.fullName) : null;
12165          };
12166          globalContext.symbols.Add((BTNode)thisSymbol);
12167          
12168          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12169          {
12170             thisClass = regClass;
12171             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12172             thisClass = null;
12173          }
12174
12175          globalContext.symbols.Remove((BTNode)thisSymbol);
12176          FreeSymbol(thisSymbol);
12177       }
12178       else if(def.type == propertyClassDef && def.propertyDef)
12179       {
12180          PropertyDef prop = def.propertyDef;
12181
12182          // Add this to the context
12183          /*
12184          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12185          globalContext.symbols.Add(thisSymbol);
12186          */
12187          
12188          thisClass = regClass;
12189          if(prop.setStmt)
12190          {
12191             if(regClass)
12192             {
12193                Symbol thisSymbol
12194                {
12195                   string = CopyString("this");
12196                   type = MkClassType(regClass.fullName);
12197                };
12198                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12199             }
12200
12201             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12202             ProcessStatement(prop.setStmt);
12203          }
12204          if(prop.getStmt)
12205          {
12206             if(regClass)
12207             {
12208                Symbol thisSymbol
12209                {
12210                   string = CopyString("this");
12211                   type = MkClassType(regClass.fullName);
12212                };
12213                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12214             }
12215
12216             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12217             ProcessStatement(prop.getStmt);
12218          }
12219          if(prop.issetStmt)
12220          {
12221             if(regClass)
12222             {
12223                Symbol thisSymbol
12224                {
12225                   string = CopyString("this");
12226                   type = MkClassType(regClass.fullName);
12227                };
12228                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12229             }
12230
12231             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12232             ProcessStatement(prop.issetStmt);
12233          }
12234
12235          thisClass = null;
12236
12237          /*
12238          globalContext.symbols.Remove(thisSymbol);
12239          FreeSymbol(thisSymbol);
12240          */
12241       }
12242       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12243       {
12244          PropertyWatch propertyWatch = def.propertyWatch;
12245         
12246          thisClass = regClass;
12247          if(propertyWatch.compound)
12248          {
12249             Symbol thisSymbol
12250             {
12251                string = CopyString("this");
12252                type = regClass ? MkClassType(regClass.fullName) : null;
12253             };
12254
12255             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12256
12257             curExternal = null;
12258             ProcessStatement(propertyWatch.compound);
12259          }
12260          thisClass = null;
12261       }
12262    }
12263 }
12264
12265 void DeclareFunctionUtil(String s)
12266 {
12267    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12268    if(function)
12269    {
12270       char name[1024];
12271       name[0] = 0;
12272       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12273          strcpy(name, "__ecereFunction_");
12274       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12275       DeclareFunction(function, name);
12276    }
12277 }
12278
12279 void ComputeDataTypes()
12280 {
12281    External external;
12282    External temp { };
12283    External after = null;
12284
12285    currentClass = null;
12286
12287    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12288
12289    for(external = ast->first; external; external = external.next)
12290    {
12291       if(external.type == declarationExternal)
12292       {
12293          Declaration decl = external.declaration;
12294          if(decl)
12295          {
12296             OldList * decls = decl.declarators;
12297             if(decls)
12298             {
12299                InitDeclarator initDecl = decls->first;
12300                if(initDecl)
12301                {
12302                   Declarator declarator = initDecl.declarator;
12303                   if(declarator && declarator.type == identifierDeclarator)
12304                   {
12305                      Identifier id = declarator.identifier;
12306                      if(id && id.string)
12307                      {
12308                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12309                         {
12310                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12311                            after = external;
12312                         }
12313                      }
12314                   }
12315                }
12316             }
12317          }
12318        }
12319    }
12320
12321    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12322    ast->Insert(after, temp);
12323    curExternal = temp;
12324
12325    DeclareFunctionUtil("eSystem_New");
12326    DeclareFunctionUtil("eSystem_New0");
12327    DeclareFunctionUtil("eSystem_Renew");
12328    DeclareFunctionUtil("eSystem_Renew0");
12329    DeclareFunctionUtil("eClass_GetProperty");
12330
12331    DeclareStruct("ecere::com::Class", false);
12332    DeclareStruct("ecere::com::Instance", false);
12333    DeclareStruct("ecere::com::Property", false);
12334    DeclareStruct("ecere::com::DataMember", false);
12335    DeclareStruct("ecere::com::Method", false);
12336    DeclareStruct("ecere::com::SerialBuffer", false);
12337    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12338
12339    ast->Remove(temp);
12340
12341    for(external = ast->first; external; external = external.next)
12342    {
12343       afterExternal = curExternal = external;
12344       if(external.type == functionExternal)
12345       {
12346          currentClass = external.function._class;
12347          ProcessFunction(external.function);
12348       }
12349       // There shouldn't be any _class member access here anyways...
12350       else if(external.type == declarationExternal)
12351       {
12352          currentClass = null;
12353          ProcessDeclaration(external.declaration);
12354       }
12355       else if(external.type == classExternal)
12356       {
12357          ClassDefinition _class = external._class;
12358          currentClass = external.symbol.registered;
12359          if(_class.definitions)
12360          {
12361             ProcessClass(_class.definitions, _class.symbol);
12362          }
12363          if(inCompiler)
12364          {
12365             // Free class data...
12366             ast->Remove(external);
12367             delete external;
12368          }
12369       }
12370       else if(external.type == nameSpaceExternal)
12371       {
12372          thisNameSpace = external.id.string;
12373       }
12374    }
12375    currentClass = null;
12376    thisNameSpace = null;
12377
12378    delete temp.symbol;
12379    delete temp;
12380 }