compiler/libec: Fix for thisclass related warnings in Map.ec
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 // UNTIL IMPLEMENTED IN GRAMMAR
4 #define ACCESS_CLASSDATA(_class, baseClass) \
5    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
6
7 #define YYLTYPE Location
8 #include "grammar.h"
9
10 extern OldList * ast;
11 extern int returnCode;
12 extern Expression parsedExpression;
13 extern bool yydebug;
14 public void SetYydebug(bool b) { yydebug = b; }
15 extern bool echoOn;
16
17 void resetScanner();
18
19 // TODO: Reset this to 0 on reinitialization
20 int propWatcherID;
21
22 int expression_yyparse();
23 static Statement curCompound;
24 External curExternal, afterExternal;
25 static Type curSwitchType;
26 static Class currentClass;
27 Class thisClass;
28 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
29 static char * thisNameSpace;
30 /*static */Class containerClass;
31 bool thisClassParams = true;
32
33 uint internalValueCounter;
34
35 #ifdef _DEBUG
36 Time findSymbolTotalTime;
37 #endif
38
39 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
40 /*static */public void PrintExpression(Expression exp, char * string)
41 {
42    //if(inCompiler)
43    {
44       TempFile f { };
45       int count;
46       bool backOutputLineNumbers = outputLineNumbers;
47       outputLineNumbers = false;
48
49       if(exp)
50          OutputExpression(exp, f);
51       f.Seek(0, start);
52       count = strlen(string);
53       count += f.Read(string + count, 1, 1023);
54       string[count] = '\0';
55       delete f;
56
57       outputLineNumbers = backOutputLineNumbers;
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 _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             symbol = (Symbol)ctx.symbols.FindString(id.string);
125             if(symbol) break;
126          }
127       }
128
129       // If it is not, check if it is a member of the _class
130       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
131       {
132          Property prop = eClass_FindProperty(_class, id.string, privateModule);
133          Method method = null;
134          DataMember member = null;
135          ClassProperty classProp = null;
136          if(!prop)
137          {
138             method = eClass_FindMethod(_class, id.string, privateModule);
139          }
140          if(!prop && !method)
141             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
142          if(!prop && !method && !member)
143          {
144             classProp = eClass_FindClassProperty(_class, id.string);
145          }
146          if(prop || method || member || classProp)
147          {
148             // Replace by this.[member]
149             exp.type = memberExp;
150             exp.member.member = id;
151             exp.member.memberType = unresolvedMember;
152             exp.member.exp = QMkExpId("this");
153             //exp.member.exp.loc = exp.loc;
154             exp.addedThis = true;
155          }
156          else if(_class && _class.templateParams.first)
157          {
158             Class sClass;
159             for(sClass = _class; sClass; sClass = sClass.base)
160             {
161                if(sClass.templateParams.first)
162                {
163                   ClassTemplateParameter param;
164                   for(param = sClass.templateParams.first; param; param = param.next)
165                   {
166                      if(param.type == expression && !strcmp(param.name, id.string))
167                      {
168                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
169
170                         if(argExp)
171                         {
172                            Declarator decl;
173                            OldList * specs = MkList();
174
175                            FreeIdentifier(exp.member.member);
176
177                            ProcessExpressionType(argExp);
178
179                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
180
181                            exp.expType = ProcessType(specs, decl);
182
183                            // *[expType] *[argExp]
184                            exp.type = bracketsExp;
185                            exp.list = MkListOne(MkExpOp(null, '*',
186                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
187                         }
188                      }
189                   }
190                }
191             }
192          }
193       }
194    }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 // PRINTING ////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200
201 public char * PrintInt(int64 result)
202 {
203    char temp[100];
204    if(result > MAXINT)
205       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
208    if(result > MAXINT || result < MININT)
209       strcat(temp, "LL");
210    return CopyString(temp);
211 }
212
213 public char * PrintUInt(uint64 result)
214 {
215    char temp[100];
216    if(result > MAXDWORD)
217       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
218    else if(result > MAXINT)
219       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
220    else
221       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
222    return CopyString(temp);
223 }
224
225 public char *  PrintInt64(int64 result)
226 {
227    char temp[100];
228    if(result > MAXINT || result < MININT)
229       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
230    else
231       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
232    return CopyString(temp);
233 }
234
235 public char * PrintUInt64(uint64 result)
236 {
237    char temp[100];
238    if(result > MAXDWORD)
239       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
240    else if(result > MAXINT)
241       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
242    else
243       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
244    return CopyString(temp);
245 }
246
247 public char * PrintHexUInt(uint64 result)
248 {
249    char temp[100];
250    if(result > MAXDWORD)
251       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
252    else
253       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
254    if(result > MAXDWORD)
255       strcat(temp, "LL");
256    return CopyString(temp);
257 }
258
259 public char * PrintHexUInt64(uint64 result)
260 {
261    char temp[100];
262    if(result > MAXDWORD)
263       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
264    else
265       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
266    return CopyString(temp);
267 }
268
269 public char * PrintShort(short result)
270 {
271    char temp[100];
272    sprintf(temp, "%d", (unsigned short)result);
273    return CopyString(temp);
274 }
275
276 public char * PrintUShort(unsigned short result)
277 {
278    char temp[100];
279    if(result > 32767)
280       sprintf(temp, "0x%X", (int)result);
281    else
282       sprintf(temp, "%d", (int)result);
283    return CopyString(temp);
284 }
285
286 public char * PrintChar(char result)
287 {
288    char temp[100];
289    if(result > 0 && isprint(result))
290       sprintf(temp, "'%c'", result);
291    else if(result < 0)
292       sprintf(temp, "%d", (int)result);
293    else
294       //sprintf(temp, "%#X", result);
295       sprintf(temp, "0x%X", (unsigned char)result);
296    return CopyString(temp);
297 }
298
299 public char * PrintUChar(unsigned char result)
300 {
301    char temp[100];
302    sprintf(temp, "0x%X", result);
303    return CopyString(temp);
304 }
305
306 public char * PrintFloat(float result)
307 {
308    char temp[350];
309    if(result.isInf)
310    {
311       if(result.signBit)
312          strcpy(temp, "-inf");
313       else
314          strcpy(temp, "inf");
315    }
316    else if(result.isNan)
317    {
318       if(result.signBit)
319          strcpy(temp, "-nan");
320       else
321          strcpy(temp, "nan");
322    }
323    else
324       sprintf(temp, "%.16ff", result);
325    return CopyString(temp);
326 }
327
328 public char * PrintDouble(double result)
329 {
330    char temp[350];
331    if(result.isInf)
332    {
333       if(result.signBit)
334          strcpy(temp, "-inf");
335       else
336          strcpy(temp, "inf");
337    }
338    else if(result.isNan)
339    {
340       if(result.signBit)
341          strcpy(temp, "-nan");
342       else
343          strcpy(temp, "nan");
344    }
345    else
346       sprintf(temp, "%.16f", result);
347    return CopyString(temp);
348 }
349
350 ////////////////////////////////////////////////////////////////////////
351 ////////////////////////////////////////////////////////////////////////
352
353 //public Operand GetOperand(Expression exp);
354
355 #define GETVALUE(name, t) \
356    public bool GetOp##name(Operand op2, t * value2) \
357    {                                                        \
358       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
359       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
360       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
361       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
362       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
363       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
364       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
365       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
366       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
367       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
368       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
369       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
370       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
371       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
372       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
373       else                                                                          \
374          return false;                                                              \
375       return true;                                                                  \
376    } \
377    public bool Get##name(Expression exp, t * value2) \
378    {                                                        \
379       Operand op2 = GetOperand(exp);                        \
380       return GetOp##name(op2, value2); \
381    }
382
383 // To help the debugger currently not preprocessing...
384 #define HELP(x) x
385
386 GETVALUE(Int, HELP(int));
387 GETVALUE(UInt, HELP(unsigned int));
388 GETVALUE(Int64, HELP(int64));
389 GETVALUE(UInt64, HELP(uint64));
390 GETVALUE(IntPtr, HELP(intptr));
391 GETVALUE(UIntPtr, HELP(uintptr));
392 GETVALUE(IntSize, HELP(intsize));
393 GETVALUE(UIntSize, HELP(uintsize));
394 GETVALUE(Short, HELP(short));
395 GETVALUE(UShort, HELP(unsigned short));
396 GETVALUE(Char, HELP(char));
397 GETVALUE(UChar, HELP(unsigned char));
398 GETVALUE(Float, HELP(float));
399 GETVALUE(Double, HELP(double));
400
401 void ComputeExpression(Expression exp);
402
403 void ComputeClassMembers(Class _class, bool isMember)
404 {
405    DataMember member = isMember ? (DataMember) _class : null;
406    Context context = isMember ? null : SetupTemplatesContext(_class);
407    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
408                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
409    {
410       int unionMemberOffset = 0;
411       int bitFields = 0;
412
413       /*
414       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
415          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
416       */
417
418       if(member)
419       {
420          member.memberOffset = 0;
421          if(targetBits < sizeof(void *) * 8)
422             member.structAlignment = 0;
423       }
424       else if(targetBits < sizeof(void *) * 8)
425          _class.structAlignment = 0;
426
427       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
428       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
429          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
430
431       if(!member && _class.destructionWatchOffset)
432          _class.memberOffset += sizeof(OldList);
433
434       // To avoid reentrancy...
435       //_class.structSize = -1;
436
437       {
438          DataMember dataMember;
439          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
440          {
441             if(!dataMember.isProperty)
442             {
443                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
444                {
445                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
446                   /*if(!dataMember.dataType)
447                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
448                      */
449                }
450             }
451          }
452       }
453
454       {
455          DataMember dataMember;
456          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
457          {
458             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
459             {
460                if(!isMember && _class.type == bitClass && dataMember.dataType)
461                {
462                   BitMember bitMember = (BitMember) dataMember;
463                   uint64 mask = 0;
464                   int d;
465
466                   ComputeTypeSize(dataMember.dataType);
467
468                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
469                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
470
471                   _class.memberOffset = bitMember.pos + bitMember.size;
472                   for(d = 0; d<bitMember.size; d++)
473                   {
474                      if(d)
475                         mask <<= 1;
476                      mask |= 1;
477                   }
478                   bitMember.mask = mask << bitMember.pos;
479                }
480                else if(dataMember.type == normalMember && dataMember.dataType)
481                {
482                   int size;
483                   int alignment = 0;
484
485                   // Prevent infinite recursion
486                   if(dataMember.dataType.kind != classType ||
487                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
488                      _class.type != structClass)))
489                      ComputeTypeSize(dataMember.dataType);
490
491                   if(dataMember.dataType.bitFieldCount)
492                   {
493                      bitFields += dataMember.dataType.bitFieldCount;
494                      size = 0;
495                   }
496                   else
497                   {
498                      if(bitFields)
499                      {
500                         int size = (bitFields + 7) / 8;
501
502                         if(isMember)
503                         {
504                            // TESTING THIS PADDING CODE
505                            if(alignment)
506                            {
507                               member.structAlignment = Max(member.structAlignment, alignment);
508
509                               if(member.memberOffset % alignment)
510                                  member.memberOffset += alignment - (member.memberOffset % alignment);
511                            }
512
513                            dataMember.offset = member.memberOffset;
514                            if(member.type == unionMember)
515                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
516                            else
517                            {
518                               member.memberOffset += size;
519                            }
520                         }
521                         else
522                         {
523                            // TESTING THIS PADDING CODE
524                            if(alignment)
525                            {
526                               _class.structAlignment = Max(_class.structAlignment, alignment);
527
528                               if(_class.memberOffset % alignment)
529                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
530                            }
531
532                            dataMember.offset = _class.memberOffset;
533                            _class.memberOffset += size;
534                         }
535                         bitFields = 0;
536                      }
537                      size = dataMember.dataType.size;
538                      alignment = dataMember.dataType.alignment;
539                   }
540
541                   if(isMember)
542                   {
543                      // TESTING THIS PADDING CODE
544                      if(alignment)
545                      {
546                         member.structAlignment = Max(member.structAlignment, alignment);
547
548                         if(member.memberOffset % alignment)
549                            member.memberOffset += alignment - (member.memberOffset % alignment);
550                      }
551
552                      dataMember.offset = member.memberOffset;
553                      if(member.type == unionMember)
554                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
555                      else
556                      {
557                         member.memberOffset += size;
558                      }
559                   }
560                   else
561                   {
562                      // TESTING THIS PADDING CODE
563                      if(alignment)
564                      {
565                         _class.structAlignment = Max(_class.structAlignment, alignment);
566
567                         if(_class.memberOffset % alignment)
568                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
569                      }
570
571                      dataMember.offset = _class.memberOffset;
572                      _class.memberOffset += size;
573                   }
574                }
575                else
576                {
577                   int alignment;
578
579                   ComputeClassMembers((Class)dataMember, true);
580                   alignment = dataMember.structAlignment;
581
582                   if(isMember)
583                   {
584                      if(alignment)
585                      {
586                         if(member.memberOffset % alignment)
587                            member.memberOffset += alignment - (member.memberOffset % alignment);
588
589                         member.structAlignment = Max(member.structAlignment, alignment);
590                      }
591                      dataMember.offset = member.memberOffset;
592                      if(member.type == unionMember)
593                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
594                      else
595                         member.memberOffset += dataMember.memberOffset;
596                   }
597                   else
598                   {
599                      if(alignment)
600                      {
601                         if(_class.memberOffset % alignment)
602                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
603                         _class.structAlignment = Max(_class.structAlignment, alignment);
604                      }
605                      dataMember.offset = _class.memberOffset;
606                      _class.memberOffset += dataMember.memberOffset;
607                   }
608                }
609             }
610          }
611          if(bitFields)
612          {
613             int alignment = 0;
614             int size = (bitFields + 7) / 8;
615
616             if(isMember)
617             {
618                // TESTING THIS PADDING CODE
619                if(alignment)
620                {
621                   member.structAlignment = Max(member.structAlignment, alignment);
622
623                   if(member.memberOffset % alignment)
624                      member.memberOffset += alignment - (member.memberOffset % alignment);
625                }
626
627                if(member.type == unionMember)
628                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
629                else
630                {
631                   member.memberOffset += size;
632                }
633             }
634             else
635             {
636                // TESTING THIS PADDING CODE
637                if(alignment)
638                {
639                   _class.structAlignment = Max(_class.structAlignment, alignment);
640
641                   if(_class.memberOffset % alignment)
642                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
643                }
644                _class.memberOffset += size;
645             }
646             bitFields = 0;
647          }
648       }
649       if(member && member.type == unionMember)
650       {
651          member.memberOffset = unionMemberOffset;
652       }
653
654       if(!isMember)
655       {
656          /*if(_class.type == structClass)
657             _class.size = _class.memberOffset;
658          else
659          */
660
661          if(_class.type != bitClass)
662          {
663             int extra = 0;
664             if(_class.structAlignment)
665             {
666                if(_class.memberOffset % _class.structAlignment)
667                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
668             }
669             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
670             if(!member)
671             {
672                Property prop;
673                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
674                {
675                   if(prop.isProperty && prop.isWatchable)
676                   {
677                      prop.watcherOffset = _class.structSize;
678                      _class.structSize += sizeof(OldList);
679                   }
680                }
681             }
682
683             // Fix Derivatives
684             {
685                OldLink derivative;
686                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
687                {
688                   Class deriv = derivative.data;
689
690                   if(deriv.computeSize)
691                   {
692                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
693                      deriv.offset = /*_class.offset + */_class.structSize;
694                      deriv.memberOffset = 0;
695                      // ----------------------
696
697                      deriv.structSize = deriv.offset;
698
699                      ComputeClassMembers(deriv, false);
700                   }
701                }
702             }
703          }
704       }
705    }
706    if(context)
707       FinishTemplatesContext(context);
708 }
709
710 public void ComputeModuleClasses(Module module)
711 {
712    Class _class;
713    OldLink subModule;
714
715    for(subModule = module.modules.first; subModule; subModule = subModule.next)
716       ComputeModuleClasses(subModule.data);
717    for(_class = module.classes.first; _class; _class = _class.next)
718       ComputeClassMembers(_class, false);
719 }
720
721
722 public int ComputeTypeSize(Type type)
723 {
724    uint size = type ? type.size : 0;
725    if(!size && type && !type.computing)
726    {
727       type.computing = true;
728       switch(type.kind)
729       {
730          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
731          case charType: type.alignment = size = sizeof(char); break;
732          case intType: type.alignment = size = sizeof(int); break;
733          case int64Type: type.alignment = size = sizeof(int64); break;
734          case intPtrType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
735          case intSizeType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
736          case longType: type.alignment = size = sizeof(long); break;
737          case shortType: type.alignment = size = sizeof(short); break;
738          case floatType: type.alignment = size = sizeof(float); break;
739          case doubleType: type.alignment = size = sizeof(double); break;
740          case classType:
741          {
742             Class _class = type._class ? type._class.registered : null;
743
744             if(_class && _class.type == structClass)
745             {
746                // Ensure all members are properly registered
747                ComputeClassMembers(_class, false);
748                type.alignment = _class.structAlignment;
749                type.pointerAlignment = (bool)_class.pointerAlignment;
750                size = _class.structSize;
751                if(type.alignment && size % type.alignment)
752                   size += type.alignment - (size % type.alignment);
753
754             }
755             else if(_class && (_class.type == unitClass ||
756                    _class.type == enumClass ||
757                    _class.type == bitClass))
758             {
759                if(!_class.dataType)
760                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
761                size = type.alignment = ComputeTypeSize(_class.dataType);
762             }
763             else
764             {
765                size = type.alignment = targetBits / 8; // sizeof(Instance *);
766                type.pointerAlignment = true;
767             }
768             break;
769          }
770          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */ type.pointerAlignment = true; break;
771          case arrayType:
772             if(type.arraySizeExp)
773             {
774                ProcessExpressionType(type.arraySizeExp);
775                ComputeExpression(type.arraySizeExp);
776                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
777                   type.arraySizeExp.expType.kind != shortType &&
778                   type.arraySizeExp.expType.kind != charType &&
779                   type.arraySizeExp.expType.kind != longType &&
780                   type.arraySizeExp.expType.kind != int64Type &&
781                   type.arraySizeExp.expType.kind != intSizeType &&
782                   type.arraySizeExp.expType.kind != intPtrType &&
783                   type.arraySizeExp.expType.kind != enumType &&
784                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
785                {
786                   Location oldLoc = yylloc;
787                   // bool isConstant = type.arraySizeExp.isConstant;
788                   char expression[10240];
789                   expression[0] = '\0';
790                   type.arraySizeExp.expType = null;
791                   yylloc = type.arraySizeExp.loc;
792                   if(inCompiler)
793                      PrintExpression(type.arraySizeExp, expression);
794                   Compiler_Error($"Array size not constant int (%s)\n", expression);
795                   yylloc = oldLoc;
796                }
797                GetInt(type.arraySizeExp, &type.arraySize);
798             }
799             else if(type.enumClass)
800             {
801                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
802                {
803                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
804                }
805                else
806                   type.arraySize = 0;
807             }
808             else
809             {
810                // Unimplemented auto size
811                type.arraySize = 0;
812             }
813
814             size = ComputeTypeSize(type.type) * type.arraySize;
815             if(type.type)
816             {
817                type.alignment = type.type.alignment;
818                type.pointerAlignment = type.type.pointerAlignment;
819             }
820
821             break;
822          case structType:
823          {
824             if(!type.members.first && type.enumName)
825             {
826                Symbol symbol = FindStruct(curContext, type.enumName);
827                if(symbol && symbol.type)
828                {
829                   ComputeTypeSize(symbol.type);
830                   size = symbol.type.size;
831                }
832             }
833             else
834             {
835                Type member;
836                for(member = type.members.first; member; member = member.next)
837                {
838                   uint addSize = ComputeTypeSize(member);
839
840                   member.offset = size;
841                   if(member.alignment && size % member.alignment)
842                      member.offset += member.alignment - (size % member.alignment);
843                   size = member.offset;
844
845                   if(member.pointerAlignment && type.size <= 4)
846                      type.pointerAlignment = true;
847                   else if(!member.pointerAlignment && member.alignment >= 8)
848                      type.pointerAlignment = false;
849
850                   type.alignment = Max(type.alignment, member.alignment);
851                   size += addSize;
852                }
853                if(type.alignment && size % type.alignment)
854                   size += type.alignment - (size % type.alignment);
855             }
856             break;
857          }
858          case unionType:
859          {
860             if(!type.members.first && type.enumName)
861             {
862                Symbol symbol = FindStruct(curContext, type.enumName);
863                if(symbol && symbol.type)
864                {
865                   ComputeTypeSize(symbol.type);
866                   size = symbol.type.size;
867                   type.alignment = symbol.type.alignment;
868                }
869             }
870             else
871             {
872                Type member;
873                for(member = type.members.first; member; member = member.next)
874                {
875                   uint addSize = ComputeTypeSize(member);
876
877                   member.offset = size;
878                   if(member.alignment && size % member.alignment)
879                      member.offset += member.alignment - (size % member.alignment);
880                   size = member.offset;
881
882                   if(member.pointerAlignment && type.size <= 4)
883                      type.pointerAlignment = true;
884                   else if(!member.pointerAlignment && member.alignment >= 8)
885                      type.pointerAlignment = false;
886
887                   type.alignment = Max(type.alignment, member.alignment);
888
889                   size = Max(size, addSize);
890                }
891                if(type.alignment && size % type.alignment)
892                   size += type.alignment - (size % type.alignment);
893             }
894             break;
895          }
896          case templateType:
897          {
898             TemplateParameter param = type.templateParameter;
899             Type baseType = ProcessTemplateParameterType(param);
900             if(baseType)
901             {
902                size = ComputeTypeSize(baseType);
903                type.alignment = baseType.alignment;
904                type.pointerAlignment = baseType.pointerAlignment;
905             }
906             else
907                type.alignment = size = sizeof(uint64);
908             break;
909          }
910          case enumType:
911          {
912             type.alignment = size = sizeof(enum { test });
913             break;
914          }
915          case thisClassType:
916          {
917             type.alignment = size = targetBits / 8; //sizeof(void *);
918             type.pointerAlignment = true;
919             break;
920          }
921       }
922       type.size = size;
923       type.computing = false;
924    }
925    return size;
926 }
927
928
929 /*static */int AddMembers(External neededBy, OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
930 {
931    // This function is in need of a major review when implementing private members etc.
932    DataMember topMember = isMember ? (DataMember) _class : null;
933    uint totalSize = 0;
934    uint maxSize = 0;
935    int alignment;
936    uint size;
937    DataMember member;
938    int anonID = 1;
939    Context context = isMember ? null : SetupTemplatesContext(_class);
940    if(addedPadding)
941       *addedPadding = false;
942
943    if(!isMember && _class.base)
944    {
945       maxSize = _class.structSize;
946       //if(_class.base.type != systemClass) // Commented out with new Instance _class
947       {
948          // DANGER: Testing this noHeadClass here...
949          if(_class.type == structClass || _class.type == noHeadClass)
950             /*totalSize = */AddMembers(neededBy, declarations, _class.base, false, &totalSize, topClass, null);
951          else
952          {
953             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
954             if(maxSize > baseSize)
955                maxSize -= baseSize;
956             else
957                maxSize = 0;
958          }
959       }
960    }
961
962    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
963    {
964       if(!member.isProperty)
965       {
966          switch(member.type)
967          {
968             case normalMember:
969             {
970                if(member.dataTypeString)
971                {
972                   OldList * specs = MkList(), * decls = MkList();
973                   Declarator decl;
974
975                   decl = SpecDeclFromString(member.dataTypeString, specs,
976                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
977                   ListAdd(decls, MkStructDeclarator(decl, null));
978                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
979
980                   if(!member.dataType)
981                      member.dataType = ProcessType(specs, decl);
982
983                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
984
985                   {
986                      Type type = ProcessType(specs, decl);
987                      DeclareType(neededBy, member.dataType, true, false);
988                      FreeType(type);
989                   }
990                   /*
991                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
992                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
993                      DeclareStruct(member.dataType._class.string, false);
994                   */
995
996                   ComputeTypeSize(member.dataType);
997                   size = member.dataType.size;
998                   alignment = member.dataType.alignment;
999
1000                   if(alignment)
1001                   {
1002                      if(totalSize % alignment)
1003                         totalSize += alignment - (totalSize % alignment);
1004                   }
1005                   totalSize += size;
1006                }
1007                break;
1008             }
1009             case unionMember:
1010             case structMember:
1011             {
1012                OldList * specs = MkList(), * list = MkList();
1013                char id[100];
1014                sprintf(id, "__anon%d", anonID++);
1015
1016                size = 0;
1017                AddMembers(neededBy, list, (Class)member, true, &size, topClass, null);
1018                ListAdd(specs,
1019                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
1020                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
1021                alignment = member.structAlignment;
1022
1023                if(alignment)
1024                {
1025                   if(totalSize % alignment)
1026                      totalSize += alignment - (totalSize % alignment);
1027                }
1028                totalSize += size;
1029                break;
1030             }
1031          }
1032       }
1033    }
1034    if(retSize)
1035    {
1036       if(topMember && topMember.type == unionMember)
1037          *retSize = Max(*retSize, totalSize);
1038       else
1039          *retSize += totalSize;
1040    }
1041    else if(totalSize < maxSize && _class.type != systemClass)
1042    {
1043       int autoPadding = 0;
1044       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1045          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1046       if(totalSize + autoPadding < maxSize)
1047       {
1048          char sizeString[50];
1049          sprintf(sizeString, "%d", maxSize - totalSize);
1050          ListAdd(declarations,
1051             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1052             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1053          if(addedPadding)
1054             *addedPadding = true;
1055       }
1056    }
1057    if(context)
1058       FinishTemplatesContext(context);
1059    return topMember ? topMember.memberID : _class.memberID;
1060 }
1061
1062 static int DeclareMembers(External neededBy, Class _class, bool isMember)
1063 {
1064    DataMember topMember = isMember ? (DataMember) _class : null;
1065    DataMember member;
1066    Context context = isMember ? null : SetupTemplatesContext(_class);
1067
1068    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1069       DeclareMembers(neededBy, _class.base, false);
1070
1071    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1072    {
1073       if(!member.isProperty)
1074       {
1075          switch(member.type)
1076          {
1077             case normalMember:
1078             {
1079                if(!member.dataType && member.dataTypeString)
1080                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1081                if(member.dataType)
1082                   DeclareType(neededBy, member.dataType, true, false);
1083                break;
1084             }
1085             case unionMember:
1086             case structMember:
1087             {
1088                DeclareMembers(neededBy, (Class)member, true);
1089                break;
1090             }
1091          }
1092       }
1093    }
1094    if(context)
1095       FinishTemplatesContext(context);
1096
1097    return topMember ? topMember.memberID : _class.memberID;
1098 }
1099
1100 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1101 {
1102    ClassDef def;
1103    int anonID = 1;
1104    for(def = definitions->first; def; def = def.next)
1105    {
1106       if(def.type == declarationClassDef)
1107       {
1108          Declaration decl = def.decl;
1109          if(decl && decl.specifiers)
1110          {
1111             Specifier spec;
1112             bool isStruct = false;
1113             for(spec = decl.specifiers->first; spec; spec = spec.next)
1114             {
1115                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1116                {
1117                   if(spec.definitions)
1118                      IdentifyAnonStructs(spec.definitions);
1119                   isStruct = true;
1120                }
1121             }
1122             if(isStruct)
1123             {
1124                Declarator d = null;
1125                if(decl.declarators)
1126                {
1127                   for(d = decl.declarators->first; d; d = d.next)
1128                   {
1129                      Identifier idDecl = GetDeclId(d);
1130                      if(idDecl)
1131                         break;
1132                   }
1133                }
1134                if(!d)
1135                {
1136                   char id[100];
1137                   sprintf(id, "__anon%d", anonID++);
1138                   if(!decl.declarators)
1139                      decl.declarators = MkList();
1140                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1141                }
1142             }
1143          }
1144       }
1145    }
1146 }
1147
1148 External DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference)
1149 {
1150    return _DeclareStruct(neededBy, name, skipNoHead, needDereference, false);
1151 }
1152
1153 External _DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference, bool fwdDecl)
1154 {
1155    External external = null;
1156    Symbol classSym = FindClass(name);
1157    OldList * curDeclarations = null;
1158    Specifier curSpec = null;
1159
1160    if(!inCompiler || !classSym) return null;
1161
1162    // We don't need any declaration for bit classes...
1163    if(classSym.registered &&
1164       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1165       return null;
1166
1167    if(!classSym.registered || (classSym.registered.type == normalClass && classSym.registered.structSize && classSym.registered.base && classSym.registered.base.base))
1168       _DeclareStruct(neededBy, "ecere::com::Instance", false, true, fwdDecl);
1169
1170    external = classSym.structExternal;
1171
1172    if(external && external.declaration)
1173    {
1174       Specifier spec;
1175       for(spec = external.declaration.specifiers ? external.declaration.specifiers->first : null; spec; spec = spec.next)
1176          if(spec.type == structSpecifier || spec.type == unionSpecifier)
1177          {
1178             curSpec = spec;
1179             curDeclarations = spec.definitions;
1180             break;
1181          }
1182    }
1183
1184    if(classSym.registered && !classSym.declaring && classSym.imported && (!classSym.declaredStructSym || (classSym.registered.type == noHeadClass && !skipNoHead && external && !curDeclarations)))
1185    {
1186       OldList * specifiers, * declarators;
1187       OldList * declarations = null;
1188       char structName[1024];
1189       bool addedPadding = false;
1190
1191       classSym.declaring++;
1192
1193       if(strchr(classSym.string, '<'))
1194       {
1195          if(classSym.registered.templateClass)
1196          {
1197             external = _DeclareStruct(neededBy, classSym.registered.templateClass.fullName, skipNoHead, needDereference, fwdDecl);
1198             classSym.declaring--;
1199          }
1200          return external;
1201       }
1202
1203       structName[0] = 0;
1204       FullClassNameCat(structName, name, false);
1205
1206       classSym.declaredStructSym = true;
1207       if(!external || (classSym.registered.type == noHeadClass && !skipNoHead && !curDeclarations))
1208       {
1209          bool add = false;
1210          if(!external)
1211          {
1212             external = MkExternalDeclaration(null);
1213             classSym.structExternal = external;
1214             external.symbol = classSym;
1215
1216             add = true;
1217          }
1218
1219          if(!skipNoHead)
1220          {
1221             declarations = MkList();
1222             AddMembers(external, declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1223          }
1224
1225          if(declarations && (!declarations->count || (declarations->count == 1 && addedPadding)))
1226          {
1227             FreeList(declarations, FreeClassDef);
1228             declarations = null;
1229          }
1230
1231          if(classSym.registered.type != noHeadClass && !declarations)
1232          {
1233             FreeExternal(external);
1234             external = null;
1235             classSym.structExternal = null;
1236          }
1237          else
1238          {
1239             if(curSpec)
1240                curSpec.definitions = declarations;
1241             else
1242             {
1243                char className[1024];
1244                strcpy(className, "__ecereClass_");
1245                FullClassNameCat(className, classSym.string, true);
1246
1247                specifiers = MkList();
1248                declarators = MkList();
1249                ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1250                external.declaration = MkDeclaration(specifiers, declarators);
1251             }
1252             if(add)
1253                ast->Add(external);
1254          }
1255       }
1256       classSym.declaring--;
1257    }
1258    else if(!classSym.declaredStructSym && classSym.structExternal)
1259    {
1260       classSym.declaredStructSym = true;
1261
1262       if(classSym.registered)
1263          DeclareMembers(classSym.structExternal, classSym.registered, false);
1264
1265       if(classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1266       {
1267          Specifier spec;
1268          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1269          {
1270             if(spec.definitions)
1271                IdentifyAnonStructs(spec.definitions);
1272          }
1273       }
1274    }
1275    if(inCompiler && neededBy && (external || !classSym.imported))
1276    {
1277       if(!external)
1278       {
1279          classSym.structExternal = external = MkExternalDeclaration(null);
1280          external.symbol = classSym;
1281          ast->Add(external);
1282       }
1283       if(fwdDecl)
1284       {
1285          External e = external.fwdDecl ? external.fwdDecl : external;
1286          if(e.incoming.count)
1287             neededBy.CreateUniqueEdge(e, !needDereference && !external.fwdDecl);
1288       }
1289       else
1290          neededBy.CreateUniqueEdge(external, !needDereference);
1291    }
1292    return external;
1293 }
1294
1295 void DeclareProperty(External neededBy, Property prop, char * setName, char * getName)
1296 {
1297    Symbol symbol = prop.symbol;
1298    bool imported = false;
1299    bool dllImport = false;
1300    External structExternal = null;
1301    External instExternal = null;
1302
1303    strcpy(setName, "__ecereProp_");
1304    FullClassNameCat(setName, prop._class.fullName, false);
1305    strcat(setName, "_Set_");
1306    FullClassNameCat(setName, prop.name, true);
1307
1308    strcpy(getName, "__ecereProp_");
1309    FullClassNameCat(getName, prop._class.fullName, false);
1310    strcat(getName, "_Get_");
1311    FullClassNameCat(getName, prop.name, true);
1312
1313    if(!symbol || symbol._import)
1314    {
1315       if(!symbol)
1316       {
1317          Symbol classSym;
1318
1319          if(!prop._class.symbol)
1320             prop._class.symbol = FindClass(prop._class.fullName);
1321          classSym = prop._class.symbol;
1322          if(classSym && !classSym._import)
1323          {
1324             ModuleImport module;
1325
1326             if(prop._class.module)
1327                module = FindModule(prop._class.module);
1328             else
1329                module = mainModule;
1330
1331             classSym._import = ClassImport
1332             {
1333                name = CopyString(prop._class.fullName);
1334                isRemote = prop._class.isRemote;
1335             };
1336             module.classes.Add(classSym._import);
1337          }
1338          symbol = prop.symbol = Symbol { };
1339          symbol._import = (ClassImport)PropertyImport
1340          {
1341             name = CopyString(prop.name);
1342             isVirtual = false; //prop.isVirtual;
1343             hasSet = prop.Set ? true : false;
1344             hasGet = prop.Get ? true : false;
1345          };
1346          if(classSym)
1347             classSym._import.properties.Add(symbol._import);
1348       }
1349       imported = true;
1350       // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1351       if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1352          prop._class.module.importType != staticImport)
1353          dllImport = true;
1354    }
1355
1356    if(!symbol.type)
1357    {
1358       Context context = SetupTemplatesContext(prop._class);
1359       symbol.type = ProcessTypeString(prop.dataTypeString, false);
1360       FinishTemplatesContext(context);
1361    }
1362
1363    if((prop.Get && !symbol.externalGet) || (prop.Set && !symbol.externalSet))
1364    {
1365       if(prop._class.type == normalClass && prop._class.structSize)
1366          instExternal = DeclareStruct(null, "ecere::com::Instance", false, true);
1367       structExternal = DeclareStruct(null, prop._class.fullName, prop._class.type != structClass /*true*/, false);
1368    }
1369
1370    // Get
1371    if(prop.Get && !symbol.externalGet)
1372    {
1373       Declaration decl;
1374       OldList * specifiers, * declarators;
1375       Declarator d;
1376       OldList * params;
1377       Specifier spec = null;
1378       External external;
1379       Declarator typeDecl;
1380       bool simple = false;
1381       bool needReference;
1382
1383       specifiers = MkList();
1384       declarators = MkList();
1385       params = MkList();
1386
1387       ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1388          MkDeclaratorIdentifier(MkIdentifier("this"))));
1389
1390       d = MkDeclaratorIdentifier(MkIdentifier(getName));
1391       //if(imported)
1392       if(dllImport)
1393          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1394
1395       {
1396          Context context = SetupTemplatesContext(prop._class);
1397          typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1398          FinishTemplatesContext(context);
1399       }
1400
1401       // Make sure the simple _class's type is declared
1402       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1403       for(spec = specifiers->first; spec; spec = spec.next)
1404       {
1405          if(spec.type == nameSpecifier)
1406          {
1407             Symbol classSym = spec.symbol;
1408             if(needReference)
1409             {
1410                symbol._class = classSym.registered;
1411                if(classSym.registered && classSym.registered.type == structClass)
1412                   simple = true;
1413             }
1414             break;
1415          }
1416       }
1417
1418       if(!simple)
1419          d = PlugDeclarator(typeDecl, d);
1420       else
1421       {
1422          ListAdd(params, MkTypeName(specifiers,
1423             PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1424          specifiers = MkList();
1425       }
1426
1427       d = MkDeclaratorFunction(d, params);
1428
1429       //if(imported)
1430       if(dllImport)
1431          specifiers->Insert(null, MkSpecifier(EXTERN));
1432       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1433          specifiers->Insert(null, MkSpecifier(STATIC));
1434       if(simple)
1435          ListAdd(specifiers, MkSpecifier(VOID));
1436
1437       ListAdd(declarators, MkInitDeclarator(d, null));
1438
1439       decl = MkDeclaration(specifiers, declarators);
1440
1441       external = MkExternalDeclaration(decl);
1442
1443       if(structExternal)
1444          external.CreateEdge(structExternal, false);
1445       if(instExternal)
1446          external.CreateEdge(instExternal, false);
1447
1448       if(spec)
1449          DeclareStruct(external, spec.name, false, needReference);
1450
1451       ast->Add(external);
1452       external.symbol = symbol;
1453       symbol.externalGet = external;
1454
1455       ReplaceThisClassSpecifiers(specifiers, prop._class);
1456
1457       if(typeDecl)
1458          FreeDeclarator(typeDecl);
1459    }
1460
1461    // Set
1462    if(prop.Set && !symbol.externalSet)
1463    {
1464       Declaration decl;
1465       OldList * specifiers, * declarators;
1466       Declarator d;
1467       OldList * params;
1468       Specifier spec = null;
1469       External external;
1470       Declarator typeDecl;
1471       bool needReference;
1472
1473       declarators = MkList();
1474       params = MkList();
1475
1476       if(!prop.conversion || prop._class.type == structClass)
1477       {
1478          ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1479             MkDeclaratorIdentifier(MkIdentifier("this"))));
1480       }
1481
1482       specifiers = MkList();
1483
1484       {
1485          Context context = SetupTemplatesContext(prop._class);
1486          typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1487             MkDeclaratorIdentifier(MkIdentifier("value")));
1488          FinishTemplatesContext(context);
1489       }
1490       if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1491          specifiers->Insert(null, MkSpecifier(CONST));
1492
1493       ListAdd(params, MkTypeName(specifiers, d));
1494
1495       d = MkDeclaratorIdentifier(MkIdentifier(setName));
1496       if(dllImport)
1497          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1498       d = MkDeclaratorFunction(d, params);
1499
1500       // Make sure the simple _class's type is declared
1501       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1502       for(spec = specifiers->first; spec; spec = spec.next)
1503       {
1504          if(spec.type == nameSpecifier)
1505          {
1506             Symbol classSym = spec.symbol;
1507             if(needReference)
1508                symbol._class = classSym.registered;
1509             break;
1510          }
1511       }
1512
1513       ListAdd(declarators, MkInitDeclarator(d, null));
1514
1515       specifiers = MkList();
1516       if(dllImport)
1517          specifiers->Insert(null, MkSpecifier(EXTERN));
1518       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1519          specifiers->Insert(null, MkSpecifier(STATIC));
1520
1521       if(!prop.conversion || prop._class.type == structClass)
1522          ListAdd(specifiers, MkSpecifier(VOID));
1523       else
1524          ListAdd(specifiers, MkSpecifierName(prop._class.fullName));
1525
1526       decl = MkDeclaration(specifiers, declarators);
1527
1528       external = MkExternalDeclaration(decl);
1529
1530       if(structExternal)
1531          external.CreateEdge(structExternal, false);
1532       if(instExternal)
1533          external.CreateEdge(instExternal, false);
1534
1535       if(spec)
1536          DeclareStruct(external, spec.name, false, needReference);
1537
1538       ast->Add(external);
1539       external.symbol = symbol;
1540       symbol.externalSet = external;
1541
1542       ReplaceThisClassSpecifiers(specifiers, prop._class);
1543    }
1544
1545    // Property (for Watchers)
1546    if(!symbol.externalPtr)
1547    {
1548       Declaration decl;
1549       External external;
1550       OldList * specifiers = MkList();
1551       char propName[1024];
1552
1553       if(imported)
1554          specifiers->Insert(null, MkSpecifier(EXTERN));
1555       else
1556       {
1557          specifiers->Insert(null, MkSpecifier(STATIC));
1558          specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1559       }
1560
1561       ListAdd(specifiers, MkSpecifierName("Property"));
1562
1563       strcpy(propName, "__ecereProp_");
1564       FullClassNameCat(propName, prop._class.fullName, false);
1565       strcat(propName, "_");
1566       FullClassNameCat(propName, prop.name, true);
1567
1568       {
1569          OldList * list = MkList();
1570          ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1571
1572          if(!imported)
1573          {
1574             strcpy(propName, "__ecerePropM_");
1575             FullClassNameCat(propName, prop._class.fullName, false);
1576             strcat(propName, "_");
1577             FullClassNameCat(propName, prop.name, true);
1578
1579             ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1580          }
1581          decl = MkDeclaration(specifiers, list);
1582       }
1583
1584       external = MkExternalDeclaration(decl);
1585       ast->Insert(curExternal.prev, external);
1586       external.symbol = symbol;
1587       symbol.externalPtr = external;
1588    }
1589
1590    if(inCompiler && neededBy)
1591    {
1592       // Could improve this to create edge on only what is needed...
1593       if(symbol.externalPtr)
1594          neededBy.CreateUniqueEdge(symbol.externalPtr, false);
1595
1596       if(symbol.externalGet)
1597          neededBy.CreateUniqueEdge(symbol.externalGet, symbol.externalGet.type == functionExternal);
1598
1599       if(symbol.externalSet)
1600          neededBy.CreateUniqueEdge(symbol.externalSet, symbol.externalSet.type == functionExternal);
1601
1602       // IsSet ?
1603    }
1604 }
1605
1606 // ***************** EXPRESSION PROCESSING ***************************
1607 public Type Dereference(Type source)
1608 {
1609    Type type = null;
1610    if(source)
1611    {
1612       if(source.kind == pointerType || source.kind == arrayType)
1613       {
1614          type = source.type;
1615          source.type.refCount++;
1616       }
1617       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1618       {
1619          type = Type
1620          {
1621             kind = charType;
1622             refCount = 1;
1623          };
1624       }
1625       // Support dereferencing of no head classes for now...
1626       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1627       {
1628          type = source;
1629          source.refCount++;
1630       }
1631       else
1632          Compiler_Error($"cannot dereference type\n");
1633    }
1634    return type;
1635 }
1636
1637 static Type Reference(Type source)
1638 {
1639    Type type = null;
1640    if(source)
1641    {
1642       type = Type
1643       {
1644          kind = pointerType;
1645          type = source;
1646          refCount = 1;
1647       };
1648       source.refCount++;
1649    }
1650    return type;
1651 }
1652
1653 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1654 {
1655    Identifier ident = member.identifiers ? member.identifiers->first : null;
1656    bool found = false;
1657    DataMember dataMember = null;
1658    Method method = null;
1659    bool freeType = false;
1660
1661    yylloc = member.loc;
1662
1663    if(!ident)
1664    {
1665       if(curMember)
1666       {
1667          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1668          if(*curMember)
1669          {
1670             found = true;
1671             dataMember = *curMember;
1672          }
1673       }
1674    }
1675    else
1676    {
1677       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1678       DataMember _subMemberStack[256];
1679       int _subMemberStackPos = 0;
1680
1681       // FILL MEMBER STACK
1682       if(!thisMember)
1683          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1684       if(thisMember)
1685       {
1686          dataMember = thisMember;
1687          if(curMember && thisMember.memberAccess == publicAccess)
1688          {
1689             *curMember = thisMember;
1690             *curClass = thisMember._class;
1691             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1692             *subMemberStackPos = _subMemberStackPos;
1693          }
1694          found = true;
1695       }
1696       else
1697       {
1698          // Setting a method
1699          method = eClass_FindMethod(_class, ident.string, privateModule);
1700          if(method && method.type == virtualMethod)
1701             found = true;
1702          else
1703             method = null;
1704       }
1705    }
1706
1707    if(found)
1708    {
1709       Type type = null;
1710       if(dataMember)
1711       {
1712          if(!dataMember.dataType && dataMember.dataTypeString)
1713          {
1714             //Context context = SetupTemplatesContext(dataMember._class);
1715             Context context = SetupTemplatesContext(_class);
1716             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1717             FinishTemplatesContext(context);
1718          }
1719          type = dataMember.dataType;
1720       }
1721       else if(method)
1722       {
1723          // This is for destination type...
1724          if(!method.dataType)
1725             ProcessMethodType(method);
1726          //DeclareMethod(method);
1727          // method.dataType = ((Symbol)method.symbol)->type;
1728          type = method.dataType;
1729       }
1730
1731       if(ident && ident.next)
1732       {
1733          for(ident = ident.next; ident && type; ident = ident.next)
1734          {
1735             if(type.kind == classType)
1736             {
1737                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1738                if(!dataMember)
1739                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1740                if(dataMember)
1741                   type = dataMember.dataType;
1742             }
1743             else if(type.kind == structType || type.kind == unionType)
1744             {
1745                Type memberType;
1746                for(memberType = type.members.first; memberType; memberType = memberType.next)
1747                {
1748                   if(!strcmp(memberType.name, ident.string))
1749                   {
1750                      type = memberType;
1751                      break;
1752                   }
1753                }
1754             }
1755          }
1756       }
1757
1758       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1759       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1760       {
1761          int id = 0;
1762          ClassTemplateParameter curParam = null;
1763          Class sClass;
1764          for(sClass = _class; sClass; sClass = sClass.base)
1765          {
1766             id = 0;
1767             if(sClass.templateClass) sClass = sClass.templateClass;
1768             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1769             {
1770                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1771                {
1772                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1773                   {
1774                      if(sClass.templateClass) sClass = sClass.templateClass;
1775                      id += sClass.templateParams.count;
1776                   }
1777                   break;
1778                }
1779                id++;
1780             }
1781             if(curParam) break;
1782          }
1783
1784          if(curParam)
1785          {
1786             ClassTemplateArgument arg = _class.templateArgs[id];
1787             if(arg.dataTypeString)
1788             {
1789                bool constant = type.constant;
1790                // FreeType(type);
1791                type = ProcessTypeString(arg.dataTypeString, false);
1792                if(type.kind == classType && constant) type.constant = true;
1793                else if(type.kind == pointerType)
1794                {
1795                   Type t = type.type;
1796                   while(t.kind == pointerType) t = t.type;
1797                   if(constant) t.constant = constant;
1798                }
1799                freeType = true;
1800                if(type && _class.templateClass)
1801                   type.passAsTemplate = true;
1802                if(type)
1803                {
1804                   // type.refCount++;
1805                   /*if(!exp.destType)
1806                   {
1807                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1808                      exp.destType.refCount++;
1809                   }*/
1810                }
1811             }
1812          }
1813       }
1814       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1815       {
1816          Class expClass = type._class.registered;
1817          Class cClass = null;
1818          int paramCount = 0;
1819          int lastParam = -1;
1820
1821          char templateString[1024];
1822          ClassTemplateParameter param;
1823          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1824          for(cClass = expClass; cClass; cClass = cClass.base)
1825          {
1826             int p = 0;
1827             if(cClass.templateClass) cClass = cClass.templateClass;
1828             for(param = cClass.templateParams.first; param; param = param.next)
1829             {
1830                int id = p;
1831                Class sClass;
1832                ClassTemplateArgument arg;
1833                for(sClass = cClass.base; sClass; sClass = sClass.base)
1834                {
1835                   if(sClass.templateClass) sClass = sClass.templateClass;
1836                   id += sClass.templateParams.count;
1837                }
1838                arg = expClass.templateArgs[id];
1839
1840                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1841                {
1842                   ClassTemplateParameter cParam;
1843                   //int p = numParams - sClass.templateParams.count;
1844                   int p = 0;
1845                   Class nextClass;
1846                   if(sClass.templateClass) sClass = sClass.templateClass;
1847
1848                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1849                   {
1850                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1851                      p += nextClass.templateParams.count;
1852                   }
1853
1854                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1855                   {
1856                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1857                      {
1858                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1859                         {
1860                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1861                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1862                            break;
1863                         }
1864                      }
1865                   }
1866                }
1867
1868                {
1869                   char argument[256];
1870                   argument[0] = '\0';
1871                   /*if(arg.name)
1872                   {
1873                      strcat(argument, arg.name.string);
1874                      strcat(argument, " = ");
1875                   }*/
1876                   switch(param.type)
1877                   {
1878                      case expression:
1879                      {
1880                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1881                         char expString[1024];
1882                         OldList * specs = MkList();
1883                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1884                         Expression exp;
1885                         char * string = PrintHexUInt64(arg.expression.ui64);
1886                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1887                         delete string;
1888
1889                         ProcessExpressionType(exp);
1890                         ComputeExpression(exp);
1891                         expString[0] = '\0';
1892                         PrintExpression(exp, expString);
1893                         strcat(argument, expString);
1894                         //delete exp;
1895                         FreeExpression(exp);
1896                         break;
1897                      }
1898                      case identifier:
1899                      {
1900                         strcat(argument, arg.member.name);
1901                         break;
1902                      }
1903                      case TemplateParameterType::type:
1904                      {
1905                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1906                            strcat(argument, arg.dataTypeString);
1907                         break;
1908                      }
1909                   }
1910                   if(argument[0])
1911                   {
1912                      if(paramCount) strcat(templateString, ", ");
1913                      if(lastParam != p - 1)
1914                      {
1915                         strcat(templateString, param.name);
1916                         strcat(templateString, " = ");
1917                      }
1918                      strcat(templateString, argument);
1919                      paramCount++;
1920                      lastParam = p;
1921                   }
1922                   p++;
1923                }
1924             }
1925          }
1926          {
1927             int len = strlen(templateString);
1928             if(templateString[len-1] == '<')
1929                len--;
1930             else
1931             {
1932                if(templateString[len-1] == '>')
1933                   templateString[len++] = ' ';
1934                templateString[len++] = '>';
1935             }
1936             templateString[len++] = '\0';
1937          }
1938          {
1939             Context context = SetupTemplatesContext(_class);
1940             if(freeType) FreeType(type);
1941             type = ProcessTypeString(templateString, false);
1942             freeType = true;
1943             FinishTemplatesContext(context);
1944          }
1945       }
1946
1947       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1948       {
1949          ProcessExpressionType(member.initializer.exp);
1950          if(!member.initializer.exp.expType)
1951          {
1952             if(inCompiler)
1953             {
1954                char expString[10240];
1955                expString[0] = '\0';
1956                PrintExpression(member.initializer.exp, expString);
1957                ChangeCh(expString, '\n', ' ');
1958                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1959             }
1960          }
1961          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1962          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1963          {
1964             Compiler_Error($"incompatible instance method %s\n", ident.string);
1965          }
1966       }
1967       else if(member.initializer)
1968       {
1969          /*
1970          FreeType(member.exp.destType);
1971          member.exp.destType = type;
1972          if(member.exp.destType)
1973             member.exp.destType.refCount++;
1974          ProcessExpressionType(member.exp);
1975          */
1976
1977          ProcessInitializer(member.initializer, type);
1978       }
1979       if(freeType) FreeType(type);
1980    }
1981    else
1982    {
1983       if(_class && _class.type == unitClass)
1984       {
1985          if(member.initializer)
1986          {
1987             /*
1988             FreeType(member.exp.destType);
1989             member.exp.destType = MkClassType(_class.fullName);
1990             ProcessExpressionType(member.initializer, type);
1991             */
1992             Type type = MkClassType(_class.fullName);
1993             ProcessInitializer(member.initializer, type);
1994             FreeType(type);
1995          }
1996       }
1997       else
1998       {
1999          if(member.initializer)
2000          {
2001             //ProcessExpressionType(member.exp);
2002             ProcessInitializer(member.initializer, null);
2003          }
2004          if(ident)
2005          {
2006             if(method)
2007             {
2008                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2009             }
2010             else if(_class)
2011             {
2012                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2013                if(inCompiler)
2014                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2015             }
2016          }
2017          else if(_class)
2018             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2019       }
2020    }
2021 }
2022
2023 void ProcessInstantiationType(Instantiation inst)
2024 {
2025    yylloc = inst.loc;
2026    if(inst._class)
2027    {
2028       MembersInit members;
2029       Symbol classSym;
2030       Class _class;
2031
2032       classSym = inst._class.symbol;
2033       _class = classSym ? classSym.registered : null;
2034
2035       if(!_class || _class.type != noHeadClass)
2036          DeclareStruct(curExternal, inst._class.name, false, true);
2037
2038       afterExternal = afterExternal ? afterExternal : curExternal;
2039
2040       if(inst.exp)
2041          ProcessExpressionType(inst.exp);
2042
2043       inst.isConstant = true;
2044       if(inst.members)
2045       {
2046          DataMember curMember = null;
2047          Class curClass = null;
2048          DataMember subMemberStack[256];
2049          int subMemberStackPos = 0;
2050
2051          for(members = inst.members->first; members; members = members.next)
2052          {
2053             switch(members.type)
2054             {
2055                case methodMembersInit:
2056                {
2057                   char name[1024];
2058                   static uint instMethodID = 0;
2059                   External external = curExternal;
2060                   Context context = curContext;
2061                   Declarator declarator = members.function.declarator;
2062                   Identifier nameID = GetDeclId(declarator);
2063                   char * unmangled = nameID ? nameID.string : null;
2064                   Expression exp;
2065                   External createdExternal = null;
2066
2067                   if(inCompiler)
2068                   {
2069                      char number[16];
2070                      strcpy(name, "__ecereInstMeth_");
2071                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2072                      strcat(name, "_");
2073                      strcat(name, nameID.string);
2074                      strcat(name, "_");
2075                      sprintf(number, "_%08d", instMethodID++);
2076                      strcat(name, number);
2077                      nameID.string = CopyString(name);
2078                   }
2079
2080                   // Do modifications here...
2081                   if(declarator)
2082                   {
2083                      Symbol symbol = declarator.symbol;
2084                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2085
2086                      if(method && method.type == virtualMethod)
2087                      {
2088                         symbol.method = method;
2089                         ProcessMethodType(method);
2090
2091                         if(!symbol.type.thisClass)
2092                         {
2093                            if(method.dataType.thisClass && currentClass &&
2094                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2095                            {
2096                               if(!currentClass.symbol)
2097                                  currentClass.symbol = FindClass(currentClass.fullName);
2098                               symbol.type.thisClass = currentClass.symbol;
2099                            }
2100                            else
2101                            {
2102                               if(!_class.symbol)
2103                                  _class.symbol = FindClass(_class.fullName);
2104                               symbol.type.thisClass = _class.symbol;
2105                            }
2106                         }
2107                         DeclareType(curExternal, symbol.type, true, true);
2108
2109                      }
2110                      else if(classSym)
2111                      {
2112                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2113                            unmangled, classSym.string);
2114                      }
2115                   }
2116
2117                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2118
2119                   if(nameID)
2120                   {
2121                      FreeSpecifier(nameID._class);
2122                      nameID._class = null;
2123                   }
2124
2125                   curExternal = createdExternal;
2126                   if(inCompiler)
2127                   {
2128                      if(createdExternal.function)
2129                         ProcessFunction(createdExternal.function);
2130                   }
2131                   else if(declarator)
2132                   {
2133                      curExternal = declarator.symbol.pointerExternal;
2134                      ProcessFunction((FunctionDefinition)members.function);
2135                   }
2136                   curExternal = external;
2137                   curContext = context;
2138
2139                   if(inCompiler)
2140                   {
2141                      FreeClassFunction(members.function);
2142
2143                      // In this pass, turn this into a MemberInitData
2144                      exp = QMkExpId(name);
2145                      members.type = dataMembersInit;
2146                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2147
2148                      delete unmangled;
2149                   }
2150                   break;
2151                }
2152                case dataMembersInit:
2153                {
2154                   if(members.dataMembers && classSym)
2155                   {
2156                      MemberInit member;
2157                      Location oldyyloc = yylloc;
2158                      for(member = members.dataMembers->first; member; member = member.next)
2159                      {
2160                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2161                         if(member.initializer && !member.initializer.isConstant)
2162                            inst.isConstant = false;
2163                      }
2164                      yylloc = oldyyloc;
2165                   }
2166                   break;
2167                }
2168             }
2169          }
2170       }
2171    }
2172 }
2173
2174 void DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2175 {
2176    _DeclareType(neededFor, type, needDereference, forFunctionDef, false);
2177 }
2178
2179 void DeclareTypeForwardDeclare(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2180 {
2181    _DeclareType(neededFor, type, needDereference, forFunctionDef, true);
2182 }
2183
2184 static void _DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef, bool fwdDecl)
2185 {
2186    if(inCompiler)
2187    {
2188       if(type.kind == functionType)
2189       {
2190          Type param;
2191          for(param = type.params.first; param; param = param.next)
2192             _DeclareType(neededFor, param, forFunctionDef, false, fwdDecl);
2193          _DeclareType(neededFor, type.returnType, forFunctionDef, false, fwdDecl);
2194       }
2195       else if(type.kind == pointerType)
2196          _DeclareType(neededFor, type.type, false, false, fwdDecl);
2197       else if(type.kind == classType)
2198       {
2199          Class c = type._class.registered;
2200          _DeclareStruct(neededFor, c ? c.fullName : "ecere::com::Instance", c ? c.type == noHeadClass : false, needDereference && c && c.type == structClass, fwdDecl);
2201       }
2202       else if(type.kind == structType || type.kind == unionType)
2203       {
2204          Type member;
2205          for(member = type.members.first; member; member = member.next)
2206             _DeclareType(neededFor, member, needDereference, forFunctionDef, fwdDecl);
2207       }
2208       else if(type.kind == arrayType)
2209          _DeclareType(neededFor, type.arrayType, true, false, fwdDecl);
2210    }
2211 }
2212
2213 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2214 {
2215    ClassTemplateArgument * arg = null;
2216    int id = 0;
2217    ClassTemplateParameter curParam = null;
2218    Class sClass;
2219    for(sClass = _class; sClass; sClass = sClass.base)
2220    {
2221       id = 0;
2222       if(sClass.templateClass) sClass = sClass.templateClass;
2223       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2224       {
2225          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2226          {
2227             for(sClass = sClass.base; sClass; sClass = sClass.base)
2228             {
2229                if(sClass.templateClass) sClass = sClass.templateClass;
2230                id += sClass.templateParams.count;
2231             }
2232             break;
2233          }
2234          id++;
2235       }
2236       if(curParam) break;
2237    }
2238    if(curParam)
2239    {
2240       arg = &_class.templateArgs[id];
2241       if(arg && param.type == type)
2242          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2243    }
2244    return arg;
2245 }
2246
2247 public Context SetupTemplatesContext(Class _class)
2248 {
2249    Context context = PushContext();
2250    context.templateTypesOnly = true;
2251    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2252    {
2253       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2254       for(; param; param = param.next)
2255       {
2256          if(param.type == type && param.identifier)
2257          {
2258             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2259             curContext.templateTypes.Add((BTNode)type);
2260          }
2261       }
2262    }
2263    else if(_class)
2264    {
2265       Class sClass;
2266       for(sClass = _class; sClass; sClass = sClass.base)
2267       {
2268          ClassTemplateParameter p;
2269          for(p = sClass.templateParams.first; p; p = p.next)
2270          {
2271             //OldList * specs = MkList();
2272             //Declarator decl = null;
2273             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2274             if(p.type == type)
2275             {
2276                TemplateParameter param = p.param;
2277                TemplatedType type;
2278                if(!param)
2279                {
2280                   // ADD DATA TYPE HERE...
2281                   p.param = param = TemplateParameter
2282                   {
2283                      identifier = MkIdentifier(p.name), type = p.type,
2284                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2285                   };
2286                }
2287                type = TemplatedType { key = (uintptr)p.name, param = param };
2288                curContext.templateTypes.Add((BTNode)type);
2289             }
2290          }
2291       }
2292    }
2293    return context;
2294 }
2295
2296 public void FinishTemplatesContext(Context context)
2297 {
2298    PopContext(context);
2299    FreeContext(context);
2300    delete context;
2301 }
2302
2303 public void ProcessMethodType(Method method)
2304 {
2305    if(!method.dataType)
2306    {
2307       Context context = SetupTemplatesContext(method._class);
2308
2309       method.dataType = ProcessTypeString(method.dataTypeString, false);
2310
2311       FinishTemplatesContext(context);
2312
2313       if(method.type != virtualMethod && method.dataType)
2314       {
2315          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2316          {
2317             if(!method._class.symbol)
2318                method._class.symbol = FindClass(method._class.fullName);
2319             method.dataType.thisClass = method._class.symbol;
2320          }
2321       }
2322
2323       // Why was this commented out? Working fine without now...
2324
2325       /*
2326       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2327          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2328          */
2329    }
2330
2331    /*
2332    if(type)
2333    {
2334       char * par = strstr(type, "(");
2335       char * classOp = null;
2336       int classOpLen = 0;
2337       if(par)
2338       {
2339          int c;
2340          for(c = par-type-1; c >= 0; c++)
2341          {
2342             if(type[c] == ':' && type[c+1] == ':')
2343             {
2344                classOp = type + c - 1;
2345                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2346                {
2347                   classOp--;
2348                   classOpLen++;
2349                }
2350                break;
2351             }
2352             else if(!isspace(type[c]))
2353                break;
2354          }
2355       }
2356       if(classOp)
2357       {
2358          char temp[1024];
2359          int typeLen = strlen(type);
2360          memcpy(temp, classOp, classOpLen);
2361          temp[classOpLen] = '\0';
2362          if(temp[0])
2363             _class = eSystem_FindClass(module, temp);
2364          else
2365             _class = null;
2366          method.dataTypeString = new char[typeLen - classOpLen + 1];
2367          memcpy(method.dataTypeString, type, classOp - type);
2368          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2369       }
2370       else
2371          method.dataTypeString = type;
2372    }
2373    */
2374 }
2375
2376
2377 public void ProcessPropertyType(Property prop)
2378 {
2379    if(!prop.dataType)
2380    {
2381       Context context = SetupTemplatesContext(prop._class);
2382       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2383       FinishTemplatesContext(context);
2384    }
2385 }
2386
2387 public void DeclareMethod(External neededFor, Method method, const char * name)
2388 {
2389    Symbol symbol = method.symbol;
2390    if(!symbol || (!symbol.pointerExternal && (!symbol.methodCodeExternal || method.type == virtualMethod)))
2391    {
2392       bool dllImport = false;
2393
2394       if(!method.dataType)
2395          method.dataType = ProcessTypeString(method.dataTypeString, false);
2396
2397       //if(!symbol || symbol._import || method.type == virtualMethod)
2398       {
2399          if(!symbol || method.type == virtualMethod)
2400          {
2401             Symbol classSym;
2402             if(!method._class.symbol)
2403                method._class.symbol = FindClass(method._class.fullName);
2404             classSym = method._class.symbol;
2405             if(!classSym._import)
2406             {
2407                ModuleImport module;
2408
2409                if(method._class.module && method._class.module.name)
2410                   module = FindModule(method._class.module);
2411                else
2412                   module = mainModule;
2413                classSym._import = ClassImport
2414                {
2415                   name = CopyString(method._class.fullName);
2416                   isRemote = method._class.isRemote;
2417                };
2418                module.classes.Add(classSym._import);
2419             }
2420             if(!symbol)
2421             {
2422                symbol = method.symbol = Symbol { };
2423             }
2424             if(!symbol._import)
2425             {
2426                symbol._import = (ClassImport)MethodImport
2427                {
2428                   name = CopyString(method.name);
2429                   isVirtual = method.type == virtualMethod;
2430                };
2431                classSym._import.methods.Add(symbol._import);
2432             }
2433             if(!symbol)
2434             {
2435                symbol.type = method.dataType;
2436                if(symbol.type) symbol.type.refCount++;
2437             }
2438          }
2439          if(!method.dataType.dllExport)
2440          {
2441             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2442                dllImport = true;
2443          }
2444       }
2445
2446       if(inCompiler)
2447       {
2448          // We need a declaration here :)
2449          Declaration decl;
2450          OldList * specifiers, * declarators;
2451          Declarator d;
2452          Declarator funcDecl;
2453          External external;
2454
2455          specifiers = MkList();
2456          declarators = MkList();
2457
2458          if(dllImport)
2459             ListAdd(specifiers, MkSpecifier(EXTERN));
2460          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2461             ListAdd(specifiers, MkSpecifier(STATIC));
2462
2463          if(method.type == virtualMethod)
2464          {
2465             ListAdd(specifiers, MkSpecifier(INT));
2466             d = MkDeclaratorIdentifier(MkIdentifier(name));
2467          }
2468          else
2469          {
2470             d = MkDeclaratorIdentifier(MkIdentifier(name));
2471             if(dllImport)
2472                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2473             {
2474                Context context = SetupTemplatesContext(method._class);
2475                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2476                FinishTemplatesContext(context);
2477             }
2478             funcDecl = GetFuncDecl(d);
2479
2480             if(dllImport)
2481             {
2482                Specifier spec, next;
2483                for(spec = specifiers->first; spec; spec = next)
2484                {
2485                   next = spec.next;
2486                   if(spec.type == extendedSpecifier)
2487                   {
2488                      specifiers->Remove(spec);
2489                      FreeSpecifier(spec);
2490                   }
2491                }
2492             }
2493
2494             // Add this parameter if not a static method
2495             if(method.dataType && !method.dataType.staticMethod)
2496             {
2497                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2498                {
2499                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2500                   TypeName thisParam = MkTypeName(MkListOne(
2501                      MkSpecifierName(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2502                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2503                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2504                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2505
2506                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2507                   {
2508                      TypeName param = funcDecl.function.parameters->first;
2509                      funcDecl.function.parameters->Remove(param);
2510                      FreeTypeName(param);
2511                   }
2512
2513                   if(!funcDecl.function.parameters)
2514                      funcDecl.function.parameters = MkList();
2515                   funcDecl.function.parameters->Insert(null, thisParam);
2516                }
2517             }
2518          }
2519          ProcessDeclarator(d, true);
2520
2521          ListAdd(declarators, MkInitDeclarator(d, null));
2522
2523          decl = MkDeclaration(specifiers, declarators);
2524
2525          ReplaceThisClassSpecifiers(specifiers, method._class);
2526
2527          external = MkExternalDeclaration(decl);
2528          external.symbol = symbol;
2529          symbol.pointerExternal = external;
2530          ast->Add(external);
2531          DeclareStruct(external, method._class.fullName, true, true);
2532          if(method.dataType)
2533             DeclareType(external, method.dataType, true, true);
2534       }
2535    }
2536    if(inCompiler && neededFor)
2537    {
2538       External external = symbol.pointerExternal ? symbol.pointerExternal : symbol.methodCodeExternal;
2539       neededFor.CreateUniqueEdge(external, external.type == functionExternal);
2540    }
2541 }
2542
2543 char * ReplaceThisClass(Class _class)
2544 {
2545    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2546    {
2547       bool first = true;
2548       int p = 0;
2549       ClassTemplateParameter param;
2550       int lastParam = -1;
2551
2552       char className[1024];
2553       strcpy(className, _class.fullName);
2554       for(param = _class.templateParams.first; param; param = param.next)
2555       {
2556          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2557          {
2558             if(first) strcat(className, "<");
2559             if(!first) strcat(className, ", ");
2560             if(lastParam + 1 != p)
2561             {
2562                strcat(className, param.name);
2563                strcat(className, " = ");
2564             }
2565             strcat(className, param.name);
2566             first = false;
2567             lastParam = p;
2568          }
2569          p++;
2570       }
2571       if(!first)
2572       {
2573          int len = strlen(className);
2574          if(className[len-1] == '>') className[len++] = ' ';
2575          className[len++] = '>';
2576          className[len++] = '\0';
2577       }
2578       return CopyString(className);
2579    }
2580    else
2581       return CopyString(_class.fullName);
2582 }
2583
2584 Type ReplaceThisClassType(Class _class)
2585 {
2586    Type type;
2587    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2588    {
2589       bool first = true;
2590       int p = 0;
2591       ClassTemplateParameter param;
2592       int lastParam = -1;
2593       char className[1024];
2594       strcpy(className, _class.fullName);
2595
2596       for(param = _class.templateParams.first; param; param = param.next)
2597       {
2598          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2599          {
2600             if(first) strcat(className, "<");
2601             if(!first) strcat(className, ", ");
2602             if(lastParam + 1 != p)
2603             {
2604                strcat(className, param.name);
2605                strcat(className, " = ");
2606             }
2607             strcat(className, param.name);
2608             first = false;
2609             lastParam = p;
2610          }
2611          p++;
2612       }
2613       if(!first)
2614       {
2615          int len = strlen(className);
2616          if(className[len-1] == '>') className[len++] = ' ';
2617          className[len++] = '>';
2618          className[len++] = '\0';
2619       }
2620       type = MkClassType(className);
2621       //type = ProcessTypeString(className, false);
2622    }
2623    else
2624    {
2625       type = MkClassType(_class.fullName);
2626       //type = ProcessTypeString(_class.fullName, false);
2627    }
2628    //type.wasThisClass = true;
2629    return type;
2630 }
2631
2632 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2633 {
2634    if(specs != null && _class)
2635    {
2636       Specifier spec;
2637       for(spec = specs.first; spec; spec = spec.next)
2638       {
2639          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2640          {
2641             spec.type = nameSpecifier;
2642             spec.name = ReplaceThisClass(_class);
2643             spec.symbol = FindClass(spec.name); //_class.symbol;
2644          }
2645       }
2646    }
2647 }
2648
2649 // Returns imported or not
2650 bool DeclareFunction(External neededFor, GlobalFunction function, char * name)
2651 {
2652    Symbol symbol = function.symbol;
2653    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2654    if(!symbol || !symbol.pointerExternal)
2655    {
2656       bool imported = false;
2657       bool dllImport = false;
2658
2659       if(!function.dataType)
2660       {
2661          function.dataType = ProcessTypeString(function.dataTypeString, false);
2662          if(!function.dataType.thisClass)
2663             function.dataType.staticMethod = true;
2664       }
2665
2666       if(inCompiler)
2667       {
2668          if(!symbol)
2669          {
2670             ModuleImport module = FindModule(function.module);
2671             // WARNING: This is not added anywhere...
2672             symbol = function.symbol = Symbol {  };
2673
2674             if(module.name)
2675             {
2676                if(!function.dataType.dllExport)
2677                {
2678                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2679                   module.functions.Add(symbol._import);
2680                }
2681             }
2682             // Set the symbol type
2683             {
2684                symbol.type = ProcessTypeString(function.dataTypeString, false);
2685                if(!symbol.type.thisClass)
2686                   symbol.type.staticMethod = true;
2687             }
2688          }
2689          imported = symbol._import ? true : false;
2690          if(imported && function.module != privateModule && function.module.importType != staticImport)
2691             dllImport = true;
2692       }
2693
2694       if(inCompiler)
2695       {
2696          // TOCHECK: What's with the functionExternal check here? Is it Edge breaking / forward declaration?
2697          //if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2698          {
2699             // We need a declaration here :)
2700             Declaration decl;
2701             OldList * specifiers, * declarators;
2702             Declarator d;
2703             Declarator funcDecl;
2704             External external;
2705
2706             specifiers = MkList();
2707             declarators = MkList();
2708
2709             ListAdd(specifiers, MkSpecifier(EXTERN));
2710
2711             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2712             if(dllImport)
2713                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2714
2715             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2716             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2717             if(function.module.importType == staticImport)
2718             {
2719                Specifier spec;
2720                for(spec = specifiers->first; spec; spec = spec.next)
2721                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2722                   {
2723                      specifiers->Remove(spec);
2724                      FreeSpecifier(spec);
2725                      break;
2726                   }
2727             }
2728
2729             funcDecl = GetFuncDecl(d);
2730
2731             // Make sure we don't have empty parameter declarations for static methods...
2732             if(funcDecl && !funcDecl.function.parameters)
2733             {
2734                funcDecl.function.parameters = MkList();
2735                funcDecl.function.parameters->Insert(null,
2736                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2737             }
2738
2739             ListAdd(declarators, MkInitDeclarator(d, null));
2740
2741             {
2742                Context oldCtx = curContext;
2743                curContext = globalContext;
2744                decl = MkDeclaration(specifiers, declarators);
2745                curContext = oldCtx;
2746             }
2747
2748             // Keep a different symbol for the function definition than the declaration...
2749             /* Note: This should be handled by the edge breaking...
2750             if(symbol.pointerExternal && symbol.pointerExternal.type == functionExternal)
2751             {
2752                Symbol functionSymbol { };
2753                // Copy symbol
2754                {
2755                   *functionSymbol = *symbol;
2756                   functionSymbol.string = CopyString(symbol.string);
2757                   if(functionSymbol.type)
2758                      functionSymbol.type.refCount++;
2759                }
2760
2761                excludedSymbols->Add(functionSymbol);
2762
2763                symbol.pointerExternal.symbol = functionSymbol;
2764             }
2765             */
2766             external = MkExternalDeclaration(decl);
2767             ast->Add(external);
2768             external.symbol = symbol;
2769             symbol.pointerExternal = external;
2770
2771             DeclareType(external, function.dataType, true, true);
2772          }
2773       }
2774    }
2775    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2776       neededFor.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
2777    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2778 }
2779
2780 void DeclareGlobalData(External neededFor, GlobalData data)
2781 {
2782    Symbol symbol = data.symbol;
2783    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2784    if(!symbol || !symbol.pointerExternal)
2785    {
2786       if(inCompiler)
2787       {
2788          if(!symbol)
2789             symbol = data.symbol = Symbol { };
2790       }
2791       if(!data.dataType)
2792          data.dataType = ProcessTypeString(data.dataTypeString, false);
2793
2794       if(inCompiler)
2795       {
2796          // We need a declaration here :)
2797          Declaration decl;
2798          OldList * specifiers, * declarators;
2799          Declarator d;
2800          External external;
2801
2802          specifiers = MkList();
2803          declarators = MkList();
2804
2805          ListAdd(specifiers, MkSpecifier(EXTERN));
2806          d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2807          d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2808
2809          ListAdd(declarators, MkInitDeclarator(d, null));
2810
2811          decl = MkDeclaration(specifiers, declarators);
2812          external = MkExternalDeclaration(decl);
2813          if(curExternal)
2814             ast->Insert(curExternal.prev, external);
2815          external.symbol = symbol;
2816          symbol.pointerExternal = external;
2817
2818          DeclareType(external, data.dataType, true, true);
2819       }
2820    }
2821    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2822       neededFor.CreateUniqueEdge(symbol.pointerExternal, false);
2823 }
2824
2825 class Conversion : struct
2826 {
2827    Conversion prev, next;
2828    Property convert;
2829    bool isGet;
2830    Type resultType;
2831 };
2832
2833 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2834 {
2835    bool status = true;
2836    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2837       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2838    {
2839       Class sourceClass = source.kind == classType ? source._class.registered : null;
2840       Class destClass = dest.kind == classType ? dest._class.registered : null;
2841       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
2842          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
2843       {
2844          Type sourceType = source, destType = dest;
2845          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
2846          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
2847          if(!destType.constant && sourceType.constant)
2848          {
2849             status = false;
2850             if(warn)
2851                Compiler_Warning($"discarding const qualifier\n");
2852          }
2853       }
2854    }
2855    return status;
2856 }
2857
2858 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
2859                        bool isConversionExploration, bool warnConst)
2860 {
2861    if(source && dest)
2862    {
2863       if(warnConst)
2864          CheckConstCompatibility(source, dest, true);
2865       // Property convert;
2866
2867       if(source.kind == templateType && dest.kind != templateType)
2868       {
2869          Type type = ProcessTemplateParameterType(source.templateParameter);
2870          if(type) source = type;
2871       }
2872
2873       if(dest.kind == templateType && source.kind != templateType)
2874       {
2875          Type type = ProcessTemplateParameterType(dest.templateParameter);
2876          if(type) dest = type;
2877       }
2878
2879       if(dest.classObjectType == typedObject && dest.kind != functionType)
2880       {
2881          if(source.classObjectType != anyObject)
2882             return true;
2883          else
2884          {
2885             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2886             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2887             {
2888                return true;
2889             }
2890          }
2891       }
2892       else
2893       {
2894          if(source.kind != functionType && source.classObjectType == anyObject)
2895             return true;
2896          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
2897             return true;
2898       }
2899
2900       if((dest.kind == structType && source.kind == structType) ||
2901          (dest.kind == unionType && source.kind == unionType))
2902       {
2903          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2904              (source.members.first && source.members.first == dest.members.first))
2905             return true;
2906       }
2907
2908       if(dest.kind == ellipsisType && source.kind != voidType)
2909          return true;
2910
2911       if(dest.kind == pointerType && dest.type.kind == voidType &&
2912          ((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))
2913          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2914
2915          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2916
2917          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2918          return true;
2919       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2920          ((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))
2921          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2922          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2923
2924          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2925          return true;
2926
2927       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2928       {
2929          if(source._class.registered && source._class.registered.type == unitClass)
2930          {
2931             if(conversions != null)
2932             {
2933                if(source._class.registered == dest._class.registered)
2934                   return true;
2935             }
2936             else
2937             {
2938                Class sourceBase, destBase;
2939                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2940                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2941                if(sourceBase == destBase)
2942                   return true;
2943             }
2944          }
2945          // Don't match enum inheriting from other enum if resolving enumeration values
2946          // TESTING: !dest.classObjectType
2947          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2948             (enumBaseType ||
2949                (!source._class.registered || source._class.registered.type != enumClass) ||
2950                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2951             return true;
2952          else
2953          {
2954             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2955             if(enumBaseType &&
2956                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2957                ((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)
2958             {
2959                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2960                {
2961                   return true;
2962                }
2963             }
2964          }
2965       }
2966
2967       // JUST ADDED THIS...
2968       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2969          return true;
2970
2971       if(doConversion)
2972       {
2973          // Just added this for Straight conversion of ColorAlpha => Color
2974          if(source.kind == classType)
2975          {
2976             Class _class;
2977             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2978             {
2979                Property convert;
2980                for(convert = _class.conversions.first; convert; convert = convert.next)
2981                {
2982                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2983                   {
2984                      Conversion after = (conversions != null) ? conversions.last : null;
2985
2986                      if(!convert.dataType)
2987                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2988                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
2989                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
2990                         MatchTypes(convert.dataType, dest, conversions, null, null,
2991                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
2992                               convert.dataType.kind == classType, false, true, warnConst))
2993                      {
2994                         if(!conversions && !convert.Get)
2995                            return true;
2996                         else if(conversions != null)
2997                         {
2998                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2999                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3000                               (dest.kind != classType || dest._class.registered != _class.base))
3001                               return true;
3002                            else
3003                            {
3004                               Conversion conv { convert = convert, isGet = true };
3005                               // conversions.Add(conv);
3006                               conversions.Insert(after, conv);
3007
3008                               return true;
3009                            }
3010                         }
3011                      }
3012                   }
3013                }
3014             }
3015          }
3016
3017          // MOVING THIS??
3018
3019          if(dest.kind == classType)
3020          {
3021             Class _class;
3022             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3023             {
3024                Property convert;
3025                for(convert = _class.conversions.first; convert; convert = convert.next)
3026                {
3027                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3028                   {
3029                      Type constType = null;
3030                      bool success = false;
3031                      // Conversion after = (conversions != null) ? conversions.last : null;
3032
3033                      if(!convert.dataType)
3034                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3035
3036                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3037                      {
3038                         Type ptrType { };
3039                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3040                         CopyTypeInto(ptrType, convert.dataType.type);
3041                         ptrType.constant = true;
3042                      }
3043
3044                      // Just added this equality check to prevent recursion.... Make it safer?
3045                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3046                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3047                      {
3048                         if(!conversions && !convert.Set)
3049                            success = true;
3050                         else if(conversions != null)
3051                         {
3052                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3053                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3054                               (source.kind != classType || source._class.registered != _class.base))
3055                               success = true;
3056                            else
3057                            {
3058                               // *** Testing this! ***
3059                               Conversion conv { convert = convert };
3060                               conversions.Add(conv);
3061                               //conversions.Insert(after, conv);
3062                               success = true;
3063                            }
3064                         }
3065                      }
3066                      if(constType)
3067                         FreeType(constType);
3068                      if(success)
3069                         return true;
3070                   }
3071                }
3072             }
3073             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3074             {
3075                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3076                   (source.kind != classType || source._class.registered.type != structClass))
3077                   return true;
3078             }*/
3079
3080             // TESTING THIS... IS THIS OK??
3081             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3082             {
3083                if(!dest._class.registered.dataType)
3084                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3085                // Only support this for classes...
3086                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3087                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3088                {
3089                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3090                   {
3091                      return true;
3092                   }
3093                }
3094             }
3095          }
3096
3097          // Moved this lower
3098          if(source.kind == classType)
3099          {
3100             Class _class;
3101             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3102             {
3103                Property convert;
3104                for(convert = _class.conversions.first; convert; convert = convert.next)
3105                {
3106                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3107                   {
3108                      Conversion after = (conversions != null) ? conversions.last : null;
3109
3110                      if(!convert.dataType)
3111                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3112                      if(convert.dataType != source &&
3113                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3114                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3115                      {
3116                         if(!conversions && !convert.Get)
3117                            return true;
3118                         else if(conversions != null)
3119                         {
3120                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3121                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3122                               (dest.kind != classType || dest._class.registered != _class.base))
3123                               return true;
3124                            else
3125                            {
3126                               Conversion conv { convert = convert, isGet = true };
3127
3128                               // conversions.Add(conv);
3129                               conversions.Insert(after, conv);
3130                               return true;
3131                            }
3132                         }
3133                      }
3134                   }
3135                }
3136             }
3137
3138             // TESTING THIS... IS THIS OK??
3139             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3140             {
3141                if(!source._class.registered.dataType)
3142                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3143                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3144                {
3145                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3146                      return true;
3147                   // For bool to be accepted by byte, short, etc.
3148                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3149                      return true;
3150                }
3151             }
3152          }
3153       }
3154
3155       if(source.kind == classType || source.kind == subClassType)
3156          ;
3157       else if(dest.kind == source.kind &&
3158          (dest.kind != structType && dest.kind != unionType &&
3159           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3160           return true;
3161       // RECENTLY ADDED THESE
3162       else if(dest.kind == doubleType && source.kind == floatType)
3163          return true;
3164       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3165          return true;
3166       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3167          return true;
3168       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3169          return true;
3170       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3171          return true;
3172       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3173          return true;
3174       else if(source.kind == enumType &&
3175          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3176           return true;
3177       else if(dest.kind == enumType && !isConversionExploration &&
3178          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3179           return true;
3180       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3181               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3182       {
3183          Type paramSource, paramDest;
3184
3185          if(dest.kind == methodType)
3186             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3187          if(source.kind == methodType)
3188             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3189
3190          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3191          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3192          if(dest.kind == methodType)
3193             dest = dest.method.dataType;
3194          if(source.kind == methodType)
3195             source = source.method.dataType;
3196
3197          paramSource = source.params.first;
3198          if(paramSource && paramSource.kind == voidType) paramSource = null;
3199          paramDest = dest.params.first;
3200          if(paramDest && paramDest.kind == voidType) paramDest = null;
3201
3202
3203          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3204             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3205          {
3206             // Source thisClass must be derived from destination thisClass
3207             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3208                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3209             {
3210                if(paramDest && paramDest.kind == classType)
3211                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3212                else
3213                   Compiler_Error($"method class should not take an object\n");
3214                return false;
3215             }
3216             paramDest = paramDest.next;
3217          }
3218          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3219          {
3220             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3221             {
3222                if(dest.thisClass)
3223                {
3224                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3225                   {
3226                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3227                      return false;
3228                   }
3229                }
3230                else
3231                {
3232                   // THIS WAS BACKWARDS:
3233                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3234                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3235                   {
3236                      if(owningClassDest)
3237                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3238                      else
3239                         Compiler_Error($"overriding class expected to be derived from method class\n");
3240                      return false;
3241                   }
3242                }
3243                paramSource = paramSource.next;
3244             }
3245             else
3246             {
3247                if(dest.thisClass)
3248                {
3249                   // Source thisClass must be derived from destination thisClass
3250                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3251                   {
3252                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3253                      return false;
3254                   }
3255                }
3256                else
3257                {
3258                   // THIS WAS BACKWARDS TOO??
3259                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3260                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3261                   {
3262                      //if(owningClass)
3263                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3264                      //else
3265                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3266                      return false;
3267                   }
3268                }
3269             }
3270          }
3271
3272
3273          // Source return type must be derived from destination return type
3274          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3275          {
3276             Compiler_Warning($"incompatible return type for function\n");
3277             return false;
3278          }
3279          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3280          else
3281             CheckConstCompatibility(dest.returnType, source.returnType, true);
3282
3283          // Check parameters
3284
3285          for(; paramDest; paramDest = paramDest.next)
3286          {
3287             if(!paramSource)
3288             {
3289                //Compiler_Warning($"not enough parameters\n");
3290                Compiler_Error($"not enough parameters\n");
3291                return false;
3292             }
3293             {
3294                Type paramDestType = paramDest;
3295                Type paramSourceType = paramSource;
3296                Type type = paramDestType;
3297
3298                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3299                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3300                   paramSource.kind != templateType)
3301                {
3302                   int id = 0;
3303                   ClassTemplateParameter curParam = null;
3304                   Class sClass;
3305                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3306                   {
3307                      id = 0;
3308                      if(sClass.templateClass) sClass = sClass.templateClass;
3309                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3310                      {
3311                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3312                         {
3313                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3314                            {
3315                               if(sClass.templateClass) sClass = sClass.templateClass;
3316                               id += sClass.templateParams.count;
3317                            }
3318                            break;
3319                         }
3320                         id++;
3321                      }
3322                      if(curParam) break;
3323                   }
3324
3325                   if(curParam)
3326                   {
3327                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3328                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3329                   }
3330                }
3331
3332                // paramDest must be derived from paramSource
3333                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3334                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3335                {
3336                   char type[1024];
3337                   type[0] = 0;
3338                   PrintType(paramDest, type, false, true);
3339                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3340
3341                   if(paramDestType != paramDest)
3342                      FreeType(paramDestType);
3343                   return false;
3344                }
3345                if(paramDestType != paramDest)
3346                   FreeType(paramDestType);
3347             }
3348
3349             paramSource = paramSource.next;
3350          }
3351          if(paramSource)
3352          {
3353             Compiler_Error($"too many parameters\n");
3354             return false;
3355          }
3356          return true;
3357       }
3358       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3359       {
3360          return true;
3361       }
3362       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3363          (source.kind == arrayType || source.kind == pointerType))
3364       {
3365          // Pointers to pointer is incompatible with non normal/nohead classes
3366          if(!(dest.type && dest.type.kind == pointerType && source.type.kind == classType && source.type._class &&
3367             source.type._class.registered && (source.type._class.registered.type != normalClass && source.type._class.registered.type != noHeadClass) && !source.type.byReference))
3368          {
3369             ComputeTypeSize(source.type);
3370             ComputeTypeSize(dest.type);
3371             if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3372                return true;
3373          }
3374       }
3375    }
3376    return false;
3377 }
3378
3379 static void FreeConvert(Conversion convert)
3380 {
3381    if(convert.resultType)
3382       FreeType(convert.resultType);
3383 }
3384
3385 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3386                               char * string, OldList conversions)
3387 {
3388    BTNamedLink link;
3389
3390    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3391    {
3392       Class _class = link.data;
3393       if(_class.type == enumClass)
3394       {
3395          OldList converts { };
3396          Type type { };
3397          type.kind = classType;
3398
3399          if(!_class.symbol)
3400             _class.symbol = FindClass(_class.fullName);
3401          type._class = _class.symbol;
3402
3403          if(MatchTypes(type, dest, &converts, null, null, dest.kind != classType || !dest._class || strcmp(dest._class.string, "bool"),
3404                false, false, false, false))
3405          {
3406             NamedLink64 value;
3407             Class enumClass = eSystem_FindClass(privateModule, "enum");
3408             if(enumClass)
3409             {
3410                Class baseClass;
3411                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3412                {
3413                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3414                   for(value = e.values.first; value; value = value.next)
3415                   {
3416                      if(!strcmp(value.name, string))
3417                         break;
3418                   }
3419                   if(value)
3420                   {
3421                      FreeType(sourceExp.expType);
3422
3423                      sourceExp.isConstant = true;
3424                      sourceExp.expType = MkClassType(baseClass.fullName);
3425                      if(inCompiler || inPreCompiler || inDebugger)
3426                      {
3427                         char constant[256];
3428                         FreeExpContents(sourceExp);
3429
3430                         sourceExp.type = constantExp;
3431                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3432                            sprintf(constant, FORMAT64D, value.data);
3433                         else
3434                            sprintf(constant, FORMAT64HEXLL, value.data);
3435                         sourceExp.constant = CopyString(constant);
3436                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3437                      }
3438
3439                      while(converts.first)
3440                      {
3441                         Conversion convert = converts.first;
3442                         converts.Remove(convert);
3443                         conversions.Add(convert);
3444                      }
3445                      delete type;
3446                      return true;
3447                   }
3448                }
3449             }
3450          }
3451          if(converts.first)
3452             converts.Free(FreeConvert);
3453          delete type;
3454       }
3455    }
3456    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3457       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3458          return true;
3459    return false;
3460 }
3461
3462 public bool ModuleVisibility(Module searchIn, Module searchFor)
3463 {
3464    SubModule subModule;
3465
3466    if(searchFor == searchIn)
3467       return true;
3468
3469    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3470    {
3471       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3472       {
3473          if(ModuleVisibility(subModule.module, searchFor))
3474             return true;
3475       }
3476    }
3477    return false;
3478 }
3479
3480 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3481 {
3482    Module module;
3483
3484    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3485       return true;
3486    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3487       return true;
3488    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3489       return true;
3490
3491    for(module = mainModule.application.allModules.first; module; module = module.next)
3492    {
3493       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3494          return true;
3495    }
3496    return false;
3497 }
3498
3499 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3500 {
3501    Type source;
3502    Type realDest = dest;
3503    Type backupSourceExpType = null;
3504    Expression computedExp = sourceExp;
3505    dest.refCount++;
3506
3507    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3508       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3509    {
3510       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3511       ComputeExpression(computedExp /*sourceExp*/);
3512    }
3513
3514    source = sourceExp.expType;
3515
3516    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3517    {
3518       if(computedExp != sourceExp)
3519       {
3520          FreeExpression(computedExp);
3521          computedExp = sourceExp;
3522       }
3523       FreeType(dest);
3524       return true;
3525    }
3526
3527    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3528    {
3529        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3530        {
3531           Class sourceBase, destBase;
3532           for(sourceBase = source._class.registered;
3533               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3534               sourceBase = sourceBase.base);
3535           for(destBase = dest._class.registered;
3536               destBase && destBase.base && destBase.base.type != systemClass;
3537               destBase = destBase.base);
3538           //if(source._class.registered == dest._class.registered)
3539           if(sourceBase == destBase)
3540           {
3541             if(computedExp != sourceExp)
3542             {
3543                FreeExpression(computedExp);
3544                computedExp = sourceExp;
3545             }
3546             FreeType(dest);
3547             return true;
3548          }
3549       }
3550    }
3551
3552    if(source)
3553    {
3554       OldList * specs;
3555       bool flag = false;
3556       int64 value = MAXINT;
3557
3558       source.refCount++;
3559
3560       if(computedExp.type == constantExp)
3561       {
3562          if(source.isSigned)
3563             value = strtoll(computedExp.constant, null, 0);
3564          else
3565             value = strtoull(computedExp.constant, null, 0);
3566       }
3567       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3568       {
3569          if(source.isSigned)
3570             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3571          else
3572             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3573       }
3574       if(computedExp != sourceExp)
3575       {
3576          FreeExpression(computedExp);
3577          computedExp = sourceExp;
3578       }
3579
3580       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3581          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3582       {
3583          FreeType(source);
3584          source = Type { kind = intType, isSigned = false, refCount = 1 };
3585       }
3586
3587       if(dest.kind == classType)
3588       {
3589          Class _class = dest._class ? dest._class.registered : null;
3590
3591          if(_class && _class.type == unitClass)
3592          {
3593             if(source.kind != classType)
3594             {
3595                Type tempType { };
3596                Type tempDest, tempSource;
3597
3598                for(; _class.base.type != systemClass; _class = _class.base);
3599                tempSource = dest;
3600                tempDest = tempType;
3601
3602                tempType.kind = classType;
3603                if(!_class.symbol)
3604                   _class.symbol = FindClass(_class.fullName);
3605
3606                tempType._class = _class.symbol;
3607                tempType.truth = dest.truth;
3608                if(tempType._class)
3609                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3610
3611                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3612                backupSourceExpType = sourceExp.expType;
3613                if(dest.passAsTemplate)
3614                {
3615                   // Don't carry passAsTemplate
3616                   sourceExp.expType = { };
3617                   CopyTypeInto(sourceExp.expType, dest);
3618                   sourceExp.expType.passAsTemplate = false;
3619                }
3620                else
3621                {
3622                   sourceExp.expType = dest;
3623                   dest.refCount++;
3624                }
3625                //sourceExp.expType = MkClassType(_class.fullName);
3626                flag = true;
3627
3628                delete tempType;
3629             }
3630          }
3631
3632
3633          // Why wasn't there something like this?
3634          if(_class && _class.type == bitClass && source.kind != classType)
3635          {
3636             if(!dest._class.registered.dataType)
3637                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3638             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3639             {
3640                FreeType(source);
3641                FreeType(sourceExp.expType);
3642                source = sourceExp.expType = MkClassType(dest._class.string);
3643                source.refCount++;
3644
3645                //source.kind = classType;
3646                //source._class = dest._class;
3647             }
3648          }
3649
3650          // Adding two enumerations
3651          /*
3652          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3653          {
3654             if(!source._class.registered.dataType)
3655                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3656             if(!dest._class.registered.dataType)
3657                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3658
3659             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3660             {
3661                FreeType(source);
3662                source = sourceExp.expType = MkClassType(dest._class.string);
3663                source.refCount++;
3664
3665                //source.kind = classType;
3666                //source._class = dest._class;
3667             }
3668          }*/
3669
3670          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3671          {
3672             OldList * specs = MkList();
3673             Declarator decl;
3674             char string[1024];
3675
3676             ReadString(string, sourceExp.string);
3677             decl = SpecDeclFromString(string, specs, null);
3678
3679             FreeExpContents(sourceExp);
3680             FreeType(sourceExp.expType);
3681
3682             sourceExp.type = classExp;
3683             sourceExp._classExp.specifiers = specs;
3684             sourceExp._classExp.decl = decl;
3685             sourceExp.expType = dest;
3686             dest.refCount++;
3687
3688             FreeType(source);
3689             FreeType(dest);
3690             if(backupSourceExpType) FreeType(backupSourceExpType);
3691             return true;
3692          }
3693       }
3694       else if(source.kind == classType)
3695       {
3696          Class _class = source._class ? source._class.registered : null;
3697
3698          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3699          {
3700             /*
3701             if(dest.kind != classType)
3702             {
3703                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3704                if(!source._class.registered.dataType)
3705                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3706
3707                FreeType(dest);
3708                dest = MkClassType(source._class.string);
3709                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3710                //   dest = MkClassType(source._class.string);
3711             }
3712             */
3713
3714             if(dest.kind != classType)
3715             {
3716                Type tempType { };
3717                Type tempDest, tempSource;
3718
3719                if(!source._class.registered.dataType)
3720                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3721
3722                for(; _class.base.type != systemClass; _class = _class.base);
3723                tempDest = source;
3724                tempSource = tempType;
3725                tempType.kind = classType;
3726                tempType._class = FindClass(_class.fullName);
3727                tempType.truth = source.truth;
3728                tempType.classObjectType = source.classObjectType;
3729
3730                if(tempType._class)
3731                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3732
3733                // PUT THIS BACK TESTING UNITS?
3734                if(conversions && conversions.last)
3735                {
3736                   ((Conversion)(conversions.last)).resultType = dest;
3737                   dest.refCount++;
3738                }
3739
3740                FreeType(sourceExp.expType);
3741                sourceExp.expType = MkClassType(_class.fullName);
3742                sourceExp.expType.truth = source.truth;
3743                sourceExp.expType.classObjectType = source.classObjectType;
3744
3745                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3746
3747                if(!sourceExp.destType)
3748                {
3749                   FreeType(sourceExp.destType);
3750                   sourceExp.destType = sourceExp.expType;
3751                   if(sourceExp.expType)
3752                      sourceExp.expType.refCount++;
3753                }
3754                //flag = true;
3755                //source = _class.dataType;
3756
3757
3758                // TOCHECK: TESTING THIS NEW CODE
3759                if(!_class.dataType)
3760                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3761                FreeType(dest);
3762                dest = MkClassType(source._class.string);
3763                dest.truth = source.truth;
3764                dest.classObjectType = source.classObjectType;
3765
3766                FreeType(source);
3767                source = _class.dataType;
3768                source.refCount++;
3769
3770                delete tempType;
3771             }
3772          }
3773       }
3774
3775       if(!flag)
3776       {
3777          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3778          {
3779             FreeType(source);
3780             FreeType(dest);
3781             return true;
3782          }
3783       }
3784
3785       // Implicit Casts
3786       /*
3787       if(source.kind == classType)
3788       {
3789          Class _class = source._class.registered;
3790          if(_class.type == unitClass)
3791          {
3792             if(!_class.dataType)
3793                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3794             source = _class.dataType;
3795          }
3796       }*/
3797
3798       if(dest.kind == classType)
3799       {
3800          Class _class = dest._class ? dest._class.registered : null;
3801          bool fittingValue = false;
3802          if(_class && _class.type == enumClass)
3803          {
3804             Class enumClass = eSystem_FindClass(privateModule, "enum");
3805             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3806             if(c && value >= 0 && value <= c.largest)
3807                fittingValue = true;
3808          }
3809
3810          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3811             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3812          {
3813             if(_class.type == normalClass || _class.type == noHeadClass)
3814             {
3815                Expression newExp { };
3816                *newExp = *sourceExp;
3817                if(sourceExp.destType) sourceExp.destType.refCount++;
3818                if(sourceExp.expType)  sourceExp.expType.refCount++;
3819                sourceExp.type = castExp;
3820                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3821                sourceExp.cast.exp = newExp;
3822                FreeType(sourceExp.expType);
3823                sourceExp.expType = null;
3824                ProcessExpressionType(sourceExp);
3825
3826                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3827                if(!inCompiler)
3828                {
3829                   FreeType(sourceExp.expType);
3830                   sourceExp.expType = dest;
3831                }
3832
3833                FreeType(source);
3834                if(inCompiler) FreeType(dest);
3835
3836                if(backupSourceExpType) FreeType(backupSourceExpType);
3837                return true;
3838             }
3839
3840             if(!_class.dataType)
3841                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3842             FreeType(dest);
3843             dest = _class.dataType;
3844             dest.refCount++;
3845          }
3846
3847          // Accept lower precision types for units, since we want to keep the unit type
3848          if(dest.kind == doubleType &&
3849             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3850              source.kind == charType || source.kind == _BoolType))
3851          {
3852             specs = MkListOne(MkSpecifier(DOUBLE));
3853          }
3854          else if(dest.kind == floatType &&
3855             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3856             source.kind == _BoolType || source.kind == doubleType))
3857          {
3858             specs = MkListOne(MkSpecifier(FLOAT));
3859          }
3860          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3861             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3862          {
3863             specs = MkList();
3864             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3865             ListAdd(specs, MkSpecifier(INT64));
3866          }
3867          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3868             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3869          {
3870             specs = MkList();
3871             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3872             ListAdd(specs, MkSpecifier(INT));
3873          }
3874          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3875             source.kind == floatType || source.kind == doubleType))
3876          {
3877             specs = MkList();
3878             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3879             ListAdd(specs, MkSpecifier(SHORT));
3880          }
3881          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3882             source.kind == floatType || source.kind == doubleType))
3883          {
3884             specs = MkList();
3885             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3886             ListAdd(specs, MkSpecifier(CHAR));
3887          }
3888          else
3889          {
3890             FreeType(source);
3891             FreeType(dest);
3892             if(backupSourceExpType)
3893             {
3894                // Failed to convert: revert previous exp type
3895                if(sourceExp.expType) FreeType(sourceExp.expType);
3896                sourceExp.expType = backupSourceExpType;
3897             }
3898             return false;
3899          }
3900       }
3901       else if(dest.kind == doubleType &&
3902          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3903           source.kind == _BoolType || source.kind == charType))
3904       {
3905          specs = MkListOne(MkSpecifier(DOUBLE));
3906       }
3907       else if(dest.kind == floatType &&
3908          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3909       {
3910          specs = MkListOne(MkSpecifier(FLOAT));
3911       }
3912       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3913          (value == 1 || value == 0))
3914       {
3915          specs = MkList();
3916          ListAdd(specs, MkSpecifier(BOOL));
3917       }
3918       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3919          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3920       {
3921          specs = MkList();
3922          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3923          ListAdd(specs, MkSpecifier(CHAR));
3924       }
3925       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3926          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3927       {
3928          specs = MkList();
3929          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3930          ListAdd(specs, MkSpecifier(SHORT));
3931       }
3932       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3933       {
3934          specs = MkList();
3935          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3936          ListAdd(specs, MkSpecifier(INT));
3937       }
3938       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3939       {
3940          specs = MkList();
3941          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3942          ListAdd(specs, MkSpecifier(INT64));
3943       }
3944       else if(dest.kind == enumType &&
3945          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3946       {
3947          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3948       }
3949       else
3950       {
3951          FreeType(source);
3952          FreeType(dest);
3953          if(backupSourceExpType)
3954          {
3955             // Failed to convert: revert previous exp type
3956             if(sourceExp.expType) FreeType(sourceExp.expType);
3957             sourceExp.expType = backupSourceExpType;
3958          }
3959          return false;
3960       }
3961
3962       if(!flag && !sourceExp.opDestType)
3963       {
3964          Expression newExp { };
3965          *newExp = *sourceExp;
3966          newExp.prev = null;
3967          newExp.next = null;
3968          if(sourceExp.destType) sourceExp.destType.refCount++;
3969          if(sourceExp.expType)  sourceExp.expType.refCount++;
3970
3971          sourceExp.type = castExp;
3972          if(realDest.kind == classType)
3973          {
3974             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3975             FreeList(specs, FreeSpecifier);
3976          }
3977          else
3978             sourceExp.cast.typeName = MkTypeName(specs, null);
3979          if(newExp.type == opExp)
3980          {
3981             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3982          }
3983          else
3984             sourceExp.cast.exp = newExp;
3985
3986          FreeType(sourceExp.expType);
3987          sourceExp.expType = null;
3988          ProcessExpressionType(sourceExp);
3989       }
3990       else
3991          FreeList(specs, FreeSpecifier);
3992
3993       FreeType(dest);
3994       FreeType(source);
3995       if(backupSourceExpType) FreeType(backupSourceExpType);
3996
3997       return true;
3998    }
3999    else
4000    {
4001       if(computedExp != sourceExp)
4002       {
4003          FreeExpression(computedExp);
4004          computedExp = sourceExp;
4005       }
4006
4007       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4008       if(sourceExp.type == identifierExp)
4009       {
4010          Identifier id = sourceExp.identifier;
4011          if(dest.kind == classType)
4012          {
4013             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4014             {
4015                Class _class = dest._class.registered;
4016                Class enumClass = eSystem_FindClass(privateModule, "enum");
4017                if(enumClass)
4018                {
4019                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4020                   {
4021                      NamedLink64 value;
4022                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4023                      for(value = e.values.first; value; value = value.next)
4024                      {
4025                         if(!strcmp(value.name, id.string))
4026                            break;
4027                      }
4028                      if(value)
4029                      {
4030                         FreeType(sourceExp.expType);
4031
4032                         sourceExp.isConstant = true;
4033                         sourceExp.expType = MkClassType(_class.fullName);
4034                         if(inCompiler || inPreCompiler || inDebugger)
4035                         {
4036                            FreeExpContents(sourceExp);
4037
4038                            sourceExp.type = constantExp;
4039                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4040                               sourceExp.constant = PrintInt64(value.data);
4041                            else
4042                               sourceExp.constant = PrintUInt64(value.data);
4043                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4044                         }
4045                         FreeType(dest);
4046                         return true;
4047                      }
4048                   }
4049                }
4050             }
4051          }
4052
4053          // Loop through all enum classes
4054          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4055          {
4056             FreeType(dest);
4057             return true;
4058          }
4059       }
4060       FreeType(dest);
4061    }
4062    return false;
4063 }
4064
4065 #define TERTIARY(o, name, m, t, p) \
4066    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4067    {                                                              \
4068       exp.type = constantExp;                                    \
4069       exp.string = p(op1.m ? op2.m : op3.m);                     \
4070       if(!exp.expType) \
4071          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4072       return true;                                                \
4073    }
4074
4075 #define BINARY(o, name, m, t, p) \
4076    static bool name(Expression exp, Operand op1, Operand op2)   \
4077    {                                                              \
4078       t value2 = op2.m;                                           \
4079       exp.type = constantExp;                                    \
4080       exp.string = p((t)(op1.m o value2));                     \
4081       if(!exp.expType) \
4082          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4083       return true;                                                \
4084    }
4085
4086 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4087    static bool name(Expression exp, Operand op1, Operand op2)   \
4088    {                                                              \
4089       t value2 = op2.m;                                           \
4090       exp.type = constantExp;                                    \
4091       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4092       if(!exp.expType) \
4093          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4094       return true;                                                \
4095    }
4096
4097 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4098    static bool name(Expression exp, Operand op1, Operand op2)   \
4099    {                                                              \
4100       t value2 = op2.m;                                           \
4101       exp.type = constantExp;                                    \
4102       exp.string = p(op1.m o value2);             \
4103       if(!exp.expType) \
4104          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4105       return true;                                                \
4106    }
4107
4108 #define UNARY(o, name, m, t, p) \
4109    static bool name(Expression exp, Operand op1)                \
4110    {                                                              \
4111       exp.type = constantExp;                                    \
4112       exp.string = p((t)(o op1.m));                                   \
4113       if(!exp.expType) \
4114          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4115       return true;                                                \
4116    }
4117
4118 #define OPERATOR_ALL(macro, o, name) \
4119    macro(o, Int##name, i, int, PrintInt) \
4120    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4121    macro(o, Int64##name, i64, int64, PrintInt64) \
4122    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4123    macro(o, Short##name, s, short, PrintShort) \
4124    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4125    macro(o, Char##name, c, char, PrintChar) \
4126    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4127    macro(o, Float##name, f, float, PrintFloat) \
4128    macro(o, Double##name, d, double, PrintDouble)
4129
4130 #define OPERATOR_INTTYPES(macro, o, name) \
4131    macro(o, Int##name, i, int, PrintInt) \
4132    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4133    macro(o, Int64##name, i64, int64, PrintInt64) \
4134    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4135    macro(o, Short##name, s, short, PrintShort) \
4136    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4137    macro(o, Char##name, c, char, PrintChar) \
4138    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4139
4140 #define OPERATOR_REALTYPES(macro, o, name) \
4141    macro(o, Float##name, f, float, PrintFloat) \
4142    macro(o, Double##name, d, double, PrintDouble)
4143
4144 // binary arithmetic
4145 OPERATOR_ALL(BINARY, +, Add)
4146 OPERATOR_ALL(BINARY, -, Sub)
4147 OPERATOR_ALL(BINARY, *, Mul)
4148 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4149 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4150 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4151
4152 // unary arithmetic
4153 OPERATOR_ALL(UNARY, -, Neg)
4154
4155 // unary arithmetic increment and decrement
4156 OPERATOR_ALL(UNARY, ++, Inc)
4157 OPERATOR_ALL(UNARY, --, Dec)
4158
4159 // binary arithmetic assignment
4160 OPERATOR_ALL(BINARY, =, Asign)
4161 OPERATOR_ALL(BINARY, +=, AddAsign)
4162 OPERATOR_ALL(BINARY, -=, SubAsign)
4163 OPERATOR_ALL(BINARY, *=, MulAsign)
4164 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4165 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4166 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4167
4168 // binary bitwise
4169 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4170 OPERATOR_INTTYPES(BINARY, |, BitOr)
4171 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4172 OPERATOR_INTTYPES(BINARY, <<, LShift)
4173 OPERATOR_INTTYPES(BINARY, >>, RShift)
4174
4175 // unary bitwise
4176 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4177
4178 // binary bitwise assignment
4179 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4180 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4181 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4182 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4183 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4184
4185 // unary logical negation
4186 OPERATOR_INTTYPES(UNARY, !, Not)
4187
4188 // binary logical equality
4189 OPERATOR_ALL(BINARY, ==, Equ)
4190 OPERATOR_ALL(BINARY, !=, Nqu)
4191
4192 // binary logical
4193 OPERATOR_ALL(BINARY, &&, And)
4194 OPERATOR_ALL(BINARY, ||, Or)
4195
4196 // binary logical relational
4197 OPERATOR_ALL(BINARY, >, Grt)
4198 OPERATOR_ALL(BINARY, <, Sma)
4199 OPERATOR_ALL(BINARY, >=, GrtEqu)
4200 OPERATOR_ALL(BINARY, <=, SmaEqu)
4201
4202 // tertiary condition operator
4203 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4204
4205 //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
4206 #define OPERATOR_TABLE_ALL(name, type) \
4207     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4208                           type##Neg, \
4209                           type##Inc, type##Dec, \
4210                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4211                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4212                           type##BitNot, \
4213                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4214                           type##Not, \
4215                           type##Equ, type##Nqu, \
4216                           type##And, type##Or, \
4217                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4218                         }; \
4219
4220 #define OPERATOR_TABLE_INTTYPES(name, type) \
4221     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4222                           type##Neg, \
4223                           type##Inc, type##Dec, \
4224                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4225                           null, null, null, null, null, \
4226                           null, \
4227                           null, null, null, null, null, \
4228                           null, \
4229                           type##Equ, type##Nqu, \
4230                           type##And, type##Or, \
4231                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4232                         }; \
4233
4234 OPERATOR_TABLE_ALL(int, Int)
4235 OPERATOR_TABLE_ALL(uint, UInt)
4236 OPERATOR_TABLE_ALL(int64, Int64)
4237 OPERATOR_TABLE_ALL(uint64, UInt64)
4238 OPERATOR_TABLE_ALL(short, Short)
4239 OPERATOR_TABLE_ALL(ushort, UShort)
4240 OPERATOR_TABLE_INTTYPES(float, Float)
4241 OPERATOR_TABLE_INTTYPES(double, Double)
4242 OPERATOR_TABLE_ALL(char, Char)
4243 OPERATOR_TABLE_ALL(uchar, UChar)
4244
4245 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4246 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4247 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4248 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4249 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4250 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4251 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4252 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4253
4254 public void ReadString(char * output,  char * string)
4255 {
4256    int len = strlen(string);
4257    int c,d = 0;
4258    bool quoted = false, escaped = false;
4259    for(c = 0; c<len; c++)
4260    {
4261       char ch = string[c];
4262       if(escaped)
4263       {
4264          switch(ch)
4265          {
4266             case 'n': output[d] = '\n'; break;
4267             case 't': output[d] = '\t'; break;
4268             case 'a': output[d] = '\a'; break;
4269             case 'b': output[d] = '\b'; break;
4270             case 'f': output[d] = '\f'; break;
4271             case 'r': output[d] = '\r'; break;
4272             case 'v': output[d] = '\v'; break;
4273             case '\\': output[d] = '\\'; break;
4274             case '\"': output[d] = '\"'; break;
4275             case '\'': output[d] = '\''; break;
4276             default: output[d] = ch;
4277          }
4278          d++;
4279          escaped = false;
4280       }
4281       else
4282       {
4283          if(ch == '\"')
4284             quoted ^= true;
4285          else if(quoted)
4286          {
4287             if(ch == '\\')
4288                escaped = true;
4289             else
4290                output[d++] = ch;
4291          }
4292       }
4293    }
4294    output[d] = '\0';
4295 }
4296
4297 // String Unescape Copy
4298
4299 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4300 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4301 public int UnescapeString(char * d, char * s, int len)
4302 {
4303    int j = 0, k = 0;
4304    char ch;
4305    while(j < len && (ch = s[j]))
4306    {
4307       switch(ch)
4308       {
4309          case '\\':
4310             switch((ch = s[++j]))
4311             {
4312                case 'n': d[k] = '\n'; break;
4313                case 't': d[k] = '\t'; break;
4314                case 'a': d[k] = '\a'; break;
4315                case 'b': d[k] = '\b'; break;
4316                case 'f': d[k] = '\f'; break;
4317                case 'r': d[k] = '\r'; break;
4318                case 'v': d[k] = '\v'; break;
4319                case '\\': d[k] = '\\'; break;
4320                case '\"': d[k] = '\"'; break;
4321                case '\'': d[k] = '\''; break;
4322                default: d[k] = '\\'; d[k] = ch;
4323             }
4324             break;
4325          default:
4326             d[k] = ch;
4327       }
4328       j++, k++;
4329    }
4330    d[k] = '\0';
4331    return k;
4332 }
4333
4334 public char * OffsetEscapedString(char * s, int len, int offset)
4335 {
4336    char ch;
4337    int j = 0, k = 0;
4338    while(j < len && k < offset && (ch = s[j]))
4339    {
4340       if(ch == '\\') ++j;
4341       j++, k++;
4342    }
4343    return (k == offset) ? s + j : null;
4344 }
4345
4346 public Operand GetOperand(Expression exp)
4347 {
4348    Operand op { };
4349    Type type = exp.expType;
4350    if(type)
4351    {
4352       while(type.kind == classType &&
4353          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4354       {
4355          if(!type._class.registered.dataType)
4356             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4357          type = type._class.registered.dataType;
4358
4359       }
4360       if(exp.type == stringExp && op.kind == pointerType)
4361       {
4362          op.ui64 = (uint64)(uintptr)exp.string;
4363          op.kind = pointerType;
4364          op.ops = uint64Ops;
4365       }
4366       else if(exp.isConstant && exp.type == constantExp)
4367       {
4368          op.kind = type.kind;
4369          op.type = type;
4370
4371          switch(op.kind)
4372          {
4373             case _BoolType:
4374             case charType:
4375             {
4376                if(exp.constant[0] == '\'')
4377                {
4378                   op.c = exp.constant[1];
4379                   op.ops = charOps;
4380                }
4381                else if(type.isSigned)
4382                {
4383                   op.c = (char)strtol(exp.constant, null, 0);
4384                   op.ops = charOps;
4385                }
4386                else
4387                {
4388                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4389                   op.ops = ucharOps;
4390                }
4391                break;
4392             }
4393             case shortType:
4394                if(type.isSigned)
4395                {
4396                   op.s = (short)strtol(exp.constant, null, 0);
4397                   op.ops = shortOps;
4398                }
4399                else
4400                {
4401                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4402                   op.ops = ushortOps;
4403                }
4404                break;
4405             case intType:
4406             case longType:
4407                if(type.isSigned)
4408                {
4409                   op.i = (int)strtol(exp.constant, null, 0);
4410                   op.ops = intOps;
4411                }
4412                else
4413                {
4414                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4415                   op.ops = uintOps;
4416                }
4417                op.kind = intType;
4418                break;
4419             case int64Type:
4420                if(type.isSigned)
4421                {
4422                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4423                   op.ops = int64Ops;
4424                }
4425                else
4426                {
4427                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4428                   op.ops = uint64Ops;
4429                }
4430                op.kind = int64Type;
4431                break;
4432             case intPtrType:
4433                if(type.isSigned)
4434                {
4435                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4436                   op.ops = int64Ops;
4437                }
4438                else
4439                {
4440                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4441                   op.ops = uint64Ops;
4442                }
4443                op.kind = int64Type;
4444                break;
4445             case intSizeType:
4446                if(type.isSigned)
4447                {
4448                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4449                   op.ops = int64Ops;
4450                }
4451                else
4452                {
4453                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4454                   op.ops = uint64Ops;
4455                }
4456                op.kind = int64Type;
4457                break;
4458             case floatType:
4459                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4460                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4461                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4462                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4463                else
4464                   op.f = (float)strtod(exp.constant, null);
4465                op.ops = floatOps;
4466                break;
4467             case doubleType:
4468                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4469                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4470                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4471                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4472                else
4473                   op.d = (double)strtod(exp.constant, null);
4474                op.ops = doubleOps;
4475                break;
4476             //case classType:    For when we have operator overloading...
4477             // Pointer additions
4478             //case functionType:
4479             case arrayType:
4480             case pointerType:
4481             case classType:
4482                op.ui64 = _strtoui64(exp.constant, null, 0);
4483                op.kind = pointerType;
4484                op.ops = uint64Ops;
4485                // op.ptrSize =
4486                break;
4487          }
4488       }
4489    }
4490    return op;
4491 }
4492
4493 static int64 GetEnumValue(Class _class, void * ptr)
4494 {
4495    int64 v = 0;
4496    switch(_class.typeSize)
4497    {
4498       case 8:
4499          if(!strcmp(_class.dataTypeString, "uint64"))
4500             v = (int64)*(uint64 *)ptr;
4501          else
4502             v = (int64)*(int64 *)ptr;
4503          break;
4504       case 4:
4505          if(!strcmp(_class.dataTypeString, "uint"))
4506             v = (int64)*(uint *)ptr;
4507          else
4508             v = (int64)*(int *)ptr;
4509          break;
4510       case 2:
4511          if(!strcmp(_class.dataTypeString, "uint16"))
4512             v = (int64)*(uint16 *)ptr;
4513          else
4514             v = (int64)*(short *)ptr;
4515          break;
4516       case 1:
4517          if(!strcmp(_class.dataTypeString, "byte"))
4518             v = (int64)*(byte *)ptr;
4519          else
4520             v = (int64)*(char *)ptr;
4521          break;
4522    }
4523    return v;
4524 }
4525
4526 static __attribute__((unused)) void UnusedFunction()
4527 {
4528    int a;
4529    a.OnGetString(0,0,0);
4530 }
4531 default:
4532 extern int __ecereVMethodID_class_OnGetString;
4533 public:
4534
4535 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4536 {
4537    DataMember dataMember;
4538    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4539    {
4540       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4541          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4542       else
4543       {
4544          Expression exp { };
4545          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4546          Type type;
4547          void * ptr = inst.data + dataMember.offset + offset;
4548          char * result = null;
4549          exp.loc = member.loc = inst.loc;
4550          ((Identifier)member.identifiers->first).loc = inst.loc;
4551
4552          if(!dataMember.dataType)
4553             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4554          type = dataMember.dataType;
4555          if(type.kind == classType)
4556          {
4557             Class _class = type._class.registered;
4558             if(_class.type == enumClass)
4559             {
4560                Class enumClass = eSystem_FindClass(privateModule, "enum");
4561                if(enumClass)
4562                {
4563                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4564                   NamedLink64 item;
4565                   for(item = e.values.first; item; item = item.next)
4566                   {
4567                      if(item.data == GetEnumValue(_class, ptr))
4568                      {
4569                         result = item.name;
4570                         break;
4571                      }
4572                   }
4573                   if(result)
4574                   {
4575                      exp.identifier = MkIdentifier(result);
4576                      exp.type = identifierExp;
4577                      exp.destType = MkClassType(_class.fullName);
4578                      ProcessExpressionType(exp);
4579                   }
4580                }
4581             }
4582             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4583             {
4584                if(!_class.dataType)
4585                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4586                type = _class.dataType;
4587             }
4588          }
4589          if(!result)
4590          {
4591             switch(type.kind)
4592             {
4593                case floatType:
4594                {
4595                   FreeExpContents(exp);
4596
4597                   exp.constant = PrintFloat(*(float*)ptr);
4598                   exp.type = constantExp;
4599                   break;
4600                }
4601                case doubleType:
4602                {
4603                   FreeExpContents(exp);
4604
4605                   exp.constant = PrintDouble(*(double*)ptr);
4606                   exp.type = constantExp;
4607                   break;
4608                }
4609                case intType:
4610                {
4611                   FreeExpContents(exp);
4612
4613                   exp.constant = PrintInt(*(int*)ptr);
4614                   exp.type = constantExp;
4615                   break;
4616                }
4617                case int64Type:
4618                {
4619                   FreeExpContents(exp);
4620
4621                   exp.constant = PrintInt64(*(int64*)ptr);
4622                   exp.type = constantExp;
4623                   break;
4624                }
4625                case intPtrType:
4626                {
4627                   FreeExpContents(exp);
4628                   // TODO: This should probably use proper type
4629                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4630                   exp.type = constantExp;
4631                   break;
4632                }
4633                case intSizeType:
4634                {
4635                   FreeExpContents(exp);
4636                   // TODO: This should probably use proper type
4637                   exp.constant = PrintInt64((int64)*(intsize*)ptr);
4638                   exp.type = constantExp;
4639                   break;
4640                }
4641                default:
4642                   Compiler_Error($"Unhandled type populating instance\n");
4643             }
4644          }
4645          ListAdd(memberList, member);
4646       }
4647
4648       if(parentDataMember.type == unionMember)
4649          break;
4650    }
4651 }
4652
4653 void PopulateInstance(Instantiation inst)
4654 {
4655    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4656    Class _class = classSym.registered;
4657    DataMember dataMember;
4658    OldList * memberList = MkList();
4659    // Added this check and ->Add to prevent memory leaks on bad code
4660    if(!inst.members)
4661       inst.members = MkListOne(MkMembersInitList(memberList));
4662    else
4663       inst.members->Add(MkMembersInitList(memberList));
4664    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4665    {
4666       if(!dataMember.isProperty)
4667       {
4668          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4669             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4670          else
4671          {
4672             Expression exp { };
4673             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4674             Type type;
4675             void * ptr = inst.data + dataMember.offset;
4676             char * result = null;
4677
4678             exp.loc = member.loc = inst.loc;
4679             ((Identifier)member.identifiers->first).loc = inst.loc;
4680
4681             if(!dataMember.dataType)
4682                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4683             type = dataMember.dataType;
4684             if(type.kind == classType)
4685             {
4686                Class _class = type._class.registered;
4687                if(_class.type == enumClass)
4688                {
4689                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4690                   if(enumClass)
4691                   {
4692                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4693                      NamedLink64 item;
4694                      for(item = e.values.first; item; item = item.next)
4695                      {
4696                         if(item.data == GetEnumValue(_class, ptr))
4697                         {
4698                            result = item.name;
4699                            break;
4700                         }
4701                      }
4702                   }
4703                   if(result)
4704                   {
4705                      exp.identifier = MkIdentifier(result);
4706                      exp.type = identifierExp;
4707                      exp.destType = MkClassType(_class.fullName);
4708                      ProcessExpressionType(exp);
4709                   }
4710                }
4711                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4712                {
4713                   if(!_class.dataType)
4714                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4715                   type = _class.dataType;
4716                }
4717             }
4718             if(!result)
4719             {
4720                switch(type.kind)
4721                {
4722                   case floatType:
4723                   {
4724                      exp.constant = PrintFloat(*(float*)ptr);
4725                      exp.type = constantExp;
4726                      break;
4727                   }
4728                   case doubleType:
4729                   {
4730                      exp.constant = PrintDouble(*(double*)ptr);
4731                      exp.type = constantExp;
4732                      break;
4733                   }
4734                   case intType:
4735                   {
4736                      exp.constant = PrintInt(*(int*)ptr);
4737                      exp.type = constantExp;
4738                      break;
4739                   }
4740                   case int64Type:
4741                   {
4742                      exp.constant = PrintInt64(*(int64*)ptr);
4743                      exp.type = constantExp;
4744                      break;
4745                   }
4746                   case intPtrType:
4747                   {
4748                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4749                      exp.type = constantExp;
4750                      break;
4751                   }
4752                   default:
4753                      Compiler_Error($"Unhandled type populating instance\n");
4754                }
4755             }
4756             ListAdd(memberList, member);
4757          }
4758       }
4759    }
4760 }
4761
4762 void ComputeInstantiation(Expression exp)
4763 {
4764    Instantiation inst = exp.instance;
4765    MembersInit members;
4766    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4767    Class _class = classSym ? classSym.registered : null;
4768    DataMember curMember = null;
4769    Class curClass = null;
4770    DataMember subMemberStack[256];
4771    int subMemberStackPos = 0;
4772    uint64 bits = 0;
4773
4774    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4775    {
4776       // Don't recompute the instantiation...
4777       // Non Simple classes will have become constants by now
4778       if(inst.data)
4779          return;
4780
4781       if(_class.type == normalClass || _class.type == noHeadClass)
4782       {
4783          inst.data = (byte *)eInstance_New(_class);
4784          if(_class.type == normalClass)
4785             ((Instance)inst.data)._refCount++;
4786       }
4787       else
4788          inst.data = new0 byte[_class.structSize];
4789    }
4790
4791    if(inst.members)
4792    {
4793       for(members = inst.members->first; members; members = members.next)
4794       {
4795          switch(members.type)
4796          {
4797             case dataMembersInit:
4798             {
4799                if(members.dataMembers)
4800                {
4801                   MemberInit member;
4802                   for(member = members.dataMembers->first; member; member = member.next)
4803                   {
4804                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4805                      bool found = false;
4806
4807                      Property prop = null;
4808                      DataMember dataMember = null;
4809                      uint dataMemberOffset;
4810
4811                      if(!ident)
4812                      {
4813                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4814                         if(curMember)
4815                         {
4816                            if(curMember.isProperty)
4817                               prop = (Property)curMember;
4818                            else
4819                            {
4820                               dataMember = curMember;
4821
4822                               // CHANGED THIS HERE
4823                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4824
4825                               // 2013/17/29 -- It seems that this was missing here!
4826                               if(_class.type == normalClass)
4827                                  dataMemberOffset += _class.base.structSize;
4828                               // dataMemberOffset = dataMember.offset;
4829                            }
4830                            found = true;
4831                         }
4832                      }
4833                      else
4834                      {
4835                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4836                         if(prop)
4837                         {
4838                            found = true;
4839                            if(prop.memberAccess == publicAccess)
4840                            {
4841                               curMember = (DataMember)prop;
4842                               curClass = prop._class;
4843                            }
4844                         }
4845                         else
4846                         {
4847                            DataMember _subMemberStack[256];
4848                            int _subMemberStackPos = 0;
4849
4850                            // FILL MEMBER STACK
4851                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4852
4853                            if(dataMember)
4854                            {
4855                               found = true;
4856                               if(dataMember.memberAccess == publicAccess)
4857                               {
4858                                  curMember = dataMember;
4859                                  curClass = dataMember._class;
4860                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4861                                  subMemberStackPos = _subMemberStackPos;
4862                               }
4863                            }
4864                         }
4865                      }
4866
4867                      if(found && member.initializer && member.initializer.type == expInitializer)
4868                      {
4869                         Expression value = member.initializer.exp;
4870                         Type type = null;
4871                         bool deepMember = false;
4872                         if(prop)
4873                         {
4874                            type = prop.dataType;
4875                         }
4876                         else if(dataMember)
4877                         {
4878                            if(!dataMember.dataType)
4879                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4880
4881                            type = dataMember.dataType;
4882                         }
4883
4884                         if(ident && ident.next)
4885                         {
4886                            deepMember = true;
4887
4888                            // for(; ident && type; ident = ident.next)
4889                            for(ident = ident.next; ident && type; ident = ident.next)
4890                            {
4891                               if(type.kind == classType)
4892                               {
4893                                  prop = eClass_FindProperty(type._class.registered,
4894                                     ident.string, privateModule);
4895                                  if(prop)
4896                                     type = prop.dataType;
4897                                  else
4898                                  {
4899                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4900                                        ident.string, &dataMemberOffset, privateModule, null, null);
4901                                     if(dataMember)
4902                                        type = dataMember.dataType;
4903                                  }
4904                               }
4905                               else if(type.kind == structType || type.kind == unionType)
4906                               {
4907                                  Type memberType;
4908                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4909                                  {
4910                                     if(!strcmp(memberType.name, ident.string))
4911                                     {
4912                                        type = memberType;
4913                                        break;
4914                                     }
4915                                  }
4916                               }
4917                            }
4918                         }
4919                         if(value)
4920                         {
4921                            FreeType(value.destType);
4922                            value.destType = type;
4923                            if(type) type.refCount++;
4924                            ComputeExpression(value);
4925                         }
4926                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4927                         {
4928                            if(type.kind == classType)
4929                            {
4930                               Class _class = type._class.registered;
4931                               if(_class && (_class.type == bitClass || _class.type == unitClass || _class.type == enumClass))
4932                               {
4933                                  if(!_class.dataType)
4934                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4935                                  type = _class.dataType;
4936                               }
4937                            }
4938
4939                            if(dataMember)
4940                            {
4941                               void * ptr = inst.data + dataMemberOffset;
4942
4943                               if(value.type == constantExp)
4944                               {
4945                                  switch(type.kind)
4946                                  {
4947                                     case intType:
4948                                     {
4949                                        GetInt(value, (int*)ptr);
4950                                        break;
4951                                     }
4952                                     case int64Type:
4953                                     {
4954                                        GetInt64(value, (int64*)ptr);
4955                                        break;
4956                                     }
4957                                     case intPtrType:
4958                                     {
4959                                        GetIntPtr(value, (intptr*)ptr);
4960                                        break;
4961                                     }
4962                                     case intSizeType:
4963                                     {
4964                                        GetIntSize(value, (intsize*)ptr);
4965                                        break;
4966                                     }
4967                                     case floatType:
4968                                     {
4969                                        GetFloat(value, (float*)ptr);
4970                                        break;
4971                                     }
4972                                     case doubleType:
4973                                     {
4974                                        GetDouble(value, (double *)ptr);
4975                                        break;
4976                                     }
4977                                  }
4978                               }
4979                               else if(value.type == instanceExp)
4980                               {
4981                                  if(type.kind == classType)
4982                                  {
4983                                     Class _class = type._class.registered;
4984                                     if(_class.type == structClass)
4985                                     {
4986                                        ComputeTypeSize(type);
4987                                        if(value.instance.data)
4988                                           memcpy(ptr, value.instance.data, type.size);
4989                                     }
4990                                  }
4991                               }
4992                            }
4993                            else if(prop && prop.Set != (void *)(intptr)1)
4994                            {
4995                               if(value.type == instanceExp && value.instance.data)
4996                               {
4997                                  if(type.kind == classType)
4998                                  {
4999                                     Class _class = type._class.registered;
5000                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5001                                     {
5002                                        void (*Set)(void *, void *) = (void *)prop.Set;
5003                                        Set(inst.data, value.instance.data);
5004                                        PopulateInstance(inst);
5005                                     }
5006                                  }
5007                               }
5008                               else if(value.type == constantExp)
5009                               {
5010                                  switch(type.kind)
5011                                  {
5012                                     case doubleType:
5013                                     {
5014                                        void (*Set)(void *, double) = (void *)prop.Set;
5015                                        Set(inst.data, strtod(value.constant, null) );
5016                                        break;
5017                                     }
5018                                     case floatType:
5019                                     {
5020                                        void (*Set)(void *, float) = (void *)prop.Set;
5021                                        Set(inst.data, (float)(strtod(value.constant, null)));
5022                                        break;
5023                                     }
5024                                     case intType:
5025                                     {
5026                                        void (*Set)(void *, int) = (void *)prop.Set;
5027                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5028                                        break;
5029                                     }
5030                                     case int64Type:
5031                                     {
5032                                        void (*Set)(void *, int64) = (void *)prop.Set;
5033                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5034                                        break;
5035                                     }
5036                                     case intPtrType:
5037                                     {
5038                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5039                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5040                                        break;
5041                                     }
5042                                     case intSizeType:
5043                                     {
5044                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5045                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5046                                        break;
5047                                     }
5048                                  }
5049                               }
5050                               else if(value.type == stringExp)
5051                               {
5052                                  char temp[1024];
5053                                  ReadString(temp, value.string);
5054                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5055                               }
5056                            }
5057                         }
5058                         else if(!deepMember && type && _class.type == unitClass)
5059                         {
5060                            if(prop)
5061                            {
5062                               // Only support converting units to units for now...
5063                               if(value.type == constantExp)
5064                               {
5065                                  if(type.kind == classType)
5066                                  {
5067                                     Class _class = type._class.registered;
5068                                     if(_class.type == unitClass)
5069                                     {
5070                                        if(!_class.dataType)
5071                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5072                                        type = _class.dataType;
5073                                     }
5074                                  }
5075                                  // TODO: Assuming same base type for units...
5076                                  switch(type.kind)
5077                                  {
5078                                     case floatType:
5079                                     {
5080                                        float fValue;
5081                                        float (*Set)(float) = (void *)prop.Set;
5082                                        GetFloat(member.initializer.exp, &fValue);
5083                                        exp.constant = PrintFloat(Set(fValue));
5084                                        exp.type = constantExp;
5085                                        break;
5086                                     }
5087                                     case doubleType:
5088                                     {
5089                                        double dValue;
5090                                        double (*Set)(double) = (void *)prop.Set;
5091                                        GetDouble(member.initializer.exp, &dValue);
5092                                        exp.constant = PrintDouble(Set(dValue));
5093                                        exp.type = constantExp;
5094                                        break;
5095                                     }
5096                                  }
5097                               }
5098                            }
5099                         }
5100                         else if(!deepMember && type && _class.type == bitClass)
5101                         {
5102                            if(prop)
5103                            {
5104                               if(value.type == instanceExp && value.instance.data)
5105                               {
5106                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5107                                  bits = Set(value.instance.data);
5108                               }
5109                               else if(value.type == constantExp)
5110                               {
5111                               }
5112                            }
5113                            else if(dataMember)
5114                            {
5115                               BitMember bitMember = (BitMember) dataMember;
5116                               Type type;
5117                               uint64 part = 0;
5118                               bits = (bits & ~bitMember.mask);
5119                               if(!bitMember.dataType)
5120                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5121                               type = bitMember.dataType;
5122                               if(type.kind == classType && type._class && type._class.registered)
5123                               {
5124                                  if(!type._class.registered.dataType)
5125                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5126                                  type = type._class.registered.dataType;
5127                               }
5128                               switch(type.kind)
5129                               {
5130                                  case _BoolType:
5131                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5132                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5133                                  case intType:
5134                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5135                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5136                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5137                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5138                               }
5139                               bits |= part << bitMember.pos;
5140                            }
5141                         }
5142                      }
5143                      else
5144                      {
5145                         if(_class && _class.type == unitClass)
5146                         {
5147                            ComputeExpression(member.initializer.exp);
5148                            exp.constant = member.initializer.exp.constant;
5149                            exp.type = constantExp;
5150
5151                            member.initializer.exp.constant = null;
5152                         }
5153                      }
5154                   }
5155                }
5156                break;
5157             }
5158          }
5159       }
5160    }
5161    if(_class && _class.type == bitClass)
5162    {
5163       exp.constant = PrintHexUInt(bits);
5164       exp.type = constantExp;
5165    }
5166    if(exp.type != instanceExp)
5167    {
5168       FreeInstance(inst);
5169    }
5170 }
5171
5172 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5173 {
5174    bool result = false;
5175    switch(kind)
5176    {
5177       case shortType:
5178          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5179             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5180          break;
5181       case intType:
5182       case longType:
5183          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5184             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5185          break;
5186       case int64Type:
5187          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5188             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5189             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5190          break;
5191       case floatType:
5192          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5193             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5194             result = GetOpFloat(op, &op.f);
5195          break;
5196       case doubleType:
5197          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5198             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5199             result = GetOpDouble(op, &op.d);
5200          break;
5201       case pointerType:
5202          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5203             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5204             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5205          break;
5206       case enumType:
5207          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5208             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5209             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5210          break;
5211       case intPtrType:
5212          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5213             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5214          break;
5215       case intSizeType:
5216          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5217             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5218          break;
5219    }
5220    return result;
5221 }
5222
5223 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5224 {
5225    if(exp.op.op == SIZEOF)
5226    {
5227       FreeExpContents(exp);
5228       exp.type = constantExp;
5229       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5230    }
5231    else
5232    {
5233       if(!exp.op.exp1)
5234       {
5235          switch(exp.op.op)
5236          {
5237             // unary arithmetic
5238             case '+':
5239             {
5240                // Provide default unary +
5241                Expression exp2 = exp.op.exp2;
5242                exp.op.exp2 = null;
5243                FreeExpContents(exp);
5244                FreeType(exp.expType);
5245                FreeType(exp.destType);
5246                *exp = *exp2;
5247                delete exp2;
5248                break;
5249             }
5250             case '-':
5251                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5252                break;
5253             // unary arithmetic increment and decrement
5254                   //OPERATOR_ALL(UNARY, ++, Inc)
5255                   //OPERATOR_ALL(UNARY, --, Dec)
5256             // unary bitwise
5257             case '~':
5258                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5259                break;
5260             // unary logical negation
5261             case '!':
5262                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5263                break;
5264          }
5265       }
5266       else
5267       {
5268          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5269          {
5270             if(Promote(op2, op1.kind, op1.type.isSigned))
5271                op2.kind = op1.kind, op2.ops = op1.ops;
5272             else if(Promote(op1, op2.kind, op2.type.isSigned))
5273                op1.kind = op2.kind, op1.ops = op2.ops;
5274          }
5275          switch(exp.op.op)
5276          {
5277             // binary arithmetic
5278             case '+':
5279                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5280                break;
5281             case '-':
5282                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5283                break;
5284             case '*':
5285                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5286                break;
5287             case '/':
5288                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5289                break;
5290             case '%':
5291                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5292                break;
5293             // binary arithmetic assignment
5294                   //OPERATOR_ALL(BINARY, =, Asign)
5295                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5296                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5297                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5298                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5299                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5300             // binary bitwise
5301             case '&':
5302                if(exp.op.exp2)
5303                {
5304                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5305                }
5306                break;
5307             case '|':
5308                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5309                break;
5310             case '^':
5311                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5312                break;
5313             case LEFT_OP:
5314                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5315                break;
5316             case RIGHT_OP:
5317                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5318                break;
5319             // binary bitwise assignment
5320                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5321                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5322                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5323                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5324                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5325             // binary logical equality
5326             case EQ_OP:
5327                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5328                break;
5329             case NE_OP:
5330                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5331                break;
5332             // binary logical
5333             case AND_OP:
5334                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5335                break;
5336             case OR_OP:
5337                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5338                break;
5339             // binary logical relational
5340             case '>':
5341                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5342                break;
5343             case '<':
5344                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5345                break;
5346             case GE_OP:
5347                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5348                break;
5349             case LE_OP:
5350                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5351                break;
5352          }
5353       }
5354    }
5355 }
5356
5357 void ComputeExpression(Expression exp)
5358 {
5359 #ifdef _DEBUG
5360    char expString[10240];
5361    expString[0] = '\0';
5362    PrintExpression(exp, expString);
5363 #endif
5364
5365    switch(exp.type)
5366    {
5367       case identifierExp:
5368       {
5369          Identifier id = exp.identifier;
5370          if(id && exp.isConstant && !inCompiler && !inPreCompiler && !inDebugger)
5371          {
5372             Class c = (exp.expType && exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
5373             if(c && c.type == enumClass)
5374             {
5375                Class enumClass = eSystem_FindClass(privateModule, "enum");
5376                if(enumClass)
5377                {
5378                   NamedLink64 value;
5379                   EnumClassData e = ACCESS_CLASSDATA(c, enumClass);
5380                   for(value = e.values.first; value; value = value.next)
5381                   {
5382                      if(!strcmp(value.name, id.string))
5383                         break;
5384                   }
5385                   if(value)
5386                   {
5387                      const String dts = c.dataTypeString;
5388                      FreeExpContents(exp);
5389                      exp.type = constantExp;
5390                      exp.constant = (dts && (!strcmp(dts, "int") || !strcmp(dts, "int64") || !strcmp(dts, "short") || !strcmp(dts, "char"))) ? PrintInt64(value.data) : PrintUInt64(value.data);
5391                   }
5392                }
5393             }
5394          }
5395          break;
5396       }
5397       case instanceExp:
5398       {
5399          ComputeInstantiation(exp);
5400          break;
5401       }
5402       /*
5403       case constantExp:
5404          break;
5405       */
5406       case opExp:
5407       {
5408          Expression exp1, exp2 = null;
5409          Operand op1 { };
5410          Operand op2 { };
5411
5412          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5413          if(exp.op.exp2)
5414          {
5415             Expression e = exp.op.exp2;
5416
5417             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5418             {
5419                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5420                {
5421                   if(e.type == extensionCompoundExp)
5422                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5423                   else
5424                      e = e.list->last;
5425                }
5426             }
5427             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5428             {
5429                if(e.type == stringExp && e.string)
5430                {
5431                   char * string = e.string;
5432                   int len = strlen(string);
5433                   char * tmp = new char[len-2+1];
5434                   len = UnescapeString(tmp, string + 1, len - 2);
5435                   delete tmp;
5436                   FreeExpContents(exp);
5437                   exp.type = constantExp;
5438                   exp.constant = PrintUInt(len + 1);
5439                }
5440                else
5441                {
5442                   Type type = e.expType;
5443                   type.refCount++;
5444                   FreeExpContents(exp);
5445                   exp.type = constantExp;
5446                   exp.constant = PrintUInt(ComputeTypeSize(type));
5447                   FreeType(type);
5448                }
5449                break;
5450             }
5451             else
5452                ComputeExpression(exp.op.exp2);
5453          }
5454          if(exp.op.exp1)
5455          {
5456             ComputeExpression(exp.op.exp1);
5457             exp1 = exp.op.exp1;
5458             exp2 = exp.op.exp2;
5459             op1 = GetOperand(exp1);
5460             if(op1.type) op1.type.refCount++;
5461             if(exp2)
5462             {
5463                op2 = GetOperand(exp2);
5464                if(op2.type) op2.type.refCount++;
5465             }
5466          }
5467          else
5468          {
5469             exp1 = exp.op.exp2;
5470             op1 = GetOperand(exp1);
5471             if(op1.type) op1.type.refCount++;
5472          }
5473
5474          CallOperator(exp, exp1, exp2, op1, op2);
5475          /*
5476          switch(exp.op.op)
5477          {
5478             // Unary operators
5479             case '&':
5480                // Also binary
5481                if(exp.op.exp1 && exp.op.exp2)
5482                {
5483                   // Binary And
5484                   if(op1.ops.BitAnd)
5485                   {
5486                      FreeExpContents(exp);
5487                      op1.ops.BitAnd(exp, op1, op2);
5488                   }
5489                }
5490                break;
5491             case '*':
5492                if(exp.op.exp1)
5493                {
5494                   if(op1.ops.Mul)
5495                   {
5496                      FreeExpContents(exp);
5497                      op1.ops.Mul(exp, op1, op2);
5498                   }
5499                }
5500                break;
5501             case '+':
5502                if(exp.op.exp1)
5503                {
5504                   if(op1.ops.Add)
5505                   {
5506                      FreeExpContents(exp);
5507                      op1.ops.Add(exp, op1, op2);
5508                   }
5509                }
5510                else
5511                {
5512                   // Provide default unary +
5513                   Expression exp2 = exp.op.exp2;
5514                   exp.op.exp2 = null;
5515                   FreeExpContents(exp);
5516                   FreeType(exp.expType);
5517                   FreeType(exp.destType);
5518
5519                   *exp = *exp2;
5520                   delete exp2;
5521                }
5522                break;
5523             case '-':
5524                if(exp.op.exp1)
5525                {
5526                   if(op1.ops.Sub)
5527                   {
5528                      FreeExpContents(exp);
5529                      op1.ops.Sub(exp, op1, op2);
5530                   }
5531                }
5532                else
5533                {
5534                   if(op1.ops.Neg)
5535                   {
5536                      FreeExpContents(exp);
5537                      op1.ops.Neg(exp, op1);
5538                   }
5539                }
5540                break;
5541             case '~':
5542                if(op1.ops.BitNot)
5543                {
5544                   FreeExpContents(exp);
5545                   op1.ops.BitNot(exp, op1);
5546                }
5547                break;
5548             case '!':
5549                if(op1.ops.Not)
5550                {
5551                   FreeExpContents(exp);
5552                   op1.ops.Not(exp, op1);
5553                }
5554                break;
5555             // Binary only operators
5556             case '/':
5557                if(op1.ops.Div)
5558                {
5559                   FreeExpContents(exp);
5560                   op1.ops.Div(exp, op1, op2);
5561                }
5562                break;
5563             case '%':
5564                if(op1.ops.Mod)
5565                {
5566                   FreeExpContents(exp);
5567                   op1.ops.Mod(exp, op1, op2);
5568                }
5569                break;
5570             case LEFT_OP:
5571                break;
5572             case RIGHT_OP:
5573                break;
5574             case '<':
5575                if(exp.op.exp1)
5576                {
5577                   if(op1.ops.Sma)
5578                   {
5579                      FreeExpContents(exp);
5580                      op1.ops.Sma(exp, op1, op2);
5581                   }
5582                }
5583                break;
5584             case '>':
5585                if(exp.op.exp1)
5586                {
5587                   if(op1.ops.Grt)
5588                   {
5589                      FreeExpContents(exp);
5590                      op1.ops.Grt(exp, op1, op2);
5591                   }
5592                }
5593                break;
5594             case LE_OP:
5595                if(exp.op.exp1)
5596                {
5597                   if(op1.ops.SmaEqu)
5598                   {
5599                      FreeExpContents(exp);
5600                      op1.ops.SmaEqu(exp, op1, op2);
5601                   }
5602                }
5603                break;
5604             case GE_OP:
5605                if(exp.op.exp1)
5606                {
5607                   if(op1.ops.GrtEqu)
5608                   {
5609                      FreeExpContents(exp);
5610                      op1.ops.GrtEqu(exp, op1, op2);
5611                   }
5612                }
5613                break;
5614             case EQ_OP:
5615                if(exp.op.exp1)
5616                {
5617                   if(op1.ops.Equ)
5618                   {
5619                      FreeExpContents(exp);
5620                      op1.ops.Equ(exp, op1, op2);
5621                   }
5622                }
5623                break;
5624             case NE_OP:
5625                if(exp.op.exp1)
5626                {
5627                   if(op1.ops.Nqu)
5628                   {
5629                      FreeExpContents(exp);
5630                      op1.ops.Nqu(exp, op1, op2);
5631                   }
5632                }
5633                break;
5634             case '|':
5635                if(op1.ops.BitOr)
5636                {
5637                   FreeExpContents(exp);
5638                   op1.ops.BitOr(exp, op1, op2);
5639                }
5640                break;
5641             case '^':
5642                if(op1.ops.BitXor)
5643                {
5644                   FreeExpContents(exp);
5645                   op1.ops.BitXor(exp, op1, op2);
5646                }
5647                break;
5648             case AND_OP:
5649                break;
5650             case OR_OP:
5651                break;
5652             case SIZEOF:
5653                FreeExpContents(exp);
5654                exp.type = constantExp;
5655                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5656                break;
5657          }
5658          */
5659          if(op1.type) FreeType(op1.type);
5660          if(op2.type) FreeType(op2.type);
5661          break;
5662       }
5663       case bracketsExp:
5664       case extensionExpressionExp:
5665       {
5666          Expression e, n;
5667          for(e = exp.list->first; e; e = n)
5668          {
5669             n = e.next;
5670             if(!n)
5671             {
5672                OldList * list = exp.list;
5673                Expression prev = exp.prev;
5674                Expression next = exp.next;
5675                ComputeExpression(e);
5676                //FreeExpContents(exp);
5677                FreeType(exp.expType);
5678                FreeType(exp.destType);
5679                *exp = *e;
5680                exp.prev = prev;
5681                exp.next = next;
5682                delete e;
5683                delete list;
5684             }
5685             else
5686             {
5687                FreeExpression(e);
5688             }
5689          }
5690          break;
5691       }
5692       /*
5693
5694       case ExpIndex:
5695       {
5696          Expression e;
5697          exp.isConstant = true;
5698
5699          ComputeExpression(exp.index.exp);
5700          if(!exp.index.exp.isConstant)
5701             exp.isConstant = false;
5702
5703          for(e = exp.index.index->first; e; e = e.next)
5704          {
5705             ComputeExpression(e);
5706             if(!e.next)
5707             {
5708                // Check if this type is int
5709             }
5710             if(!e.isConstant)
5711                exp.isConstant = false;
5712          }
5713          exp.expType = Dereference(exp.index.exp.expType);
5714          break;
5715       }
5716       */
5717       case memberExp:
5718       {
5719          Expression memberExp = exp.member.exp;
5720          Identifier memberID = exp.member.member;
5721
5722          Type type;
5723          ComputeExpression(exp.member.exp);
5724          type = exp.member.exp.expType;
5725          if(type)
5726          {
5727             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);
5728             Property prop = null;
5729             DataMember member = null;
5730             Class convertTo = null;
5731             if(type.kind == subClassType && exp.member.exp.type == classExp)
5732                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5733
5734             if(!_class)
5735             {
5736                char string[256];
5737                Symbol classSym;
5738                string[0] = '\0';
5739                PrintTypeNoConst(type, string, false, true);
5740                classSym = FindClass(string);
5741                _class = classSym ? classSym.registered : null;
5742             }
5743
5744             if(exp.member.member)
5745             {
5746                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5747                if(!prop)
5748                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5749             }
5750             if(!prop && !member && _class && exp.member.member)
5751             {
5752                Symbol classSym = FindClass(exp.member.member.string);
5753                convertTo = _class;
5754                _class = classSym ? classSym.registered : null;
5755                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5756             }
5757
5758             if(prop)
5759             {
5760                if(prop.compiled)
5761                {
5762                   Type type = prop.dataType;
5763                   // TODO: Assuming same base type for units...
5764                   if(_class.type == unitClass)
5765                   {
5766                      if(type.kind == classType)
5767                      {
5768                         Class _class = type._class.registered;
5769                         if(_class.type == unitClass)
5770                         {
5771                            if(!_class.dataType)
5772                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5773                            type = _class.dataType;
5774                         }
5775                      }
5776                      switch(type.kind)
5777                      {
5778                         case floatType:
5779                         {
5780                            float value;
5781                            float (*Get)(float) = (void *)prop.Get;
5782                            GetFloat(exp.member.exp, &value);
5783                            exp.constant = PrintFloat(Get ? Get(value) : value);
5784                            exp.type = constantExp;
5785                            break;
5786                         }
5787                         case doubleType:
5788                         {
5789                            double value;
5790                            double (*Get)(double);
5791                            GetDouble(exp.member.exp, &value);
5792
5793                            if(convertTo)
5794                               Get = (void *)prop.Set;
5795                            else
5796                               Get = (void *)prop.Get;
5797                            exp.constant = PrintDouble(Get ? Get(value) : value);
5798                            exp.type = constantExp;
5799                            break;
5800                         }
5801                      }
5802                   }
5803                   else
5804                   {
5805                      if(convertTo)
5806                      {
5807                         Expression value = exp.member.exp;
5808                         Type type;
5809                         if(!prop.dataType)
5810                            ProcessPropertyType(prop);
5811
5812                         type = prop.dataType;
5813                         if(!type)
5814                         {
5815                             // printf("Investigate this\n");
5816                         }
5817                         else if(_class.type == structClass)
5818                         {
5819                            switch(type.kind)
5820                            {
5821                               case classType:
5822                               {
5823                                  Class propertyClass = type._class.registered;
5824                                  if(propertyClass.type == structClass && value.type == instanceExp)
5825                                  {
5826                                     void (*Set)(void *, void *) = (void *)prop.Set;
5827                                     exp.instance = Instantiation { };
5828                                     exp.instance.data = new0 byte[_class.structSize];
5829                                     exp.instance._class = MkSpecifierName(_class.fullName);
5830                                     exp.instance.loc = exp.loc;
5831                                     exp.type = instanceExp;
5832                                     Set(exp.instance.data, value.instance.data);
5833                                     PopulateInstance(exp.instance);
5834                                  }
5835                                  break;
5836                               }
5837                               case intType:
5838                               {
5839                                  int intValue;
5840                                  void (*Set)(void *, int) = (void *)prop.Set;
5841
5842                                  exp.instance = Instantiation { };
5843                                  exp.instance.data = new0 byte[_class.structSize];
5844                                  exp.instance._class = MkSpecifierName(_class.fullName);
5845                                  exp.instance.loc = exp.loc;
5846                                  exp.type = instanceExp;
5847
5848                                  GetInt(value, &intValue);
5849
5850                                  Set(exp.instance.data, intValue);
5851                                  PopulateInstance(exp.instance);
5852                                  break;
5853                               }
5854                               case int64Type:
5855                               {
5856                                  int64 intValue;
5857                                  void (*Set)(void *, int64) = (void *)prop.Set;
5858
5859                                  exp.instance = Instantiation { };
5860                                  exp.instance.data = new0 byte[_class.structSize];
5861                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5862                                  exp.instance.loc = exp.loc;
5863                                  exp.type = instanceExp;
5864
5865                                  GetInt64(value, &intValue);
5866
5867                                  Set(exp.instance.data, intValue);
5868                                  PopulateInstance(exp.instance);
5869                                  break;
5870                               }
5871                               case intPtrType:
5872                               {
5873                                  // TOFIX:
5874                                  intptr intValue;
5875                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5876
5877                                  exp.instance = Instantiation { };
5878                                  exp.instance.data = new0 byte[_class.structSize];
5879                                  exp.instance._class = MkSpecifierName(_class.fullName);
5880                                  exp.instance.loc = exp.loc;
5881                                  exp.type = instanceExp;
5882
5883                                  GetIntPtr(value, &intValue);
5884
5885                                  Set(exp.instance.data, intValue);
5886                                  PopulateInstance(exp.instance);
5887                                  break;
5888                               }
5889                               case intSizeType:
5890                               {
5891                                  // TOFIX:
5892                                  intsize intValue;
5893                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5894
5895                                  exp.instance = Instantiation { };
5896                                  exp.instance.data = new0 byte[_class.structSize];
5897                                  exp.instance._class = MkSpecifierName(_class.fullName);
5898                                  exp.instance.loc = exp.loc;
5899                                  exp.type = instanceExp;
5900
5901                                  GetIntSize(value, &intValue);
5902
5903                                  Set(exp.instance.data, intValue);
5904                                  PopulateInstance(exp.instance);
5905                                  break;
5906                               }
5907                               case floatType:
5908                               {
5909                                  float floatValue;
5910                                  void (*Set)(void *, float) = (void *)prop.Set;
5911
5912                                  exp.instance = Instantiation { };
5913                                  exp.instance.data = new0 byte[_class.structSize];
5914                                  exp.instance._class = MkSpecifierName(_class.fullName);
5915                                  exp.instance.loc = exp.loc;
5916                                  exp.type = instanceExp;
5917
5918                                  GetFloat(value, &floatValue);
5919
5920                                  Set(exp.instance.data, floatValue);
5921                                  PopulateInstance(exp.instance);
5922                                  break;
5923                               }
5924                               case doubleType:
5925                               {
5926                                  double doubleValue;
5927                                  void (*Set)(void *, double) = (void *)prop.Set;
5928
5929                                  exp.instance = Instantiation { };
5930                                  exp.instance.data = new0 byte[_class.structSize];
5931                                  exp.instance._class = MkSpecifierName(_class.fullName);
5932                                  exp.instance.loc = exp.loc;
5933                                  exp.type = instanceExp;
5934
5935                                  GetDouble(value, &doubleValue);
5936
5937                                  Set(exp.instance.data, doubleValue);
5938                                  PopulateInstance(exp.instance);
5939                                  break;
5940                               }
5941                            }
5942                         }
5943                         else if(_class.type == bitClass)
5944                         {
5945                            switch(type.kind)
5946                            {
5947                               case classType:
5948                               {
5949                                  Class propertyClass = type._class.registered;
5950                                  if(propertyClass.type == structClass && value.instance.data)
5951                                  {
5952                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5953                                     unsigned int bits = Set(value.instance.data);
5954                                     exp.constant = PrintHexUInt(bits);
5955                                     exp.type = constantExp;
5956                                     break;
5957                                  }
5958                                  else if(_class.type == bitClass)
5959                                  {
5960                                     unsigned int value;
5961                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5962                                     unsigned int bits;
5963
5964                                     GetUInt(exp.member.exp, &value);
5965                                     bits = Set(value);
5966                                     exp.constant = PrintHexUInt(bits);
5967                                     exp.type = constantExp;
5968                                  }
5969                               }
5970                            }
5971                         }
5972                      }
5973                      else
5974                      {
5975                         if(_class.type == bitClass)
5976                         {
5977                            unsigned int value;
5978                            GetUInt(exp.member.exp, &value);
5979
5980                            switch(type.kind)
5981                            {
5982                               case classType:
5983                               {
5984                                  Class _class = type._class.registered;
5985                                  if(_class.type == structClass)
5986                                  {
5987                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5988
5989                                     exp.instance = Instantiation { };
5990                                     exp.instance.data = new0 byte[_class.structSize];
5991                                     exp.instance._class = MkSpecifierName(_class.fullName);
5992                                     exp.instance.loc = exp.loc;
5993                                     //exp.instance.fullSet = true;
5994                                     exp.type = instanceExp;
5995                                     Get(value, exp.instance.data);
5996                                     PopulateInstance(exp.instance);
5997                                  }
5998                                  else if(_class.type == bitClass)
5999                                  {
6000                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6001                                     uint64 bits = Get(value);
6002                                     exp.constant = PrintHexUInt64(bits);
6003                                     exp.type = constantExp;
6004                                  }
6005                                  break;
6006                               }
6007                            }
6008                         }
6009                         else if(_class.type == structClass)
6010                         {
6011                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6012                            switch(type.kind)
6013                            {
6014                               case classType:
6015                               {
6016                                  Class _class = type._class.registered;
6017                                  if(_class.type == structClass && value)
6018                                  {
6019                                     void (*Get)(void *, void *) = (void *)prop.Get;
6020
6021                                     exp.instance = Instantiation { };
6022                                     exp.instance.data = new0 byte[_class.structSize];
6023                                     exp.instance._class = MkSpecifierName(_class.fullName);
6024                                     exp.instance.loc = exp.loc;
6025                                     //exp.instance.fullSet = true;
6026                                     exp.type = instanceExp;
6027                                     Get(value, exp.instance.data);
6028                                     PopulateInstance(exp.instance);
6029                                  }
6030                                  break;
6031                               }
6032                            }
6033                         }
6034                         /*else
6035                         {
6036                            char * value = exp.member.exp.instance.data;
6037                            switch(type.kind)
6038                            {
6039                               case classType:
6040                               {
6041                                  Class _class = type._class.registered;
6042                                  if(_class.type == normalClass)
6043                                  {
6044                                     void *(*Get)(void *) = (void *)prop.Get;
6045
6046                                     exp.instance = Instantiation { };
6047                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6048                                     exp.type = instanceExp;
6049                                     exp.instance.data = Get(value, exp.instance.data);
6050                                  }
6051                                  break;
6052                               }
6053                            }
6054                         }
6055                         */
6056                      }
6057                   }
6058                }
6059                else
6060                {
6061                   exp.isConstant = false;
6062                }
6063             }
6064             else if(member)
6065             {
6066             }
6067          }
6068
6069          if(exp.type != ExpressionType::memberExp)
6070          {
6071             FreeExpression(memberExp);
6072             FreeIdentifier(memberID);
6073          }
6074          break;
6075       }
6076       case typeSizeExp:
6077       {
6078          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6079          FreeExpContents(exp);
6080          exp.constant = PrintUInt(ComputeTypeSize(type));
6081          exp.type = constantExp;
6082          FreeType(type);
6083          break;
6084       }
6085       case classSizeExp:
6086       {
6087          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6088          if(classSym && classSym.registered)
6089          {
6090             if(classSym.registered.fixed)
6091             {
6092                FreeSpecifier(exp._class);
6093                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6094                exp.type = constantExp;
6095             }
6096             else
6097             {
6098                char className[1024];
6099                strcpy(className, "__ecereClass_");
6100                FullClassNameCat(className, classSym.string, true);
6101
6102                DeclareClass(curExternal, classSym, className);
6103
6104                FreeExpContents(exp);
6105                exp.type = pointerExp;
6106                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6107                exp.member.member = MkIdentifier("structSize");
6108             }
6109          }
6110          break;
6111       }
6112       case castExp:
6113       //case constantExp:
6114       {
6115          Type type;
6116          Expression e = exp;
6117          if(exp.type == castExp)
6118          {
6119             if(exp.cast.exp)
6120                ComputeExpression(exp.cast.exp);
6121             e = exp.cast.exp;
6122          }
6123          if(e && exp.expType)
6124          {
6125             /*if(exp.destType)
6126                type = exp.destType;
6127             else*/
6128                type = exp.expType;
6129             if(type.kind == classType)
6130             {
6131                Class _class = type._class.registered;
6132                if(_class && (_class.type == unitClass || _class.type == bitClass))
6133                {
6134                   if(!_class.dataType)
6135                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6136                   type = _class.dataType;
6137                }
6138             }
6139
6140             switch(type.kind)
6141             {
6142                case _BoolType:
6143                case charType:
6144                   if(type.isSigned)
6145                   {
6146                      char value = 0;
6147                      if(GetChar(e, &value))
6148                      {
6149                         FreeExpContents(exp);
6150                         exp.constant = PrintChar(value);
6151                         exp.type = constantExp;
6152                      }
6153                   }
6154                   else
6155                   {
6156                      unsigned char value = 0;
6157                      if(GetUChar(e, &value))
6158                      {
6159                         FreeExpContents(exp);
6160                         exp.constant = PrintUChar(value);
6161                         exp.type = constantExp;
6162                      }
6163                   }
6164                   break;
6165                case shortType:
6166                   if(type.isSigned)
6167                   {
6168                      short value = 0;
6169                      if(GetShort(e, &value))
6170                      {
6171                         FreeExpContents(exp);
6172                         exp.constant = PrintShort(value);
6173                         exp.type = constantExp;
6174                      }
6175                   }
6176                   else
6177                   {
6178                      unsigned short value = 0;
6179                      if(GetUShort(e, &value))
6180                      {
6181                         FreeExpContents(exp);
6182                         exp.constant = PrintUShort(value);
6183                         exp.type = constantExp;
6184                      }
6185                   }
6186                   break;
6187                case intType:
6188                   if(type.isSigned)
6189                   {
6190                      int value = 0;
6191                      if(GetInt(e, &value))
6192                      {
6193                         FreeExpContents(exp);
6194                         exp.constant = PrintInt(value);
6195                         exp.type = constantExp;
6196                      }
6197                   }
6198                   else
6199                   {
6200                      unsigned int value = 0;
6201                      if(GetUInt(e, &value))
6202                      {
6203                         FreeExpContents(exp);
6204                         exp.constant = PrintUInt(value);
6205                         exp.type = constantExp;
6206                      }
6207                   }
6208                   break;
6209                case int64Type:
6210                   if(type.isSigned)
6211                   {
6212                      int64 value = 0;
6213                      if(GetInt64(e, &value))
6214                      {
6215                         FreeExpContents(exp);
6216                         exp.constant = PrintInt64(value);
6217                         exp.type = constantExp;
6218                      }
6219                   }
6220                   else
6221                   {
6222                      uint64 value = 0;
6223                      if(GetUInt64(e, &value))
6224                      {
6225                         FreeExpContents(exp);
6226                         exp.constant = PrintUInt64(value);
6227                         exp.type = constantExp;
6228                      }
6229                   }
6230                   break;
6231                case intPtrType:
6232                   if(type.isSigned)
6233                   {
6234                      intptr value = 0;
6235                      if(GetIntPtr(e, &value))
6236                      {
6237                         FreeExpContents(exp);
6238                         exp.constant = PrintInt64((int64)value);
6239                         exp.type = constantExp;
6240                      }
6241                   }
6242                   else
6243                   {
6244                      uintptr value = 0;
6245                      if(GetUIntPtr(e, &value))
6246                      {
6247                         FreeExpContents(exp);
6248                         exp.constant = PrintUInt64((uint64)value);
6249                         exp.type = constantExp;
6250                      }
6251                   }
6252                   break;
6253                case intSizeType:
6254                   if(type.isSigned)
6255                   {
6256                      intsize value = 0;
6257                      if(GetIntSize(e, &value))
6258                      {
6259                         FreeExpContents(exp);
6260                         exp.constant = PrintInt64((int64)value);
6261                         exp.type = constantExp;
6262                      }
6263                   }
6264                   else
6265                   {
6266                      uintsize value = 0;
6267                      if(GetUIntSize(e, &value))
6268                      {
6269                         FreeExpContents(exp);
6270                         exp.constant = PrintUInt64((uint64)value);
6271                         exp.type = constantExp;
6272                      }
6273                   }
6274                   break;
6275                case floatType:
6276                {
6277                   float value = 0;
6278                   if(GetFloat(e, &value))
6279                   {
6280                      FreeExpContents(exp);
6281                      exp.constant = PrintFloat(value);
6282                      exp.type = constantExp;
6283                   }
6284                   break;
6285                }
6286                case doubleType:
6287                {
6288                   double value = 0;
6289                   if(GetDouble(e, &value))
6290                   {
6291                      FreeExpContents(exp);
6292                      exp.constant = PrintDouble(value);
6293                      exp.type = constantExp;
6294                   }
6295                   break;
6296                }
6297             }
6298          }
6299          break;
6300       }
6301       case conditionExp:
6302       {
6303          Operand op1 { };
6304          Operand op2 { };
6305          Operand op3 { };
6306
6307          if(exp.cond.exp)
6308             // Caring only about last expression for now...
6309             ComputeExpression(exp.cond.exp->last);
6310          if(exp.cond.elseExp)
6311             ComputeExpression(exp.cond.elseExp);
6312          if(exp.cond.cond)
6313             ComputeExpression(exp.cond.cond);
6314
6315          op1 = GetOperand(exp.cond.cond);
6316          if(op1.type) op1.type.refCount++;
6317          op2 = GetOperand(exp.cond.exp->last);
6318          if(op2.type) op2.type.refCount++;
6319          op3 = GetOperand(exp.cond.elseExp);
6320          if(op3.type) op3.type.refCount++;
6321
6322          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6323          if(op1.type) FreeType(op1.type);
6324          if(op2.type) FreeType(op2.type);
6325          if(op3.type) FreeType(op3.type);
6326          break;
6327       }
6328    }
6329 }
6330
6331 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6332 {
6333    bool result = true;
6334    if(destType)
6335    {
6336       OldList converts { };
6337       Conversion convert;
6338
6339       if(destType.kind == voidType)
6340          return false;
6341
6342       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6343          result = false;
6344       if(converts.count)
6345       {
6346          // for(convert = converts.last; convert; convert = convert.prev)
6347          for(convert = converts.first; convert; convert = convert.next)
6348          {
6349             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6350             if(!empty)
6351             {
6352                Expression newExp { };
6353                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6354
6355                // TODO: Check this...
6356                *newExp = *exp;
6357                newExp.prev = null;
6358                newExp.next = null;
6359                newExp.destType = null;
6360
6361                if(convert.isGet)
6362                {
6363                   // [exp].ColorRGB
6364                   exp.type = memberExp;
6365                   exp.addedThis = true;
6366                   exp.member.exp = newExp;
6367                   FreeType(exp.member.exp.expType);
6368
6369                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6370                   exp.member.exp.expType.classObjectType = objectType;
6371                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6372                   exp.member.memberType = propertyMember;
6373                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6374                   // TESTING THIS... for (int)degrees
6375                   exp.needCast = true;
6376                   if(exp.expType) exp.expType.refCount++;
6377                   ApplyAnyObjectLogic(exp.member.exp);
6378                }
6379                else
6380                {
6381
6382                   /*if(exp.isConstant)
6383                   {
6384                      // Color { ColorRGB = [exp] };
6385                      exp.type = instanceExp;
6386                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6387                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6388                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6389                   }
6390                   else*/
6391                   {
6392                      // If not constant, don't turn it yet into an instantiation
6393                      // (Go through the deep members system first)
6394                      exp.type = memberExp;
6395                      exp.addedThis = true;
6396                      exp.member.exp = newExp;
6397
6398                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6399                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6400                         newExp.expType._class.registered.type == noHeadClass)
6401                      {
6402                         newExp.byReference = true;
6403                      }
6404
6405                      FreeType(exp.member.exp.expType);
6406                      /*exp.member.exp.expType = convert.convert.dataType;
6407                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6408                      exp.member.exp.expType = null;
6409                      if(convert.convert.dataType)
6410                      {
6411                         exp.member.exp.expType = { };
6412                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6413                         exp.member.exp.expType.refCount = 1;
6414                         exp.member.exp.expType.classObjectType = objectType;
6415                         ApplyAnyObjectLogic(exp.member.exp);
6416                      }
6417
6418                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6419                      exp.member.memberType = reverseConversionMember;
6420                      exp.expType = convert.resultType ? convert.resultType :
6421                         MkClassType(convert.convert._class.fullName);
6422                      exp.needCast = true;
6423                      if(convert.resultType) convert.resultType.refCount++;
6424                   }
6425                }
6426             }
6427             else
6428             {
6429                FreeType(exp.expType);
6430                if(convert.isGet)
6431                {
6432                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6433                   if(exp.destType.casted)
6434                      exp.needCast = true;
6435                   if(exp.expType) exp.expType.refCount++;
6436                }
6437                else
6438                {
6439                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6440                   if(exp.destType.casted)
6441                      exp.needCast = true;
6442                   if(convert.resultType)
6443                      convert.resultType.refCount++;
6444                }
6445             }
6446          }
6447          if(exp.isConstant && inCompiler)
6448             ComputeExpression(exp);
6449
6450          converts.Free(FreeConvert);
6451       }
6452
6453       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6454       {
6455          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6456       }
6457       if(!result && exp.expType && exp.destType)
6458       {
6459          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6460              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6461             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6462             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6463             result = true;
6464       }
6465    }
6466    // if(result) CheckTemplateTypes(exp);
6467    return result;
6468 }
6469
6470 void CheckTemplateTypes(Expression exp)
6471 {
6472    /*
6473    bool et = exp.expType ? exp.expType.passAsTemplate : false;
6474    bool dt = exp.destType ? exp.destType.passAsTemplate : false;
6475    */
6476    Expression nbExp = GetNonBracketsExp(exp);
6477    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6478       (nbExp == exp || nbExp.type != castExp))
6479    {
6480       Expression newExp { };
6481       Context context;
6482       TypeKind kind = exp.expType.kind;
6483       *newExp = *exp;
6484       if(exp.destType) exp.destType.refCount++;
6485       if(exp.expType)  exp.expType.refCount++;
6486       newExp.prev = null;
6487       newExp.next = null;
6488
6489       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6490       {
6491          Class c = exp.expType._class.registered;
6492          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6493          {
6494             if(!c.dataType)
6495                c.dataType = ProcessTypeString(c.dataTypeString, false);
6496             kind = c.dataType.kind;
6497          }
6498       }
6499
6500       switch(kind)
6501       {
6502          case doubleType:
6503             if(exp.destType.classObjectType)
6504             {
6505                // We need to pass the address, just pass it along (Undo what was done above)
6506                if(exp.destType) exp.destType.refCount--;
6507                if(exp.expType)  exp.expType.refCount--;
6508                delete newExp;
6509             }
6510             else
6511             {
6512                // If we're looking for value:
6513                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6514                OldList * specs;
6515                OldList * unionDefs = MkList();
6516                OldList * statements = MkList();
6517                context = PushContext();
6518                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6519                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6520                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6521                exp.type = extensionCompoundExp;
6522                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6523                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6524                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6525                exp.compound.compound.context = context;
6526                PopContext(context);
6527             }
6528             break;
6529          default:
6530             exp.type = castExp;
6531             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6532             if((exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass) || exp.expType.isPointerType)
6533                exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), MkExpBrackets(MkListOne(newExp)));
6534             else
6535                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6536             exp.needCast = true;
6537             break;
6538       }
6539    }
6540    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6541    {
6542       Expression newExp { };
6543       Context context;
6544       TypeKind kind = exp.expType.kind;
6545       *newExp = *exp;
6546       if(exp.destType) exp.destType.refCount++;
6547       if(exp.expType)  exp.expType.refCount++;
6548       newExp.prev = null;
6549       newExp.next = null;
6550
6551       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6552       {
6553          Class c = exp.expType._class.registered;
6554          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6555          {
6556             if(!c.dataType)
6557                c.dataType = ProcessTypeString(c.dataTypeString, false);
6558             kind = c.dataType.kind;
6559          }
6560       }
6561
6562       switch(kind)
6563       {
6564          case doubleType:
6565             if(exp.destType.classObjectType)
6566             {
6567                // We need to pass the address, just pass it along (Undo what was done above)
6568                if(exp.destType) exp.destType.refCount--;
6569                if(exp.expType)  exp.expType.refCount--;
6570                delete newExp;
6571             }
6572             else
6573             {
6574                // If we're looking for value:
6575                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6576                OldList * specs;
6577                OldList * unionDefs = MkList();
6578                OldList * statements = MkList();
6579                context = PushContext();
6580                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6581                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6582                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6583                exp.type = extensionCompoundExp;
6584                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6585                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6586                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6587                exp.compound.compound.context = context;
6588                PopContext(context);
6589             }
6590             break;
6591          case classType:
6592          {
6593             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6594             {
6595                exp.type = bracketsExp;
6596
6597                newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6598                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6599                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6600                ProcessExpressionType(exp.list->first);
6601                break;
6602             }
6603             else
6604             {
6605                exp.type = bracketsExp;
6606                if(exp.expType.isPointerType)
6607                {
6608                   exp.needTemplateCast = 2;
6609                   newExp.needCast = true;
6610                   newExp.needTemplateCast = 2;
6611                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6612                }
6613
6614                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6615                exp.needTemplateCast = 2;
6616                newExp.needCast = true;
6617                newExp.needTemplateCast = 2;
6618                ProcessExpressionType(exp.list->first);
6619                break;
6620             }
6621          }
6622          default:
6623          {
6624             if(exp.expType.kind == templateType)
6625             {
6626                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6627                if(type)
6628                {
6629                   FreeType(exp.destType);
6630                   FreeType(exp.expType);
6631                   delete newExp;
6632                   break;
6633                }
6634             }
6635             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6636             {
6637                exp.type = opExp;
6638                exp.op.op = '*';
6639                exp.op.exp1 = null;
6640                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6641                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6642             }
6643             else
6644             {
6645                char typeString[1024];
6646                Declarator decl;
6647                OldList * specs = MkList();
6648                typeString[0] = '\0';
6649                PrintType(exp.expType, typeString, false, false);
6650                decl = SpecDeclFromString(typeString, specs, null);
6651
6652                exp.type = castExp;
6653                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6654                exp.cast.typeName = MkTypeName(specs, decl);
6655                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6656                exp.cast.exp.needCast = true;
6657             }
6658             break;
6659          }
6660       }
6661    }
6662 }
6663 // TODO: The Symbol tree should be reorganized by namespaces
6664 // Name Space:
6665 //    - Tree of all symbols within (stored without namespace)
6666 //    - Tree of sub-namespaces
6667
6668 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6669 {
6670    int nsLen = strlen(nameSpace);
6671    Symbol symbol;
6672    // Start at the name space prefix
6673    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6674    {
6675       char * s = symbol.string;
6676       if(!strncmp(s, nameSpace, nsLen))
6677       {
6678          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6679          int c;
6680          char * namePart;
6681          for(c = strlen(s)-1; c >= 0; c--)
6682             if(s[c] == ':')
6683                break;
6684
6685          namePart = s+c+1;
6686          if(!strcmp(namePart, name))
6687          {
6688             // TODO: Error on ambiguity
6689             return symbol;
6690          }
6691       }
6692       else
6693          break;
6694    }
6695    return null;
6696 }
6697
6698 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6699 {
6700    int c;
6701    char nameSpace[1024];
6702    const char * namePart;
6703    bool gotColon = false;
6704
6705    nameSpace[0] = '\0';
6706    for(c = strlen(name)-1; c >= 0; c--)
6707       if(name[c] == ':')
6708       {
6709          gotColon = true;
6710          break;
6711       }
6712
6713    namePart = name+c+1;
6714    while(c >= 0 && name[c] == ':') c--;
6715    if(c >= 0)
6716    {
6717       // Try an exact match first
6718       Symbol symbol = (Symbol)tree.FindString(name);
6719       if(symbol)
6720          return symbol;
6721
6722       // Namespace specified
6723       memcpy(nameSpace, name, c + 1);
6724       nameSpace[c+1] = 0;
6725
6726       return ScanWithNameSpace(tree, nameSpace, namePart);
6727    }
6728    else if(gotColon)
6729    {
6730       // Looking for a global symbol, e.g. ::Sleep()
6731       Symbol symbol = (Symbol)tree.FindString(namePart);
6732       return symbol;
6733    }
6734    else
6735    {
6736       // Name only (no namespace specified)
6737       Symbol symbol = (Symbol)tree.FindString(namePart);
6738       if(symbol)
6739          return symbol;
6740       return ScanWithNameSpace(tree, "", namePart);
6741    }
6742    return null;
6743 }
6744
6745 static void ProcessDeclaration(Declaration decl);
6746
6747 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6748 {
6749 #ifdef _DEBUG
6750    //Time startTime = GetTime();
6751 #endif
6752    // Optimize this later? Do this before/less?
6753    Context ctx;
6754    Symbol symbol = null;
6755
6756    // First, check if the identifier is declared inside the function
6757    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6758
6759    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6760    {
6761       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6762       {
6763          symbol = null;
6764          if(thisNameSpace)
6765          {
6766             char curName[1024];
6767             strcpy(curName, thisNameSpace);
6768             strcat(curName, "::");
6769             strcat(curName, name);
6770             // Try to resolve in current namespace first
6771             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6772          }
6773          if(!symbol)
6774             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6775       }
6776       else
6777          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6778
6779       if(symbol || ctx == endContext) break;
6780    }
6781    if(inCompiler && symbol && ctx == globalContext && symbol.pointerExternal && curExternal && symbol.pointerExternal != curExternal)
6782       curExternal.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
6783 #ifdef _DEBUG
6784    //findSymbolTotalTime += GetTime() - startTime;
6785 #endif
6786    return symbol;
6787 }
6788
6789 static void GetTypeSpecs(Type type, OldList * specs)
6790 {
6791    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6792    switch(type.kind)
6793    {
6794       case classType:
6795       {
6796          if(type._class.registered)
6797          {
6798             if(!type._class.registered.dataType)
6799                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6800             GetTypeSpecs(type._class.registered.dataType, specs);
6801          }
6802          break;
6803       }
6804       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6805       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6806       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6807       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6808       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6809       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6810       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6811       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6812       case intType:
6813       default:
6814          ListAdd(specs, MkSpecifier(INT)); break;
6815    }
6816 }
6817
6818 static void PrintArraySize(Type arrayType, char * string)
6819 {
6820    char size[256];
6821    size[0] = '\0';
6822    strcat(size, "[");
6823    if(arrayType.enumClass)
6824       strcat(size, arrayType.enumClass.string);
6825    else if(arrayType.arraySizeExp)
6826       PrintExpression(arrayType.arraySizeExp, size);
6827    strcat(size, "]");
6828    strcat(string, size);
6829 }
6830
6831 // WARNING : This function expects a null terminated string since it recursively concatenate...
6832 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6833 {
6834    if(type)
6835    {
6836       if(printConst && type.constant)
6837          strcat(string, "const ");
6838       switch(type.kind)
6839       {
6840          case classType:
6841          {
6842             Symbol c = type._class;
6843             bool isObjectBaseClass = !c || !c.string || !strcmp(c.string, "class");
6844             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6845             //       look into merging with thisclass ?
6846             if(type.classObjectType == typedObject && isObjectBaseClass)
6847                strcat(string, "typed_object");
6848             else if(type.classObjectType == anyObject && isObjectBaseClass)
6849                strcat(string, "any_object");
6850             else
6851             {
6852                if(c && c.string)
6853                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6854             }
6855             if(type.byReference)
6856                strcat(string, " &");
6857             break;
6858          }
6859          case voidType: strcat(string, "void"); break;
6860          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6861          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6862          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6863          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6864          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6865          case _BoolType: strcat(string, "_Bool"); break;
6866          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6867          case floatType: strcat(string, "float"); break;
6868          case doubleType: strcat(string, "double"); break;
6869          case structType:
6870             if(type.enumName)
6871             {
6872                strcat(string, "struct ");
6873                strcat(string, type.enumName);
6874             }
6875             else if(type.typeName)
6876                strcat(string, type.typeName);
6877             else
6878             {
6879                Type member;
6880                strcat(string, "struct { ");
6881                for(member = type.members.first; member; member = member.next)
6882                {
6883                   PrintType(member, string, true, fullName);
6884                   strcat(string,"; ");
6885                }
6886                strcat(string,"}");
6887             }
6888             break;
6889          case unionType:
6890             if(type.enumName)
6891             {
6892                strcat(string, "union ");
6893                strcat(string, type.enumName);
6894             }
6895             else if(type.typeName)
6896                strcat(string, type.typeName);
6897             else
6898             {
6899                strcat(string, "union ");
6900                strcat(string,"(unnamed)");
6901             }
6902             break;
6903          case enumType:
6904             if(type.enumName)
6905             {
6906                strcat(string, "enum ");
6907                strcat(string, type.enumName);
6908             }
6909             else if(type.typeName)
6910                strcat(string, type.typeName);
6911             else
6912                strcat(string, "int"); // "enum");
6913             break;
6914          case ellipsisType:
6915             strcat(string, "...");
6916             break;
6917          case subClassType:
6918             strcat(string, "subclass(");
6919             strcat(string, type._class ? type._class.string : "int");
6920             strcat(string, ")");
6921             break;
6922          case templateType:
6923             strcat(string, type.templateParameter.identifier.string);
6924             break;
6925          case thisClassType:
6926             strcat(string, "thisclass");
6927             break;
6928          case vaListType:
6929             strcat(string, "__builtin_va_list");
6930             break;
6931       }
6932    }
6933 }
6934
6935 static void PrintName(Type type, char * string, bool fullName)
6936 {
6937    if(type.name && type.name[0])
6938    {
6939       if(fullName)
6940          strcat(string, type.name);
6941       else
6942       {
6943          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6944          if(name) name += 2; else name = type.name;
6945          strcat(string, name);
6946       }
6947    }
6948 }
6949
6950 static void PrintAttribs(Type type, char * string)
6951 {
6952    if(type)
6953    {
6954       if(type.dllExport)   strcat(string, "dllexport ");
6955       if(type.attrStdcall) strcat(string, "stdcall ");
6956    }
6957 }
6958
6959 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6960 {
6961    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6962    {
6963       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6964          PrintAttribs(type, string);
6965       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6966          strcat(string, " const");
6967       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6968       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6969          strcat(string, " (");
6970       if(type.kind == pointerType)
6971       {
6972          if(type.type.kind == functionType || type.type.kind == methodType)
6973             PrintAttribs(type.type, string);
6974       }
6975       if(type.kind == pointerType)
6976       {
6977          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6978             strcat(string, "*");
6979          else
6980             strcat(string, " *");
6981       }
6982       if(printConst && type.constant && type.kind == pointerType)
6983          strcat(string, " const");
6984    }
6985    else
6986       PrintTypeSpecs(type, string, fullName, printConst);
6987 }
6988
6989 static void PostPrintType(Type type, char * string, bool fullName)
6990 {
6991    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6992       strcat(string, ")");
6993    if(type.kind == arrayType)
6994       PrintArraySize(type, string);
6995    else if(type.kind == functionType)
6996    {
6997       Type param;
6998       strcat(string, "(");
6999       for(param = type.params.first; param; param = param.next)
7000       {
7001          PrintType(param, string, true, fullName);
7002          if(param.next) strcat(string, ", ");
7003       }
7004       strcat(string, ")");
7005    }
7006    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7007       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7008 }
7009
7010 // *****
7011 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7012 // *****
7013 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7014 {
7015    PrePrintType(type, string, fullName, null, printConst);
7016
7017    if(type.thisClass || (printName && type.name && type.name[0]))
7018       strcat(string, " ");
7019    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7020    {
7021       Symbol _class = type.thisClass;
7022       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7023       {
7024          if(type.classObjectType == classPointer)
7025             strcat(string, "class");
7026          else
7027             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7028       }
7029       else if(_class && _class.string)
7030       {
7031          String s = _class.string;
7032          if(fullName)
7033             strcat(string, s);
7034          else
7035          {
7036             char * name = RSearchString(s, "::", strlen(s), true, false);
7037             if(name) name += 2; else name = s;
7038             strcat(string, name);
7039          }
7040       }
7041       strcat(string, "::");
7042    }
7043
7044    if(printName && type.name)
7045       PrintName(type, string, fullName);
7046    PostPrintType(type, string, fullName);
7047    if(type.bitFieldCount)
7048    {
7049       char count[100];
7050       sprintf(count, ":%d", type.bitFieldCount);
7051       strcat(string, count);
7052    }
7053 }
7054
7055 void PrintType(Type type, char * string, bool printName, bool fullName)
7056 {
7057    _PrintType(type, string, printName, fullName, true);
7058 }
7059
7060 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7061 {
7062    _PrintType(type, string, printName, fullName, false);
7063 }
7064
7065 static Type FindMember(Type type, char * string)
7066 {
7067    Type memberType;
7068    for(memberType = type.members.first; memberType; memberType = memberType.next)
7069    {
7070       if(!memberType.name)
7071       {
7072          Type subType = FindMember(memberType, string);
7073          if(subType)
7074             return subType;
7075       }
7076       else if(!strcmp(memberType.name, string))
7077          return memberType;
7078    }
7079    return null;
7080 }
7081
7082 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7083 {
7084    Type memberType;
7085    for(memberType = type.members.first; memberType; memberType = memberType.next)
7086    {
7087       if(!memberType.name)
7088       {
7089          Type subType = FindMember(memberType, string);
7090          if(subType)
7091          {
7092             *offset += memberType.offset;
7093             return subType;
7094          }
7095       }
7096       else if(!strcmp(memberType.name, string))
7097       {
7098          *offset += memberType.offset;
7099          return memberType;
7100       }
7101    }
7102    return null;
7103 }
7104
7105 public bool GetParseError() { return parseError; }
7106
7107 Expression ParseExpressionString(char * expression)
7108 {
7109    parseError = false;
7110
7111    fileInput = TempFile { };
7112    fileInput.Write(expression, 1, strlen(expression));
7113    fileInput.Seek(0, start);
7114
7115    echoOn = false;
7116    parsedExpression = null;
7117    resetScanner();
7118    expression_yyparse();
7119    delete fileInput;
7120
7121    return parsedExpression;
7122 }
7123
7124 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7125 {
7126    Identifier id = exp.identifier;
7127    Method method = null;
7128    Property prop = null;
7129    DataMember member = null;
7130    ClassProperty classProp = null;
7131
7132    if(_class && _class.type == enumClass)
7133    {
7134       NamedLink64 value = null;
7135       Class enumClass = eSystem_FindClass(privateModule, "enum");
7136       if(enumClass)
7137       {
7138          Class baseClass;
7139          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7140          {
7141             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7142             for(value = e.values.first; value; value = value.next)
7143             {
7144                if(!strcmp(value.name, id.string))
7145                   break;
7146             }
7147             if(value)
7148             {
7149                exp.isConstant = true;
7150                if(inCompiler || inPreCompiler || inDebugger)
7151                {
7152                   char constant[256];
7153                   FreeExpContents(exp);
7154
7155                   exp.type = constantExp;
7156                   if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7157                      sprintf(constant, FORMAT64D, value.data);
7158                   else
7159                      sprintf(constant, FORMAT64HEX, value.data);
7160                   exp.constant = CopyString(constant);
7161                }
7162                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7163                exp.expType = MkClassType(baseClass.fullName);
7164                break;
7165             }
7166          }
7167       }
7168       if(value)
7169          return true;
7170    }
7171    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7172    {
7173       ProcessMethodType(method);
7174       exp.expType = Type
7175       {
7176          refCount = 1;
7177          kind = methodType;
7178          method = method;
7179          // Crash here?
7180          // TOCHECK: Put it back to what it was...
7181          // methodClass = _class;
7182          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7183       };
7184       //id._class = null;
7185       return true;
7186    }
7187    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7188    {
7189       if(!prop.dataType)
7190          ProcessPropertyType(prop);
7191       exp.expType = prop.dataType;
7192       if(prop.dataType) prop.dataType.refCount++;
7193       return true;
7194    }
7195    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7196    {
7197       if(!member.dataType)
7198          member.dataType = ProcessTypeString(member.dataTypeString, false);
7199       exp.expType = member.dataType;
7200       if(member.dataType) member.dataType.refCount++;
7201       return true;
7202    }
7203    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7204    {
7205       if(!classProp.dataType)
7206          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7207
7208       if(classProp.constant)
7209       {
7210          FreeExpContents(exp);
7211
7212          exp.isConstant = true;
7213          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7214          {
7215             //char constant[256];
7216             exp.type = stringExp;
7217             exp.constant = QMkString((char *)(uintptr)classProp.Get(_class));
7218          }
7219          else
7220          {
7221             char constant[256];
7222             exp.type = constantExp;
7223             sprintf(constant, "%d", (int)classProp.Get(_class));
7224             exp.constant = CopyString(constant);
7225          }
7226       }
7227       else
7228       {
7229          // TO IMPLEMENT...
7230       }
7231
7232       exp.expType = classProp.dataType;
7233       if(classProp.dataType) classProp.dataType.refCount++;
7234       return true;
7235    }
7236    return false;
7237 }
7238
7239 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7240 {
7241    BinaryTree * tree = &nameSpace.functions;
7242    GlobalData data = (GlobalData)tree->FindString(name);
7243    NameSpace * child;
7244    if(!data)
7245    {
7246       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7247       {
7248          data = ScanGlobalData(child, name);
7249          if(data)
7250             break;
7251       }
7252    }
7253    return data;
7254 }
7255
7256 static GlobalData FindGlobalData(char * name)
7257 {
7258    int start = 0, c;
7259    NameSpace * nameSpace;
7260    nameSpace = globalData;
7261    for(c = 0; name[c]; c++)
7262    {
7263       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7264       {
7265          NameSpace * newSpace;
7266          char * spaceName = new char[c - start + 1];
7267          strncpy(spaceName, name + start, c - start);
7268          spaceName[c-start] = '\0';
7269          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7270          delete spaceName;
7271          if(!newSpace)
7272             return null;
7273          nameSpace = newSpace;
7274          if(name[c] == ':') c++;
7275          start = c+1;
7276       }
7277    }
7278    if(c - start)
7279    {
7280       return ScanGlobalData(nameSpace, name + start);
7281    }
7282    return null;
7283 }
7284
7285 static int definedExpStackPos;
7286 static void * definedExpStack[512];
7287
7288 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7289 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7290 {
7291    Expression prev = checkedExp.prev, next = checkedExp.next;
7292
7293    FreeExpContents(checkedExp);
7294    FreeType(checkedExp.expType);
7295    FreeType(checkedExp.destType);
7296
7297    *checkedExp = *newExp;
7298
7299    delete newExp;
7300
7301    checkedExp.prev = prev;
7302    checkedExp.next = next;
7303 }
7304
7305 void ApplyAnyObjectLogic(Expression e)
7306 {
7307    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7308 #ifdef _DEBUG
7309    char debugExpString[4096];
7310    debugExpString[0] = '\0';
7311    PrintExpression(e, debugExpString);
7312 #endif
7313
7314    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7315    {
7316       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7317       //ellipsisDestType = destType;
7318       if(e && e.expType)
7319       {
7320          Type type = e.expType;
7321          Class _class = null;
7322          //Type destType = e.destType;
7323
7324          if(type.kind == classType && type._class && type._class.registered)
7325          {
7326             _class = type._class.registered;
7327          }
7328          else if(type.kind == subClassType)
7329          {
7330             _class = FindClass("ecere::com::Class").registered;
7331          }
7332          else
7333          {
7334             char string[1024] = "";
7335             Symbol classSym;
7336
7337             PrintTypeNoConst(type, string, false, true);
7338             classSym = FindClass(string);
7339             if(classSym) _class = classSym.registered;
7340          }
7341
7342          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...
7343             (!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))) ||
7344             destType.byReference)))
7345          {
7346             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7347             {
7348                Expression checkedExp = e, newExp;
7349
7350                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7351                {
7352                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7353                   {
7354                      if(checkedExp.type == extensionCompoundExp)
7355                      {
7356                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7357                      }
7358                      else
7359                         checkedExp = checkedExp.list->last;
7360                   }
7361                   else if(checkedExp.type == castExp)
7362                      checkedExp = checkedExp.cast.exp;
7363                }
7364
7365                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7366                {
7367                   newExp = checkedExp.op.exp2;
7368                   checkedExp.op.exp2 = null;
7369                   FreeExpContents(checkedExp);
7370
7371                   if(e.expType && e.expType.passAsTemplate)
7372                   {
7373                      char size[100];
7374                      ComputeTypeSize(e.expType);
7375                      sprintf(size, "%d", e.expType.size);   // Potential 32/64 Bootstrap issue
7376                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7377                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7378                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7379                   }
7380
7381                   ReplaceExpContents(checkedExp, newExp);
7382                   e.byReference = true;
7383                }
7384                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7385                {
7386                   Expression checkedExp; //, newExp;
7387
7388                   {
7389                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7390                      bool hasAddress =
7391                         e.type == identifierExp ||
7392                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7393                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7394                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7395                         e.type == indexExp;
7396
7397                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7398                      {
7399                         Context context = PushContext();
7400                         Declarator decl;
7401                         OldList * specs = MkList();
7402                         char typeString[1024];
7403                         Expression newExp { };
7404
7405                         typeString[0] = '\0';
7406                         *newExp = *e;
7407
7408                         //if(e.destType) e.destType.refCount++;
7409                         // if(exp.expType) exp.expType.refCount++;
7410                         newExp.prev = null;
7411                         newExp.next = null;
7412                         newExp.expType = null;
7413
7414                         PrintTypeNoConst(e.expType, typeString, false, true);
7415                         decl = SpecDeclFromString(typeString, specs, null);
7416                         newExp.destType = ProcessType(specs, decl);
7417
7418                         curContext = context;
7419
7420                         // We need a current compound for this
7421                         if(curCompound)
7422                         {
7423                            char name[100];
7424                            OldList * stmts = MkList();
7425                            e.type = extensionCompoundExp;
7426                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7427                            if(!curCompound.compound.declarations)
7428                               curCompound.compound.declarations = MkList();
7429                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7430                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7431                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7432                            e.compound = MkCompoundStmt(null, stmts);
7433                         }
7434                         else
7435                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7436
7437                         /*
7438                         e.compound = MkCompoundStmt(
7439                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7440                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7441
7442                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7443                         */
7444
7445                         {
7446                            Type type = e.destType;
7447                            e.destType = { };
7448                            CopyTypeInto(e.destType, type);
7449                            e.destType.refCount = 1;
7450                            e.destType.classObjectType = none;
7451                            FreeType(type);
7452                         }
7453
7454                         e.compound.compound.context = context;
7455                         PopContext(context);
7456                         curContext = context.parent;
7457                      }
7458                   }
7459
7460                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7461                   checkedExp = e;
7462                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7463                   {
7464                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7465                      {
7466                         if(checkedExp.type == extensionCompoundExp)
7467                         {
7468                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7469                         }
7470                         else
7471                            checkedExp = checkedExp.list->last;
7472                      }
7473                      else if(checkedExp.type == castExp)
7474                         checkedExp = checkedExp.cast.exp;
7475                   }
7476                   {
7477                      Expression operand { };
7478                      operand = *checkedExp;
7479                      checkedExp.Clear();
7480                      checkedExp.destType = ProcessTypeString("void *", false);
7481                      checkedExp.expType = checkedExp.destType;
7482                      checkedExp.destType.refCount++;
7483
7484                      checkedExp.type = opExp;
7485                      checkedExp.op.op = '&';
7486                      checkedExp.op.exp1 = null;
7487                      checkedExp.op.exp2 = operand;
7488
7489                      //newExp = MkExpOp(null, '&', checkedExp);
7490                   }
7491                   //ReplaceExpContents(checkedExp, newExp);
7492                }
7493             }
7494          }
7495       }
7496    }
7497    {
7498       // If expression type is a simple class, make it an address
7499       // FixReference(e, true);
7500    }
7501 //#if 0
7502    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7503       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7504          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7505    {
7506       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"))
7507       {
7508          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7509       }
7510       else
7511       {
7512          Expression thisExp { };
7513
7514          *thisExp = *e;
7515          thisExp.prev = null;
7516          thisExp.next = null;
7517          e.Clear();
7518
7519          e.type = bracketsExp;
7520          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7521          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7522             ((Expression)e.list->first).byReference = true;
7523
7524          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7525          {
7526             e.expType = thisExp.expType;
7527             e.expType.refCount++;
7528          }
7529          else*/
7530          {
7531             e.expType = { };
7532             CopyTypeInto(e.expType, thisExp.expType);
7533             e.expType.byReference = false;
7534             e.expType.refCount = 1;
7535
7536             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7537                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7538             {
7539                e.expType.classObjectType = none;
7540             }
7541          }
7542       }
7543    }
7544 // TOFIX: Try this for a nice IDE crash!
7545 //#endif
7546    // The other way around
7547    else
7548 //#endif
7549    if(destType && e.expType &&
7550          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7551          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7552          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7553    {
7554       if(destType.kind == ellipsisType)
7555       {
7556          Compiler_Error($"Unspecified type\n");
7557       }
7558       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7559       {
7560          bool byReference = e.expType.byReference;
7561          Expression thisExp { };
7562          Declarator decl;
7563          OldList * specs = MkList();
7564          char typeString[1024]; // Watch buffer overruns
7565          Type type;
7566          ClassObjectType backupClassObjectType;
7567          bool backupByReference;
7568
7569          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7570             type = e.expType;
7571          else
7572             type = destType;
7573
7574          backupClassObjectType = type.classObjectType;
7575          backupByReference = type.byReference;
7576
7577          type.classObjectType = none;
7578          type.byReference = false;
7579
7580          typeString[0] = '\0';
7581          PrintType(type, typeString, false, true);
7582          decl = SpecDeclFromString(typeString, specs, null);
7583
7584          type.classObjectType = backupClassObjectType;
7585          type.byReference = backupByReference;
7586
7587          *thisExp = *e;
7588          thisExp.prev = null;
7589          thisExp.next = null;
7590          e.Clear();
7591
7592          if( ( type.kind == classType && type._class && type._class.registered &&
7593                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7594                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7595              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7596              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7597          {
7598             bool passAsTemplate = thisExp.destType.passAsTemplate;
7599             Type t;
7600
7601             destType.refCount++;
7602
7603             e.type = opExp;
7604             e.op.op = '*';
7605             e.op.exp1 = null;
7606             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7607
7608             t = { };
7609             CopyTypeInto(t, thisExp.destType);
7610             t.passAsTemplate = false;
7611             FreeType(thisExp.destType);
7612             thisExp.destType = t;
7613
7614             t = { };
7615             CopyTypeInto(t, destType);
7616             t.passAsTemplate = passAsTemplate;
7617             FreeType(destType);
7618             destType = t;
7619             destType.refCount = 0;
7620
7621             e.expType = { };
7622             CopyTypeInto(e.expType, type);
7623             if(type.passAsTemplate)
7624             {
7625                e.expType.classObjectType = none;
7626                e.expType.passAsTemplate = false;
7627             }
7628             e.expType.byReference = false;
7629             e.expType.refCount = 1;
7630          }
7631          else
7632          {
7633             e.type = castExp;
7634             e.cast.typeName = MkTypeName(specs, decl);
7635             e.cast.exp = thisExp;
7636             e.byReference = true;
7637             e.expType = type;
7638             type.refCount++;
7639          }
7640
7641          if(e.destType)
7642             FreeType(e.destType);
7643
7644          e.destType = destType;
7645          destType.refCount++;
7646       }
7647    }
7648 }
7649
7650 void ApplyLocation(Expression exp, Location loc)
7651 {
7652    exp.loc = loc;
7653    switch(exp.type)
7654    {
7655       case opExp:
7656          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7657          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7658          break;
7659       case bracketsExp:
7660          if(exp.list)
7661          {
7662             Expression e;
7663             for(e = exp.list->first; e; e = e.next)
7664                ApplyLocation(e, loc);
7665          }
7666          break;
7667       case indexExp:
7668          if(exp.index.index)
7669          {
7670             Expression e;
7671             for(e = exp.index.index->first; e; e = e.next)
7672                ApplyLocation(e, loc);
7673          }
7674          if(exp.index.exp)
7675             ApplyLocation(exp.index.exp, loc);
7676          break;
7677       case callExp:
7678          if(exp.call.arguments)
7679          {
7680             Expression arg;
7681             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7682                ApplyLocation(arg, loc);
7683          }
7684          if(exp.call.exp)
7685             ApplyLocation(exp.call.exp, loc);
7686          break;
7687       case memberExp:
7688       case pointerExp:
7689          if(exp.member.exp)
7690             ApplyLocation(exp.member.exp, loc);
7691          break;
7692       case castExp:
7693          if(exp.cast.exp)
7694             ApplyLocation(exp.cast.exp, loc);
7695          break;
7696       case conditionExp:
7697          if(exp.cond.exp)
7698          {
7699             Expression e;
7700             for(e = exp.cond.exp->first; e; e = e.next)
7701                ApplyLocation(e, loc);
7702          }
7703          if(exp.cond.cond)
7704             ApplyLocation(exp.cond.cond, loc);
7705          if(exp.cond.elseExp)
7706             ApplyLocation(exp.cond.elseExp, loc);
7707          break;
7708       case vaArgExp:
7709          if(exp.vaArg.exp)
7710             ApplyLocation(exp.vaArg.exp, loc);
7711          break;
7712       default:
7713          break;
7714    }
7715 }
7716
7717 void ProcessExpressionType(Expression exp)
7718 {
7719    bool unresolved = false;
7720    Location oldyylloc = yylloc;
7721    bool notByReference = false;
7722 #ifdef _DEBUG
7723    char debugExpString[4096];
7724    debugExpString[0] = '\0';
7725    PrintExpression(exp, debugExpString);
7726 #endif
7727    if(!exp || exp.expType)
7728       return;
7729
7730    //eSystem_Logf("%s\n", expString);
7731
7732    // Testing this here
7733    yylloc = exp.loc;
7734    switch(exp.type)
7735    {
7736       case identifierExp:
7737       {
7738          Identifier id = exp.identifier;
7739          if(!id || !topContext) return;
7740
7741          // DOING THIS LATER NOW...
7742          if(id._class && id._class.name)
7743          {
7744             id.classSym = id._class.symbol; // FindClass(id._class.name);
7745             /* TODO: Name Space Fix ups
7746             if(!id.classSym)
7747                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7748             */
7749          }
7750
7751          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7752          {
7753             exp.expType = ProcessTypeString("Module", true);
7754             break;
7755          }
7756          else */
7757          if(!strcmp(id.string, "__runtimePlatform"))
7758          {
7759             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7760             break;
7761          }
7762          else if(strstr(id.string, "__ecereClass") == id.string)
7763          {
7764             exp.expType = ProcessTypeString("ecere::com::Class", true);
7765             break;
7766          }
7767          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7768          {
7769             // Added this here as well
7770             ReplaceClassMembers(exp, thisClass);
7771             if(exp.type != identifierExp)
7772             {
7773                ProcessExpressionType(exp);
7774                break;
7775             }
7776
7777             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7778                break;
7779          }
7780          else
7781          {
7782             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7783             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7784             if(!symbol/* && exp.destType*/)
7785             {
7786                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7787                   break;
7788                else
7789                {
7790                   if(thisClass)
7791                   {
7792                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7793                      if(exp.type != identifierExp)
7794                      {
7795                         ProcessExpressionType(exp);
7796                         break;
7797                      }
7798                   }
7799                   // Static methods called from inside the _class
7800                   else if(currentClass && !id._class)
7801                   {
7802                      if(ResolveIdWithClass(exp, currentClass, true))
7803                         break;
7804                   }
7805                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7806                }
7807             }
7808
7809             // If we manage to resolve this symbol
7810             if(symbol)
7811             {
7812                Type type = symbol.type;
7813                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7814
7815                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7816                {
7817                   Context context = SetupTemplatesContext(_class);
7818                   type = ReplaceThisClassType(_class);
7819                   FinishTemplatesContext(context);
7820                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7821                }
7822
7823                FreeSpecifier(id._class);
7824                id._class = null;
7825                delete id.string;
7826                id.string = CopyString(symbol.string);
7827
7828                id.classSym = null;
7829                exp.expType = type;
7830                if(type)
7831                   type.refCount++;
7832
7833                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7834                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7835                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7836                   // Add missing cases here... enum Classes...
7837                   exp.isConstant = true;
7838
7839                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7840                if(symbol.isParam || !strcmp(id.string, "this"))
7841                {
7842                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7843                      exp.byReference = true;
7844
7845                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7846                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7847                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7848                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7849                   {
7850                      Identifier id = exp.identifier;
7851                      exp.type = bracketsExp;
7852                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7853                   }*/
7854                }
7855
7856                if(symbol.isIterator)
7857                {
7858                   if(symbol.isIterator == 3)
7859                   {
7860                      exp.type = bracketsExp;
7861                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7862                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7863                      exp.expType = null;
7864                      ProcessExpressionType(exp);
7865                   }
7866                   else if(symbol.isIterator != 4)
7867                   {
7868                      exp.type = memberExp;
7869                      exp.member.exp = MkExpIdentifier(exp.identifier);
7870                      exp.member.exp.expType = exp.expType;
7871                      /*if(symbol.isIterator == 6)
7872                         exp.member.member = MkIdentifier("key");
7873                      else*/
7874                         exp.member.member = MkIdentifier("data");
7875                      exp.expType = null;
7876                      ProcessExpressionType(exp);
7877                   }
7878                }
7879                break;
7880             }
7881             else
7882             {
7883                DefinedExpression definedExp = null;
7884                if(thisNameSpace && !(id._class && !id._class.name))
7885                {
7886                   char name[1024];
7887                   strcpy(name, thisNameSpace);
7888                   strcat(name, "::");
7889                   strcat(name, id.string);
7890                   definedExp = eSystem_FindDefine(privateModule, name);
7891                }
7892                if(!definedExp)
7893                   definedExp = eSystem_FindDefine(privateModule, id.string);
7894                if(definedExp)
7895                {
7896                   int c;
7897                   for(c = 0; c<definedExpStackPos; c++)
7898                      if(definedExpStack[c] == definedExp)
7899                         break;
7900                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7901                   {
7902                      Location backupYylloc = yylloc;
7903                      File backInput = fileInput;
7904                      definedExpStack[definedExpStackPos++] = definedExp;
7905
7906                      fileInput = TempFile { };
7907                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7908                      fileInput.Seek(0, start);
7909
7910                      echoOn = false;
7911                      parsedExpression = null;
7912                      resetScanner();
7913                      expression_yyparse();
7914                      delete fileInput;
7915                      if(backInput)
7916                         fileInput = backInput;
7917
7918                      yylloc = backupYylloc;
7919
7920                      if(parsedExpression)
7921                      {
7922                         FreeIdentifier(id);
7923                         exp.type = bracketsExp;
7924                         exp.list = MkListOne(parsedExpression);
7925                         ApplyLocation(parsedExpression, yylloc);
7926                         ProcessExpressionType(exp);
7927                         definedExpStackPos--;
7928                         return;
7929                      }
7930                      definedExpStackPos--;
7931                   }
7932                   else
7933                   {
7934                      if(inCompiler)
7935                      {
7936                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7937                      }
7938                   }
7939                }
7940                else
7941                {
7942                   GlobalData data = null;
7943                   if(thisNameSpace && !(id._class && !id._class.name))
7944                   {
7945                      char name[1024];
7946                      strcpy(name, thisNameSpace);
7947                      strcat(name, "::");
7948                      strcat(name, id.string);
7949                      data = FindGlobalData(name);
7950                   }
7951                   if(!data)
7952                      data = FindGlobalData(id.string);
7953                   if(data)
7954                   {
7955                      DeclareGlobalData(curExternal, data);
7956                      exp.expType = data.dataType;
7957                      if(data.dataType) data.dataType.refCount++;
7958
7959                      delete id.string;
7960                      id.string = CopyString(data.fullName);
7961                      FreeSpecifier(id._class);
7962                      id._class = null;
7963
7964                      break;
7965                   }
7966                   else
7967                   {
7968                      GlobalFunction function = null;
7969                      if(thisNameSpace && !(id._class && !id._class.name))
7970                      {
7971                         char name[1024];
7972                         strcpy(name, thisNameSpace);
7973                         strcat(name, "::");
7974                         strcat(name, id.string);
7975                         function = eSystem_FindFunction(privateModule, name);
7976                      }
7977                      if(!function)
7978                         function = eSystem_FindFunction(privateModule, id.string);
7979                      if(function)
7980                      {
7981                         char name[1024];
7982                         delete id.string;
7983                         id.string = CopyString(function.name);
7984                         name[0] = 0;
7985
7986                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7987                            strcpy(name, "__ecereFunction_");
7988                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7989                         if(DeclareFunction(curExternal, function, name))
7990                         {
7991                            delete id.string;
7992                            id.string = CopyString(name);
7993                         }
7994                         exp.expType = function.dataType;
7995                         if(function.dataType) function.dataType.refCount++;
7996
7997                         FreeSpecifier(id._class);
7998                         id._class = null;
7999
8000                         break;
8001                      }
8002                   }
8003                }
8004             }
8005          }
8006          unresolved = true;
8007          break;
8008       }
8009       case instanceExp:
8010       {
8011          // Class _class;
8012          // Symbol classSym;
8013
8014          if(!exp.instance._class)
8015          {
8016             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8017             {
8018                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8019             }
8020          }
8021
8022          //classSym = FindClass(exp.instance._class.fullName);
8023          //_class = classSym ? classSym.registered : null;
8024
8025          ProcessInstantiationType(exp.instance);
8026
8027          exp.isConstant = exp.instance.isConstant;
8028
8029          /*
8030          if(_class.type == unitClass && _class.base.type != systemClass)
8031          {
8032             {
8033                Type destType = exp.destType;
8034
8035                exp.destType = MkClassType(_class.base.fullName);
8036                exp.expType = MkClassType(_class.fullName);
8037                CheckExpressionType(exp, exp.destType, true);
8038
8039                exp.destType = destType;
8040             }
8041             exp.expType = MkClassType(_class.fullName);
8042          }
8043          else*/
8044          if(exp.instance._class)
8045          {
8046             exp.expType = MkClassType(exp.instance._class.name);
8047             /*if(exp.expType._class && exp.expType._class.registered &&
8048                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8049                exp.expType.byReference = true;*/
8050          }
8051          break;
8052       }
8053       case constantExp:
8054       {
8055          if(!exp.expType)
8056          {
8057             char * constant = exp.constant;
8058             Type type
8059             {
8060                refCount = 1;
8061                constant = true;
8062             };
8063             exp.expType = type;
8064
8065             if(constant[0] == '\'')
8066             {
8067                if((int)((byte *)constant)[1] > 127)
8068                {
8069                   int nb;
8070                   unichar ch = UTF8GetChar(constant + 1, &nb);
8071                   if(nb < 2) ch = constant[1];
8072                   delete constant;
8073                   exp.constant = PrintUInt(ch);
8074                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8075                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8076                   type._class = FindClass("unichar");
8077
8078                   type.isSigned = false;
8079                }
8080                else
8081                {
8082                   type.kind = charType;
8083                   type.isSigned = true;
8084                }
8085             }
8086             else
8087             {
8088                char * dot = strchr(constant, '.');
8089                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8090                char * exponent;
8091                if(isHex)
8092                {
8093                   exponent = strchr(constant, 'p');
8094                   if(!exponent) exponent = strchr(constant, 'P');
8095                }
8096                else
8097                {
8098                   exponent = strchr(constant, 'e');
8099                   if(!exponent) exponent = strchr(constant, 'E');
8100                }
8101
8102                if(dot || exponent)
8103                {
8104                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8105                      type.kind = floatType;
8106                   else
8107                      type.kind = doubleType;
8108                   type.isSigned = true;
8109                }
8110                else
8111                {
8112                   bool isSigned = constant[0] == '-';
8113                   char * endP = null;
8114                   int64 i64 = strtoll(constant, &endP, 0);
8115                   uint64 ui64 = strtoull(constant, &endP, 0);
8116                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8117                   if(isSigned)
8118                   {
8119                      if(i64 < MININT)
8120                         is64Bit = true;
8121                   }
8122                   else
8123                   {
8124                      if(ui64 > MAXINT)
8125                      {
8126                         if(ui64 > MAXDWORD)
8127                         {
8128                            is64Bit = true;
8129                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8130                               isSigned = true;
8131                         }
8132                      }
8133                      else if(constant[0] != '0' || !constant[1])
8134                         isSigned = true;
8135                   }
8136                   type.kind = is64Bit ? int64Type : intType;
8137                   type.isSigned = isSigned;
8138                }
8139             }
8140             exp.isConstant = true;
8141             if(exp.destType && exp.destType.kind == doubleType)
8142                type.kind = doubleType;
8143             else if(exp.destType && exp.destType.kind == floatType)
8144                type.kind = floatType;
8145             else if(exp.destType && exp.destType.kind == int64Type)
8146                type.kind = int64Type;
8147          }
8148          break;
8149       }
8150       case stringExp:
8151       {
8152          exp.isConstant = true;      // Why wasn't this constant?
8153          exp.expType = Type
8154          {
8155             refCount = 1;
8156             kind = pointerType;
8157             type = Type
8158             {
8159                refCount = 1;
8160                kind = exp.wideString ? shortType : charType;
8161                constant = true;
8162                isSigned = exp.wideString ? false : true;
8163             }
8164          };
8165          break;
8166       }
8167       case newExp:
8168       case new0Exp:
8169          ProcessExpressionType(exp._new.size);
8170          exp.expType = Type
8171          {
8172             refCount = 1;
8173             kind = pointerType;
8174             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8175          };
8176          DeclareType(curExternal, exp.expType.type, true, false);
8177          break;
8178       case renewExp:
8179       case renew0Exp:
8180          ProcessExpressionType(exp._renew.size);
8181          ProcessExpressionType(exp._renew.exp);
8182          exp.expType = Type
8183          {
8184             refCount = 1;
8185             kind = pointerType;
8186             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8187          };
8188          DeclareType(curExternal, exp.expType.type, true, false);
8189          break;
8190       case opExp:
8191       {
8192          bool assign = false, boolResult = false, boolOps = false;
8193          Type type1 = null, type2 = null;
8194          bool useDestType = false, useSideType = false;
8195          Location oldyylloc = yylloc;
8196          bool useSideUnit = false;
8197          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8198
8199          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8200          Type dummy
8201          {
8202             count = 1;
8203             refCount = 1;
8204          };
8205
8206          switch(exp.op.op)
8207          {
8208             // Assignment Operators
8209             case '=':
8210             case MUL_ASSIGN:
8211             case DIV_ASSIGN:
8212             case MOD_ASSIGN:
8213             case ADD_ASSIGN:
8214             case SUB_ASSIGN:
8215             case LEFT_ASSIGN:
8216             case RIGHT_ASSIGN:
8217             case AND_ASSIGN:
8218             case XOR_ASSIGN:
8219             case OR_ASSIGN:
8220                assign = true;
8221                break;
8222             // boolean Operators
8223             case '!':
8224                // Expect boolean operators
8225                //boolOps = true;
8226                //boolResult = true;
8227                break;
8228             case AND_OP:
8229             case OR_OP:
8230                // Expect boolean operands
8231                boolOps = true;
8232                boolResult = true;
8233                break;
8234             // Comparisons
8235             case EQ_OP:
8236             case '<':
8237             case '>':
8238             case LE_OP:
8239             case GE_OP:
8240             case NE_OP:
8241                // Gives boolean result
8242                boolResult = true;
8243                useSideType = true;
8244                break;
8245             case '+':
8246             case '-':
8247                useSideUnit = true;
8248                useSideType = true;
8249                useDestType = true;
8250                break;
8251
8252             case LEFT_OP:
8253             case RIGHT_OP:
8254                useSideType = true;
8255                useDestType = true;
8256                break;
8257
8258             case '|':
8259             case '^':
8260                useSideType = true;
8261                useDestType = true;
8262                break;
8263
8264             case '/':
8265             case '%':
8266                useSideType = true;
8267                useDestType = true;
8268                break;
8269             case '&':
8270             case '*':
8271                if(exp.op.exp1)
8272                {
8273                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8274                   useSideType = true;
8275                   useDestType = true;
8276                }
8277                break;
8278
8279             /*// Implement speed etc.
8280             case '*':
8281             case '/':
8282                break;
8283             */
8284          }
8285          if(exp.op.op == '&')
8286          {
8287             // Added this here earlier for Iterator address as key
8288             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8289             {
8290                Identifier id = exp.op.exp2.identifier;
8291                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8292                if(symbol && symbol.isIterator == 2)
8293                {
8294                   exp.type = memberExp;
8295                   exp.member.exp = exp.op.exp2;
8296                   exp.member.member = MkIdentifier("key");
8297                   exp.expType = null;
8298                   exp.op.exp2.expType = symbol.type;
8299                   symbol.type.refCount++;
8300                   ProcessExpressionType(exp);
8301                   FreeType(dummy);
8302                   break;
8303                }
8304                // exp.op.exp2.usage.usageRef = true;
8305             }
8306          }
8307
8308          //dummy.kind = TypeDummy;
8309          if(exp.op.exp1)
8310          {
8311             // Added this check here to use the dest type only for units derived from the base unit
8312             // So that untyped units will use the side unit as opposed to the untyped destination unit
8313             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8314             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8315                useDestType = false;
8316
8317             if(destClass && useDestType &&
8318               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8319
8320               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8321             {
8322                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8323                exp.op.exp1.destType = exp.destType;
8324                exp.op.exp1.opDestType = true;
8325                if(exp.destType)
8326                   exp.destType.refCount++;
8327             }
8328             else if(!assign)
8329             {
8330                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8331                exp.op.exp1.destType = dummy;
8332                dummy.refCount++;
8333             }
8334
8335             // TESTING THIS HERE...
8336             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8337                ProcessExpressionType(exp.op.exp1);
8338             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8339
8340             exp.op.exp1.opDestType = false;
8341
8342             // Fix for unit and ++ / --
8343             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8344                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8345             {
8346                exp.op.exp2 = MkExpConstant("1");
8347                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8348                assign = true;
8349             }
8350
8351             if(exp.op.exp1.destType == dummy)
8352             {
8353                FreeType(dummy);
8354                exp.op.exp1.destType = null;
8355             }
8356             type1 = exp.op.exp1.expType;
8357          }
8358
8359          if(exp.op.exp2)
8360          {
8361             char expString[10240];
8362             expString[0] = '\0';
8363             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8364             {
8365                if(exp.op.exp1)
8366                {
8367                   exp.op.exp2.destType = exp.op.exp1.expType;
8368                   if(exp.op.exp1.expType)
8369                      exp.op.exp1.expType.refCount++;
8370                }
8371                else
8372                {
8373                   exp.op.exp2.destType = exp.destType;
8374                   if(!exp.op.exp1 || (exp.op.op != '&' && exp.op.op != '^'))
8375                      exp.op.exp2.opDestType = true;
8376                   if(exp.destType)
8377                      exp.destType.refCount++;
8378                }
8379
8380                if(type1) type1.refCount++;
8381                exp.expType = type1;
8382             }
8383             else if(assign)
8384             {
8385                if(inCompiler)
8386                   PrintExpression(exp.op.exp2, expString);
8387
8388                if(type1 && type1.kind == pointerType)
8389                {
8390                   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 ||
8391                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8392                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8393                   else if(exp.op.op == '=')
8394                   {
8395                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8396                      exp.op.exp2.destType = type1;
8397                      if(type1)
8398                         type1.refCount++;
8399                   }
8400                }
8401                else
8402                {
8403                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8404                   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/* ||
8405                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8406                   else
8407                   {
8408                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8409                      exp.op.exp2.destType = type1;
8410                      if(type1)
8411                         type1.refCount++;
8412                   }
8413                }
8414                if(type1) type1.refCount++;
8415                exp.expType = type1;
8416             }
8417             else if(destClass &&
8418                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8419                   (destClass.type == enumClass && useDestType)))
8420             {
8421                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8422                exp.op.exp2.destType = exp.destType;
8423                if(exp.op.op != '&' && exp.op.op != '^')
8424                   exp.op.exp2.opDestType = true;
8425                if(exp.destType)
8426                   exp.destType.refCount++;
8427             }
8428             else
8429             {
8430                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8431                exp.op.exp2.destType = dummy;
8432                dummy.refCount++;
8433             }
8434
8435             // TESTING THIS HERE... (DANGEROUS)
8436             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8437                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8438             {
8439                FreeType(exp.op.exp2.destType);
8440                exp.op.exp2.destType = type1;
8441                type1.refCount++;
8442             }
8443             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8444             // Cannot lose the cast on a sizeof
8445             if(exp.op.op == SIZEOF)
8446             {
8447                Expression e = exp.op.exp2;
8448                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8449                {
8450                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8451                   {
8452                      if(e.type == extensionCompoundExp)
8453                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8454                      else
8455                         e = e.list->last;
8456                   }
8457                }
8458                if(e.type == castExp && e.cast.exp)
8459                   e.cast.exp.needCast = true;
8460             }
8461             ProcessExpressionType(exp.op.exp2);
8462             exp.op.exp2.opDestType = false;
8463             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8464
8465             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8466             {
8467                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)
8468                {
8469                   if(exp.op.op != '=' && type1.type.kind == voidType)
8470                      Compiler_Error($"void *: unknown size\n");
8471                }
8472                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||
8473                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8474                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8475                               exp.op.exp2.expType._class.registered.type == structClass ||
8476                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8477                {
8478                   if(exp.op.op == ADD_ASSIGN)
8479                      Compiler_Error($"cannot add two pointers\n");
8480                }
8481                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8482                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8483                {
8484                   if(exp.op.op == ADD_ASSIGN)
8485                      Compiler_Error($"cannot add two pointers\n");
8486                }
8487                else if(inCompiler)
8488                {
8489                   char type1String[1024];
8490                   char type2String[1024];
8491                   type1String[0] = '\0';
8492                   type2String[0] = '\0';
8493
8494                   PrintType(exp.op.exp2.expType, type1String, false, true);
8495                   PrintType(type1, type2String, false, true);
8496                   ChangeCh(expString, '\n', ' ');
8497                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8498                }
8499             }
8500
8501             if(exp.op.exp2.destType == dummy)
8502             {
8503                FreeType(dummy);
8504                exp.op.exp2.destType = null;
8505             }
8506
8507             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8508             {
8509                type2 = { };
8510                type2.refCount = 1;
8511                CopyTypeInto(type2, exp.op.exp2.expType);
8512                type2.isSigned = true;
8513             }
8514             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8515             {
8516                type2 = { kind = intType };
8517                type2.refCount = 1;
8518                type2.isSigned = true;
8519             }
8520             else
8521             {
8522                type2 = exp.op.exp2.expType;
8523                if(type2) type2.refCount++;
8524             }
8525          }
8526
8527          dummy.kind = voidType;
8528
8529          if(exp.op.op == SIZEOF)
8530          {
8531             exp.expType = Type
8532             {
8533                refCount = 1;
8534                kind = intSizeType;
8535             };
8536             exp.isConstant = true;
8537          }
8538          // Get type of dereferenced pointer
8539          else if(exp.op.op == '*' && !exp.op.exp1)
8540          {
8541             exp.expType = Dereference(type2);
8542             if(type2 && type2.kind == classType)
8543                notByReference = true;
8544          }
8545          else if(exp.op.op == '&' && !exp.op.exp1)
8546             exp.expType = Reference(type2);
8547          else if(!assign)
8548          {
8549             if(boolOps)
8550             {
8551                if(exp.op.exp1)
8552                {
8553                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8554                   exp.op.exp1.destType = MkClassType("bool");
8555                   exp.op.exp1.destType.truth = true;
8556                   if(!exp.op.exp1.expType)
8557                      ProcessExpressionType(exp.op.exp1);
8558                   else
8559                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8560                   FreeType(exp.op.exp1.expType);
8561                   exp.op.exp1.expType = MkClassType("bool");
8562                   exp.op.exp1.expType.truth = true;
8563                }
8564                if(exp.op.exp2)
8565                {
8566                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8567                   exp.op.exp2.destType = MkClassType("bool");
8568                   exp.op.exp2.destType.truth = true;
8569                   if(!exp.op.exp2.expType)
8570                      ProcessExpressionType(exp.op.exp2);
8571                   else
8572                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8573                   FreeType(exp.op.exp2.expType);
8574                   exp.op.exp2.expType = MkClassType("bool");
8575                   exp.op.exp2.expType.truth = true;
8576                }
8577             }
8578             else if(exp.op.exp1 && exp.op.exp2 &&
8579                ((useSideType /*&&
8580                      (useSideUnit ||
8581                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8582                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8583                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8584                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8585             {
8586                if(type1 && type2 &&
8587                   // If either both are class or both are not class
8588                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8589                {
8590                   // Added this check for enum subtraction to result in an int type:
8591                   if(exp.op.op == '-' &&
8592                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8593                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8594                   {
8595                      Type intType;
8596                      if(!type1._class.registered.dataType)
8597                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8598                      if(!type2._class.registered.dataType)
8599                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8600
8601                      intType = ProcessTypeString(
8602                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8603
8604                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8605                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8606                      exp.op.exp1.destType = intType;
8607                      exp.op.exp2.destType = intType;
8608                      intType.refCount++;
8609                   }
8610                   else
8611                   {
8612                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8613                      exp.op.exp2.destType = type1;
8614                      type1.refCount++;
8615                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8616                      exp.op.exp1.destType = type2;
8617                      type2.refCount++;
8618                   }
8619
8620                   // Warning here for adding Radians + Degrees with no destination type
8621                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8622                      type1._class.registered && type1._class.registered.type == unitClass &&
8623                      type2._class.registered && type2._class.registered.type == unitClass &&
8624                      type1._class.registered != type2._class.registered)
8625                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8626                         type1._class.string, type2._class.string, type1._class.string);
8627
8628                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8629                   {
8630                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8631                      if(argExp)
8632                      {
8633                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8634
8635                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8636                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8637                            exp.op.exp1)));
8638
8639                         ProcessExpressionType(exp.op.exp1);
8640
8641                         if(type2.kind != pointerType)
8642                         {
8643                            ProcessExpressionType(classExp);
8644
8645                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8646
8647                            if(!exp.op.exp2.expType)
8648                            {
8649                               if(type2)
8650                                  FreeType(type2);
8651                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8652                               type2.refCount++;
8653                            }
8654
8655                            ProcessExpressionType(exp.op.exp2);
8656                         }
8657                      }
8658                   }
8659
8660                   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)))
8661                   {
8662                      if(type1.kind != classType && type1.type.kind == voidType)
8663                         Compiler_Error($"void *: unknown size\n");
8664                      exp.expType = type1;
8665                      if(type1) type1.refCount++;
8666                   }
8667                   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)))
8668                   {
8669                      if(type2.kind != classType && type2.type.kind == voidType)
8670                         Compiler_Error($"void *: unknown size\n");
8671                      exp.expType = type2;
8672                      if(type2) type2.refCount++;
8673                   }
8674                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8675                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8676                   {
8677                      Compiler_Warning($"different levels of indirection\n");
8678                   }
8679                   else
8680                   {
8681                      bool success = false;
8682                      if(type1.kind == pointerType && type2.kind == pointerType)
8683                      {
8684                         if(exp.op.op == '+')
8685                            Compiler_Error($"cannot add two pointers\n");
8686                         else if(exp.op.op == '-')
8687                         {
8688                            // Pointer Subtraction gives integer
8689                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8690                            {
8691                               exp.expType = Type
8692                               {
8693                                  kind = intType;
8694                                  refCount = 1;
8695                               };
8696                               success = true;
8697
8698                               if(type1.type.kind == templateType)
8699                               {
8700                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8701                                  if(argExp)
8702                                  {
8703                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8704
8705                                     ProcessExpressionType(classExp);
8706
8707                                     exp.type = bracketsExp;
8708                                     exp.list = MkListOne(MkExpOp(
8709                                        MkExpBrackets(MkListOne(MkExpOp(
8710                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8711                                              , exp.op.op,
8712                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8713                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8714
8715                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8716                                     FreeType(dummy);
8717                                     return;
8718                                  }
8719                               }
8720                            }
8721                         }
8722                      }
8723
8724                      if(!success && exp.op.exp1.type == constantExp)
8725                      {
8726                         // If first expression is constant, try to match that first
8727                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8728                         {
8729                            if(exp.expType) FreeType(exp.expType);
8730                            exp.expType = exp.op.exp1.destType;
8731                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8732                            success = true;
8733                         }
8734                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8735                         {
8736                            if(exp.expType) FreeType(exp.expType);
8737                            exp.expType = exp.op.exp2.destType;
8738                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8739                            success = true;
8740                         }
8741                      }
8742                      else if(!success)
8743                      {
8744                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8745                         {
8746                            if(exp.expType) FreeType(exp.expType);
8747                            exp.expType = exp.op.exp2.destType;
8748                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8749                            success = true;
8750                         }
8751                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8752                         {
8753                            if(exp.expType) FreeType(exp.expType);
8754                            exp.expType = exp.op.exp1.destType;
8755                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8756                            success = true;
8757                         }
8758                      }
8759                      if(!success)
8760                      {
8761                         char expString1[10240];
8762                         char expString2[10240];
8763                         char type1[1024];
8764                         char type2[1024];
8765                         expString1[0] = '\0';
8766                         expString2[0] = '\0';
8767                         type1[0] = '\0';
8768                         type2[0] = '\0';
8769                         if(inCompiler)
8770                         {
8771                            PrintExpression(exp.op.exp1, expString1);
8772                            ChangeCh(expString1, '\n', ' ');
8773                            PrintExpression(exp.op.exp2, expString2);
8774                            ChangeCh(expString2, '\n', ' ');
8775                            PrintType(exp.op.exp1.expType, type1, false, true);
8776                            PrintType(exp.op.exp2.expType, type2, false, true);
8777                         }
8778
8779                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8780                      }
8781                   }
8782                }
8783                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8784                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8785                {
8786                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8787                   // Convert e.g. / 4 into / 4.0
8788                   exp.op.exp1.destType = type2._class.registered.dataType;
8789                   if(type2._class.registered.dataType)
8790                      type2._class.registered.dataType.refCount++;
8791                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8792                   exp.expType = type2;
8793                   if(type2) type2.refCount++;
8794                }
8795                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8796                {
8797                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8798                   // Convert e.g. / 4 into / 4.0
8799                   exp.op.exp2.destType = type1._class.registered.dataType;
8800                   if(type1._class.registered.dataType)
8801                      type1._class.registered.dataType.refCount++;
8802                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8803                   exp.expType = type1;
8804                   if(type1) type1.refCount++;
8805                }
8806                else if(type1)
8807                {
8808                   bool valid = false;
8809
8810                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8811                   {
8812                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8813
8814                      if(!type1._class.registered.dataType)
8815                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8816                      exp.op.exp2.destType = type1._class.registered.dataType;
8817                      exp.op.exp2.destType.refCount++;
8818
8819                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8820                      if(type2)
8821                         FreeType(type2);
8822                      type2 = exp.op.exp2.destType;
8823                      if(type2) type2.refCount++;
8824
8825                      exp.expType = type2;
8826                      type2.refCount++;
8827                   }
8828
8829                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8830                   {
8831                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8832
8833                      if(!type2._class.registered.dataType)
8834                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8835                      exp.op.exp1.destType = type2._class.registered.dataType;
8836                      exp.op.exp1.destType.refCount++;
8837
8838                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8839                      type1 = exp.op.exp1.destType;
8840                      exp.expType = type1;
8841                      type1.refCount++;
8842                   }
8843
8844                   // TESTING THIS NEW CODE
8845                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8846                   {
8847                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8848                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8849                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8850                      {
8851                         // Convert the enum to an int instead for these operators
8852                         if(op1IsEnum && exp.op.exp2.expType)
8853                         {
8854                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8855                            {
8856                               if(exp.expType) FreeType(exp.expType);
8857                               exp.expType = exp.op.exp2.expType;
8858                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8859                               valid = true;
8860                            }
8861                         }
8862                         else if(op2IsEnum && exp.op.exp1.expType)
8863                         {
8864                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8865                            {
8866                               if(exp.expType) FreeType(exp.expType);
8867                               exp.expType = exp.op.exp1.expType;
8868                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8869                               valid = true;
8870                            }
8871                         }
8872                      }
8873                      else
8874                      {
8875                         if(op1IsEnum && exp.op.exp2.expType)
8876                         {
8877                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8878                            {
8879                               if(exp.expType) FreeType(exp.expType);
8880                               exp.expType = exp.op.exp1.expType;
8881                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8882                               valid = true;
8883                            }
8884                         }
8885                         else if(op2IsEnum && exp.op.exp1.expType)
8886                         {
8887                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8888                            {
8889                               if(exp.expType) FreeType(exp.expType);
8890                               exp.expType = exp.op.exp2.expType;
8891                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8892                               valid = true;
8893                            }
8894                         }
8895                      }
8896                   }
8897
8898                   if(!valid)
8899                   {
8900                      // Added this first part of the if here to handle  5 + Degrees { 5 } with either a base unit dest or not a unit dest type
8901                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8902                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8903                      {
8904                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8905                         exp.op.exp1.destType = type2;
8906                         type2.refCount++;
8907
8908                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8909                         {
8910                            if(exp.expType) FreeType(exp.expType);
8911                            exp.expType = exp.op.exp1.destType;
8912                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8913                         }
8914                      }
8915                      else
8916                      {
8917                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8918                         exp.op.exp2.destType = type1;
8919                         type1.refCount++;
8920
8921                      /*
8922                      // Maybe this was meant to be an enum...
8923                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8924                      {
8925                         Type oldType = exp.op.exp2.expType;
8926                         exp.op.exp2.expType = null;
8927                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8928                            FreeType(oldType);
8929                         else
8930                            exp.op.exp2.expType = oldType;
8931                      }
8932                      */
8933
8934                      /*
8935                      // TESTING THIS HERE... LATEST ADDITION
8936                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8937                      {
8938                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8939                         exp.op.exp2.destType = type2._class.registered.dataType;
8940                         if(type2._class.registered.dataType)
8941                            type2._class.registered.dataType.refCount++;
8942                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8943
8944                         //exp.expType = type2._class.registered.dataType; //type2;
8945                         //if(type2) type2.refCount++;
8946                      }
8947
8948                      // TESTING THIS HERE... LATEST ADDITION
8949                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8950                      {
8951                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8952                         exp.op.exp1.destType = type1._class.registered.dataType;
8953                         if(type1._class.registered.dataType)
8954                            type1._class.registered.dataType.refCount++;
8955                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8956                         exp.expType = type1._class.registered.dataType; //type1;
8957                         if(type1) type1.refCount++;
8958                      }
8959                      */
8960
8961                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8962                         {
8963                            if(exp.expType) FreeType(exp.expType);
8964                            exp.expType = exp.op.exp2.destType;
8965                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8966                         }
8967                         else if(type1 && type2)
8968                         {
8969                            char expString1[10240];
8970                            char expString2[10240];
8971                            char type1String[1024];
8972                            char type2String[1024];
8973                            expString1[0] = '\0';
8974                            expString2[0] = '\0';
8975                            type1String[0] = '\0';
8976                            type2String[0] = '\0';
8977                            if(inCompiler)
8978                            {
8979                               PrintExpression(exp.op.exp1, expString1);
8980                               ChangeCh(expString1, '\n', ' ');
8981                               PrintExpression(exp.op.exp2, expString2);
8982                               ChangeCh(expString2, '\n', ' ');
8983                               PrintType(exp.op.exp1.expType, type1String, false, true);
8984                               PrintType(exp.op.exp2.expType, type2String, false, true);
8985                            }
8986
8987                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8988
8989                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8990                            {
8991                               exp.expType = exp.op.exp1.expType;
8992                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8993                            }
8994                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8995                            {
8996                               exp.expType = exp.op.exp2.expType;
8997                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8998                            }
8999                         }
9000                      }
9001                   }
9002                }
9003                else if(type2)
9004                {
9005                   // Maybe this was meant to be an enum...
9006                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9007                   {
9008                      Type oldType = exp.op.exp1.expType;
9009                      exp.op.exp1.expType = null;
9010                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9011                         FreeType(oldType);
9012                      else
9013                         exp.op.exp1.expType = oldType;
9014                   }
9015
9016                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9017                   exp.op.exp1.destType = type2;
9018                   type2.refCount++;
9019                   /*
9020                   // TESTING THIS HERE... LATEST ADDITION
9021                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9022                   {
9023                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9024                      exp.op.exp1.destType = type1._class.registered.dataType;
9025                      if(type1._class.registered.dataType)
9026                         type1._class.registered.dataType.refCount++;
9027                   }
9028
9029                   // TESTING THIS HERE... LATEST ADDITION
9030                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9031                   {
9032                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9033                      exp.op.exp2.destType = type2._class.registered.dataType;
9034                      if(type2._class.registered.dataType)
9035                         type2._class.registered.dataType.refCount++;
9036                   }
9037                   */
9038
9039                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9040                   {
9041                      if(exp.expType) FreeType(exp.expType);
9042                      exp.expType = exp.op.exp1.destType;
9043                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9044                   }
9045                }
9046             }
9047             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9048             {
9049                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9050                {
9051                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9052                   // Convert e.g. / 4 into / 4.0
9053                   exp.op.exp1.destType = type2._class.registered.dataType;
9054                   if(type2._class.registered.dataType)
9055                      type2._class.registered.dataType.refCount++;
9056                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9057                }
9058                if(exp.op.op == '!')
9059                {
9060                   exp.expType = MkClassType("bool");
9061                   exp.expType.truth = true;
9062                }
9063                else
9064                {
9065                   exp.expType = type2;
9066                   if(type2) type2.refCount++;
9067                }
9068             }
9069             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9070             {
9071                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9072                {
9073                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9074                   // Convert e.g. / 4 into / 4.0
9075                   exp.op.exp2.destType = type1._class.registered.dataType;
9076                   if(type1._class.registered.dataType)
9077                      type1._class.registered.dataType.refCount++;
9078                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9079                }
9080                exp.expType = type1;
9081                if(type1) type1.refCount++;
9082             }
9083          }
9084
9085          yylloc = exp.loc;
9086          if(exp.op.exp1 && !exp.op.exp1.expType)
9087          {
9088             char expString[10000];
9089             expString[0] = '\0';
9090             if(inCompiler)
9091             {
9092                PrintExpression(exp.op.exp1, expString);
9093                ChangeCh(expString, '\n', ' ');
9094             }
9095             if(expString[0])
9096                Compiler_Error($"couldn't determine type of %s\n", expString);
9097          }
9098          if(exp.op.exp2 && !exp.op.exp2.expType)
9099          {
9100             char expString[10240];
9101             expString[0] = '\0';
9102             if(inCompiler)
9103             {
9104                PrintExpression(exp.op.exp2, expString);
9105                ChangeCh(expString, '\n', ' ');
9106             }
9107             if(expString[0])
9108                Compiler_Error($"couldn't determine type of %s\n", expString);
9109          }
9110
9111          if(boolResult)
9112          {
9113             FreeType(exp.expType);
9114             exp.expType = MkClassType("bool");
9115             exp.expType.truth = true;
9116          }
9117
9118          if(exp.op.op != SIZEOF)
9119             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9120                (!exp.op.exp2 || exp.op.exp2.isConstant);
9121
9122          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9123          {
9124             DeclareType(curExternal, exp.op.exp2.expType, true, false);
9125          }
9126
9127          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9128             Compiler_Warning($"deleting const qualified object\n");
9129
9130          yylloc = oldyylloc;
9131
9132          FreeType(dummy);
9133          if(type2)
9134             FreeType(type2);
9135          break;
9136       }
9137       case bracketsExp:
9138       case extensionExpressionExp:
9139       {
9140          Expression e;
9141          exp.isConstant = true;
9142          for(e = exp.list->first; e; e = e.next)
9143          {
9144             //bool inced = false;
9145             if(!e.next)
9146             {
9147                FreeType(e.destType);
9148                e.opDestType = exp.opDestType;
9149                e.destType = exp.destType;
9150                if(e.destType) { exp.destType.refCount++; /*e.destType.count++; inced = true;*/ }
9151             }
9152             ProcessExpressionType(e);
9153             /*if(inced)
9154                exp.destType.count--;*/
9155             if(!exp.expType && !e.next)
9156             {
9157                exp.expType = e.expType;
9158                if(e.expType) e.expType.refCount++;
9159             }
9160             if(!e.isConstant)
9161                exp.isConstant = false;
9162          }
9163
9164          // In case a cast became a member...
9165          e = exp.list->first;
9166          if(!e.next && e.type == memberExp)
9167          {
9168             // Preserve prev, next
9169             Expression next = exp.next, prev = exp.prev;
9170
9171
9172             FreeType(exp.expType);
9173             FreeType(exp.destType);
9174             delete exp.list;
9175
9176             *exp = *e;
9177
9178             exp.prev = prev;
9179             exp.next = next;
9180
9181             delete e;
9182
9183             ProcessExpressionType(exp);
9184          }
9185          break;
9186       }
9187       case indexExp:
9188       {
9189          Expression e;
9190          exp.isConstant = true;
9191
9192          ProcessExpressionType(exp.index.exp);
9193          if(!exp.index.exp.isConstant)
9194             exp.isConstant = false;
9195
9196          if(exp.index.exp.expType)
9197          {
9198             Type source = exp.index.exp.expType;
9199             if(source.kind == classType && source._class && source._class.registered)
9200             {
9201                Class _class = source._class.registered;
9202                Class c = _class.templateClass ? _class.templateClass : _class;
9203                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9204                {
9205                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9206
9207                   if(exp.index.index && exp.index.index->last)
9208                   {
9209                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9210
9211                      if(type.kind == classType) type.constant = true;
9212                      else if(type.kind == pointerType)
9213                      {
9214                         Type t = type;
9215                         while(t.kind == pointerType) t = t.type;
9216                         t.constant = true;
9217                      }
9218
9219                      ((Expression)exp.index.index->last).destType = type;
9220                   }
9221                }
9222             }
9223          }
9224
9225          for(e = exp.index.index->first; e; e = e.next)
9226          {
9227             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9228             {
9229                if(e.destType) FreeType(e.destType);
9230                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9231             }
9232             ProcessExpressionType(e);
9233             if(!e.next)
9234             {
9235                // Check if this type is int
9236             }
9237             if(!e.isConstant)
9238                exp.isConstant = false;
9239          }
9240
9241          if(!exp.expType)
9242             exp.expType = Dereference(exp.index.exp.expType);
9243          if(exp.expType)
9244             DeclareType(curExternal, exp.expType, true, false);
9245          break;
9246       }
9247       case callExp:
9248       {
9249          Expression e;
9250          Type functionType;
9251          Type methodType = null;
9252          char name[1024];
9253          name[0] = '\0';
9254
9255          if(inCompiler)
9256          {
9257             PrintExpression(exp.call.exp,  name);
9258             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9259             {
9260                //exp.call.exp.expType = null;
9261                PrintExpression(exp.call.exp,  name);
9262             }
9263          }
9264          if(exp.call.exp.type == identifierExp)
9265          {
9266             Expression idExp = exp.call.exp;
9267             Identifier id = idExp.identifier;
9268             if(!strcmp(id.string, "__builtin_frame_address"))
9269             {
9270                exp.expType = ProcessTypeString("void *", true);
9271                if(exp.call.arguments && exp.call.arguments->first)
9272                   ProcessExpressionType(exp.call.arguments->first);
9273                break;
9274             }
9275             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9276             {
9277                exp.expType = ProcessTypeString("int", true);
9278                if(exp.call.arguments && exp.call.arguments->first)
9279                   ProcessExpressionType(exp.call.arguments->first);
9280                break;
9281             }
9282             else if(!strcmp(id.string, "Max") ||
9283                !strcmp(id.string, "Min") ||
9284                !strcmp(id.string, "Sgn") ||
9285                !strcmp(id.string, "Abs"))
9286             {
9287                Expression a = null;
9288                Expression b = null;
9289                Expression tempExp1 = null, tempExp2 = null;
9290                if((!strcmp(id.string, "Max") ||
9291                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9292                {
9293                   a = exp.call.arguments->first;
9294                   b = exp.call.arguments->last;
9295                   tempExp1 = a;
9296                   tempExp2 = b;
9297                }
9298                else if(exp.call.arguments->count == 1)
9299                {
9300                   a = exp.call.arguments->first;
9301                   tempExp1 = a;
9302                }
9303
9304                if(a)
9305                {
9306                   exp.call.arguments->Clear();
9307                   idExp.identifier = null;
9308
9309                   FreeExpContents(exp);
9310
9311                   ProcessExpressionType(a);
9312                   if(b)
9313                      ProcessExpressionType(b);
9314
9315                   exp.type = bracketsExp;
9316                   exp.list = MkList();
9317
9318                   if(a.expType && (!b || b.expType))
9319                   {
9320                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9321                      {
9322                         // Use the simpleStruct name/ids for now...
9323                         if(inCompiler)
9324                         {
9325                            OldList * specs = MkList();
9326                            OldList * decls = MkList();
9327                            Declaration decl;
9328                            char temp1[1024], temp2[1024];
9329
9330                            GetTypeSpecs(a.expType, specs);
9331
9332                            if(a && !a.isConstant && a.type != identifierExp)
9333                            {
9334                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9335                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9336                               tempExp1 = QMkExpId(temp1);
9337                               tempExp1.expType = a.expType;
9338                               if(a.expType)
9339                                  a.expType.refCount++;
9340                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9341                            }
9342                            if(b && !b.isConstant && b.type != identifierExp)
9343                            {
9344                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9345                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9346                               tempExp2 = QMkExpId(temp2);
9347                               tempExp2.expType = b.expType;
9348                               if(b.expType)
9349                                  b.expType.refCount++;
9350                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9351                            }
9352
9353                            decl = MkDeclaration(specs, decls);
9354                            if(!curCompound.compound.declarations)
9355                               curCompound.compound.declarations = MkList();
9356                            curCompound.compound.declarations->Insert(null, decl);
9357                         }
9358                      }
9359                   }
9360
9361                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9362                   {
9363                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9364                      ListAdd(exp.list,
9365                         MkExpCondition(MkExpBrackets(MkListOne(
9366                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9367                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9368                      exp.expType = a.expType;
9369                      if(a.expType)
9370                         a.expType.refCount++;
9371                   }
9372                   else if(!strcmp(id.string, "Abs"))
9373                   {
9374                      ListAdd(exp.list,
9375                         MkExpCondition(MkExpBrackets(MkListOne(
9376                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9377                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9378                      exp.expType = a.expType;
9379                      if(a.expType)
9380                         a.expType.refCount++;
9381                   }
9382                   else if(!strcmp(id.string, "Sgn"))
9383                   {
9384                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9385                      ListAdd(exp.list,
9386                         MkExpCondition(MkExpBrackets(MkListOne(
9387                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9388                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9389                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9390                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9391                      exp.expType = ProcessTypeString("int", false);
9392                   }
9393
9394                   FreeExpression(tempExp1);
9395                   if(tempExp2) FreeExpression(tempExp2);
9396
9397                   FreeIdentifier(id);
9398                   break;
9399                }
9400             }
9401          }
9402
9403          {
9404             Type dummy
9405             {
9406                count = 1;
9407                refCount = 1;
9408             };
9409             if(!exp.call.exp.destType)
9410             {
9411                exp.call.exp.destType = dummy;
9412                dummy.refCount++;
9413             }
9414             ProcessExpressionType(exp.call.exp);
9415             if(exp.call.exp.destType == dummy)
9416             {
9417                FreeType(dummy);
9418                exp.call.exp.destType = null;
9419             }
9420             FreeType(dummy);
9421          }
9422
9423          // Check argument types against parameter types
9424          functionType = exp.call.exp.expType;
9425
9426          if(functionType && functionType.kind == TypeKind::methodType)
9427          {
9428             methodType = functionType;
9429             functionType = methodType.method.dataType;
9430
9431             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9432             // TOCHECK: Instead of doing this here could this be done per param?
9433             if(exp.call.exp.expType.usedClass)
9434             {
9435                char typeString[1024];
9436                typeString[0] = '\0';
9437                {
9438                   Symbol back = functionType.thisClass;
9439                   // Do not output class specifier here (thisclass was added to this)
9440                   functionType.thisClass = null;
9441                   PrintType(functionType, typeString, true, true);
9442                   functionType.thisClass = back;
9443                }
9444                if(strstr(typeString, "thisclass"))
9445                {
9446                   OldList * specs = MkList();
9447                   Declarator decl;
9448                   {
9449                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9450
9451                      decl = SpecDeclFromString(typeString, specs, null);
9452
9453                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9454                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9455                         exp.call.exp.expType.usedClass))
9456                         thisClassParams = false;
9457
9458                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9459                      {
9460                         Class backupThisClass = thisClass;
9461                         thisClass = exp.call.exp.expType.usedClass;
9462                         ProcessDeclarator(decl, true);
9463                         thisClass = backupThisClass;
9464                      }
9465
9466                      thisClassParams = true;
9467
9468                      functionType = ProcessType(specs, decl);
9469                      functionType.refCount = 0;
9470                      FinishTemplatesContext(context);
9471
9472                      // Mark parameters that were 'thisclass'
9473                      {
9474                         Type p, op;
9475                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9476                         {
9477                            //p.wasThisClass = op.kind == thisClassType;
9478                            if(op.kind == thisClassType)
9479                               p.thisClassFrom = methodType.method._class;
9480                         }
9481                      }
9482                      if(methodType.method.dataType.returnType.kind == thisClassType)
9483                      {
9484                         // functionType.returnType.wasThisClass = true;
9485                         functionType.returnType.thisClassFrom = methodType.method._class;
9486                      }
9487                   }
9488
9489                   FreeList(specs, FreeSpecifier);
9490                   FreeDeclarator(decl);
9491                 }
9492             }
9493          }
9494          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9495          {
9496             Type type = functionType.type;
9497             if(!functionType.refCount)
9498             {
9499                functionType.type = null;
9500                FreeType(functionType);
9501             }
9502             //methodType = functionType;
9503             functionType = type;
9504          }
9505          if(functionType && functionType.kind != TypeKind::functionType)
9506          {
9507             Compiler_Error($"called object %s is not a function\n", name);
9508          }
9509          else if(functionType)
9510          {
9511             bool emptyParams = false, noParams = false;
9512             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9513             Type type = functionType.params.first;
9514             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9515             int extra = 0;
9516             Location oldyylloc = yylloc;
9517
9518             if(!type) emptyParams = true;
9519
9520             // WORKING ON THIS:
9521             if(functionType.extraParam && e && functionType.thisClass)
9522             {
9523                e.destType = MkClassType(functionType.thisClass.string);
9524                e = e.next;
9525             }
9526
9527             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9528             // Fixed #141 by adding '&& !functionType.extraParam'
9529             if(!functionType.staticMethod && !functionType.extraParam)
9530             {
9531                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9532                   memberExp.member.exp.expType._class)
9533                {
9534                   type = MkClassType(memberExp.member.exp.expType._class.string);
9535                   if(e)
9536                   {
9537                      e.destType = type;
9538                      e = e.next;
9539                      type = functionType.params.first;
9540                   }
9541                   else
9542                      type.refCount = 0;
9543                }
9544                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9545                {
9546                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9547                   type.byReference = functionType.byReference;
9548                   type.typedByReference = functionType.typedByReference;
9549                   if(e)
9550                   {
9551                      // Allow manually passing a class for typed object
9552                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9553                         e = e.next;
9554                      e.destType = type;
9555                      e = e.next;
9556                      type = functionType.params.first;
9557                   }
9558                   else
9559                      type.refCount = 0;
9560                   //extra = 1;
9561                }
9562             }
9563
9564             if(type && type.kind == voidType)
9565             {
9566                noParams = true;
9567                if(!type.refCount) FreeType(type);
9568                type = null;
9569             }
9570
9571             for( ; e; e = e.next)
9572             {
9573                if(!type && !emptyParams)
9574                {
9575                   yylloc = e.loc;
9576                   if(methodType && methodType.methodClass)
9577                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9578                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9579                         noParams ? 0 : functionType.params.count);
9580                   else
9581                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9582                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9583                         noParams ? 0 : functionType.params.count);
9584                   break;
9585                }
9586
9587                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9588                {
9589                   Type templatedType = null;
9590                   Class _class = methodType.usedClass;
9591                   ClassTemplateParameter curParam = null;
9592                   int id = 0;
9593                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9594                   {
9595                      Class sClass;
9596                      for(sClass = _class; sClass; sClass = sClass.base)
9597                      {
9598                         if(sClass.templateClass) sClass = sClass.templateClass;
9599                         id = 0;
9600                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9601                         {
9602                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9603                            {
9604                               Class nextClass;
9605                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9606                               {
9607                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9608                                  id += nextClass.templateParams.count;
9609                               }
9610                               break;
9611                            }
9612                            id++;
9613                         }
9614                         if(curParam) break;
9615                      }
9616                   }
9617                   if(curParam && _class.templateArgs[id].dataTypeString)
9618                   {
9619                      bool constant = type.constant;
9620                      ClassTemplateArgument arg = _class.templateArgs[id];
9621                      {
9622                         Context context = SetupTemplatesContext(_class);
9623
9624                         /*if(!arg.dataType)
9625                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9626                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9627                         FinishTemplatesContext(context);
9628                      }
9629
9630                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9631                      else if(templatedType.kind == pointerType)
9632                      {
9633                         Type t = templatedType.type;
9634                         while(t.kind == pointerType) t = t.type;
9635                         if(constant) t.constant = constant;
9636                      }
9637
9638                      e.destType = templatedType;
9639                      if(templatedType)
9640                      {
9641                         templatedType.passAsTemplate = true;
9642                         // templatedType.refCount++;
9643                      }
9644                   }
9645                   else
9646                   {
9647                      e.destType = type;
9648                      if(type) type.refCount++;
9649                   }
9650                }
9651                else
9652                {
9653                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9654                   {
9655                      e.destType = type.prev;
9656                      e.destType.refCount++;
9657                   }
9658                   else
9659                   {
9660                      e.destType = type;
9661                      if(type) type.refCount++;
9662                   }
9663                }
9664                // Don't reach the end for the ellipsis
9665                if(type && type.kind != ellipsisType)
9666                {
9667                   Type next = type.next;
9668                   if(!type.refCount) FreeType(type);
9669                   type = next;
9670                }
9671             }
9672
9673             if(type && type.kind != ellipsisType)
9674             {
9675                if(methodType && methodType.methodClass)
9676                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9677                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9678                      functionType.params.count + extra);
9679                else
9680                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9681                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9682                      functionType.params.count + extra);
9683             }
9684             yylloc = oldyylloc;
9685             if(type && !type.refCount) FreeType(type);
9686          }
9687          else
9688          {
9689             functionType = Type
9690             {
9691                refCount = 0;
9692                kind = TypeKind::functionType;
9693             };
9694
9695             if(exp.call.exp.type == identifierExp)
9696             {
9697                char * string = exp.call.exp.identifier.string;
9698                if(inCompiler)
9699                {
9700                   Symbol symbol;
9701                   Location oldyylloc = yylloc;
9702
9703                   yylloc = exp.call.exp.identifier.loc;
9704                   if(strstr(string, "__builtin_") == string)
9705                   {
9706                      if(exp.destType)
9707                      {
9708                         functionType.returnType = exp.destType;
9709                         exp.destType.refCount++;
9710                      }
9711                   }
9712                   else
9713                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9714                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9715                   globalContext.symbols.Add((BTNode)symbol);
9716                   if(strstr(symbol.string, "::"))
9717                      globalContext.hasNameSpace = true;
9718
9719                   yylloc = oldyylloc;
9720                }
9721             }
9722             else if(exp.call.exp.type == memberExp)
9723             {
9724                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9725                   exp.call.exp.member.member.string);*/
9726             }
9727             else
9728                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9729
9730             if(!functionType.returnType)
9731             {
9732                functionType.returnType = Type
9733                {
9734                   refCount = 1;
9735                   kind = intType;
9736                };
9737             }
9738          }
9739          if(functionType && functionType.kind == TypeKind::functionType)
9740          {
9741             exp.expType = functionType.returnType;
9742
9743             if(functionType.returnType)
9744                functionType.returnType.refCount++;
9745
9746             if(!functionType.refCount)
9747                FreeType(functionType);
9748          }
9749
9750          if(exp.call.arguments)
9751          {
9752             for(e = exp.call.arguments->first; e; e = e.next)
9753                ProcessExpressionType(e);
9754          }
9755          break;
9756       }
9757       case memberExp:
9758       {
9759          Type type;
9760          Location oldyylloc = yylloc;
9761          bool thisPtr;
9762          Expression checkExp = exp.member.exp;
9763          while(checkExp)
9764          {
9765             if(checkExp.type == castExp)
9766                checkExp = checkExp.cast.exp;
9767             else if(checkExp.type == bracketsExp)
9768                checkExp = checkExp.list ? checkExp.list->first : null;
9769             else
9770                break;
9771          }
9772
9773          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9774          exp.thisPtr = thisPtr;
9775
9776          // DOING THIS LATER NOW...
9777          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9778          {
9779             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9780             /* TODO: Name Space Fix ups
9781             if(!exp.member.member.classSym)
9782                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9783             */
9784          }
9785
9786          ProcessExpressionType(exp.member.exp);
9787          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9788             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9789          {
9790             exp.isConstant = false;
9791          }
9792          else
9793             exp.isConstant = exp.member.exp.isConstant;
9794          type = exp.member.exp.expType;
9795
9796          yylloc = exp.loc;
9797
9798          if(type && (type.kind == templateType))
9799          {
9800             Class _class = thisClass ? thisClass : currentClass;
9801             ClassTemplateParameter param = null;
9802             if(_class)
9803             {
9804                for(param = _class.templateParams.first; param; param = param.next)
9805                {
9806                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9807                      break;
9808                }
9809             }
9810             if(param && param.defaultArg.member)
9811             {
9812                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9813                if(argExp)
9814                {
9815                   Expression expMember = exp.member.exp;
9816                   Declarator decl;
9817                   OldList * specs = MkList();
9818                   char thisClassTypeString[1024];
9819
9820                   FreeIdentifier(exp.member.member);
9821
9822                   ProcessExpressionType(argExp);
9823
9824                   {
9825                      char * colon = strstr(param.defaultArg.memberString, "::");
9826                      if(colon)
9827                      {
9828                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9829                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9830                      }
9831                      else
9832                         strcpy(thisClassTypeString, _class.fullName);
9833                   }
9834
9835                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9836
9837                   exp.expType = ProcessType(specs, decl);
9838                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9839                   {
9840                      Class expClass = exp.expType._class.registered;
9841                      Class cClass = null;
9842                      int paramCount = 0;
9843                      int lastParam = -1;
9844
9845                      char templateString[1024];
9846                      ClassTemplateParameter param;
9847                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9848                      for(cClass = expClass; cClass; cClass = cClass.base)
9849                      {
9850                         int p = 0;
9851                         for(param = cClass.templateParams.first; param; param = param.next)
9852                         {
9853                            int id = p;
9854                            Class sClass;
9855                            ClassTemplateArgument arg;
9856                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9857                            arg = expClass.templateArgs[id];
9858
9859                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9860                            {
9861                               ClassTemplateParameter cParam;
9862                               //int p = numParams - sClass.templateParams.count;
9863                               int p = 0;
9864                               Class nextClass;
9865                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9866
9867                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9868                               {
9869                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9870                                  {
9871                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9872                                     {
9873                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9874                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9875                                        break;
9876                                     }
9877                                  }
9878                               }
9879                            }
9880
9881                            {
9882                               char argument[256];
9883                               argument[0] = '\0';
9884                               /*if(arg.name)
9885                               {
9886                                  strcat(argument, arg.name.string);
9887                                  strcat(argument, " = ");
9888                               }*/
9889                               switch(param.type)
9890                               {
9891                                  case expression:
9892                                  {
9893                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9894                                     char expString[1024];
9895                                     OldList * specs = MkList();
9896                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9897                                     Expression exp;
9898                                     char * string = PrintHexUInt64(arg.expression.ui64);
9899                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9900                                     delete string;
9901
9902                                     ProcessExpressionType(exp);
9903                                     ComputeExpression(exp);
9904                                     expString[0] = '\0';
9905                                     PrintExpression(exp, expString);
9906                                     strcat(argument, expString);
9907                                     // delete exp;
9908                                     FreeExpression(exp);
9909                                     break;
9910                                  }
9911                                  case identifier:
9912                                  {
9913                                     strcat(argument, arg.member.name);
9914                                     break;
9915                                  }
9916                                  case TemplateParameterType::type:
9917                                  {
9918                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9919                                     {
9920                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9921                                           strcat(argument, thisClassTypeString);
9922                                        else
9923                                           strcat(argument, arg.dataTypeString);
9924                                     }
9925                                     break;
9926                                  }
9927                               }
9928                               if(argument[0])
9929                               {
9930                                  if(paramCount) strcat(templateString, ", ");
9931                                  if(lastParam != p - 1)
9932                                  {
9933                                     strcat(templateString, param.name);
9934                                     strcat(templateString, " = ");
9935                                  }
9936                                  strcat(templateString, argument);
9937                                  paramCount++;
9938                                  lastParam = p;
9939                               }
9940                               p++;
9941                            }
9942                         }
9943                      }
9944                      {
9945                         int len = strlen(templateString);
9946                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9947                         templateString[len++] = '>';
9948                         templateString[len++] = '\0';
9949                      }
9950                      {
9951                         Context context = SetupTemplatesContext(_class);
9952                         FreeType(exp.expType);
9953                         exp.expType = ProcessTypeString(templateString, false);
9954                         FinishTemplatesContext(context);
9955                      }
9956                   }
9957
9958                   if(!expMember.expType.isPointerType)
9959                      expMember = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), expMember);
9960                   // *([expType] *)(((byte *)(uintptr)[exp.member.exp]) + [argExp].member.offset)
9961                   exp.type = bracketsExp;
9962                   exp.list = MkListOne(MkExpOp(null, '*',
9963                   /*opExp;
9964                   exp.op.op = '*';
9965                   exp.op.exp1 = null;
9966                   exp.op.exp2 = */
9967                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9968                      MkExpBrackets(MkListOne(
9969                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
9970                            expMember))),
9971                               '+',
9972                               MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9973                               '+',
9974                               MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9975
9976                            ));
9977                }
9978             }
9979             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9980                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9981             {
9982                type = ProcessTemplateParameterType(type.templateParameter);
9983             }
9984          }
9985          // TODO: *** This seems to be where we should add method support for all basic types ***
9986          if(type && (type.kind == templateType));
9987          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9988                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9989                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9990                           (type.kind == pointerType && type.type.kind == charType)))
9991          {
9992             Identifier id = exp.member.member;
9993             TypeKind typeKind = type.kind;
9994             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9995             if(typeKind == subClassType && exp.member.exp.type == classExp)
9996             {
9997                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9998                typeKind = classType;
9999             }
10000
10001             if(id)
10002             {
10003                if(typeKind == intType || typeKind == enumType)
10004                   _class = eSystem_FindClass(privateModule, "int");
10005                else if(!_class)
10006                {
10007                   if(type.kind == classType && type._class && type._class.registered)
10008                   {
10009                      _class = type._class.registered;
10010                   }
10011                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10012                   {
10013                      _class = FindClass("char *").registered;
10014                   }
10015                   else if(type.kind == pointerType)
10016                   {
10017                      _class = eSystem_FindClass(privateModule, "uintptr");
10018                      FreeType(exp.expType);
10019                      exp.expType = ProcessTypeString("uintptr", false);
10020                      exp.byReference = true;
10021                   }
10022                   else
10023                   {
10024                      char string[1024] = "";
10025                      Symbol classSym;
10026                      PrintTypeNoConst(type, string, false, true);
10027                      classSym = FindClass(string);
10028                      if(classSym) _class = classSym.registered;
10029                   }
10030                }
10031             }
10032
10033             if(_class && id)
10034             {
10035                /*bool thisPtr =
10036                   (exp.member.exp.type == identifierExp &&
10037                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10038                Property prop = null;
10039                Method method = null;
10040                DataMember member = null;
10041                Property revConvert = null;
10042                ClassProperty classProp = null;
10043
10044                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10045                   exp.member.memberType = propertyMember;
10046
10047                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10048                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10049
10050                if(typeKind != subClassType)
10051                {
10052                   // Prioritize data members over properties for "this"
10053                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10054                   {
10055                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10056                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10057                      {
10058                         prop = eClass_FindProperty(_class, id.string, privateModule);
10059                         if(prop)
10060                            member = null;
10061                      }
10062                      if(!member && !prop)
10063                         prop = eClass_FindProperty(_class, id.string, privateModule);
10064                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10065                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10066                         exp.member.thisPtr = true;
10067                   }
10068                   // Prioritize properties over data members otherwise
10069                   else
10070                   {
10071                      bool useMemberForNonConst = false;
10072                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10073                      if(!id.classSym)
10074                      {
10075                         prop = eClass_FindProperty(_class, id.string, null);
10076
10077                         useMemberForNonConst = prop && exp.destType &&
10078                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10079                               !strncmp(prop.dataTypeString, "const ", 6);
10080
10081                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10082                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10083                      }
10084
10085                      if((!prop || useMemberForNonConst) && !member)
10086                      {
10087                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10088                         if(!method)
10089                         {
10090                            prop = eClass_FindProperty(_class, id.string, privateModule);
10091
10092                            useMemberForNonConst |= prop && exp.destType &&
10093                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10094                                  !strncmp(prop.dataTypeString, "const ", 6);
10095
10096                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10097                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10098                         }
10099                      }
10100
10101                      if(member && prop)
10102                      {
10103                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10104                            prop = null;
10105                         else
10106                            member = null;
10107                      }
10108                   }
10109                }
10110                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10111                   method = eClass_FindMethod(_class, id.string, privateModule);
10112                if(!prop && !member && !method)
10113                {
10114                   if(typeKind == subClassType)
10115                   {
10116                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10117                      if(classProp)
10118                      {
10119                         exp.member.memberType = classPropertyMember;
10120                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10121                      }
10122                      else
10123                      {
10124                         // Assume this is a class_data member
10125                         char structName[1024];
10126                         Identifier id = exp.member.member;
10127                         Expression classExp = exp.member.exp;
10128                         type.refCount++;
10129
10130                         FreeType(classExp.expType);
10131                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10132
10133                         strcpy(structName, "__ecereClassData_");
10134                         FullClassNameCat(structName, type._class.string, false);
10135                         exp.type = pointerExp;
10136                         exp.member.member = id;
10137
10138                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10139                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10140                               MkExpBrackets(MkListOne(MkExpOp(
10141                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10142                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10143                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10144                                  )));
10145
10146                         FreeType(type);
10147
10148                         ProcessExpressionType(exp);
10149                         return;
10150                      }
10151                   }
10152                   else
10153                   {
10154                      // Check for reverse conversion
10155                      // (Convert in an instantiation later, so that we can use
10156                      //  deep properties system)
10157                      Symbol classSym = FindClass(id.string);
10158                      if(classSym)
10159                      {
10160                         Class convertClass = classSym.registered;
10161                         if(convertClass)
10162                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10163                      }
10164                   }
10165                }
10166
10167                //if(!exp.member.exp.destType)
10168                if(exp.member.exp.destType)
10169                   FreeType(exp.member.exp.destType);
10170                {
10171                   if(method && !method._class.symbol)
10172                      method._class.symbol = FindClass(method._class.fullName);
10173                   if(prop && !prop._class.symbol)
10174                      prop._class.symbol = FindClass(prop._class.fullName);
10175
10176                   exp.member.exp.destType = Type
10177                   {
10178                      refCount = 1;
10179                      kind = classType;
10180                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10181                      // wasThisClass = type ? type.wasThisClass : false;
10182                      thisClassFrom = type ? type.thisClassFrom : null;
10183                   };
10184                }
10185
10186                if(prop)
10187                {
10188                   exp.member.memberType = propertyMember;
10189                   if(!prop.dataType)
10190                      ProcessPropertyType(prop);
10191                   exp.expType = prop.dataType;
10192                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10193                   {
10194                      Type type { };
10195                      CopyTypeInto(type, exp.expType);
10196                      type.refCount = 1;
10197                      type.constant = true;
10198                      exp.expType = type;
10199                   }
10200                   else if(prop.dataType)
10201                      prop.dataType.refCount++;
10202                }
10203                else if(member)
10204                {
10205                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10206                   {
10207                      FreeExpContents(exp);
10208                      exp.type = identifierExp;
10209                      exp.identifier = MkIdentifier("class");
10210                      ProcessExpressionType(exp);
10211                      return;
10212                   }
10213
10214                   exp.member.memberType = dataMember;
10215                   DeclareStruct(curExternal, _class.fullName, false, true);
10216                   if(member._class != _class)
10217                      DeclareStruct(curExternal, member._class.fullName, false, true);
10218
10219                   if(!member.dataType)
10220                   {
10221                      Context context = SetupTemplatesContext(_class);
10222                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10223                      FinishTemplatesContext(context);
10224                   }
10225                   exp.expType = member.dataType;
10226                   if(member.dataType) member.dataType.refCount++;
10227                }
10228                else if(revConvert)
10229                {
10230                   exp.member.memberType = reverseConversionMember;
10231                   exp.expType = MkClassType(revConvert._class.fullName);
10232                }
10233                else if(method)
10234                {
10235                   //if(inCompiler)
10236                   {
10237                      /*if(id._class)
10238                      {
10239                         exp.type = identifierExp;
10240                         exp.identifier = exp.member.member;
10241                      }
10242                      else*/
10243                         exp.member.memberType = methodMember;
10244                   }
10245                   if(!method.dataType)
10246                      ProcessMethodType(method);
10247                   exp.expType = Type
10248                   {
10249                      refCount = 1;
10250                      kind = methodType;
10251                      method = method;
10252                   };
10253
10254                   // Tricky spot here... To use instance versus class virtual table
10255                   // Put it back to what it was... What did we break?
10256
10257                   // Had to put it back for overriding Main of Thread global instance
10258
10259                   //exp.expType.methodClass = _class;
10260                   exp.expType.methodClass = (id && id._class) ? _class : null;
10261
10262                   // Need the actual class used for templated classes
10263                   exp.expType.usedClass = _class;
10264                }
10265                else if(!classProp)
10266                {
10267                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10268                   {
10269                      FreeExpContents(exp);
10270                      exp.type = identifierExp;
10271                      exp.identifier = MkIdentifier("class");
10272                      FreeType(exp.expType);
10273                      exp.expType = MkClassType("ecere::com::Class");
10274                      return;
10275                   }
10276                   yylloc = exp.member.member.loc;
10277                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10278                   if(inCompiler)
10279                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10280                }
10281
10282                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10283                {
10284                   Class tClass;
10285
10286                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10287                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10288
10289                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10290                   {
10291                      int id = 0;
10292                      ClassTemplateParameter curParam = null;
10293                      Class sClass;
10294
10295                      for(sClass = tClass; sClass; sClass = sClass.base)
10296                      {
10297                         id = 0;
10298                         if(sClass.templateClass) sClass = sClass.templateClass;
10299                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10300                         {
10301                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10302                            {
10303                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10304                                  id += sClass.templateParams.count;
10305                               break;
10306                            }
10307                            id++;
10308                         }
10309                         if(curParam) break;
10310                      }
10311
10312                      if(curParam && tClass.templateArgs[id].dataTypeString)
10313                      {
10314                         ClassTemplateArgument arg = tClass.templateArgs[id];
10315                         Context context = SetupTemplatesContext(tClass);
10316                         bool constant = exp.expType.constant;
10317                         /*if(!arg.dataType)
10318                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10319                         FreeType(exp.expType);
10320
10321                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10322                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10323                         else if(exp.expType.kind == pointerType)
10324                         {
10325                            Type t = exp.expType.type;
10326                            while(t.kind == pointerType) t = t.type;
10327                            if(constant) t.constant = constant;
10328                         }
10329                         if(exp.expType)
10330                         {
10331                            if(exp.expType.kind == thisClassType)
10332                            {
10333                               FreeType(exp.expType);
10334                               exp.expType = ReplaceThisClassType(_class);
10335                            }
10336
10337                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10338                               exp.expType.passAsTemplate = true;
10339                            //exp.expType.refCount++;
10340                            if(!exp.destType)
10341                            {
10342                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10343                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10344                               else if(exp.destType.kind == pointerType)
10345                               {
10346                                  Type t = exp.destType.type;
10347                                  while(t.kind == pointerType) t = t.type;
10348                                  if(constant) t.constant = constant;
10349                               }
10350
10351                               //exp.destType.refCount++;
10352
10353                               if(exp.destType.kind == thisClassType)
10354                               {
10355                                  FreeType(exp.destType);
10356                                  exp.destType = ReplaceThisClassType(_class);
10357                               }
10358                            }
10359                         }
10360                         FinishTemplatesContext(context);
10361                      }
10362                   }
10363                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10364                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10365                   {
10366                      int id = 0;
10367                      ClassTemplateParameter curParam = null;
10368                      Class sClass;
10369
10370                      for(sClass = tClass; sClass; sClass = sClass.base)
10371                      {
10372                         id = 0;
10373                         if(sClass.templateClass) sClass = sClass.templateClass;
10374                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10375                         {
10376                            if(curParam.type == TemplateParameterType::type &&
10377                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10378                            {
10379                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10380                                  id += sClass.templateParams.count;
10381                               break;
10382                            }
10383                            id++;
10384                         }
10385                         if(curParam) break;
10386                      }
10387
10388                      if(curParam)
10389                      {
10390                         ClassTemplateArgument arg = tClass.templateArgs[id];
10391                         Context context = SetupTemplatesContext(tClass);
10392                         Type basicType;
10393                         /*if(!arg.dataType)
10394                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10395
10396                         basicType = ProcessTypeString(arg.dataTypeString, false);
10397                         if(basicType)
10398                         {
10399                            if(basicType.kind == thisClassType)
10400                            {
10401                               FreeType(basicType);
10402                               basicType = ReplaceThisClassType(_class);
10403                            }
10404
10405                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10406                            if(tClass.templateClass)
10407                               basicType.passAsTemplate = true;
10408                            */
10409
10410                            FreeType(exp.expType);
10411
10412                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10413                            //exp.expType.refCount++;
10414                            if(!exp.destType)
10415                            {
10416                               exp.destType = exp.expType;
10417                               exp.destType.refCount++;
10418                            }
10419
10420                            {
10421                               Expression newExp { };
10422                               OldList * specs = MkList();
10423                               Declarator decl;
10424                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10425                               *newExp = *exp;
10426                               if(exp.destType) exp.destType.refCount++;
10427                               if(exp.expType)  exp.expType.refCount++;
10428                               exp.type = castExp;
10429                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10430                               exp.cast.exp = newExp;
10431                               //FreeType(exp.expType);
10432                               //exp.expType = null;
10433                               //ProcessExpressionType(sourceExp);
10434                            }
10435                         }
10436                         FinishTemplatesContext(context);
10437                      }
10438                   }
10439                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10440                   {
10441                      Class expClass = exp.expType._class.registered;
10442                      if(expClass)
10443                      {
10444                         Class cClass = null;
10445                         int p = 0;
10446                         int paramCount = 0;
10447                         int lastParam = -1;
10448                         char templateString[1024];
10449                         ClassTemplateParameter param;
10450                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10451                         while(cClass != expClass)
10452                         {
10453                            Class sClass;
10454                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10455                            cClass = sClass;
10456
10457                            for(param = cClass.templateParams.first; param; param = param.next)
10458                            {
10459                               Class cClassCur = null;
10460                               int cp = 0;
10461                               ClassTemplateParameter paramCur = null;
10462                               ClassTemplateArgument arg;
10463                               while(cClassCur != tClass && !paramCur)
10464                               {
10465                                  Class sClassCur;
10466                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10467                                  cClassCur = sClassCur;
10468
10469                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10470                                  {
10471                                     if(!strcmp(paramCur.name, param.name))
10472                                     {
10473
10474                                        break;
10475                                     }
10476                                     cp++;
10477                                  }
10478                               }
10479                               if(paramCur && paramCur.type == TemplateParameterType::type)
10480                                  arg = tClass.templateArgs[cp];
10481                               else
10482                                  arg = expClass.templateArgs[p];
10483
10484                               {
10485                                  char argument[256];
10486                                  argument[0] = '\0';
10487                                  /*if(arg.name)
10488                                  {
10489                                     strcat(argument, arg.name.string);
10490                                     strcat(argument, " = ");
10491                                  }*/
10492                                  switch(param.type)
10493                                  {
10494                                     case expression:
10495                                     {
10496                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10497                                        char expString[1024];
10498                                        OldList * specs = MkList();
10499                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10500                                        Expression exp;
10501                                        char * string = PrintHexUInt64(arg.expression.ui64);
10502                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10503                                        delete string;
10504
10505                                        ProcessExpressionType(exp);
10506                                        ComputeExpression(exp);
10507                                        expString[0] = '\0';
10508                                        PrintExpression(exp, expString);
10509                                        strcat(argument, expString);
10510                                        // delete exp;
10511                                        FreeExpression(exp);
10512                                        break;
10513                                     }
10514                                     case identifier:
10515                                     {
10516                                        strcat(argument, arg.member.name);
10517                                        break;
10518                                     }
10519                                     case TemplateParameterType::type:
10520                                     {
10521                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10522                                           strcat(argument, arg.dataTypeString);
10523                                        break;
10524                                     }
10525                                  }
10526                                  if(argument[0])
10527                                  {
10528                                     if(paramCount) strcat(templateString, ", ");
10529                                     if(lastParam != p - 1)
10530                                     {
10531                                        strcat(templateString, param.name);
10532                                        strcat(templateString, " = ");
10533                                     }
10534                                     strcat(templateString, argument);
10535                                     paramCount++;
10536                                     lastParam = p;
10537                                  }
10538                               }
10539                               p++;
10540                            }
10541                         }
10542                         {
10543                            int len = strlen(templateString);
10544                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10545                            templateString[len++] = '>';
10546                            templateString[len++] = '\0';
10547                         }
10548
10549                         FreeType(exp.expType);
10550                         {
10551                            Context context = SetupTemplatesContext(tClass);
10552                            exp.expType = ProcessTypeString(templateString, false);
10553                            FinishTemplatesContext(context);
10554                         }
10555                      }
10556                   }
10557                }
10558             }
10559             else
10560                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10561          }
10562          else if(type && (type.kind == structType || type.kind == unionType))
10563          {
10564             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10565             if(memberType)
10566             {
10567                exp.expType = memberType;
10568                if(memberType)
10569                   memberType.refCount++;
10570             }
10571          }
10572          else
10573          {
10574             char expString[10240];
10575             expString[0] = '\0';
10576             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10577             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10578          }
10579
10580          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10581          {
10582             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10583             {
10584                Identifier id = exp.member.member;
10585                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10586                if(_class)
10587                {
10588                   FreeType(exp.expType);
10589                   exp.expType = ReplaceThisClassType(_class);
10590                }
10591             }
10592          }
10593          yylloc = oldyylloc;
10594          break;
10595       }
10596       // Convert x->y into (*x).y
10597       case pointerExp:
10598       {
10599          Type destType = exp.destType;
10600
10601          // DOING THIS LATER NOW...
10602          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10603          {
10604             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10605             /* TODO: Name Space Fix ups
10606             if(!exp.member.member.classSym)
10607                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10608             */
10609          }
10610
10611          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10612          exp.type = memberExp;
10613          if(destType)
10614             destType.count++;
10615          ProcessExpressionType(exp);
10616          if(destType)
10617             destType.count--;
10618          break;
10619       }
10620       case classSizeExp:
10621       {
10622          //ComputeExpression(exp);
10623
10624          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10625          if(classSym && classSym.registered)
10626          {
10627             if(classSym.registered.type == noHeadClass || (classSym.registered.fixed && classSym.registered.structSize))
10628             {
10629                char name[1024];
10630                Class b = classSym.registered;
10631                name[0] = '\0';
10632                DeclareStruct(curExternal, classSym.string, false, true);
10633                FreeSpecifier(exp._class);
10634                FullClassNameCat(name, classSym.string, false);
10635
10636                if(b.offset == 0)
10637                {
10638                   exp.type = typeSizeExp;
10639                   exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10640                }
10641                else
10642                {
10643                   Expression e;
10644                   exp.type = opExp;
10645                   if(b.structSize == b.offset)
10646                      exp.op.exp1 = MkExpConstant("0");
10647                   else
10648                      exp.op.exp1 = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10649                   exp.op.op = '+';
10650                   e = exp;
10651                   while(b.offset != 0)
10652                   {
10653                      Symbol sym;
10654                      Expression typeSize;
10655
10656                      b = b.base;
10657                      sym = FindClass(b.fullName);
10658
10659                      name[0] = '\0';
10660                      DeclareStruct(curExternal, sym.string, false, true);
10661                      FullClassNameCat(name, sym.string, false);
10662
10663                      if(b.structSize == b.offset)
10664                         typeSize = MkExpConstant("0");
10665                      else
10666                         typeSize = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10667                      e.op.exp2 = b.offset ? MkExpOp(typeSize, '+', null) : typeSize;
10668                      e = e.op.exp2;
10669                   }
10670                }
10671             }
10672             else
10673             {
10674                if(classSym.registered.fixed && !classSym.registered.structSize)
10675                {
10676                   FreeSpecifier(exp._class);
10677                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10678                   exp.type = constantExp;
10679                }
10680                else
10681                {
10682                   char className[1024];
10683                   strcpy(className, "__ecereClass_");
10684                   FullClassNameCat(className, classSym.string, true);
10685
10686                   DeclareClass(curExternal, classSym, className);
10687
10688                   FreeExpContents(exp);
10689                   exp.type = pointerExp;
10690                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10691                   exp.member.member = MkIdentifier("structSize");
10692                }
10693             }
10694          }
10695
10696          exp.expType = Type
10697          {
10698             refCount = 1;
10699             kind = intSizeType;
10700          };
10701          // exp.isConstant = true;
10702          break;
10703       }
10704       case typeSizeExp:
10705       {
10706          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10707
10708          exp.expType = Type
10709          {
10710             refCount = 1;
10711             kind = intSizeType;
10712          };
10713          exp.isConstant = true;
10714
10715          DeclareType(curExternal, type, true, false);
10716          FreeType(type);
10717          break;
10718       }
10719       case castExp:
10720       {
10721          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10722          type.count = 1;
10723          FreeType(exp.cast.exp.destType);
10724          exp.cast.exp.destType = type;
10725          type.refCount++;
10726          type.casted = true;
10727          ProcessExpressionType(exp.cast.exp);
10728          type.casted = false;
10729          type.count = 0;
10730          exp.expType = type;
10731          //type.refCount++;
10732
10733          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10734          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10735          {
10736             void * prev = exp.prev, * next = exp.next;
10737             Type expType = exp.cast.exp.destType;
10738             Expression castExp = exp.cast.exp;
10739             Type destType = exp.destType;
10740
10741             if(expType) expType.refCount++;
10742
10743             //FreeType(exp.destType);
10744             FreeType(exp.expType);
10745             FreeTypeName(exp.cast.typeName);
10746
10747             *exp = *castExp;
10748             FreeType(exp.expType);
10749             FreeType(exp.destType);
10750
10751             exp.expType = expType;
10752             exp.destType = destType;
10753
10754             delete castExp;
10755
10756             exp.prev = prev;
10757             exp.next = next;
10758
10759          }
10760          else
10761          {
10762             exp.isConstant = exp.cast.exp.isConstant;
10763          }
10764          //FreeType(type);
10765          break;
10766       }
10767       case extensionInitializerExp:
10768       {
10769          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10770          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10771          // ProcessInitializer(exp.initializer.initializer, type);
10772          exp.expType = type;
10773          break;
10774       }
10775       case vaArgExp:
10776       {
10777          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10778          ProcessExpressionType(exp.vaArg.exp);
10779          exp.expType = type;
10780          break;
10781       }
10782       case conditionExp:
10783       {
10784          Expression e;
10785          Type t = exp.destType;
10786          if(t && !exp.destType.casted)
10787          {
10788             t = { };
10789             CopyTypeInto(t, exp.destType);
10790             t.count = 0;
10791          }
10792          else if(t)
10793             t.refCount++;
10794
10795          exp.isConstant = true;
10796
10797          FreeType(exp.cond.cond.destType);
10798          exp.cond.cond.destType = MkClassType("bool");
10799          exp.cond.cond.destType.truth = true;
10800          ProcessExpressionType(exp.cond.cond);
10801          if(!exp.cond.cond.isConstant)
10802             exp.isConstant = false;
10803          for(e = exp.cond.exp->first; e; e = e.next)
10804          {
10805             if(!e.next)
10806             {
10807                FreeType(e.destType);
10808                e.destType = t;
10809                if(e.destType) e.destType.refCount++;
10810             }
10811             ProcessExpressionType(e);
10812             if(!e.next)
10813             {
10814                exp.expType = e.expType;
10815                if(e.expType) e.expType.refCount++;
10816             }
10817             if(!e.isConstant)
10818                exp.isConstant = false;
10819          }
10820
10821          FreeType(exp.cond.elseExp.destType);
10822          // Added this check if we failed to find an expType
10823          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10824
10825          // Reversed it...
10826          exp.cond.elseExp.destType = t ? t : exp.expType;
10827
10828          if(exp.cond.elseExp.destType)
10829             exp.cond.elseExp.destType.refCount++;
10830          ProcessExpressionType(exp.cond.elseExp);
10831
10832          // FIXED THIS: Was done before calling process on elseExp
10833          if(!exp.cond.elseExp.isConstant)
10834             exp.isConstant = false;
10835
10836          FreeType(t);
10837          break;
10838       }
10839       case extensionCompoundExp:
10840       {
10841          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10842          {
10843             Statement last = exp.compound.compound.statements->last;
10844             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10845             {
10846                ((Expression)last.expressions->last).destType = exp.destType;
10847                if(exp.destType)
10848                   exp.destType.refCount++;
10849             }
10850             ProcessStatement(exp.compound);
10851             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10852             if(exp.expType)
10853                exp.expType.refCount++;
10854          }
10855          break;
10856       }
10857       case classExp:
10858       {
10859          Specifier spec = exp._classExp.specifiers->first;
10860          if(spec && spec.type == nameSpecifier)
10861          {
10862             exp.expType = MkClassType(spec.name);
10863             exp.expType.kind = subClassType;
10864             exp.byReference = true;
10865          }
10866          else
10867          {
10868             exp.expType = MkClassType("ecere::com::Class");
10869             exp.byReference = true;
10870          }
10871          break;
10872       }
10873       case classDataExp:
10874       {
10875          Class _class = thisClass ? thisClass : currentClass;
10876          if(_class)
10877          {
10878             Identifier id = exp.classData.id;
10879             char structName[1024];
10880             Expression classExp;
10881             strcpy(structName, "__ecereClassData_");
10882             FullClassNameCat(structName, _class.fullName, false);
10883             exp.type = pointerExp;
10884             exp.member.member = id;
10885             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10886                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10887             else
10888                classExp = MkExpIdentifier(MkIdentifier("class"));
10889
10890             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10891                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10892                   MkExpBrackets(MkListOne(MkExpOp(
10893                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10894                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10895                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10896                      )));
10897
10898             ProcessExpressionType(exp);
10899             return;
10900          }
10901          break;
10902       }
10903       case arrayExp:
10904       {
10905          Type type = null;
10906          const char * typeString = null;
10907          char typeStringBuf[1024];
10908          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10909             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10910          {
10911             Class templateClass = exp.destType._class.registered;
10912             typeString = templateClass.templateArgs[2].dataTypeString;
10913          }
10914          else if(exp.list)
10915          {
10916             // Guess type from expressions in the array
10917             Expression e;
10918             for(e = exp.list->first; e; e = e.next)
10919             {
10920                ProcessExpressionType(e);
10921                if(e.expType)
10922                {
10923                   if(!type) { type = e.expType; type.refCount++; }
10924                   else
10925                   {
10926                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10927                      if(!MatchTypeExpression(e, type, null, false, true))
10928                      {
10929                         FreeType(type);
10930                         type = e.expType;
10931                         e.expType = null;
10932
10933                         e = exp.list->first;
10934                         ProcessExpressionType(e);
10935                         if(e.expType)
10936                         {
10937                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10938                            if(!MatchTypeExpression(e, type, null, false, true))
10939                            {
10940                               FreeType(e.expType);
10941                               e.expType = null;
10942                               FreeType(type);
10943                               type = null;
10944                               break;
10945                            }
10946                         }
10947                      }
10948                   }
10949                   if(e.expType)
10950                   {
10951                      FreeType(e.expType);
10952                      e.expType = null;
10953                   }
10954                }
10955             }
10956             if(type)
10957             {
10958                typeStringBuf[0] = '\0';
10959                PrintTypeNoConst(type, typeStringBuf, false, true);
10960                typeString = typeStringBuf;
10961                FreeType(type);
10962                type = null;
10963             }
10964          }
10965          if(typeString)
10966          {
10967             /*
10968             (Container)& (struct BuiltInContainer)
10969             {
10970                ._vTbl = class(BuiltInContainer)._vTbl,
10971                ._class = class(BuiltInContainer),
10972                .refCount = 0,
10973                .data = (int[]){ 1, 7, 3, 4, 5 },
10974                .count = 5,
10975                .type = class(int),
10976             }
10977             */
10978             char templateString[1024];
10979             OldList * initializers = MkList();
10980             OldList * structInitializers = MkList();
10981             OldList * specs = MkList();
10982             Expression expExt;
10983             Declarator decl = SpecDeclFromString(typeString, specs, null);
10984             sprintf(templateString, "Container<%s>", typeString);
10985
10986             if(exp.list)
10987             {
10988                Expression e;
10989                type = ProcessTypeString(typeString, false);
10990                while((e = exp.list->first))
10991                {
10992                   exp.list->Remove(e);
10993                   e.destType = type;
10994                   type.refCount++;
10995                   ProcessExpressionType(e);
10996                   ListAdd(initializers, MkInitializerAssignment(e));
10997                }
10998                FreeType(type);
10999                delete exp.list;
11000             }
11001
11002             DeclareStruct(curExternal, "ecere::com::BuiltInContainer", false, true);
11003
11004             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11005                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11006             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11007                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11008             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11009                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11010             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11011                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11012                MkInitializerList(initializers))));
11013                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11014             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11015                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11016             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11017                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11018             exp.expType = ProcessTypeString(templateString, false);
11019             exp.type = bracketsExp;
11020             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11021                MkExpOp(null, '&',
11022                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11023                   MkInitializerList(structInitializers)))));
11024             ProcessExpressionType(expExt);
11025          }
11026          else
11027          {
11028             exp.expType = ProcessTypeString("Container", false);
11029             Compiler_Error($"Couldn't determine type of array elements\n");
11030          }
11031          break;
11032       }
11033    }
11034
11035    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11036    {
11037       FreeType(exp.expType);
11038       exp.expType = ReplaceThisClassType(thisClass);
11039    }
11040
11041    // Resolve structures here
11042    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11043    {
11044       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11045       // TODO: Fix members reference...
11046       if(symbol)
11047       {
11048          if(exp.expType.kind != enumType)
11049          {
11050             Type member;
11051             String enumName = CopyString(exp.expType.enumName);
11052
11053             // Fixed a memory leak on self-referencing C structs typedefs
11054             // by instantiating a new type rather than simply copying members
11055             // into exp.expType
11056             FreeType(exp.expType);
11057             exp.expType = Type { };
11058             exp.expType.kind = symbol.type.kind;
11059             exp.expType.refCount++;
11060             exp.expType.enumName = enumName;
11061
11062             exp.expType.members = symbol.type.members;
11063             for(member = symbol.type.members.first; member; member = member.next)
11064                member.refCount++;
11065          }
11066          else
11067          {
11068             NamedLink64 member;
11069             for(member = symbol.type.members.first; member; member = member.next)
11070             {
11071                NamedLink64 value { name = CopyString(member.name) };
11072                exp.expType.members.Add(value);
11073             }
11074          }
11075       }
11076    }
11077
11078    yylloc = exp.loc;
11079    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11080    else if(exp.destType && !exp.destType.keepCast)
11081    {
11082       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11083          exp.needTemplateCast = 1;
11084
11085       if(exp.destType.kind == voidType);
11086       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11087       {
11088          // Warn for casting unrelated types to/from struct classes
11089          bool invalidCast = false;
11090          if(inCompiler && exp.destType.count && exp.expType)
11091          {
11092             Class c1 = (exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
11093             Class c2 = (exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
11094             if(c1 && c1.type != structClass) c1 = null;
11095             if(c2 && c2.type != structClass) c2 = null;
11096             if((c1 && !exp.expType.byReference && !c2 && !exp.destType.isPointerType) || (c2 && !exp.destType.byReference && !c1 && !exp.expType.isPointerType))
11097                invalidCast = true;
11098          }
11099          if(!exp.destType.count || unresolved || invalidCast)
11100          {
11101             if(!exp.expType)
11102             {
11103                yylloc = exp.loc;
11104                if(exp.destType.kind != ellipsisType)
11105                {
11106                   char type2[1024];
11107                   type2[0] = '\0';
11108                   if(inCompiler)
11109                   {
11110                      char expString[10240];
11111                      expString[0] = '\0';
11112
11113                      PrintType(exp.destType, type2, false, true);
11114
11115                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11116                      if(unresolved)
11117                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11118                      else if(exp.type != dummyExp)
11119                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11120                   }
11121                }
11122                else
11123                {
11124                   char expString[10240] ;
11125                   expString[0] = '\0';
11126                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11127
11128                   if(unresolved)
11129                      Compiler_Error($"unresolved identifier %s\n", expString);
11130                   else if(exp.type != dummyExp)
11131                      Compiler_Error($"couldn't determine type of %s\n", expString);
11132                }
11133             }
11134             else
11135             {
11136                char type1[1024];
11137                char type2[1024];
11138                type1[0] = '\0';
11139                type2[0] = '\0';
11140                if(inCompiler)
11141                {
11142                   PrintType(exp.expType, type1, false, true);
11143                   PrintType(exp.destType, type2, false, true);
11144                }
11145
11146                //CheckExpressionType(exp, exp.destType, false);
11147
11148                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11149                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11150                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11151                else
11152                {
11153                   char expString[10240];
11154                   expString[0] = '\0';
11155                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11156
11157 #ifdef _DEBUG
11158                   CheckExpressionType(exp, exp.destType, false, true);
11159 #endif
11160                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11161                   if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11162                                      !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11163                                      !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11164                                      !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11165                   {
11166                      if(invalidCast)
11167                         Compiler_Error($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11168                      else
11169                         Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11170                   }
11171
11172                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11173                   if(!inCompiler)
11174                   {
11175                      FreeType(exp.expType);
11176                      exp.destType.refCount++;
11177                      exp.expType = exp.destType;
11178                   }
11179                }
11180             }
11181          }
11182       }
11183       // Cast function pointers to void * as eC already checked compatibility
11184       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11185               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11186       {
11187          Expression nbExp = GetNonBracketsExp(exp);
11188          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11189          {
11190             Expression e = MoveExpContents(exp);
11191             exp.cast.exp = MkExpBrackets(MkListOne(e));
11192             exp.type = castExp;
11193             exp.cast.exp.destType = exp.destType;
11194             if(exp.destType) exp.destType.refCount++;
11195             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11196          }
11197       }
11198    }
11199    else if(unresolved)
11200    {
11201       if(exp.identifier._class && exp.identifier._class.name)
11202          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11203       else if(exp.identifier.string && exp.identifier.string[0])
11204          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11205    }
11206    else if(!exp.expType && exp.type != dummyExp)
11207    {
11208       char expString[10240];
11209       expString[0] = '\0';
11210       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11211       Compiler_Error($"couldn't determine type of %s\n", expString);
11212    }
11213
11214    // Let's try to support any_object & typed_object here:
11215    if(inCompiler)
11216       ApplyAnyObjectLogic(exp);
11217
11218    // Mark nohead classes as by reference, unless we're casting them to an integral type
11219    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11220       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11221          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11222           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11223    {
11224       exp.byReference = true;
11225    }
11226    yylloc = oldyylloc;
11227 }
11228
11229 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11230 {
11231    // THIS CODE WILL FIND NEXT MEMBER...
11232    if(*curMember)
11233    {
11234       *curMember = (*curMember).next;
11235
11236       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11237       {
11238          *curMember = subMemberStack[--(*subMemberStackPos)];
11239          *curMember = (*curMember).next;
11240       }
11241
11242       // SKIP ALL PROPERTIES HERE...
11243       while((*curMember) && (*curMember).isProperty)
11244          *curMember = (*curMember).next;
11245
11246       if(subMemberStackPos)
11247       {
11248          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11249          {
11250             subMemberStack[(*subMemberStackPos)++] = *curMember;
11251
11252             *curMember = (*curMember).members.first;
11253             while(*curMember && (*curMember).isProperty)
11254                *curMember = (*curMember).next;
11255          }
11256       }
11257    }
11258    while(!*curMember)
11259    {
11260       if(!*curMember)
11261       {
11262          if(subMemberStackPos && *subMemberStackPos)
11263          {
11264             *curMember = subMemberStack[--(*subMemberStackPos)];
11265             *curMember = (*curMember).next;
11266          }
11267          else
11268          {
11269             Class lastCurClass = *curClass;
11270
11271             if(*curClass == _class) break;     // REACHED THE END
11272
11273             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11274             *curMember = (*curClass).membersAndProperties.first;
11275          }
11276
11277          while((*curMember) && (*curMember).isProperty)
11278             *curMember = (*curMember).next;
11279          if(subMemberStackPos)
11280          {
11281             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11282             {
11283                subMemberStack[(*subMemberStackPos)++] = *curMember;
11284
11285                *curMember = (*curMember).members.first;
11286                while(*curMember && (*curMember).isProperty)
11287                   *curMember = (*curMember).next;
11288             }
11289          }
11290       }
11291    }
11292 }
11293
11294
11295 static void ProcessInitializer(Initializer init, Type type)
11296 {
11297    switch(init.type)
11298    {
11299       case expInitializer:
11300          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11301          {
11302             // TESTING THIS FOR SHUTTING = 0 WARNING
11303             if(init.exp && !init.exp.destType)
11304             {
11305                FreeType(init.exp.destType);
11306                init.exp.destType = type;
11307                if(type) type.refCount++;
11308             }
11309             if(init.exp)
11310             {
11311                ProcessExpressionType(init.exp);
11312                init.isConstant = init.exp.isConstant;
11313             }
11314             break;
11315          }
11316          else
11317          {
11318             Expression exp = init.exp;
11319             Instantiation inst = exp.instance;
11320             MembersInit members;
11321
11322             init.type = listInitializer;
11323             init.list = MkList();
11324
11325             if(inst.members)
11326             {
11327                for(members = inst.members->first; members; members = members.next)
11328                {
11329                   if(members.type == dataMembersInit)
11330                   {
11331                      MemberInit member;
11332                      for(member = members.dataMembers->first; member; member = member.next)
11333                      {
11334                         ListAdd(init.list, member.initializer);
11335                         member.initializer = null;
11336                      }
11337                   }
11338                   // Discard all MembersInitMethod
11339                }
11340             }
11341             FreeExpression(exp);
11342          }
11343       case listInitializer:
11344       {
11345          Initializer i;
11346          Type initializerType = null;
11347          Class curClass = null;
11348          DataMember curMember = null;
11349          DataMember subMemberStack[256];
11350          int subMemberStackPos = 0;
11351
11352          if(type && type.kind == arrayType)
11353             initializerType = Dereference(type);
11354          else if(type && (type.kind == structType || type.kind == unionType))
11355             initializerType = type.members.first;
11356
11357          for(i = init.list->first; i; i = i.next)
11358          {
11359             if(type && type.kind == classType && type._class && type._class.registered)
11360             {
11361                // 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)
11362                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11363                // TODO: Generate error on initializing a private data member this way from another module...
11364                if(curMember)
11365                {
11366                   if(!curMember.dataType)
11367                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11368                   initializerType = curMember.dataType;
11369                }
11370             }
11371             ProcessInitializer(i, initializerType);
11372             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11373                initializerType = initializerType.next;
11374             if(!i.isConstant)
11375                init.isConstant = false;
11376          }
11377
11378          if(type && type.kind == arrayType)
11379             FreeType(initializerType);
11380
11381          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11382          {
11383             Compiler_Error($"Assigning list initializer to non list\n");
11384          }
11385          break;
11386       }
11387    }
11388 }
11389
11390 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11391 {
11392    switch(spec.type)
11393    {
11394       case baseSpecifier:
11395       {
11396          if(spec.specifier == THISCLASS)
11397          {
11398             if(thisClass)
11399             {
11400                spec.type = nameSpecifier;
11401                spec.name = ReplaceThisClass(thisClass);
11402                spec.symbol = FindClass(spec.name);
11403                ProcessSpecifier(spec, declareStruct);
11404             }
11405          }
11406          break;
11407       }
11408       case nameSpecifier:
11409       {
11410          Symbol symbol = FindType(curContext, spec.name);
11411          if(symbol)
11412             DeclareType(curExternal, symbol.type, true, true);
11413          else if(spec.symbol /*&& declareStruct*/)
11414          {
11415             Class c = spec.symbol.registered;
11416             DeclareStruct(curExternal, spec.name, c && c.type == noHeadClass, declareStruct && c && c.type == structClass);
11417          }
11418          break;
11419       }
11420       case enumSpecifier:
11421       {
11422          Enumerator e;
11423          if(spec.list)
11424          {
11425             for(e = spec.list->first; e; e = e.next)
11426             {
11427                if(e.exp)
11428                   ProcessExpressionType(e.exp);
11429             }
11430          }
11431          // Fall through for IDE type processing
11432          if(inCompiler)
11433             break;
11434       }
11435       case structSpecifier:
11436       case unionSpecifier:
11437       {
11438          if(spec.definitions)
11439          {
11440             //ClassDef def;
11441             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11442             //if(symbol)
11443                ProcessClass(spec.definitions, symbol);
11444             /*else
11445             {
11446                for(def = spec.definitions->first; def; def = def.next)
11447                {
11448                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11449                      ProcessDeclaration(def.decl);
11450                }
11451             }*/
11452          }
11453          break;
11454       }
11455       /*
11456       case classSpecifier:
11457       {
11458          Symbol classSym = FindClass(spec.name);
11459          if(classSym && classSym.registered && classSym.registered.type == structClass)
11460             DeclareStruct(spec.name, false, true);
11461          break;
11462       }
11463       */
11464    }
11465 }
11466
11467
11468 static void ProcessDeclarator(Declarator decl, bool isFunction)
11469 {
11470    switch(decl.type)
11471    {
11472       case identifierDeclarator:
11473          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11474          {
11475             FreeSpecifier(decl.identifier._class);
11476             decl.identifier._class = null;
11477          }
11478          break;
11479       case arrayDeclarator:
11480          if(decl.array.exp)
11481             ProcessExpressionType(decl.array.exp);
11482       case structDeclarator:
11483       case bracketsDeclarator:
11484       case functionDeclarator:
11485       case pointerDeclarator:
11486       case extendedDeclarator:
11487       case extendedDeclaratorEnd:
11488       {
11489          Identifier id = null;
11490          Specifier classSpec = null;
11491          if(decl.type == functionDeclarator)
11492          {
11493             id = GetDeclId(decl);
11494             if(id && id._class)
11495             {
11496                classSpec = id._class;
11497                id._class = null;
11498             }
11499          }
11500          if(decl.declarator)
11501             ProcessDeclarator(decl.declarator, isFunction);
11502          if(decl.type == functionDeclarator)
11503          {
11504             if(classSpec)
11505             {
11506                TypeName param
11507                {
11508                   qualifiers = MkListOne(classSpec);
11509                   declarator = null;
11510                };
11511                if(!decl.function.parameters)
11512                   decl.function.parameters = MkList();
11513                decl.function.parameters->Insert(null, param);
11514             }
11515             if(decl.function.parameters)
11516             {
11517                TypeName param;
11518
11519                for(param = decl.function.parameters->first; param; param = param.next)
11520                {
11521                   if(param.qualifiers)
11522                   {
11523                      Specifier spec;
11524                      for(spec = param.qualifiers->first; spec; spec = spec.next)
11525                      {
11526                         if(spec.type == baseSpecifier)
11527                         {
11528                            if(spec.specifier == TYPED_OBJECT)
11529                            {
11530                               Declarator d = param.declarator;
11531                               TypeName newParam
11532                               {
11533                                  qualifiers = MkListOne(MkSpecifier(VOID));
11534                                  declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11535                               };
11536                               if(d.type != pointerDeclarator)
11537                                  newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11538
11539                               FreeList(param.qualifiers, FreeSpecifier);
11540
11541                               param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11542                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11543
11544                               DeclareStruct(curExternal, "ecere::com::Class", false, true);
11545
11546                               decl.function.parameters->Insert(param, newParam);
11547                               param = newParam;
11548                               break;
11549                            }
11550                            else if(spec.specifier == ANY_OBJECT)
11551                            {
11552                               Declarator d = param.declarator;
11553
11554                               FreeList(param.qualifiers, FreeSpecifier);
11555
11556                               param.qualifiers = MkListOne(MkSpecifier(VOID));
11557                               if(d.type != pointerDeclarator)
11558                                  param.qualifiers->Insert(null, MkSpecifier(CONST));
11559                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11560                               break;
11561                            }
11562                            else if(spec.specifier == THISCLASS)
11563                            {
11564                               if(thisClass)
11565                               {
11566                                  spec.type = nameSpecifier;
11567                                  spec.name = ReplaceThisClass(thisClass);
11568                                  spec.symbol = FindClass(spec.name);
11569                                  ProcessSpecifier(spec, false);
11570                               }
11571                               break;
11572                            }
11573                         }
11574                         else if(spec.type == nameSpecifier)
11575                         {
11576                            ProcessSpecifier(spec, isFunction);
11577                         }
11578                      }
11579                   }
11580
11581                   if(param.declarator)
11582                      ProcessDeclarator(param.declarator, false);
11583                }
11584             }
11585          }
11586          break;
11587       }
11588    }
11589 }
11590
11591 static void ProcessDeclaration(Declaration decl)
11592 {
11593    yylloc = decl.loc;
11594    switch(decl.type)
11595    {
11596       case initDeclaration:
11597       {
11598          bool declareStruct = false;
11599          /*
11600          lineNum = decl.pos.line;
11601          column = decl.pos.col;
11602          */
11603
11604          if(decl.declarators)
11605          {
11606             InitDeclarator d;
11607
11608             for(d = decl.declarators->first; d; d = d.next)
11609             {
11610                Type type, subType;
11611                ProcessDeclarator(d.declarator, false);
11612
11613                type = ProcessType(decl.specifiers, d.declarator);
11614
11615                if(d.initializer)
11616                {
11617                   ProcessInitializer(d.initializer, type);
11618
11619                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11620
11621                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11622                      d.initializer.exp.type == instanceExp)
11623                   {
11624                      if(type.kind == classType && type._class ==
11625                         d.initializer.exp.expType._class)
11626                      {
11627                         Instantiation inst = d.initializer.exp.instance;
11628                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11629
11630                         d.initializer.exp.instance = null;
11631                         if(decl.specifiers)
11632                            FreeList(decl.specifiers, FreeSpecifier);
11633                         FreeList(decl.declarators, FreeInitDeclarator);
11634
11635                         d = null;
11636
11637                         decl.type = instDeclaration;
11638                         decl.inst = inst;
11639                      }
11640                   }
11641                }
11642                for(subType = type; subType;)
11643                {
11644                   if(subType.kind == classType)
11645                   {
11646                      declareStruct = true;
11647                      break;
11648                   }
11649                   else if(subType.kind == pointerType)
11650                      break;
11651                   else if(subType.kind == arrayType)
11652                      subType = subType.arrayType;
11653                   else
11654                      break;
11655                }
11656
11657                FreeType(type);
11658                if(!d) break;
11659             }
11660          }
11661
11662          if(decl.specifiers)
11663          {
11664             Specifier s;
11665             for(s = decl.specifiers->first; s; s = s.next)
11666             {
11667                ProcessSpecifier(s, declareStruct);
11668             }
11669          }
11670          break;
11671       }
11672       case instDeclaration:
11673       {
11674          ProcessInstantiationType(decl.inst);
11675          break;
11676       }
11677       case structDeclaration:
11678       {
11679          Specifier spec;
11680          Declarator d;
11681          bool declareStruct = false;
11682
11683          if(decl.declarators)
11684          {
11685             for(d = decl.declarators->first; d; d = d.next)
11686             {
11687                Type type = ProcessType(decl.specifiers, d.declarator);
11688                Type subType;
11689                ProcessDeclarator(d, false);
11690                for(subType = type; subType;)
11691                {
11692                   if(subType.kind == classType)
11693                   {
11694                      declareStruct = true;
11695                      break;
11696                   }
11697                   else if(subType.kind == pointerType)
11698                      break;
11699                   else if(subType.kind == arrayType)
11700                      subType = subType.arrayType;
11701                   else
11702                      break;
11703                }
11704                FreeType(type);
11705             }
11706          }
11707          if(decl.specifiers)
11708          {
11709             for(spec = decl.specifiers->first; spec; spec = spec.next)
11710                ProcessSpecifier(spec, declareStruct);
11711          }
11712          break;
11713       }
11714    }
11715 }
11716
11717 static FunctionDefinition curFunction;
11718
11719 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11720 {
11721    char propName[1024], propNameM[1024];
11722    char getName[1024], setName[1024];
11723    OldList * args;
11724
11725    DeclareProperty(curExternal, prop, setName, getName);
11726
11727    // eInstance_FireWatchers(object, prop);
11728    strcpy(propName, "__ecereProp_");
11729    FullClassNameCat(propName, prop._class.fullName, false);
11730    strcat(propName, "_");
11731    FullClassNameCat(propName, prop.name, true);
11732
11733    strcpy(propNameM, "__ecerePropM_");
11734    FullClassNameCat(propNameM, prop._class.fullName, false);
11735    strcat(propNameM, "_");
11736    FullClassNameCat(propNameM, prop.name, true);
11737
11738    if(prop.isWatchable)
11739    {
11740       args = MkList();
11741       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11742       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11743       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11744
11745       args = MkList();
11746       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11747       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11748       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11749
11750       DeclareFunctionUtil(curExternal, "eInstance_FireWatchers");
11751    }
11752
11753    {
11754       args = MkList();
11755       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11756       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11757       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11758
11759       args = MkList();
11760       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11761       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11762       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11763
11764       DeclareFunctionUtil(curExternal, "eInstance_FireSelfWatchers");
11765    }
11766
11767    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11768       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11769       curFunction.propSet.fireWatchersDone = true;
11770 }
11771
11772 static void ProcessStatement(Statement stmt)
11773 {
11774    yylloc = stmt.loc;
11775    /*
11776    lineNum = stmt.pos.line;
11777    column = stmt.pos.col;
11778    */
11779    switch(stmt.type)
11780    {
11781       case labeledStmt:
11782          ProcessStatement(stmt.labeled.stmt);
11783          break;
11784       case caseStmt:
11785          // This expression should be constant...
11786          if(stmt.caseStmt.exp)
11787          {
11788             FreeType(stmt.caseStmt.exp.destType);
11789             stmt.caseStmt.exp.destType = curSwitchType;
11790             if(curSwitchType) curSwitchType.refCount++;
11791             ProcessExpressionType(stmt.caseStmt.exp);
11792             ComputeExpression(stmt.caseStmt.exp);
11793          }
11794          if(stmt.caseStmt.stmt)
11795             ProcessStatement(stmt.caseStmt.stmt);
11796          break;
11797       case compoundStmt:
11798       {
11799          if(stmt.compound.context)
11800          {
11801             Declaration decl;
11802             Statement s;
11803
11804             Statement prevCompound = curCompound;
11805             Context prevContext = curContext;
11806
11807             if(!stmt.compound.isSwitch)
11808                curCompound = stmt;
11809             curContext = stmt.compound.context;
11810
11811             if(stmt.compound.declarations)
11812             {
11813                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11814                   ProcessDeclaration(decl);
11815             }
11816             if(stmt.compound.statements)
11817             {
11818                for(s = stmt.compound.statements->first; s; s = s.next)
11819                   ProcessStatement(s);
11820             }
11821
11822             curContext = prevContext;
11823             curCompound = prevCompound;
11824          }
11825          break;
11826       }
11827       case expressionStmt:
11828       {
11829          Expression exp;
11830          if(stmt.expressions)
11831          {
11832             for(exp = stmt.expressions->first; exp; exp = exp.next)
11833                ProcessExpressionType(exp);
11834          }
11835          break;
11836       }
11837       case ifStmt:
11838       {
11839          Expression exp;
11840
11841          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11842          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11843          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11844          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11845          {
11846             ProcessExpressionType(exp);
11847          }
11848          if(stmt.ifStmt.stmt)
11849             ProcessStatement(stmt.ifStmt.stmt);
11850          if(stmt.ifStmt.elseStmt)
11851             ProcessStatement(stmt.ifStmt.elseStmt);
11852          break;
11853       }
11854       case switchStmt:
11855       {
11856          Type oldSwitchType = curSwitchType;
11857          if(stmt.switchStmt.exp)
11858          {
11859             Expression exp;
11860             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11861             {
11862                if(!exp.next)
11863                {
11864                   /*
11865                   Type destType
11866                   {
11867                      kind = intType;
11868                      refCount = 1;
11869                   };
11870                   e.exp.destType = destType;
11871                   */
11872
11873                   ProcessExpressionType(exp);
11874                }
11875                if(!exp.next)
11876                   curSwitchType = exp.expType;
11877             }
11878          }
11879          ProcessStatement(stmt.switchStmt.stmt);
11880          curSwitchType = oldSwitchType;
11881          break;
11882       }
11883       case whileStmt:
11884       {
11885          if(stmt.whileStmt.exp)
11886          {
11887             Expression exp;
11888
11889             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11890             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11891             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11892             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11893             {
11894                ProcessExpressionType(exp);
11895             }
11896          }
11897          if(stmt.whileStmt.stmt)
11898             ProcessStatement(stmt.whileStmt.stmt);
11899          break;
11900       }
11901       case doWhileStmt:
11902       {
11903          if(stmt.doWhile.exp)
11904          {
11905             Expression exp;
11906
11907             if(stmt.doWhile.exp->last)
11908             {
11909                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11910                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11911                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11912             }
11913             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11914             {
11915                ProcessExpressionType(exp);
11916             }
11917          }
11918          if(stmt.doWhile.stmt)
11919             ProcessStatement(stmt.doWhile.stmt);
11920          break;
11921       }
11922       case forStmt:
11923       {
11924          Expression exp;
11925          if(stmt.forStmt.init)
11926             ProcessStatement(stmt.forStmt.init);
11927
11928          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11929          {
11930             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11931             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11932             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11933          }
11934
11935          if(stmt.forStmt.check)
11936             ProcessStatement(stmt.forStmt.check);
11937          if(stmt.forStmt.increment)
11938          {
11939             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11940                ProcessExpressionType(exp);
11941          }
11942
11943          if(stmt.forStmt.stmt)
11944             ProcessStatement(stmt.forStmt.stmt);
11945          break;
11946       }
11947       case forEachStmt:
11948       {
11949          Identifier id = stmt.forEachStmt.id;
11950          OldList * exp = stmt.forEachStmt.exp;
11951          OldList * filter = stmt.forEachStmt.filter;
11952          Statement block = stmt.forEachStmt.stmt;
11953          char iteratorType[1024];
11954          Type source;
11955          Expression e;
11956          bool isBuiltin = exp && exp->last &&
11957             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11958               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11959          Expression arrayExp;
11960          const char * typeString = null;
11961          int builtinCount = 0;
11962
11963          for(e = exp ? exp->first : null; e; e = e.next)
11964          {
11965             if(!e.next)
11966             {
11967                FreeType(e.destType);
11968                e.destType = ProcessTypeString("Container", false);
11969             }
11970             if(!isBuiltin || e.next)
11971                ProcessExpressionType(e);
11972          }
11973
11974          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11975          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11976             eClass_IsDerived(source._class.registered, containerClass)))
11977          {
11978             Class _class = source ? source._class.registered : null;
11979             Symbol symbol;
11980             Expression expIt = null;
11981             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11982             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11983             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11984             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11985
11986             if(inCompiler)
11987             {
11988                stmt.type = compoundStmt;
11989
11990                stmt.compound.context = Context { };
11991                stmt.compound.context.parent = curContext;
11992                curContext = stmt.compound.context;
11993             }
11994
11995             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11996             {
11997                Class mapClass = eSystem_FindClass(privateModule, "Map");
11998                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11999                isCustomAVLTree = true;
12000                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
12001                   isAVLTree = true;
12002                else */if(eClass_IsDerived(source._class.registered, mapClass))
12003                   isMap = true;
12004             }
12005             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
12006             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
12007             {
12008                Class listClass = eSystem_FindClass(privateModule, "List");
12009                isLinkList = true;
12010                isList = eClass_IsDerived(source._class.registered, listClass);
12011             }
12012
12013             if(inCompiler && isArray)
12014             {
12015                Declarator decl;
12016                OldList * specs = MkList();
12017                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12018                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12019                stmt.compound.declarations = MkListOne(
12020                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12021                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12022                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12023                      MkInitializerAssignment(MkExpBrackets(exp))))));
12024             }
12025             else if(isBuiltin)
12026             {
12027                Type type = null;
12028                char typeStringBuf[1024];
12029
12030                // TODO: Merge this code?
12031                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12032                if(((Expression)exp->last).type == castExp)
12033                {
12034                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12035                   if(typeName)
12036                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12037                }
12038
12039                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12040                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12041                   arrayExp.destType._class.registered.templateArgs)
12042                {
12043                   Class templateClass = arrayExp.destType._class.registered;
12044                   typeString = templateClass.templateArgs[2].dataTypeString;
12045                }
12046                else if(arrayExp.list)
12047                {
12048                   // Guess type from expressions in the array
12049                   Expression e;
12050                   for(e = arrayExp.list->first; e; e = e.next)
12051                   {
12052                      ProcessExpressionType(e);
12053                      if(e.expType)
12054                      {
12055                         if(!type) { type = e.expType; type.refCount++; }
12056                         else
12057                         {
12058                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12059                            if(!MatchTypeExpression(e, type, null, false, true))
12060                            {
12061                               FreeType(type);
12062                               type = e.expType;
12063                               e.expType = null;
12064
12065                               e = arrayExp.list->first;
12066                               ProcessExpressionType(e);
12067                               if(e.expType)
12068                               {
12069                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12070                                  if(!MatchTypeExpression(e, type, null, false, true))
12071                                  {
12072                                     FreeType(e.expType);
12073                                     e.expType = null;
12074                                     FreeType(type);
12075                                     type = null;
12076                                     break;
12077                                  }
12078                               }
12079                            }
12080                         }
12081                         if(e.expType)
12082                         {
12083                            FreeType(e.expType);
12084                            e.expType = null;
12085                         }
12086                      }
12087                   }
12088                   if(type)
12089                   {
12090                      typeStringBuf[0] = '\0';
12091                      PrintType(type, typeStringBuf, false, true);
12092                      typeString = typeStringBuf;
12093                      FreeType(type);
12094                   }
12095                }
12096                if(typeString)
12097                {
12098                   if(inCompiler)
12099                   {
12100                      OldList * initializers = MkList();
12101                      Declarator decl;
12102                      OldList * specs = MkList();
12103                      if(arrayExp.list)
12104                      {
12105                         Expression e;
12106
12107                         builtinCount = arrayExp.list->count;
12108                         type = ProcessTypeString(typeString, false);
12109                         while((e = arrayExp.list->first))
12110                         {
12111                            arrayExp.list->Remove(e);
12112                            e.destType = type;
12113                            type.refCount++;
12114                            ProcessExpressionType(e);
12115                            if(inCompiler)
12116                               ListAdd(initializers, MkInitializerAssignment(e));
12117                         }
12118                         FreeType(type);
12119                         delete arrayExp.list;
12120                      }
12121                      decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12122
12123                      stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12124                         MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12125
12126                      ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12127                         PlugDeclarator(
12128                            /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12129                            ), MkInitializerList(initializers)))));
12130                      FreeList(exp, FreeExpression);
12131                   }
12132                   else if(arrayExp.list)
12133                   {
12134                      Expression e;
12135                      type = ProcessTypeString(typeString, false);
12136                      for(e = arrayExp.list->first; e; e = e.next)
12137                      {
12138                         e.destType = type;
12139                         type.refCount++;
12140                         ProcessExpressionType(e);
12141                      }
12142                      FreeType(type);
12143                   }
12144                }
12145                else
12146                {
12147                   arrayExp.expType = ProcessTypeString("Container", false);
12148                   Compiler_Error($"Couldn't determine type of array elements\n");
12149                }
12150
12151                /*
12152                Declarator decl;
12153                OldList * specs = MkList();
12154
12155                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12156                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12157                stmt.compound.declarations = MkListOne(
12158                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12159                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12160                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12161                      MkInitializerAssignment(MkExpBrackets(exp))))));
12162                */
12163             }
12164             else if(inCompiler && isLinkList && !isList)
12165             {
12166                Declarator decl;
12167                OldList * specs = MkList();
12168                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12169                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12170                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12171                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12172                      MkInitializerAssignment(MkExpBrackets(exp))))));
12173             }
12174             /*else if(isCustomAVLTree)
12175             {
12176                Declarator decl;
12177                OldList * specs = MkList();
12178                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12179                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12180                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12181                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12182                      MkInitializerAssignment(MkExpBrackets(exp))))));
12183             }*/
12184             else if(inCompiler && _class.templateArgs)
12185             {
12186                if(isMap)
12187                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12188                else
12189                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12190
12191                stmt.compound.declarations = MkListOne(
12192                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12193                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12194                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12195             }
12196             if(inCompiler)
12197             {
12198                symbol = FindSymbol(id.string, curContext, curContext, false, false);
12199
12200                if(block)
12201                {
12202                   // Reparent sub-contexts in this statement
12203                   switch(block.type)
12204                   {
12205                      case compoundStmt:
12206                         if(block.compound.context)
12207                            block.compound.context.parent = stmt.compound.context;
12208                         break;
12209                      case ifStmt:
12210                         if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12211                            block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12212                         if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12213                            block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12214                         break;
12215                      case switchStmt:
12216                         if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12217                            block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12218                         break;
12219                      case whileStmt:
12220                         if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12221                            block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12222                         break;
12223                      case doWhileStmt:
12224                         if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12225                            block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12226                         break;
12227                      case forStmt:
12228                         if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12229                            block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12230                         break;
12231                      case forEachStmt:
12232                         if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12233                            block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12234                         break;
12235                      /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12236                      case labeledStmt:
12237                      case caseStmt
12238                      case expressionStmt:
12239                      case gotoStmt:
12240                      case continueStmt:
12241                      case breakStmt
12242                      case returnStmt:
12243                      case asmStmt:
12244                      case badDeclarationStmt:
12245                      case fireWatchersStmt:
12246                      case stopWatchingStmt:
12247                      case watchStmt:
12248                      */
12249                   }
12250                }
12251
12252                if(filter)
12253                {
12254                   block = MkIfStmt(filter, block, null);
12255                }
12256                if(isArray)
12257                {
12258                   stmt.compound.statements = MkListOne(MkForStmt(
12259                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12260                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12261                         MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12262                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12263                      block));
12264                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12265                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12266                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12267                }
12268                else if(isBuiltin)
12269                {
12270                   char count[128];
12271                   //OldList * specs = MkList();
12272                   // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12273
12274                   sprintf(count, "%d", builtinCount);
12275
12276                   stmt.compound.statements = MkListOne(MkForStmt(
12277                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12278                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12279                         MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12280                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12281                      block));
12282
12283                   /*
12284                   Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12285                   stmt.compound.statements = MkListOne(MkForStmt(
12286                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12287                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12288                         MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12289                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12290                      block));
12291                  */
12292                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12293                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12294                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12295                }
12296                else if(isLinkList && !isList)
12297                {
12298                   Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12299                   Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12300                   if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12301                      !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12302                   {
12303                      stmt.compound.statements = MkListOne(MkForStmt(
12304                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12305                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12306                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12307                         block));
12308                   }
12309                   else
12310                   {
12311                      OldList * specs = MkList();
12312                      Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12313                      stmt.compound.statements = MkListOne(MkForStmt(
12314                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12315                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12316                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12317                            MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12318                               MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12319                         block));
12320                   }
12321                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12322                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12323                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12324                }
12325                /*else if(isCustomAVLTree)
12326                {
12327                   stmt.compound.statements = MkListOne(MkForStmt(
12328                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12329                         MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12330                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12331                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12332                      block));
12333
12334                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12335                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12336                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12337                }*/
12338                else
12339                {
12340                   stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12341                      MkIdentifier("Next")), null)), block));
12342                }
12343                ProcessExpressionType(expIt);
12344                if(stmt.compound.declarations->first)
12345                   ProcessDeclaration(stmt.compound.declarations->first);
12346
12347                if(symbol)
12348                   symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12349
12350                ProcessStatement(stmt);
12351             }
12352             else
12353                ProcessStatement(stmt.forEachStmt.stmt);
12354             if(inCompiler)
12355                curContext = stmt.compound.context.parent;
12356             break;
12357          }
12358          else
12359          {
12360             Compiler_Error($"Expression is not a container\n");
12361          }
12362          break;
12363       }
12364       case gotoStmt:
12365          break;
12366       case continueStmt:
12367          break;
12368       case breakStmt:
12369          break;
12370       case returnStmt:
12371       {
12372          Expression exp;
12373          if(stmt.expressions)
12374          {
12375             for(exp = stmt.expressions->first; exp; exp = exp.next)
12376             {
12377                if(!exp.next)
12378                {
12379                   if(curFunction && !curFunction.type)
12380                      curFunction.type = ProcessType(
12381                         curFunction.specifiers, curFunction.declarator);
12382                   FreeType(exp.destType);
12383                   // TODO: current property if not compiling
12384                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12385                   if(exp.destType) exp.destType.refCount++;
12386                }
12387                ProcessExpressionType(exp);
12388             }
12389          }
12390          break;
12391       }
12392       case badDeclarationStmt:
12393       {
12394          ProcessDeclaration(stmt.decl);
12395          break;
12396       }
12397       case asmStmt:
12398       {
12399          AsmField field;
12400          if(stmt.asmStmt.inputFields)
12401          {
12402             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12403                if(field.expression)
12404                   ProcessExpressionType(field.expression);
12405          }
12406          if(stmt.asmStmt.outputFields)
12407          {
12408             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12409                if(field.expression)
12410                   ProcessExpressionType(field.expression);
12411          }
12412          if(stmt.asmStmt.clobberedFields)
12413          {
12414             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12415             {
12416                if(field.expression)
12417                   ProcessExpressionType(field.expression);
12418             }
12419          }
12420          break;
12421       }
12422       case watchStmt:
12423       {
12424          PropertyWatch propWatch;
12425          OldList * watches = stmt._watch.watches;
12426          Expression object = stmt._watch.object;
12427          Expression watcher = stmt._watch.watcher;
12428          if(watcher)
12429             ProcessExpressionType(watcher);
12430          if(object)
12431             ProcessExpressionType(object);
12432
12433          if(inCompiler)
12434          {
12435             if(watcher || thisClass)
12436             {
12437                External external = curExternal;
12438                Context context = curContext;
12439
12440                stmt.type = expressionStmt;
12441                stmt.expressions = MkList();
12442
12443                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12444                {
12445                   ClassFunction func;
12446                   char watcherName[1024];
12447                   Class watcherClass = watcher ?
12448                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12449                   External createdExternal;
12450
12451                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12452                   if(propWatch.deleteWatch)
12453                      strcat(watcherName, "_delete");
12454                   else
12455                   {
12456                      Identifier propID;
12457                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12458                      {
12459                         strcat(watcherName, "_");
12460                         strcat(watcherName, propID.string);
12461                      }
12462                   }
12463
12464                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12465                   {
12466                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12467                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12468                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12469                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12470                      ProcessClassFunctionBody(func, propWatch.compound);
12471                      propWatch.compound = null;
12472
12473                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12474
12475                      curExternal = createdExternal;
12476                      ProcessFunction(createdExternal.function);
12477
12478                      if(propWatch.deleteWatch)
12479                      {
12480                         OldList * args = MkList();
12481                         ListAdd(args, CopyExpression(object));
12482                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12483                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12484                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12485                      }
12486                      else
12487                      {
12488                         Class _class = object.expType._class.registered;
12489                         Identifier propID;
12490
12491                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12492                         {
12493                            char propName[1024];
12494                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12495                            if(prop)
12496                            {
12497                               char getName[1024], setName[1024];
12498                               OldList * args = MkList();
12499
12500                               DeclareProperty(createdExternal, prop, setName, getName);
12501
12502                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12503                               strcpy(propName, "__ecereProp_");
12504                               FullClassNameCat(propName, prop._class.fullName, false);
12505                               strcat(propName, "_");
12506                               FullClassNameCat(propName, prop.name, true);
12507
12508                               ListAdd(args, CopyExpression(object));
12509                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12510                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12511                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12512
12513                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12514
12515                               external.CreateUniqueEdge(createdExternal, true);
12516                            }
12517                            else
12518                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12519                         }
12520                      }
12521                   }
12522                   else
12523                      Compiler_Error($"Invalid watched object\n");
12524                }
12525
12526                curExternal = external;
12527                curContext = context;
12528
12529                if(watcher)
12530                   FreeExpression(watcher);
12531                if(object)
12532                   FreeExpression(object);
12533                FreeList(watches, FreePropertyWatch);
12534             }
12535             else
12536                Compiler_Error($"No observer specified and not inside a class\n");
12537          }
12538          else
12539          {
12540             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12541             {
12542                ProcessStatement(propWatch.compound);
12543             }
12544
12545          }
12546          break;
12547       }
12548       case fireWatchersStmt:
12549       {
12550          OldList * watches = stmt._watch.watches;
12551          Expression object = stmt._watch.object;
12552          Class _class;
12553          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12554          // printf("%X\n", watches);
12555          // printf("%X\n", stmt._watch.watches);
12556          if(object)
12557             ProcessExpressionType(object);
12558
12559          if(inCompiler)
12560          {
12561             _class = object ?
12562                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12563
12564             if(_class)
12565             {
12566                Identifier propID;
12567
12568                stmt.type = expressionStmt;
12569                stmt.expressions = MkList();
12570
12571                // Check if we're inside a property set
12572                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12573                {
12574                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12575                }
12576                else if(!watches)
12577                {
12578                   //Compiler_Error($"No property specified and not inside a property set\n");
12579                }
12580                if(watches)
12581                {
12582                   for(propID = watches->first; propID; propID = propID.next)
12583                   {
12584                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12585                      if(prop)
12586                      {
12587                         CreateFireWatcher(prop, object, stmt);
12588                      }
12589                      else
12590                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12591                   }
12592                }
12593                else
12594                {
12595                   // Fire all properties!
12596                   Property prop;
12597                   Class base;
12598                   for(base = _class; base; base = base.base)
12599                   {
12600                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12601                      {
12602                         if(prop.isProperty && prop.isWatchable)
12603                         {
12604                            CreateFireWatcher(prop, object, stmt);
12605                         }
12606                      }
12607                   }
12608                }
12609
12610                if(object)
12611                   FreeExpression(object);
12612                FreeList(watches, FreeIdentifier);
12613             }
12614             else
12615                Compiler_Error($"Invalid object specified and not inside a class\n");
12616          }
12617          break;
12618       }
12619       case stopWatchingStmt:
12620       {
12621          OldList * watches = stmt._watch.watches;
12622          Expression object = stmt._watch.object;
12623          Expression watcher = stmt._watch.watcher;
12624          Class _class;
12625          if(object)
12626             ProcessExpressionType(object);
12627          if(watcher)
12628             ProcessExpressionType(watcher);
12629          if(inCompiler)
12630          {
12631             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12632
12633             if(watcher || thisClass)
12634             {
12635                if(_class)
12636                {
12637                   Identifier propID;
12638
12639                   stmt.type = expressionStmt;
12640                   stmt.expressions = MkList();
12641
12642                   if(!watches)
12643                   {
12644                      OldList * args;
12645                      // eInstance_StopWatching(object, null, watcher);
12646                      args = MkList();
12647                      ListAdd(args, CopyExpression(object));
12648                      ListAdd(args, MkExpConstant("0"));
12649                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12650                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12651                   }
12652                   else
12653                   {
12654                      for(propID = watches->first; propID; propID = propID.next)
12655                      {
12656                         char propName[1024];
12657                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12658                         if(prop)
12659                         {
12660                            char getName[1024], setName[1024];
12661                            OldList * args = MkList();
12662
12663                            DeclareProperty(curExternal, prop, setName, getName);
12664
12665                            // eInstance_StopWatching(object, prop, watcher);
12666                            strcpy(propName, "__ecereProp_");
12667                            FullClassNameCat(propName, prop._class.fullName, false);
12668                            strcat(propName, "_");
12669                            FullClassNameCat(propName, prop.name, true);
12670
12671                            ListAdd(args, CopyExpression(object));
12672                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12673                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12674                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12675                         }
12676                         else
12677                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12678                      }
12679                   }
12680
12681                   if(object)
12682                      FreeExpression(object);
12683                   if(watcher)
12684                      FreeExpression(watcher);
12685                   FreeList(watches, FreeIdentifier);
12686                }
12687                else
12688                   Compiler_Error($"Invalid object specified and not inside a class\n");
12689             }
12690             else
12691                Compiler_Error($"No observer specified and not inside a class\n");
12692          }
12693          break;
12694       }
12695    }
12696 }
12697
12698 static void ProcessFunction(FunctionDefinition function)
12699 {
12700    Identifier id = GetDeclId(function.declarator);
12701    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12702    Type type = symbol ? symbol.type : null;
12703    Class oldThisClass = thisClass;
12704    Context oldTopContext = topContext;
12705
12706    yylloc = function.loc;
12707    // Process thisClass
12708
12709    if(type && type.thisClass)
12710    {
12711       Symbol classSym = type.thisClass;
12712       Class _class = type.thisClass.registered;
12713       char className[1024];
12714       char structName[1024];
12715       Declarator funcDecl;
12716       Symbol thisSymbol;
12717
12718       bool typedObject = false;
12719
12720       if(_class && !_class.base)
12721       {
12722          _class = currentClass;
12723          if(_class && !_class.symbol)
12724             _class.symbol = FindClass(_class.fullName);
12725          classSym = _class ? _class.symbol : null;
12726          typedObject = true;
12727       }
12728
12729       thisClass = _class;
12730
12731       if(inCompiler && _class)
12732       {
12733          if(type.kind == functionType)
12734          {
12735             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12736             {
12737                //TypeName param = symbol.type.params.first;
12738                Type param = symbol.type.params.first;
12739                symbol.type.params.Remove(param);
12740                //FreeTypeName(param);
12741                FreeType(param);
12742             }
12743             if(type.classObjectType != classPointer)
12744             {
12745                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12746                symbol.type.staticMethod = true;
12747                symbol.type.thisClass = null;
12748
12749                // HIGH DANGER: VERIFYING THIS...
12750                symbol.type.extraParam = false;
12751             }
12752          }
12753
12754          strcpy(className, "__ecereClass_");
12755          FullClassNameCat(className, _class.fullName, true);
12756
12757          structName[0] = 0;
12758          FullClassNameCat(structName, _class.fullName, false);
12759
12760          // [class] this
12761          funcDecl = GetFuncDecl(function.declarator);
12762          if(funcDecl)
12763          {
12764             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12765             {
12766                TypeName param = funcDecl.function.parameters->first;
12767                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12768                {
12769                   funcDecl.function.parameters->Remove(param);
12770                   FreeTypeName(param);
12771                }
12772             }
12773
12774             if(!function.propertyNoThis)
12775             {
12776                TypeName thisParam = null;
12777
12778                if(type.classObjectType != classPointer)
12779                {
12780                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12781                   if(!funcDecl.function.parameters)
12782                      funcDecl.function.parameters = MkList();
12783                   funcDecl.function.parameters->Insert(null, thisParam);
12784                }
12785
12786                if(typedObject)
12787                {
12788                   if(type.classObjectType != classPointer)
12789                   {
12790                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12791                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12792                   }
12793
12794                   thisParam = TypeName
12795                   {
12796                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12797                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12798                   };
12799                   DeclareStruct(curExternal, "ecere::com::Class", false, true);
12800                   funcDecl.function.parameters->Insert(null, thisParam);
12801                }
12802             }
12803          }
12804
12805          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12806          {
12807             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12808             funcDecl = GetFuncDecl(initDecl.declarator);
12809             if(funcDecl)
12810             {
12811                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12812                {
12813                   TypeName param = funcDecl.function.parameters->first;
12814                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12815                   {
12816                      funcDecl.function.parameters->Remove(param);
12817                      FreeTypeName(param);
12818                   }
12819                }
12820
12821                if(type.classObjectType != classPointer)
12822                {
12823                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12824                   {
12825                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12826
12827                      if(!funcDecl.function.parameters)
12828                         funcDecl.function.parameters = MkList();
12829                      funcDecl.function.parameters->Insert(null, thisParam);
12830                   }
12831                }
12832             }
12833          }
12834       }
12835
12836       // Add this to the context
12837       if(function.body)
12838       {
12839          if(type.classObjectType != classPointer)
12840          {
12841             thisSymbol = Symbol
12842             {
12843                string = CopyString("this");
12844                type = classSym ? MkClassType(classSym.string) : null;
12845             };
12846             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12847
12848             if(typedObject && thisSymbol.type)
12849             {
12850                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12851                thisSymbol.type.byReference = type.byReference;
12852                thisSymbol.type.typedByReference = type.byReference;
12853             }
12854          }
12855       }
12856
12857       // Pointer to class data
12858       if(inCompiler && _class && _class.type == normalClass && type.classObjectType != classPointer)
12859       {
12860          DataMember member = null;
12861          {
12862             Class base;
12863             for(base = _class; base && base.type != systemClass; base = base.next)
12864             {
12865                for(member = base.membersAndProperties.first; member; member = member.next)
12866                   if(!member.isProperty)
12867                      break;
12868                if(member)
12869                   break;
12870             }
12871          }
12872          for(member = _class.membersAndProperties.first; member; member = member.next)
12873             if(!member.isProperty)
12874                break;
12875          if(member)
12876          {
12877             char pointerName[1024];
12878
12879             Declaration decl;
12880             Initializer initializer;
12881             Expression exp, bytePtr;
12882
12883             strcpy(pointerName, "__ecerePointer_");
12884             FullClassNameCat(pointerName, _class.fullName, false);
12885             {
12886                char className[1024];
12887                strcpy(className, "__ecereClass_");
12888                FullClassNameCat(className, classSym.string, true);
12889
12890                DeclareClass(curExternal, classSym, className);
12891             }
12892
12893             // ((byte *) this)
12894             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12895
12896             if(_class.fixed)
12897             {
12898                Expression e;
12899                if(_class.offset && _class.offset == _class.base.structSize)
12900                {
12901                   e = MkExpClassSize(MkSpecifierName(_class.base.fullName));
12902                   ProcessExpressionType(e);
12903                }
12904                else
12905                {
12906                   char string[256];
12907                   sprintf(string, "%d", _class.offset);  // Need Bootstrap Fix
12908                   e = MkExpConstant(string);
12909                }
12910                exp = QBrackets(MkExpOp(bytePtr, '+', e));
12911             }
12912             else
12913             {
12914                // ([bytePtr] + [className]->offset)
12915                exp = QBrackets(MkExpOp(bytePtr, '+',
12916                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12917             }
12918
12919             // (this ? [exp] : 0)
12920             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12921             exp.expType = Type
12922             {
12923                refCount = 1;
12924                kind = pointerType;
12925                type = Type { refCount = 1, kind = voidType };
12926             };
12927
12928             if(function.body)
12929             {
12930                yylloc = function.body.loc;
12931                // ([structName] *) [exp]
12932                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12933                initializer = MkInitializerAssignment(
12934                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12935
12936                // [structName] * [pointerName] = [initializer];
12937                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12938
12939                {
12940                   Context prevContext = curContext;
12941                   OldList * list;
12942                   curContext = function.body.compound.context;
12943
12944                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
12945                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12946                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
12947
12948                   curContext = prevContext;
12949                }
12950
12951                // WHY?
12952                decl.symbol = null;
12953
12954                if(!function.body.compound.declarations)
12955                   function.body.compound.declarations = MkList();
12956                function.body.compound.declarations->Insert(null, decl);
12957             }
12958          }
12959       }
12960
12961
12962       // Loop through the function and replace undeclared identifiers
12963       // which are a member of the class (methods, properties or data)
12964       // by "this.[member]"
12965    }
12966    else
12967       thisClass = null;
12968
12969    if(id)
12970    {
12971       FreeSpecifier(id._class);
12972       id._class = null;
12973
12974       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12975       {
12976          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12977          id = GetDeclId(initDecl.declarator);
12978
12979          FreeSpecifier(id._class);
12980          id._class = null;
12981       }
12982    }
12983    if(function.body)
12984       topContext = function.body.compound.context;
12985    {
12986       FunctionDefinition oldFunction = curFunction;
12987       curFunction = function;
12988       if(function.body)
12989          ProcessStatement(function.body);
12990
12991       // If this is a property set and no firewatchers has been done yet, add one here
12992       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12993       {
12994          Statement prevCompound = curCompound;
12995          Context prevContext = curContext;
12996
12997          Statement fireWatchers = MkFireWatchersStmt(null, null);
12998          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12999          ListAdd(function.body.compound.statements, fireWatchers);
13000
13001          curCompound = function.body;
13002          curContext = function.body.compound.context;
13003
13004          ProcessStatement(fireWatchers);
13005
13006          curContext = prevContext;
13007          curCompound = prevCompound;
13008
13009       }
13010
13011       curFunction = oldFunction;
13012    }
13013
13014    if(function.declarator)
13015    {
13016       ProcessDeclarator(function.declarator, true);
13017    }
13018
13019    topContext = oldTopContext;
13020    thisClass = oldThisClass;
13021 }
13022
13023 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13024 static void ProcessClass(OldList definitions, Symbol symbol)
13025 {
13026    ClassDef def;
13027    External external = curExternal;
13028    Class regClass = symbol ? symbol.registered : null;
13029
13030    // Process all functions
13031    for(def = definitions.first; def; def = def.next)
13032    {
13033       if(def.type == functionClassDef)
13034       {
13035          if(def.function.declarator)
13036             curExternal = def.function.declarator.symbol.pointerExternal;
13037          else
13038             curExternal = external;
13039
13040          ProcessFunction((FunctionDefinition)def.function);
13041       }
13042       else if(def.type == declarationClassDef)
13043       {
13044          if(def.decl.type == instDeclaration)
13045          {
13046             thisClass = regClass;
13047             ProcessInstantiationType(def.decl.inst);
13048             thisClass = null;
13049          }
13050          // Testing this
13051          else
13052          {
13053             Class backThisClass = thisClass;
13054             if(regClass) thisClass = regClass;
13055             ProcessDeclaration(def.decl);
13056             thisClass = backThisClass;
13057          }
13058       }
13059       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13060       {
13061          MemberInit defProperty;
13062
13063          // Add this to the context
13064          Symbol thisSymbol = Symbol
13065          {
13066             string = CopyString("this");
13067             type = regClass ? MkClassType(regClass.fullName) : null;
13068          };
13069          globalContext.symbols.Add((BTNode)thisSymbol);
13070
13071          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13072          {
13073             thisClass = regClass;
13074             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13075             thisClass = null;
13076          }
13077
13078          globalContext.symbols.Remove((BTNode)thisSymbol);
13079          FreeSymbol(thisSymbol);
13080       }
13081       else if(def.type == propertyClassDef && def.propertyDef)
13082       {
13083          PropertyDef prop = def.propertyDef;
13084
13085          // Add this to the context
13086          thisClass = regClass;
13087          if(prop.setStmt)
13088          {
13089             if(regClass)
13090             {
13091                Symbol thisSymbol
13092                {
13093                   string = CopyString("this");
13094                   type = MkClassType(regClass.fullName);
13095                };
13096                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13097             }
13098
13099             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13100             ProcessStatement(prop.setStmt);
13101          }
13102          if(prop.getStmt)
13103          {
13104             if(regClass)
13105             {
13106                Symbol thisSymbol
13107                {
13108                   string = CopyString("this");
13109                   type = MkClassType(regClass.fullName);
13110                };
13111                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13112             }
13113
13114             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13115             ProcessStatement(prop.getStmt);
13116          }
13117          if(prop.issetStmt)
13118          {
13119             if(regClass)
13120             {
13121                Symbol thisSymbol
13122                {
13123                   string = CopyString("this");
13124                   type = MkClassType(regClass.fullName);
13125                };
13126                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13127             }
13128
13129             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13130             ProcessStatement(prop.issetStmt);
13131          }
13132
13133          thisClass = null;
13134       }
13135       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13136       {
13137          PropertyWatch propertyWatch = def.propertyWatch;
13138
13139          thisClass = regClass;
13140          if(propertyWatch.compound)
13141          {
13142             Symbol thisSymbol
13143             {
13144                string = CopyString("this");
13145                type = regClass ? MkClassType(regClass.fullName) : null;
13146             };
13147
13148             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13149
13150             curExternal = null;
13151             ProcessStatement(propertyWatch.compound);
13152          }
13153          thisClass = null;
13154       }
13155    }
13156 }
13157
13158 void DeclareFunctionUtil(External neededBy, const String s)
13159 {
13160    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13161    if(function)
13162    {
13163       char name[1024];
13164       name[0] = 0;
13165       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13166          strcpy(name, "__ecereFunction_");
13167       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13168       DeclareFunction(neededBy, function, name);
13169    }
13170    else if(neededBy)
13171       FindSymbol(s, globalContext, globalContext, false, false);
13172 }
13173
13174 void ComputeDataTypes()
13175 {
13176    External external;
13177
13178    currentClass = null;
13179
13180    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13181
13182    DeclareStruct(null, "ecere::com::Class", false, true);
13183    DeclareStruct(null, "ecere::com::Instance", false, true);
13184    DeclareStruct(null, "ecere::com::Property", false, true);
13185    DeclareStruct(null, "ecere::com::DataMember", false, true);
13186    DeclareStruct(null, "ecere::com::Method", false, true);
13187    DeclareStruct(null, "ecere::com::SerialBuffer", false, true);
13188    DeclareStruct(null, "ecere::com::ClassTemplateArgument", false, true);
13189
13190    DeclareFunctionUtil(null, "eSystem_New");
13191    DeclareFunctionUtil(null, "eSystem_New0");
13192    DeclareFunctionUtil(null, "eSystem_Renew");
13193    DeclareFunctionUtil(null, "eSystem_Renew0");
13194    DeclareFunctionUtil(null, "eSystem_Delete");
13195    DeclareFunctionUtil(null, "eClass_GetProperty");
13196    DeclareFunctionUtil(null, "eClass_SetProperty");
13197    DeclareFunctionUtil(null, "eInstance_FireSelfWatchers");
13198    DeclareFunctionUtil(null, "eInstance_SetMethod");
13199    DeclareFunctionUtil(null, "eInstance_IncRef");
13200    DeclareFunctionUtil(null, "eInstance_StopWatching");
13201    DeclareFunctionUtil(null, "eInstance_Watch");
13202    DeclareFunctionUtil(null, "eInstance_FireWatchers");
13203
13204    for(external = ast->first; external; external = external.next)
13205    {
13206       afterExternal = curExternal = external;
13207       if(external.type == functionExternal)
13208       {
13209          if(memoryGuard)
13210          {
13211             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13212             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13213          }
13214
13215          currentClass = external.function._class;
13216          ProcessFunction(external.function);
13217       }
13218       // There shouldn't be any _class member access here anyways...
13219       else if(external.type == declarationExternal)
13220       {
13221          if(memoryGuard && external.declaration && external.declaration.type == instDeclaration)
13222          {
13223             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13224             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13225          }
13226
13227          currentClass = null;
13228          if(external.declaration)
13229             ProcessDeclaration(external.declaration);
13230       }
13231       else if(external.type == classExternal)
13232       {
13233          ClassDefinition _class = external._class;
13234          currentClass = external.symbol.registered;
13235          if(memoryGuard)
13236          {
13237             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13238             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13239          }
13240          if(_class.definitions)
13241          {
13242             ProcessClass(_class.definitions, _class.symbol);
13243          }
13244          if(inCompiler)
13245          {
13246             // Free class data...
13247             ast->Remove(external);
13248             delete external;
13249          }
13250       }
13251       else if(external.type == nameSpaceExternal)
13252       {
13253          thisNameSpace = external.id.string;
13254       }
13255    }
13256    currentClass = null;
13257    thisNameSpace = null;
13258    curExternal = null;
13259 }