compiler/libec: Fixed 64 bit operations
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case _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 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 > MAXINT64)
205       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
208    return CopyString(temp);
209 }
210
211 public char * PrintUInt(uint64 result)
212 {
213    char temp[100];
214    if(result > MAXDWORD)
215       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
216    else if(result > MAXINT)
217       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
218    else
219       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
220    return CopyString(temp);
221 }
222
223 public char * PrintInt64(int64 result)
224 {
225    char temp[100];
226    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
227    return CopyString(temp);
228 }
229
230 public char * PrintUInt64(uint64 result)
231 {
232    char temp[100];
233    if(result > MAXINT64)
234       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
235    else
236       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
237    return CopyString(temp);
238 }
239
240 public char * PrintHexUInt(uint64 result)
241 {
242    char temp[100];
243    if(result > MAXDWORD)
244       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
245    else
246       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
247    return CopyString(temp);
248 }
249
250 public char * PrintHexUInt64(uint64 result)
251 {
252    char temp[100];
253    if(result > MAXDWORD)
254       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
255    else
256       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
257    return CopyString(temp);
258 }
259
260 public char * PrintShort(short result)
261 {
262    char temp[100];
263    sprintf(temp, "%d", (unsigned short)result);
264    return CopyString(temp);
265 }
266
267 public char * PrintUShort(unsigned short result)
268 {
269    char temp[100];
270    if(result > 32767)
271       sprintf(temp, "0x%X", (int)result);
272    else
273       sprintf(temp, "%d", (int)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintChar(char result)
278 {
279    char temp[100];
280    if(result > 0 && isprint(result))
281       sprintf(temp, "'%c'", result);
282    else if(result < 0)
283       sprintf(temp, "%d", (int)result);
284    else
285       //sprintf(temp, "%#X", result);
286       sprintf(temp, "0x%X", (unsigned char)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintUChar(unsigned char result)
291 {
292    char temp[100];
293    sprintf(temp, "0x%X", result);
294    return CopyString(temp);
295 }
296
297 public char * PrintFloat(float result)
298 {
299    char temp[350];
300    sprintf(temp, "%.16ff", result);
301    return CopyString(temp);
302 }
303
304 public char * PrintDouble(double result)
305 {
306    char temp[350];
307    sprintf(temp, "%.16f", result);
308    return CopyString(temp);
309 }
310
311 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
313
314 //public Operand GetOperand(Expression exp);
315
316 #define GETVALUE(name, t) \
317    public bool Get##name(Expression exp, t * value2) \
318    {                                                        \
319       Operand op2 = GetOperand(exp);                        \
320       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
321       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
322       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
323       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
324       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
325       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
326       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
327       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
328       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
329       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
330       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
331       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
332       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
333       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
334       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
335       else                                                                          \
336          return false;                                                              \
337       return true;                                                                  \
338    }
339
340 // To help the deubugger currently not preprocessing...
341 #define HELP(x) x
342
343 GETVALUE(Int, HELP(int));
344 GETVALUE(UInt, HELP(unsigned int));
345 GETVALUE(Int64, HELP(int64));
346 GETVALUE(UInt64, HELP(uint64));
347 GETVALUE(IntPtr, HELP(intptr));
348 GETVALUE(UIntPtr, HELP(uintptr));
349 GETVALUE(IntSize, HELP(intsize));
350 GETVALUE(UIntSize, HELP(uintsize));
351 GETVALUE(Short, HELP(short));
352 GETVALUE(UShort, HELP(unsigned short));
353 GETVALUE(Char, HELP(char));
354 GETVALUE(UChar, HELP(unsigned char));
355 GETVALUE(Float, HELP(float));
356 GETVALUE(Double, HELP(double));
357
358 void ComputeExpression(Expression exp);
359
360 void ComputeClassMembers(Class _class, bool isMember)
361 {
362    DataMember member = isMember ? (DataMember) _class : null;
363    Context context = isMember ? null : SetupTemplatesContext(_class);
364    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
365                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
366    {
367       int c;
368       int unionMemberOffset = 0;
369       int bitFields = 0;
370
371       /*
372       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
373          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
374       */
375
376       if(member)
377       {
378          member.memberOffset = 0;
379          if(targetBits < sizeof(void *) * 8)
380             member.structAlignment = 0;
381       }
382       else if(targetBits < sizeof(void *) * 8)
383          _class.structAlignment = 0;
384
385       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
386       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
387          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
388
389       if(!member && _class.destructionWatchOffset)
390          _class.memberOffset += sizeof(OldList);
391
392       // To avoid reentrancy...
393       //_class.structSize = -1;
394
395       {
396          DataMember dataMember;
397          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
398          {
399             if(!dataMember.isProperty)
400             {
401                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
402                {
403                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
404                   /*if(!dataMember.dataType)
405                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
406                      */
407                }
408             }
409          }
410       }
411
412       {
413          DataMember dataMember;
414          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
415          {
416             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
417             {
418                if(!isMember && _class.type == bitClass && dataMember.dataType)
419                {
420                   BitMember bitMember = (BitMember) dataMember;
421                   uint64 mask = 0;
422                   int d;
423
424                   ComputeTypeSize(dataMember.dataType);
425
426                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
427                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
428
429                   _class.memberOffset = bitMember.pos + bitMember.size;
430                   for(d = 0; d<bitMember.size; d++)
431                   {
432                      if(d)
433                         mask <<= 1;
434                      mask |= 1;
435                   }
436                   bitMember.mask = mask << bitMember.pos;
437                }
438                else if(dataMember.type == normalMember && dataMember.dataType)
439                {
440                   int size;
441                   int alignment = 0;
442
443                   // Prevent infinite recursion
444                   if(dataMember.dataType.kind != classType ||
445                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
446                      _class.type != structClass)))
447                      ComputeTypeSize(dataMember.dataType);
448
449                   if(dataMember.dataType.bitFieldCount)
450                   {
451                      bitFields += dataMember.dataType.bitFieldCount;
452                      size = 0;
453                   }
454                   else
455                   {
456                      if(bitFields)
457                      {
458                         int size = (bitFields + 7) / 8;
459
460                         if(isMember)
461                         {
462                            // TESTING THIS PADDING CODE
463                            if(alignment)
464                            {
465                               member.structAlignment = Max(member.structAlignment, alignment);
466
467                               if(member.memberOffset % alignment)
468                                  member.memberOffset += alignment - (member.memberOffset % alignment);
469                            }
470
471                            dataMember.offset = member.memberOffset;
472                            if(member.type == unionMember)
473                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
474                            else
475                            {
476                               member.memberOffset += size;
477                            }
478                         }
479                         else
480                         {
481                            // TESTING THIS PADDING CODE
482                            if(alignment)
483                            {
484                               _class.structAlignment = Max(_class.structAlignment, alignment);
485
486                               if(_class.memberOffset % alignment)
487                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
488                            }
489
490                            dataMember.offset = _class.memberOffset;
491                            _class.memberOffset += size;
492                         }
493                         bitFields = 0;
494                      }
495                      size = dataMember.dataType.size;
496                      alignment = dataMember.dataType.alignment;
497                   }
498
499                   if(isMember)
500                   {
501                      // TESTING THIS PADDING CODE
502                      if(alignment)
503                      {
504                         member.structAlignment = Max(member.structAlignment, alignment);
505
506                         if(member.memberOffset % alignment)
507                            member.memberOffset += alignment - (member.memberOffset % alignment);
508                      }
509
510                      dataMember.offset = member.memberOffset;
511                      if(member.type == unionMember)
512                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
513                      else
514                      {
515                         member.memberOffset += size;
516                      }
517                   }
518                   else
519                   {
520                      // TESTING THIS PADDING CODE
521                      if(alignment)
522                      {
523                         _class.structAlignment = Max(_class.structAlignment, alignment);
524
525                         if(_class.memberOffset % alignment)
526                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
527                      }
528
529                      dataMember.offset = _class.memberOffset;
530                      _class.memberOffset += size;
531                   }
532                }
533                else
534                {
535                   int alignment;
536
537                   ComputeClassMembers((Class)dataMember, true);
538                   alignment = dataMember.structAlignment;
539
540                   if(isMember)
541                   {
542                      if(alignment)
543                      {
544                         if(member.memberOffset % alignment)
545                            member.memberOffset += alignment - (member.memberOffset % alignment);
546
547                         member.structAlignment = Max(member.structAlignment, alignment);
548                      }
549                      dataMember.offset = member.memberOffset;
550                      if(member.type == unionMember)
551                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
552                      else
553                         member.memberOffset += dataMember.memberOffset;
554                   }
555                   else
556                   {
557                      if(alignment)
558                      {
559                         if(_class.memberOffset % alignment)
560                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
561                         _class.structAlignment = Max(_class.structAlignment, alignment);
562                      }
563                      dataMember.offset = _class.memberOffset;
564                      _class.memberOffset += dataMember.memberOffset;
565                   }
566                }
567             }
568          }
569          if(bitFields)
570          {
571             int alignment = 0;
572             int size = (bitFields + 7) / 8;
573
574             if(isMember)
575             {
576                // TESTING THIS PADDING CODE
577                if(alignment)
578                {
579                   member.structAlignment = Max(member.structAlignment, alignment);
580
581                   if(member.memberOffset % alignment)
582                      member.memberOffset += alignment - (member.memberOffset % alignment);
583                }
584
585                if(member.type == unionMember)
586                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
587                else
588                {
589                   member.memberOffset += size;
590                }
591             }
592             else
593             {
594                // TESTING THIS PADDING CODE
595                if(alignment)
596                {
597                   _class.structAlignment = Max(_class.structAlignment, alignment);
598
599                   if(_class.memberOffset % alignment)
600                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
601                }
602                _class.memberOffset += size;
603             }
604             bitFields = 0;
605          }
606       }
607       if(member && member.type == unionMember)
608       {
609          member.memberOffset = unionMemberOffset;
610       }
611
612       if(!isMember)
613       {
614          /*if(_class.type == structClass)
615             _class.size = _class.memberOffset;
616          else
617          */
618
619          if(_class.type != bitClass)
620          {
621             int extra = 0;
622             if(_class.structAlignment)
623             {
624                if(_class.memberOffset % _class.structAlignment)
625                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
626             }
627             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
628             if(!member)
629             {
630                Property prop;
631                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
632                {
633                   if(prop.isProperty && prop.isWatchable)
634                   {
635                      prop.watcherOffset = _class.structSize;
636                      _class.structSize += sizeof(OldList);
637                   }
638                }
639             }
640
641             // Fix Derivatives
642             {
643                OldLink derivative;
644                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
645                {
646                   Class deriv = derivative.data;
647
648                   if(deriv.computeSize)
649                   {
650                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
651                      deriv.offset = /*_class.offset + */_class.structSize;
652                      deriv.memberOffset = 0;
653                      // ----------------------
654
655                      deriv.structSize = deriv.offset;
656
657                      ComputeClassMembers(deriv, false);
658                   }
659                }
660             }
661          }
662       }
663    }
664    if(context)
665       FinishTemplatesContext(context);
666 }
667
668 public void ComputeModuleClasses(Module module)
669 {
670    Class _class;
671    OldLink subModule;
672
673    for(subModule = module.modules.first; subModule; subModule = subModule.next)
674       ComputeModuleClasses(subModule.data);
675    for(_class = module.classes.first; _class; _class = _class.next)
676       ComputeClassMembers(_class, false);
677 }
678
679
680 public int ComputeTypeSize(Type type)
681 {
682    uint size = type ? type.size : 0;
683    if(!size && type && !type.computing)
684    {
685       type.computing = true;
686       switch(type.kind)
687       {
688          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
689          case charType: type.alignment = size = sizeof(char); break;
690          case intType: type.alignment = size = sizeof(int); break;
691          case int64Type: type.alignment = size = sizeof(int64); break;
692          case intPtrType: type.alignment = size = targetBits / 8; break;
693          case intSizeType: type.alignment = size = targetBits / 8; break;
694          case longType: type.alignment = size = sizeof(long); break;
695          case shortType: type.alignment = size = sizeof(short); break;
696          case floatType: type.alignment = size = sizeof(float); break;
697          case doubleType: type.alignment = size = sizeof(double); break;
698          case classType:
699          {
700             Class _class = type._class ? type._class.registered : null;
701
702             if(_class && _class.type == structClass)
703             {
704                // Ensure all members are properly registered
705                ComputeClassMembers(_class, false);
706                type.alignment = _class.structAlignment;
707                size = _class.structSize;
708                if(type.alignment && size % type.alignment)
709                   size += type.alignment - (size % type.alignment);
710
711             }
712             else if(_class && (_class.type == unitClass ||
713                    _class.type == enumClass ||
714                    _class.type == bitClass))
715             {
716                if(!_class.dataType)
717                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
718                size = type.alignment = ComputeTypeSize(_class.dataType);
719             }
720             else
721                size = type.alignment = targetBits / 8; // sizeof(Instance *);
722             break;
723          }
724          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
725          case arrayType:
726             if(type.arraySizeExp)
727             {
728                ProcessExpressionType(type.arraySizeExp);
729                ComputeExpression(type.arraySizeExp);
730                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
731                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
732                {
733                   Location oldLoc = yylloc;
734                   // bool isConstant = type.arraySizeExp.isConstant;
735                   char expression[10240];
736                   expression[0] = '\0';
737                   type.arraySizeExp.expType = null;
738                   yylloc = type.arraySizeExp.loc;
739                   if(inCompiler)
740                      PrintExpression(type.arraySizeExp, expression);
741                   Compiler_Error($"Array size not constant int (%s)\n", expression);
742                   yylloc = oldLoc;
743                }
744                GetInt(type.arraySizeExp, &type.arraySize);
745             }
746             else if(type.enumClass)
747             {
748                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
749                {
750                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
751                }
752                else
753                   type.arraySize = 0;
754             }
755             else
756             {
757                // Unimplemented auto size
758                type.arraySize = 0;
759             }
760
761             size = ComputeTypeSize(type.type) * type.arraySize;
762             if(type.type)
763                type.alignment = type.type.alignment;
764
765             break;
766          case structType:
767          {
768             Type member;
769             for(member = type.members.first; member; member = member.next)
770             {
771                uint addSize = ComputeTypeSize(member);
772
773                member.offset = size;
774                if(member.alignment && size % member.alignment)
775                   member.offset += member.alignment - (size % member.alignment);
776                size = member.offset;
777
778                type.alignment = Max(type.alignment, member.alignment);
779                size += addSize;
780             }
781             if(type.alignment && size % type.alignment)
782                size += type.alignment - (size % type.alignment);
783             break;
784          }
785          case unionType:
786          {
787             Type member;
788             for(member = type.members.first; member; member = member.next)
789             {
790                uint addSize = ComputeTypeSize(member);
791
792                member.offset = size;
793                if(member.alignment && size % member.alignment)
794                   member.offset += member.alignment - (size % member.alignment);
795                size = member.offset;
796
797                type.alignment = Max(type.alignment, member.alignment);
798                size = Max(size, addSize);
799             }
800             if(type.alignment && size % type.alignment)
801                size += type.alignment - (size % type.alignment);
802             break;
803          }
804          case templateType:
805          {
806             TemplateParameter param = type.templateParameter;
807             Type baseType = ProcessTemplateParameterType(param);
808             if(baseType)
809             {
810                size = ComputeTypeSize(baseType);
811                type.alignment = baseType.alignment;
812             }
813             else
814                type.alignment = size = sizeof(uint64);
815             break;
816          }
817          case enumType:
818          {
819             type.alignment = size = sizeof(enum { test });
820             break;
821          }
822          case thisClassType:
823          {
824             type.alignment = size = targetBits / 8; //sizeof(void *);
825             break;
826          }
827       }
828       type.size = size;
829       type.computing = false;
830    }
831    return size;
832 }
833
834
835 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
836 {
837    // This function is in need of a major review when implementing private members etc.
838    DataMember topMember = isMember ? (DataMember) _class : null;
839    uint totalSize = 0;
840    uint maxSize = 0;
841    int alignment, size;
842    DataMember member;
843    Context context = isMember ? null : SetupTemplatesContext(_class);
844    if(addedPadding)
845       *addedPadding = false;
846
847    if(!isMember && _class.base)
848    {
849       maxSize = _class.structSize;
850       //if(_class.base.type != systemClass) // Commented out with new Instance _class
851       {
852          // DANGER: Testing this noHeadClass here...
853          if(_class.type == structClass || _class.type == noHeadClass)
854             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
855          else
856          {
857             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
858             if(maxSize > baseSize)
859                maxSize -= baseSize;
860             else
861                maxSize = 0;
862          }
863       }
864    }
865
866    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
867    {
868       if(!member.isProperty)
869       {
870          switch(member.type)
871          {
872             case normalMember:
873             {
874                if(member.dataTypeString)
875                {
876                   OldList * specs = MkList(), * decls = MkList();
877                   Declarator decl;
878
879                   decl = SpecDeclFromString(member.dataTypeString, specs,
880                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
881                   ListAdd(decls, MkStructDeclarator(decl, null));
882                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
883
884                   if(!member.dataType)
885                      member.dataType = ProcessType(specs, decl);
886
887                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
888
889                   {
890                      Type type = ProcessType(specs, decl);
891                      DeclareType(member.dataType, false, false);
892                      FreeType(type);
893                   }
894                   /*
895                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
896                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
897                      DeclareStruct(member.dataType._class.string, false);
898                   */
899
900                   ComputeTypeSize(member.dataType);
901                   size = member.dataType.size;
902                   alignment = member.dataType.alignment;
903
904                   if(alignment)
905                   {
906                      if(totalSize % alignment)
907                         totalSize += alignment - (totalSize % alignment);
908                   }
909                   totalSize += size;
910                }
911                break;
912             }
913             case unionMember:
914             case structMember:
915             {
916                OldList * specs = MkList(), * list = MkList();
917
918                size = 0;
919                AddMembers(list, (Class)member, true, &size, topClass, null);
920                ListAdd(specs,
921                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
922                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
923                alignment = member.structAlignment;
924
925                if(alignment)
926                {
927                   if(totalSize % alignment)
928                      totalSize += alignment - (totalSize % alignment);
929                }
930                totalSize += size;
931                break;
932             }
933          }
934       }
935    }
936    if(retSize)
937    {
938       if(topMember && topMember.type == unionMember)
939          *retSize = Max(*retSize, totalSize);
940       else
941          *retSize += totalSize;
942    }
943    else if(totalSize < maxSize && _class.type != systemClass)
944    {
945       int autoPadding = 0;
946       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
947          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
948       if(totalSize + autoPadding < maxSize)
949       {
950          char sizeString[50];
951          sprintf(sizeString, "%d", maxSize - totalSize);
952          ListAdd(declarations,
953             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
954             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
955          if(addedPadding)
956             *addedPadding = true;
957       }
958    }
959    if(context)
960       FinishTemplatesContext(context);
961    return topMember ? topMember.memberID : _class.memberID;
962 }
963
964 static int DeclareMembers(Class _class, bool isMember)
965 {
966    DataMember topMember = isMember ? (DataMember) _class : null;
967    uint totalSize = 0;
968    DataMember member;
969    Context context = isMember ? null : SetupTemplatesContext(_class);
970
971    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
972       DeclareMembers(_class.base, false);
973
974    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
975    {
976       if(!member.isProperty)
977       {
978          switch(member.type)
979          {
980             case normalMember:
981             {
982                /*
983                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
984                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
985                   DeclareStruct(member.dataType._class.string, false);
986                   */
987                if(!member.dataType && member.dataTypeString)
988                   member.dataType = ProcessTypeString(member.dataTypeString, false);
989                if(member.dataType)
990                   DeclareType(member.dataType, false, false);
991                break;
992             }
993             case unionMember:
994             case structMember:
995             {
996                DeclareMembers((Class)member, true);
997                break;
998             }
999          }
1000       }
1001    }
1002    if(context)
1003       FinishTemplatesContext(context);
1004
1005    return topMember ? topMember.memberID : _class.memberID;
1006 }
1007
1008 void DeclareStruct(char * name, bool skipNoHead)
1009 {
1010    External external = null;
1011    Symbol classSym = FindClass(name);
1012
1013    if(!inCompiler || !classSym) return;
1014
1015    // We don't need any declaration for bit classes...
1016    if(classSym.registered &&
1017       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1018       return;
1019
1020    /*if(classSym.registered.templateClass)
1021       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1022    */
1023
1024    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1025    {
1026       // Add typedef struct
1027       Declaration decl;
1028       OldList * specifiers, * declarators;
1029       OldList * declarations = null;
1030       char structName[1024];
1031       external = (classSym.registered && classSym.registered.type == structClass) ?
1032          classSym.pointerExternal : classSym.structExternal;
1033
1034       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1035       // Moved this one up because DeclareClass done later will need it
1036
1037       classSym.declaring++;
1038
1039       if(strchr(classSym.string, '<'))
1040       {
1041          if(classSym.registered.templateClass)
1042          {
1043             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1044             classSym.declaring--;
1045          }
1046          return;
1047       }
1048
1049       //if(!skipNoHead)
1050          DeclareMembers(classSym.registered, false);
1051
1052       structName[0] = 0;
1053       FullClassNameCat(structName, name, false);
1054
1055       /*if(!external)
1056          external = MkExternalDeclaration(null);*/
1057
1058       if(!skipNoHead)
1059       {
1060          bool addedPadding = false;
1061          classSym.declaredStructSym = true;
1062
1063          declarations = MkList();
1064
1065          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1066
1067          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1068          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1069
1070          if(!declarations->count || (declarations->count == 1 && addedPadding))
1071          {
1072             FreeList(declarations, FreeClassDef);
1073             declarations = null;
1074          }
1075       }
1076       if(skipNoHead || declarations)
1077       {
1078          if(external && external.declaration)
1079          {
1080             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1081
1082             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1083             {
1084                // TODO: Fix this
1085                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1086
1087                // DANGER
1088                if(classSym.structExternal)
1089                   ast->Move(classSym.structExternal, curExternal.prev);
1090                ast->Move(classSym.pointerExternal, curExternal.prev);
1091
1092                classSym.id = curExternal.symbol.idCode;
1093                classSym.idCode = curExternal.symbol.idCode;
1094                // external = classSym.pointerExternal;
1095                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1096             }
1097          }
1098          else
1099          {
1100             if(!external)
1101                external = MkExternalDeclaration(null);
1102
1103             specifiers = MkList();
1104             declarators = MkList();
1105             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1106
1107             /*
1108             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1109             ListAdd(declarators, MkInitDeclarator(d, null));
1110             */
1111             external.declaration = decl = MkDeclaration(specifiers, declarators);
1112             if(decl.symbol && !decl.symbol.pointerExternal)
1113                decl.symbol.pointerExternal = external;
1114
1115             // For simple classes, keep the declaration as the external to move around
1116             if(classSym.registered && classSym.registered.type == structClass)
1117             {
1118                char className[1024];
1119                strcpy(className, "__ecereClass_");
1120                FullClassNameCat(className, classSym.string, true);
1121                MangleClassName(className);
1122
1123                // Testing This
1124                DeclareClass(classSym, className);
1125
1126                external.symbol = classSym;
1127                classSym.pointerExternal = external;
1128                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1129                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1130             }
1131             else
1132             {
1133                char className[1024];
1134                strcpy(className, "__ecereClass_");
1135                FullClassNameCat(className, classSym.string, true);
1136                MangleClassName(className);
1137
1138                // TOFIX: TESTING THIS...
1139                classSym.structExternal = external;
1140                DeclareClass(classSym, className);
1141                external.symbol = classSym;
1142             }
1143
1144             //if(curExternal)
1145                ast->Insert(curExternal ? curExternal.prev : null, external);
1146          }
1147       }
1148
1149       classSym.declaring--;
1150    }
1151    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1152    {
1153       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1154       // Moved this one up because DeclareClass done later will need it
1155
1156       // TESTING THIS:
1157       classSym.declaring++;
1158
1159       //if(!skipNoHead)
1160       {
1161          if(classSym.registered)
1162             DeclareMembers(classSym.registered, false);
1163       }
1164
1165       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1166       {
1167          // TODO: Fix this
1168          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1169
1170          // DANGER
1171          if(classSym.structExternal)
1172             ast->Move(classSym.structExternal, curExternal.prev);
1173          ast->Move(classSym.pointerExternal, curExternal.prev);
1174
1175          classSym.id = curExternal.symbol.idCode;
1176          classSym.idCode = curExternal.symbol.idCode;
1177          // external = classSym.pointerExternal;
1178          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1179       }
1180
1181       classSym.declaring--;
1182    }
1183    //return external;
1184 }
1185
1186 void DeclareProperty(Property prop, char * setName, char * getName)
1187 {
1188    Symbol symbol = prop.symbol;
1189    char propName[1024];
1190
1191    strcpy(setName, "__ecereProp_");
1192    FullClassNameCat(setName, prop._class.fullName, false);
1193    strcat(setName, "_Set_");
1194    // strcat(setName, prop.name);
1195    FullClassNameCat(setName, prop.name, true);
1196
1197    strcpy(getName, "__ecereProp_");
1198    FullClassNameCat(getName, prop._class.fullName, false);
1199    strcat(getName, "_Get_");
1200    FullClassNameCat(getName, prop.name, true);
1201    // strcat(getName, prop.name);
1202
1203    strcpy(propName, "__ecereProp_");
1204    FullClassNameCat(propName, prop._class.fullName, false);
1205    strcat(propName, "_");
1206    FullClassNameCat(propName, prop.name, true);
1207    // strcat(propName, prop.name);
1208
1209    // To support "char *" property
1210    MangleClassName(getName);
1211    MangleClassName(setName);
1212    MangleClassName(propName);
1213
1214    if(prop._class.type == structClass)
1215       DeclareStruct(prop._class.fullName, false);
1216
1217    if(!symbol || curExternal.symbol.idCode < symbol.id)
1218    {
1219       bool imported = false;
1220       bool dllImport = false;
1221       if(!symbol || symbol._import)
1222       {
1223          if(!symbol)
1224          {
1225             Symbol classSym;
1226             if(!prop._class.symbol)
1227                prop._class.symbol = FindClass(prop._class.fullName);
1228             classSym = prop._class.symbol;
1229             if(classSym && !classSym._import)
1230             {
1231                ModuleImport module;
1232
1233                if(prop._class.module)
1234                   module = FindModule(prop._class.module);
1235                else
1236                   module = mainModule;
1237
1238                classSym._import = ClassImport
1239                {
1240                   name = CopyString(prop._class.fullName);
1241                   isRemote = prop._class.isRemote;
1242                };
1243                module.classes.Add(classSym._import);
1244             }
1245             symbol = prop.symbol = Symbol { };
1246             symbol._import = (ClassImport)PropertyImport
1247             {
1248                name = CopyString(prop.name);
1249                isVirtual = false; //prop.isVirtual;
1250                hasSet = prop.Set ? true : false;
1251                hasGet = prop.Get ? true : false;
1252             };
1253             if(classSym)
1254                classSym._import.properties.Add(symbol._import);
1255          }
1256          imported = true;
1257          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1258             dllImport = true;
1259       }
1260
1261       if(!symbol.type)
1262       {
1263          Context context = SetupTemplatesContext(prop._class);
1264          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1265          FinishTemplatesContext(context);
1266       }
1267
1268       // Get
1269       if(prop.Get)
1270       {
1271          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1272          {
1273             Declaration decl;
1274             OldList * specifiers, * declarators;
1275             Declarator d;
1276             OldList * params;
1277             Specifier spec;
1278             External external;
1279             Declarator typeDecl;
1280             bool simple = false;
1281
1282             specifiers = MkList();
1283             declarators = MkList();
1284             params = MkList();
1285
1286             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1287                MkDeclaratorIdentifier(MkIdentifier("this"))));
1288
1289             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1290             //if(imported)
1291             if(dllImport)
1292                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1293
1294             {
1295                Context context = SetupTemplatesContext(prop._class);
1296                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1297                FinishTemplatesContext(context);
1298             }
1299
1300             // Make sure the simple _class's type is declared
1301             for(spec = specifiers->first; spec; spec = spec.next)
1302             {
1303                if(spec.type == nameSpecifier /*SpecifierClass*/)
1304                {
1305                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1306                   {
1307                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1308                      symbol._class = classSym.registered;
1309                      if(classSym.registered && classSym.registered.type == structClass)
1310                      {
1311                         DeclareStruct(spec.name, false);
1312                         simple = true;
1313                      }
1314                   }
1315                }
1316             }
1317
1318             if(!simple)
1319                d = PlugDeclarator(typeDecl, d);
1320             else
1321             {
1322                ListAdd(params, MkTypeName(specifiers,
1323                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1324                specifiers = MkList();
1325             }
1326
1327             d = MkDeclaratorFunction(d, params);
1328
1329             //if(imported)
1330             if(dllImport)
1331                specifiers->Insert(null, MkSpecifier(EXTERN));
1332             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1333                specifiers->Insert(null, MkSpecifier(STATIC));
1334             if(simple)
1335                ListAdd(specifiers, MkSpecifier(VOID));
1336
1337             ListAdd(declarators, MkInitDeclarator(d, null));
1338
1339             decl = MkDeclaration(specifiers, declarators);
1340
1341             external = MkExternalDeclaration(decl);
1342             ast->Insert(curExternal.prev, external);
1343             external.symbol = symbol;
1344             symbol.externalGet = external;
1345
1346             ReplaceThisClassSpecifiers(specifiers, prop._class);
1347
1348             if(typeDecl)
1349                FreeDeclarator(typeDecl);
1350          }
1351          else
1352          {
1353             // Move declaration higher...
1354             ast->Move(symbol.externalGet, curExternal.prev);
1355          }
1356       }
1357
1358       // Set
1359       if(prop.Set)
1360       {
1361          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1362          {
1363             Declaration decl;
1364             OldList * specifiers, * declarators;
1365             Declarator d;
1366             OldList * params;
1367             Specifier spec;
1368             External external;
1369             Declarator typeDecl;
1370
1371             declarators = MkList();
1372             params = MkList();
1373
1374             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1375             if(!prop.conversion || prop._class.type == structClass)
1376             {
1377                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1378                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1379             }
1380
1381             specifiers = MkList();
1382
1383             {
1384                Context context = SetupTemplatesContext(prop._class);
1385                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1386                   MkDeclaratorIdentifier(MkIdentifier("value")));
1387                FinishTemplatesContext(context);
1388             }
1389             ListAdd(params, MkTypeName(specifiers, d));
1390
1391             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1392             //if(imported)
1393             if(dllImport)
1394                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1395             d = MkDeclaratorFunction(d, params);
1396
1397             // Make sure the simple _class's type is declared
1398             for(spec = specifiers->first; spec; spec = spec.next)
1399             {
1400                if(spec.type == nameSpecifier /*SpecifierClass*/)
1401                {
1402                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1403                   {
1404                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1405                      symbol._class = classSym.registered;
1406                      if(classSym.registered && classSym.registered.type == structClass)
1407                         DeclareStruct(spec.name, false);
1408                   }
1409                }
1410             }
1411
1412             ListAdd(declarators, MkInitDeclarator(d, null));
1413
1414             specifiers = MkList();
1415             //if(imported)
1416             if(dllImport)
1417                specifiers->Insert(null, MkSpecifier(EXTERN));
1418             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1419                specifiers->Insert(null, MkSpecifier(STATIC));
1420
1421             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1422             if(!prop.conversion || prop._class.type == structClass)
1423                ListAdd(specifiers, MkSpecifier(VOID));
1424             else
1425                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1426
1427             decl = MkDeclaration(specifiers, declarators);
1428
1429             external = MkExternalDeclaration(decl);
1430             ast->Insert(curExternal.prev, external);
1431             external.symbol = symbol;
1432             symbol.externalSet = external;
1433
1434             ReplaceThisClassSpecifiers(specifiers, prop._class);
1435          }
1436          else
1437          {
1438             // Move declaration higher...
1439             ast->Move(symbol.externalSet, curExternal.prev);
1440          }
1441       }
1442
1443       // Property (for Watchers)
1444       if(!symbol.externalPtr)
1445       {
1446          Declaration decl;
1447          External external;
1448          OldList * specifiers = MkList();
1449
1450          if(imported)
1451             specifiers->Insert(null, MkSpecifier(EXTERN));
1452          else
1453             specifiers->Insert(null, MkSpecifier(STATIC));
1454
1455          ListAdd(specifiers, MkSpecifierName("Property"));
1456
1457          {
1458             OldList * list = MkList();
1459             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1460                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1461
1462             if(!imported)
1463             {
1464                strcpy(propName, "__ecerePropM_");
1465                FullClassNameCat(propName, prop._class.fullName, false);
1466                strcat(propName, "_");
1467                // strcat(propName, prop.name);
1468                FullClassNameCat(propName, prop.name, true);
1469
1470                MangleClassName(propName);
1471
1472                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1473                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1474             }
1475             decl = MkDeclaration(specifiers, list);
1476          }
1477
1478          external = MkExternalDeclaration(decl);
1479          ast->Insert(curExternal.prev, external);
1480          external.symbol = symbol;
1481          symbol.externalPtr = external;
1482       }
1483       else
1484       {
1485          // Move declaration higher...
1486          ast->Move(symbol.externalPtr, curExternal.prev);
1487       }
1488
1489       symbol.id = curExternal.symbol.idCode;
1490    }
1491 }
1492
1493 // ***************** EXPRESSION PROCESSING ***************************
1494 public Type Dereference(Type source)
1495 {
1496    Type type = null;
1497    if(source)
1498    {
1499       if(source.kind == pointerType || source.kind == arrayType)
1500       {
1501          type = source.type;
1502          source.type.refCount++;
1503       }
1504       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1505       {
1506          type = Type
1507          {
1508             kind = charType;
1509             refCount = 1;
1510          };
1511       }
1512       // Support dereferencing of no head classes for now...
1513       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1514       {
1515          type = source;
1516          source.refCount++;
1517       }
1518       else
1519          Compiler_Error($"cannot dereference type\n");
1520    }
1521    return type;
1522 }
1523
1524 static Type Reference(Type source)
1525 {
1526    Type type = null;
1527    if(source)
1528    {
1529       type = Type
1530       {
1531          kind = pointerType;
1532          type = source;
1533          refCount = 1;
1534       };
1535       source.refCount++;
1536    }
1537    return type;
1538 }
1539
1540 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1541 {
1542    Identifier ident = member.identifiers ? member.identifiers->first : null;
1543    bool found = false;
1544    DataMember dataMember = null;
1545    Method method = null;
1546    bool freeType = false;
1547
1548    yylloc = member.loc;
1549
1550    if(!ident)
1551    {
1552       if(curMember)
1553       {
1554          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1555          if(*curMember)
1556          {
1557             found = true;
1558             dataMember = *curMember;
1559          }
1560       }
1561    }
1562    else
1563    {
1564       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1565       DataMember _subMemberStack[256];
1566       int _subMemberStackPos = 0;
1567
1568       // FILL MEMBER STACK
1569       if(!thisMember)
1570          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1571       if(thisMember)
1572       {
1573          dataMember = thisMember;
1574          if(curMember && thisMember.memberAccess == publicAccess)
1575          {
1576             *curMember = thisMember;
1577             *curClass = thisMember._class;
1578             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1579             *subMemberStackPos = _subMemberStackPos;
1580          }
1581          found = true;
1582       }
1583       else
1584       {
1585          // Setting a method
1586          method = eClass_FindMethod(_class, ident.string, privateModule);
1587          if(method && method.type == virtualMethod)
1588             found = true;
1589          else
1590             method = null;
1591       }
1592    }
1593
1594    if(found)
1595    {
1596       Type type = null;
1597       if(dataMember)
1598       {
1599          if(!dataMember.dataType && dataMember.dataTypeString)
1600          {
1601             //Context context = SetupTemplatesContext(dataMember._class);
1602             Context context = SetupTemplatesContext(_class);
1603             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1604             FinishTemplatesContext(context);
1605          }
1606          type = dataMember.dataType;
1607       }
1608       else if(method)
1609       {
1610          // This is for destination type...
1611          if(!method.dataType)
1612             ProcessMethodType(method);
1613          //DeclareMethod(method);
1614          // method.dataType = ((Symbol)method.symbol)->type;
1615          type = method.dataType;
1616       }
1617
1618       if(ident && ident.next)
1619       {
1620          for(ident = ident.next; ident && type; ident = ident.next)
1621          {
1622             if(type.kind == classType)
1623             {
1624                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1625                if(!dataMember)
1626                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1627                if(dataMember)
1628                   type = dataMember.dataType;
1629             }
1630             else if(type.kind == structType || type.kind == unionType)
1631             {
1632                Type memberType;
1633                for(memberType = type.members.first; memberType; memberType = memberType.next)
1634                {
1635                   if(!strcmp(memberType.name, ident.string))
1636                   {
1637                      type = memberType;
1638                      break;
1639                   }
1640                }
1641             }
1642          }
1643       }
1644
1645       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1646       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1647       {
1648          int id = 0;
1649          ClassTemplateParameter curParam = null;
1650          Class sClass;
1651          for(sClass = _class; sClass; sClass = sClass.base)
1652          {
1653             id = 0;
1654             if(sClass.templateClass) sClass = sClass.templateClass;
1655             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1656             {
1657                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1658                {
1659                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1660                   {
1661                      if(sClass.templateClass) sClass = sClass.templateClass;
1662                      id += sClass.templateParams.count;
1663                   }
1664                   break;
1665                }
1666                id++;
1667             }
1668             if(curParam) break;
1669          }
1670
1671          if(curParam)
1672          {
1673             ClassTemplateArgument arg = _class.templateArgs[id];
1674             if(arg.dataTypeString)
1675             {
1676                // FreeType(type);
1677                type = ProcessTypeString(arg.dataTypeString, false);
1678                freeType = true;
1679                if(type && _class.templateClass)
1680                   type.passAsTemplate = true;
1681                if(type)
1682                {
1683                   // type.refCount++;
1684                   /*if(!exp.destType)
1685                   {
1686                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1687                      exp.destType.refCount++;
1688                   }*/
1689                }
1690             }
1691          }
1692       }
1693       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1694       {
1695          Class expClass = type._class.registered;
1696          Class cClass = null;
1697          int c;
1698          int paramCount = 0;
1699          int lastParam = -1;
1700
1701          char templateString[1024];
1702          ClassTemplateParameter param;
1703          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1704          for(cClass = expClass; cClass; cClass = cClass.base)
1705          {
1706             int p = 0;
1707             if(cClass.templateClass) cClass = cClass.templateClass;
1708             for(param = cClass.templateParams.first; param; param = param.next)
1709             {
1710                int id = p;
1711                Class sClass;
1712                ClassTemplateArgument arg;
1713                for(sClass = cClass.base; sClass; sClass = sClass.base)
1714                {
1715                   if(sClass.templateClass) sClass = sClass.templateClass;
1716                   id += sClass.templateParams.count;
1717                }
1718                arg = expClass.templateArgs[id];
1719
1720                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1721                {
1722                   ClassTemplateParameter cParam;
1723                   //int p = numParams - sClass.templateParams.count;
1724                   int p = 0;
1725                   Class nextClass;
1726                   if(sClass.templateClass) sClass = sClass.templateClass;
1727
1728                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1729                   {
1730                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1731                      p += nextClass.templateParams.count;
1732                   }
1733
1734                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1735                   {
1736                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1737                      {
1738                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1739                         {
1740                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1741                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1742                            break;
1743                         }
1744                      }
1745                   }
1746                }
1747
1748                {
1749                   char argument[256];
1750                   argument[0] = '\0';
1751                   /*if(arg.name)
1752                   {
1753                      strcat(argument, arg.name.string);
1754                      strcat(argument, " = ");
1755                   }*/
1756                   switch(param.type)
1757                   {
1758                      case expression:
1759                      {
1760                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1761                         char expString[1024];
1762                         OldList * specs = MkList();
1763                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1764                         Expression exp;
1765                         char * string = PrintHexUInt64(arg.expression.ui64);
1766                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1767                         delete string;
1768
1769                         ProcessExpressionType(exp);
1770                         ComputeExpression(exp);
1771                         expString[0] = '\0';
1772                         PrintExpression(exp, expString);
1773                         strcat(argument, expString);
1774                         //delete exp;
1775                         FreeExpression(exp);
1776                         break;
1777                      }
1778                      case identifier:
1779                      {
1780                         strcat(argument, arg.member.name);
1781                         break;
1782                      }
1783                      case TemplateParameterType::type:
1784                      {
1785                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1786                            strcat(argument, arg.dataTypeString);
1787                         break;
1788                      }
1789                   }
1790                   if(argument[0])
1791                   {
1792                      if(paramCount) strcat(templateString, ", ");
1793                      if(lastParam != p - 1)
1794                      {
1795                         strcat(templateString, param.name);
1796                         strcat(templateString, " = ");
1797                      }
1798                      strcat(templateString, argument);
1799                      paramCount++;
1800                      lastParam = p;
1801                   }
1802                   p++;
1803                }
1804             }
1805          }
1806          {
1807             int len = strlen(templateString);
1808             if(templateString[len-1] == '<')
1809                len--;
1810             else
1811             {
1812                if(templateString[len-1] == '>')
1813                   templateString[len++] = ' ';
1814                templateString[len++] = '>';
1815             }
1816             templateString[len++] = '\0';
1817          }
1818          {
1819             Context context = SetupTemplatesContext(_class);
1820             if(freeType) FreeType(type);
1821             type = ProcessTypeString(templateString, false);
1822             freeType = true;
1823             FinishTemplatesContext(context);
1824          }
1825       }
1826
1827       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1828       {
1829          ProcessExpressionType(member.initializer.exp);
1830          if(!member.initializer.exp.expType)
1831          {
1832             if(inCompiler)
1833             {
1834                char expString[10240];
1835                expString[0] = '\0';
1836                PrintExpression(member.initializer.exp, expString);
1837                ChangeCh(expString, '\n', ' ');
1838                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1839             }
1840          }
1841          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1842          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1843          {
1844             Compiler_Error($"incompatible instance method %s\n", ident.string);
1845          }
1846       }
1847       else if(member.initializer)
1848       {
1849          /*
1850          FreeType(member.exp.destType);
1851          member.exp.destType = type;
1852          if(member.exp.destType)
1853             member.exp.destType.refCount++;
1854          ProcessExpressionType(member.exp);
1855          */
1856
1857          ProcessInitializer(member.initializer, type);
1858       }
1859       if(freeType) FreeType(type);
1860    }
1861    else
1862    {
1863       if(_class && _class.type == unitClass)
1864       {
1865          if(member.initializer)
1866          {
1867             /*
1868             FreeType(member.exp.destType);
1869             member.exp.destType = MkClassType(_class.fullName);
1870             ProcessExpressionType(member.initializer, type);
1871             */
1872             Type type = MkClassType(_class.fullName);
1873             ProcessInitializer(member.initializer, type);
1874             FreeType(type);
1875          }
1876       }
1877       else
1878       {
1879          if(member.initializer)
1880          {
1881             //ProcessExpressionType(member.exp);
1882             ProcessInitializer(member.initializer, null);
1883          }
1884          if(ident)
1885          {
1886             if(method)
1887             {
1888                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1889             }
1890             else if(_class)
1891             {
1892                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1893                if(inCompiler)
1894                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1895             }
1896          }
1897          else if(_class)
1898             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1899       }
1900    }
1901 }
1902
1903 void ProcessInstantiationType(Instantiation inst)
1904 {
1905    yylloc = inst.loc;
1906    if(inst._class)
1907    {
1908       MembersInit members;
1909       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1910       Class _class;
1911
1912       /*if(!inst._class.symbol)
1913          inst._class.symbol = FindClass(inst._class.name);*/
1914       classSym = inst._class.symbol;
1915       _class = classSym ? classSym.registered : null;
1916
1917       // DANGER: Patch for mutex not declaring its struct when not needed
1918       if(!_class || _class.type != noHeadClass)
1919          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1920
1921       afterExternal = afterExternal ? afterExternal : curExternal;
1922
1923       if(inst.exp)
1924          ProcessExpressionType(inst.exp);
1925
1926       inst.isConstant = true;
1927       if(inst.members)
1928       {
1929          DataMember curMember = null;
1930          Class curClass = null;
1931          DataMember subMemberStack[256];
1932          int subMemberStackPos = 0;
1933
1934          for(members = inst.members->first; members; members = members.next)
1935          {
1936             switch(members.type)
1937             {
1938                case methodMembersInit:
1939                {
1940                   char name[1024];
1941                   static uint instMethodID = 0;
1942                   External external = curExternal;
1943                   Context context = curContext;
1944                   Declarator declarator = members.function.declarator;
1945                   Identifier nameID = GetDeclId(declarator);
1946                   char * unmangled = nameID ? nameID.string : null;
1947                   Expression exp;
1948                   External createdExternal = null;
1949
1950                   if(inCompiler)
1951                   {
1952                      char number[16];
1953                      //members.function.dontMangle = true;
1954                      strcpy(name, "__ecereInstMeth_");
1955                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1956                      strcat(name, "_");
1957                      strcat(name, nameID.string);
1958                      strcat(name, "_");
1959                      sprintf(number, "_%08d", instMethodID++);
1960                      strcat(name, number);
1961                      nameID.string = CopyString(name);
1962                   }
1963
1964                   // Do modifications here...
1965                   if(declarator)
1966                   {
1967                      Symbol symbol = declarator.symbol;
1968                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1969
1970                      if(method && method.type == virtualMethod)
1971                      {
1972                         symbol.method = method;
1973                         ProcessMethodType(method);
1974
1975                         if(!symbol.type.thisClass)
1976                         {
1977                            if(method.dataType.thisClass && currentClass &&
1978                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1979                            {
1980                               if(!currentClass.symbol)
1981                                  currentClass.symbol = FindClass(currentClass.fullName);
1982                               symbol.type.thisClass = currentClass.symbol;
1983                            }
1984                            else
1985                            {
1986                               if(!_class.symbol)
1987                                  _class.symbol = FindClass(_class.fullName);
1988                               symbol.type.thisClass = _class.symbol;
1989                            }
1990                         }
1991                         // TESTING THIS HERE:
1992                         DeclareType(symbol.type, true, true);
1993
1994                      }
1995                      else if(classSym)
1996                      {
1997                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1998                            unmangled, classSym.string);
1999                      }
2000                   }
2001
2002                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2003                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2004
2005                   if(nameID)
2006                   {
2007                      FreeSpecifier(nameID._class);
2008                      nameID._class = null;
2009                   }
2010
2011                   if(inCompiler)
2012                   {
2013
2014                      Type type = declarator.symbol.type;
2015                      External oldExternal = curExternal;
2016
2017                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2018                      // *** It was commented out for problems such as
2019                      /*
2020                            class VirtualDesktop : Window
2021                            {
2022                               clientSize = Size { };
2023                               Timer timer
2024                               {
2025                                  bool DelayExpired()
2026                                  {
2027                                     clientSize.w;
2028                                     return true;
2029                                  }
2030                               };
2031                            }
2032                      */
2033                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2034
2035                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2036
2037                      /*
2038                      if(strcmp(declarator.symbol.string, name))
2039                      {
2040                         printf("TOCHECK: Look out for this\n");
2041                         delete declarator.symbol.string;
2042                         declarator.symbol.string = CopyString(name);
2043                      }
2044
2045                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2046                      {
2047                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2048                         excludedSymbols->Remove(declarator.symbol);
2049                         globalContext.symbols.Add((BTNode)declarator.symbol);
2050                         if(strstr(declarator.symbol.string), "::")
2051                            globalContext.hasNameSpace = true;
2052
2053                      }
2054                      */
2055
2056                      //curExternal = curExternal.prev;
2057                      //afterExternal = afterExternal->next;
2058
2059                      //ProcessFunction(afterExternal->function);
2060
2061                      //curExternal = afterExternal;
2062                      {
2063                         External externalDecl;
2064                         externalDecl = MkExternalDeclaration(null);
2065                         ast->Insert(oldExternal.prev, externalDecl);
2066
2067                         // Which function does this process?
2068                         if(createdExternal.function)
2069                         {
2070                            ProcessFunction(createdExternal.function);
2071
2072                            //curExternal = oldExternal;
2073
2074                            {
2075                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076
2077                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2078                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2079
2080                               //externalDecl = MkExternalDeclaration(decl);
2081
2082                               //***** ast->Insert(external.prev, externalDecl);
2083                               //ast->Insert(curExternal.prev, externalDecl);
2084                               externalDecl.declaration = decl;
2085                               if(decl.symbol && !decl.symbol.pointerExternal)
2086                                  decl.symbol.pointerExternal = externalDecl;
2087
2088                               // Trying this out...
2089                               declarator.symbol.pointerExternal = externalDecl;
2090                            }
2091                         }
2092                      }
2093                   }
2094                   else if(declarator)
2095                   {
2096                      curExternal = declarator.symbol.pointerExternal;
2097                      ProcessFunction((FunctionDefinition)members.function);
2098                   }
2099                   curExternal = external;
2100                   curContext = context;
2101
2102                   if(inCompiler)
2103                   {
2104                      FreeClassFunction(members.function);
2105
2106                      // In this pass, turn this into a MemberInitData
2107                      exp = QMkExpId(name);
2108                      members.type = dataMembersInit;
2109                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2110
2111                      delete unmangled;
2112                   }
2113                   break;
2114                }
2115                case dataMembersInit:
2116                {
2117                   if(members.dataMembers && classSym)
2118                   {
2119                      MemberInit member;
2120                      Location oldyyloc = yylloc;
2121                      for(member = members.dataMembers->first; member; member = member.next)
2122                      {
2123                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2124                         if(member.initializer && !member.initializer.isConstant)
2125                            inst.isConstant = false;
2126                      }
2127                      yylloc = oldyyloc;
2128                   }
2129                   break;
2130                }
2131             }
2132          }
2133       }
2134    }
2135 }
2136
2137 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2138 {
2139    // OPTIMIZATIONS: TESTING THIS...
2140    if(inCompiler)
2141    {
2142       if(type.kind == functionType)
2143       {
2144          Type param;
2145          if(declareParams)
2146          {
2147             for(param = type.params.first; param; param = param.next)
2148                DeclareType(param, declarePointers, true);
2149          }
2150          DeclareType(type.returnType, declarePointers, true);
2151       }
2152       else if(type.kind == pointerType && declarePointers)
2153          DeclareType(type.type, declarePointers, false);
2154       else if(type.kind == classType)
2155       {
2156          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2157             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2158       }
2159       else if(type.kind == structType || type.kind == unionType)
2160       {
2161          Type member;
2162          for(member = type.members.first; member; member = member.next)
2163             DeclareType(member, false, false);
2164       }
2165       else if(type.kind == arrayType)
2166          DeclareType(type.arrayType, declarePointers, false);
2167    }
2168 }
2169
2170 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2171 {
2172    ClassTemplateArgument * arg = null;
2173    int id = 0;
2174    ClassTemplateParameter curParam = null;
2175    Class sClass;
2176    for(sClass = _class; sClass; sClass = sClass.base)
2177    {
2178       id = 0;
2179       if(sClass.templateClass) sClass = sClass.templateClass;
2180       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2181       {
2182          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2183          {
2184             for(sClass = sClass.base; sClass; sClass = sClass.base)
2185             {
2186                if(sClass.templateClass) sClass = sClass.templateClass;
2187                id += sClass.templateParams.count;
2188             }
2189             break;
2190          }
2191          id++;
2192       }
2193       if(curParam) break;
2194    }
2195    if(curParam)
2196    {
2197       arg = &_class.templateArgs[id];
2198       if(arg && param.type == type)
2199          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2200    }
2201    return arg;
2202 }
2203
2204 public Context SetupTemplatesContext(Class _class)
2205 {
2206    Context context = PushContext();
2207    context.templateTypesOnly = true;
2208    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2209    {
2210       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2211       for(; param; param = param.next)
2212       {
2213          if(param.type == type && param.identifier)
2214          {
2215             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2216             curContext.templateTypes.Add((BTNode)type);
2217          }
2218       }
2219    }
2220    else if(_class)
2221    {
2222       Class sClass;
2223       for(sClass = _class; sClass; sClass = sClass.base)
2224       {
2225          ClassTemplateParameter p;
2226          for(p = sClass.templateParams.first; p; p = p.next)
2227          {
2228             //OldList * specs = MkList();
2229             //Declarator decl = null;
2230             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2231             if(p.type == type)
2232             {
2233                TemplateParameter param = p.param;
2234                TemplatedType type;
2235                if(!param)
2236                {
2237                   // ADD DATA TYPE HERE...
2238                   p.param = param = TemplateParameter
2239                   {
2240                      identifier = MkIdentifier(p.name), type = p.type,
2241                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2242                   };
2243                }
2244                type = TemplatedType { key = (uintptr)p.name, param = param };
2245                curContext.templateTypes.Add((BTNode)type);
2246             }
2247          }
2248       }
2249    }
2250    return context;
2251 }
2252
2253 public void FinishTemplatesContext(Context context)
2254 {
2255    PopContext(context);
2256    FreeContext(context);
2257    delete context;
2258 }
2259
2260 public void ProcessMethodType(Method method)
2261 {
2262    if(!method.dataType)
2263    {
2264       Context context = SetupTemplatesContext(method._class);
2265
2266       method.dataType = ProcessTypeString(method.dataTypeString, false);
2267
2268       FinishTemplatesContext(context);
2269
2270       if(method.type != virtualMethod && method.dataType)
2271       {
2272          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2273          {
2274             if(!method._class.symbol)
2275                method._class.symbol = FindClass(method._class.fullName);
2276             method.dataType.thisClass = method._class.symbol;
2277          }
2278       }
2279
2280       // Why was this commented out? Working fine without now...
2281
2282       /*
2283       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2284          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2285          */
2286    }
2287
2288    /*
2289    if(type)
2290    {
2291       char * par = strstr(type, "(");
2292       char * classOp = null;
2293       int classOpLen = 0;
2294       if(par)
2295       {
2296          int c;
2297          for(c = par-type-1; c >= 0; c++)
2298          {
2299             if(type[c] == ':' && type[c+1] == ':')
2300             {
2301                classOp = type + c - 1;
2302                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2303                {
2304                   classOp--;
2305                   classOpLen++;
2306                }
2307                break;
2308             }
2309             else if(!isspace(type[c]))
2310                break;
2311          }
2312       }
2313       if(classOp)
2314       {
2315          char temp[1024];
2316          int typeLen = strlen(type);
2317          memcpy(temp, classOp, classOpLen);
2318          temp[classOpLen] = '\0';
2319          if(temp[0])
2320             _class = eSystem_FindClass(module, temp);
2321          else
2322             _class = null;
2323          method.dataTypeString = new char[typeLen - classOpLen + 1];
2324          memcpy(method.dataTypeString, type, classOp - type);
2325          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2326       }
2327       else
2328          method.dataTypeString = type;
2329    }
2330    */
2331 }
2332
2333
2334 public void ProcessPropertyType(Property prop)
2335 {
2336    if(!prop.dataType)
2337    {
2338       Context context = SetupTemplatesContext(prop._class);
2339       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2340       FinishTemplatesContext(context);
2341    }
2342 }
2343
2344 public void DeclareMethod(Method method, char * name)
2345 {
2346    Symbol symbol = method.symbol;
2347    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2348    {
2349       bool imported = false;
2350       bool dllImport = false;
2351
2352       if(!method.dataType)
2353          method.dataType = ProcessTypeString(method.dataTypeString, false);
2354
2355       if(!symbol || symbol._import || method.type == virtualMethod)
2356       {
2357          if(!symbol || method.type == virtualMethod)
2358          {
2359             Symbol classSym;
2360             if(!method._class.symbol)
2361                method._class.symbol = FindClass(method._class.fullName);
2362             classSym = method._class.symbol;
2363             if(!classSym._import)
2364             {
2365                ModuleImport module;
2366
2367                if(method._class.module && method._class.module.name)
2368                   module = FindModule(method._class.module);
2369                else
2370                   module = mainModule;
2371                classSym._import = ClassImport
2372                {
2373                   name = CopyString(method._class.fullName);
2374                   isRemote = method._class.isRemote;
2375                };
2376                module.classes.Add(classSym._import);
2377             }
2378             if(!symbol)
2379             {
2380                symbol = method.symbol = Symbol { };
2381             }
2382             if(!symbol._import)
2383             {
2384                symbol._import = (ClassImport)MethodImport
2385                {
2386                   name = CopyString(method.name);
2387                   isVirtual = method.type == virtualMethod;
2388                };
2389                classSym._import.methods.Add(symbol._import);
2390             }
2391             if(!symbol)
2392             {
2393                // Set the symbol type
2394                /*
2395                if(!type.thisClass)
2396                {
2397                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2398                }
2399                else if(type.thisClass == (void *)-1)
2400                {
2401                   type.thisClass = null;
2402                }
2403                */
2404                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2405                symbol.type = method.dataType;
2406                if(symbol.type) symbol.type.refCount++;
2407             }
2408             /*
2409             if(!method.thisClass || strcmp(method.thisClass, "void"))
2410                symbol.type.params.Insert(null,
2411                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2412             */
2413          }
2414          if(!method.dataType.dllExport)
2415          {
2416             imported = true;
2417             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2418                dllImport = true;
2419          }
2420       }
2421
2422       /* MOVING THIS UP
2423       if(!method.dataType)
2424          method.dataType = ((Symbol)method.symbol).type;
2425          //ProcessMethodType(method);
2426       */
2427
2428       if(method.type != virtualMethod && method.dataType)
2429          DeclareType(method.dataType, true, true);
2430
2431       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2432       {
2433          // We need a declaration here :)
2434          Declaration decl;
2435          OldList * specifiers, * declarators;
2436          Declarator d;
2437          Declarator funcDecl;
2438          External external;
2439
2440          specifiers = MkList();
2441          declarators = MkList();
2442
2443          //if(imported)
2444          if(dllImport)
2445             ListAdd(specifiers, MkSpecifier(EXTERN));
2446          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2447             ListAdd(specifiers, MkSpecifier(STATIC));
2448
2449          if(method.type == virtualMethod)
2450          {
2451             ListAdd(specifiers, MkSpecifier(INT));
2452             d = MkDeclaratorIdentifier(MkIdentifier(name));
2453          }
2454          else
2455          {
2456             d = MkDeclaratorIdentifier(MkIdentifier(name));
2457             //if(imported)
2458             if(dllImport)
2459                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2460             {
2461                Context context = SetupTemplatesContext(method._class);
2462                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2463                FinishTemplatesContext(context);
2464             }
2465             funcDecl = GetFuncDecl(d);
2466
2467             if(dllImport)
2468             {
2469                Specifier spec, next;
2470                for(spec = specifiers->first; spec; spec = next)
2471                {
2472                   next = spec.next;
2473                   if(spec.type == extendedSpecifier)
2474                   {
2475                      specifiers->Remove(spec);
2476                      FreeSpecifier(spec);
2477                   }
2478                }
2479             }
2480
2481             // Add this parameter if not a static method
2482             if(method.dataType && !method.dataType.staticMethod)
2483             {
2484                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2485                {
2486                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2487                   TypeName thisParam = MkTypeName(MkListOne(
2488                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2489                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2490                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2491                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2492
2493                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2494                   {
2495                      TypeName param = funcDecl.function.parameters->first;
2496                      funcDecl.function.parameters->Remove(param);
2497                      FreeTypeName(param);
2498                   }
2499
2500                   if(!funcDecl.function.parameters)
2501                      funcDecl.function.parameters = MkList();
2502                   funcDecl.function.parameters->Insert(null, thisParam);
2503                }
2504             }
2505             // Make sure we don't have empty parameter declarations for static methods...
2506             /*
2507             else if(!funcDecl.function.parameters)
2508             {
2509                funcDecl.function.parameters = MkList();
2510                funcDecl.function.parameters->Insert(null,
2511                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2512             }*/
2513          }
2514          // TESTING THIS:
2515          ProcessDeclarator(d);
2516
2517          ListAdd(declarators, MkInitDeclarator(d, null));
2518
2519          decl = MkDeclaration(specifiers, declarators);
2520
2521          ReplaceThisClassSpecifiers(specifiers, method._class);
2522
2523          // Keep a different symbol for the function definition than the declaration...
2524          if(symbol.pointerExternal)
2525          {
2526             Symbol functionSymbol { };
2527
2528             // Copy symbol
2529             {
2530                *functionSymbol = *symbol;
2531                functionSymbol.string = CopyString(symbol.string);
2532                if(functionSymbol.type)
2533                   functionSymbol.type.refCount++;
2534             }
2535
2536             excludedSymbols->Add(functionSymbol);
2537             symbol.pointerExternal.symbol = functionSymbol;
2538          }
2539          external = MkExternalDeclaration(decl);
2540          if(curExternal)
2541             ast->Insert(curExternal ? curExternal.prev : null, external);
2542          external.symbol = symbol;
2543          symbol.pointerExternal = external;
2544       }
2545       else if(ast)
2546       {
2547          // Move declaration higher...
2548          ast->Move(symbol.pointerExternal, curExternal.prev);
2549       }
2550
2551       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2552    }
2553 }
2554
2555 char * ReplaceThisClass(Class _class)
2556 {
2557    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2558    {
2559       bool first = true;
2560       int p = 0;
2561       ClassTemplateParameter param;
2562       int lastParam = -1;
2563
2564       char className[1024];
2565       strcpy(className, _class.fullName);
2566       for(param = _class.templateParams.first; param; param = param.next)
2567       {
2568          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2569          {
2570             if(first) strcat(className, "<");
2571             if(!first) strcat(className, ", ");
2572             if(lastParam + 1 != p)
2573             {
2574                strcat(className, param.name);
2575                strcat(className, " = ");
2576             }
2577             strcat(className, param.name);
2578             first = false;
2579             lastParam = p;
2580          }
2581          p++;
2582       }
2583       if(!first)
2584       {
2585          int len = strlen(className);
2586          if(className[len-1] == '>') className[len++] = ' ';
2587          className[len++] = '>';
2588          className[len++] = '\0';
2589       }
2590       return CopyString(className);
2591    }
2592    else
2593       return CopyString(_class.fullName);
2594 }
2595
2596 Type ReplaceThisClassType(Class _class)
2597 {
2598    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2599    {
2600       bool first = true;
2601       int p = 0;
2602       ClassTemplateParameter param;
2603       int lastParam = -1;
2604       char className[1024];
2605       strcpy(className, _class.fullName);
2606
2607       for(param = _class.templateParams.first; param; param = param.next)
2608       {
2609          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2610          {
2611             if(first) strcat(className, "<");
2612             if(!first) strcat(className, ", ");
2613             if(lastParam + 1 != p)
2614             {
2615                strcat(className, param.name);
2616                strcat(className, " = ");
2617             }
2618             strcat(className, param.name);
2619             first = false;
2620             lastParam = p;
2621          }
2622          p++;
2623       }
2624       if(!first)
2625       {
2626          int len = strlen(className);
2627          if(className[len-1] == '>') className[len++] = ' ';
2628          className[len++] = '>';
2629          className[len++] = '\0';
2630       }
2631       return MkClassType(className);
2632       //return ProcessTypeString(className, false);
2633    }
2634    else
2635    {
2636       return MkClassType(_class.fullName);
2637       //return ProcessTypeString(_class.fullName, false);
2638    }
2639 }
2640
2641 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2642 {
2643    if(specs != null && _class)
2644    {
2645       Specifier spec;
2646       for(spec = specs.first; spec; spec = spec.next)
2647       {
2648          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2649          {
2650             spec.type = nameSpecifier;
2651             spec.name = ReplaceThisClass(_class);
2652             spec.symbol = FindClass(spec.name); //_class.symbol;
2653          }
2654       }
2655    }
2656 }
2657
2658 // Returns imported or not
2659 bool DeclareFunction(GlobalFunction function, char * name)
2660 {
2661    Symbol symbol = function.symbol;
2662    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2663    {
2664       bool imported = false;
2665       bool dllImport = false;
2666
2667       if(!function.dataType)
2668       {
2669          function.dataType = ProcessTypeString(function.dataTypeString, false);
2670          if(!function.dataType.thisClass)
2671             function.dataType.staticMethod = true;
2672       }
2673
2674       if(inCompiler)
2675       {
2676          if(!symbol)
2677          {
2678             ModuleImport module = FindModule(function.module);
2679             // WARNING: This is not added anywhere...
2680             symbol = function.symbol = Symbol {  };
2681
2682             if(module.name)
2683             {
2684                if(!function.dataType.dllExport)
2685                {
2686                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2687                   module.functions.Add(symbol._import);
2688                }
2689             }
2690             // Set the symbol type
2691             {
2692                symbol.type = ProcessTypeString(function.dataTypeString, false);
2693                if(!symbol.type.thisClass)
2694                   symbol.type.staticMethod = true;
2695             }
2696          }
2697          imported = symbol._import ? true : false;
2698          if(imported && function.module != privateModule && function.module.importType != staticImport)
2699             dllImport = true;
2700       }
2701
2702       DeclareType(function.dataType, true, true);
2703
2704       if(inCompiler)
2705       {
2706          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2707          {
2708             // We need a declaration here :)
2709             Declaration decl;
2710             OldList * specifiers, * declarators;
2711             Declarator d;
2712             Declarator funcDecl;
2713             External external;
2714
2715             specifiers = MkList();
2716             declarators = MkList();
2717
2718             //if(imported)
2719                ListAdd(specifiers, MkSpecifier(EXTERN));
2720             /*
2721             else
2722                ListAdd(specifiers, MkSpecifier(STATIC));
2723             */
2724
2725             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2726             //if(imported)
2727             if(dllImport)
2728                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2729
2730             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2731             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2732             if(function.module.importType == staticImport)
2733             {
2734                Specifier spec;
2735                for(spec = specifiers->first; spec; spec = spec.next)
2736                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2737                   {
2738                      specifiers->Remove(spec);
2739                      FreeSpecifier(spec);
2740                      break;
2741                   }
2742             }
2743
2744             funcDecl = GetFuncDecl(d);
2745
2746             // Make sure we don't have empty parameter declarations for static methods...
2747             if(funcDecl && !funcDecl.function.parameters)
2748             {
2749                funcDecl.function.parameters = MkList();
2750                funcDecl.function.parameters->Insert(null,
2751                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2752             }
2753
2754             ListAdd(declarators, MkInitDeclarator(d, null));
2755
2756             {
2757                Context oldCtx = curContext;
2758                curContext = globalContext;
2759                decl = MkDeclaration(specifiers, declarators);
2760                curContext = oldCtx;
2761             }
2762
2763             // Keep a different symbol for the function definition than the declaration...
2764             if(symbol.pointerExternal)
2765             {
2766                Symbol functionSymbol { };
2767                // Copy symbol
2768                {
2769                   *functionSymbol = *symbol;
2770                   functionSymbol.string = CopyString(symbol.string);
2771                   if(functionSymbol.type)
2772                      functionSymbol.type.refCount++;
2773                }
2774
2775                excludedSymbols->Add(functionSymbol);
2776
2777                symbol.pointerExternal.symbol = functionSymbol;
2778             }
2779             external = MkExternalDeclaration(decl);
2780             if(curExternal)
2781                ast->Insert(curExternal.prev, external);
2782             external.symbol = symbol;
2783             symbol.pointerExternal = external;
2784          }
2785          else
2786          {
2787             // Move declaration higher...
2788             ast->Move(symbol.pointerExternal, curExternal.prev);
2789          }
2790
2791          if(curExternal)
2792             symbol.id = curExternal.symbol.idCode;
2793       }
2794    }
2795    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2796 }
2797
2798 void DeclareGlobalData(GlobalData data)
2799 {
2800    Symbol symbol = data.symbol;
2801    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2802    {
2803       if(inCompiler)
2804       {
2805          if(!symbol)
2806             symbol = data.symbol = Symbol { };
2807       }
2808       if(!data.dataType)
2809          data.dataType = ProcessTypeString(data.dataTypeString, false);
2810       DeclareType(data.dataType, true, true);
2811       if(inCompiler)
2812       {
2813          if(!symbol.pointerExternal)
2814          {
2815             // We need a declaration here :)
2816             Declaration decl;
2817             OldList * specifiers, * declarators;
2818             Declarator d;
2819             External external;
2820
2821             specifiers = MkList();
2822             declarators = MkList();
2823
2824             ListAdd(specifiers, MkSpecifier(EXTERN));
2825             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2826             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2827
2828             ListAdd(declarators, MkInitDeclarator(d, null));
2829
2830             decl = MkDeclaration(specifiers, declarators);
2831             external = MkExternalDeclaration(decl);
2832             if(curExternal)
2833                ast->Insert(curExternal.prev, external);
2834             external.symbol = symbol;
2835             symbol.pointerExternal = external;
2836          }
2837          else
2838          {
2839             // Move declaration higher...
2840             ast->Move(symbol.pointerExternal, curExternal.prev);
2841          }
2842
2843          if(curExternal)
2844             symbol.id = curExternal.symbol.idCode;
2845       }
2846    }
2847 }
2848
2849 class Conversion : struct
2850 {
2851    Conversion prev, next;
2852    Property convert;
2853    bool isGet;
2854    Type resultType;
2855 };
2856
2857 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2858 {
2859    if(source && dest)
2860    {
2861       // Property convert;
2862
2863       if(source.kind == templateType && dest.kind != templateType)
2864       {
2865          Type type = ProcessTemplateParameterType(source.templateParameter);
2866          if(type) source = type;
2867       }
2868
2869       if(dest.kind == templateType && source.kind != templateType)
2870       {
2871          Type type = ProcessTemplateParameterType(dest.templateParameter);
2872          if(type) dest = type;
2873       }
2874
2875       if(dest.classObjectType == typedObject)
2876       {
2877          if(source.classObjectType != anyObject)
2878             return true;
2879          else
2880          {
2881             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2882             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2883             {
2884                return true;
2885             }
2886          }
2887       }
2888       else
2889       {
2890          if(source.classObjectType == anyObject)
2891             return true;
2892          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2893             return true;
2894       }
2895
2896       if((dest.kind == structType && source.kind == structType) ||
2897          (dest.kind == unionType && source.kind == unionType))
2898       {
2899          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2900              (source.members.first && source.members.first == dest.members.first))
2901             return true;
2902       }
2903
2904       if(dest.kind == ellipsisType && source.kind != voidType)
2905          return true;
2906
2907       if(dest.kind == pointerType && dest.type.kind == voidType &&
2908          ((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))
2909          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2910
2911          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2912
2913          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2914          return true;
2915       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2916          ((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))
2917          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2918
2919          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2920
2921          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2922          return true;
2923
2924       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2925       {
2926          if(source._class.registered && source._class.registered.type == unitClass)
2927          {
2928             if(conversions != null)
2929             {
2930                if(source._class.registered == dest._class.registered)
2931                   return true;
2932             }
2933             else
2934             {
2935                Class sourceBase, destBase;
2936                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2937                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2938                if(sourceBase == destBase)
2939                   return true;
2940             }
2941          }
2942          // Don't match enum inheriting from other enum if resolving enumeration values
2943          // TESTING: !dest.classObjectType
2944          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2945             (enumBaseType ||
2946                (!source._class.registered || source._class.registered.type != enumClass) ||
2947                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2948             return true;
2949          else
2950          {
2951             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2952             if(enumBaseType &&
2953                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2954                ((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)
2955             {
2956                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2957                {
2958                   return true;
2959                }
2960             }
2961          }
2962       }
2963
2964       // JUST ADDED THIS...
2965       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2966          return true;
2967
2968       if(doConversion)
2969       {
2970          // Just added this for Straight conversion of ColorAlpha => Color
2971          if(source.kind == classType)
2972          {
2973             Class _class;
2974             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2975             {
2976                Property convert;
2977                for(convert = _class.conversions.first; convert; convert = convert.next)
2978                {
2979                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2980                   {
2981                      Conversion after = (conversions != null) ? conversions.last : null;
2982
2983                      if(!convert.dataType)
2984                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2985                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2986                      {
2987                         if(!conversions && !convert.Get)
2988                            return true;
2989                         else if(conversions != null)
2990                         {
2991                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2992                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2993                               (dest.kind != classType || dest._class.registered != _class.base))
2994                               return true;
2995                            else
2996                            {
2997                               Conversion conv { convert = convert, isGet = true };
2998                               // conversions.Add(conv);
2999                               conversions.Insert(after, conv);
3000                               return true;
3001                            }
3002                         }
3003                      }
3004                   }
3005                }
3006             }
3007          }
3008
3009          // MOVING THIS??
3010
3011          if(dest.kind == classType)
3012          {
3013             Class _class;
3014             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3015             {
3016                Property convert;
3017                for(convert = _class.conversions.first; convert; convert = convert.next)
3018                {
3019                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3020                   {
3021                      // Conversion after = (conversions != null) ? conversions.last : null;
3022
3023                      if(!convert.dataType)
3024                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3025                      // Just added this equality check to prevent recursion.... Make it safer?
3026                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3027                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3028                      {
3029                         if(!conversions && !convert.Set)
3030                            return true;
3031                         else if(conversions != null)
3032                         {
3033                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3034                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3035                               (source.kind != classType || source._class.registered != _class.base))
3036                               return true;
3037                            else
3038                            {
3039                               // *** Testing this! ***
3040                               Conversion conv { convert = convert };
3041                               conversions.Add(conv);
3042                               //conversions.Insert(after, conv);
3043                               return true;
3044                            }
3045                         }
3046                      }
3047                   }
3048                }
3049             }
3050             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3051             {
3052                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3053                   (source.kind != classType || source._class.registered.type != structClass))
3054                   return true;
3055             }*/
3056
3057             // TESTING THIS... IS THIS OK??
3058             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3059             {
3060                if(!dest._class.registered.dataType)
3061                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3062                // Only support this for classes...
3063                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3064                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3065                {
3066                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3067                   {
3068                      return true;
3069                   }
3070                }
3071             }
3072          }
3073
3074          // Moved this lower
3075          if(source.kind == classType)
3076          {
3077             Class _class;
3078             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3079             {
3080                Property convert;
3081                for(convert = _class.conversions.first; convert; convert = convert.next)
3082                {
3083                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3084                   {
3085                      Conversion after = (conversions != null) ? conversions.last : null;
3086
3087                      if(!convert.dataType)
3088                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3089                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3090                      {
3091                         if(!conversions && !convert.Get)
3092                            return true;
3093                         else if(conversions != null)
3094                         {
3095                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3096                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3097                               (dest.kind != classType || dest._class.registered != _class.base))
3098                               return true;
3099                            else
3100                            {
3101                               Conversion conv { convert = convert, isGet = true };
3102
3103                               // conversions.Add(conv);
3104                               conversions.Insert(after, conv);
3105                               return true;
3106                            }
3107                         }
3108                      }
3109                   }
3110                }
3111             }
3112
3113             // TESTING THIS... IS THIS OK??
3114             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3115             {
3116                if(!source._class.registered.dataType)
3117                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3118                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3119                {
3120                   return true;
3121                }
3122             }
3123          }
3124       }
3125
3126       if(source.kind == classType || source.kind == subClassType)
3127          ;
3128       else if(dest.kind == source.kind &&
3129          (dest.kind != structType && dest.kind != unionType &&
3130           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3131           return true;
3132       // RECENTLY ADDED THESE
3133       else if(dest.kind == doubleType && source.kind == floatType)
3134          return true;
3135       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3136          return true;
3137       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3138          return true;
3139       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3140          return true;
3141       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3142          return true;
3143       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3144          return true;
3145       else if(source.kind == enumType &&
3146          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3147           return true;
3148       else if(dest.kind == enumType &&
3149          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3150           return true;
3151       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3152               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3153       {
3154          Type paramSource, paramDest;
3155
3156          if(dest.kind == methodType)
3157             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3158          if(source.kind == methodType)
3159             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3160
3161          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3162          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3163          if(dest.kind == methodType)
3164             dest = dest.method.dataType;
3165          if(source.kind == methodType)
3166             source = source.method.dataType;
3167
3168          paramSource = source.params.first;
3169          if(paramSource && paramSource.kind == voidType) paramSource = null;
3170          paramDest = dest.params.first;
3171          if(paramDest && paramDest.kind == voidType) paramDest = null;
3172
3173
3174          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3175             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3176          {
3177             // Source thisClass must be derived from destination thisClass
3178             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3179                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3180             {
3181                if(paramDest && paramDest.kind == classType)
3182                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3183                else
3184                   Compiler_Error($"method class should not take an object\n");
3185                return false;
3186             }
3187             paramDest = paramDest.next;
3188          }
3189          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3190          {
3191             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3192             {
3193                if(dest.thisClass)
3194                {
3195                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3196                   {
3197                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3198                      return false;
3199                   }
3200                }
3201                else
3202                {
3203                   // THIS WAS BACKWARDS:
3204                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3205                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3206                   {
3207                      if(owningClassDest)
3208                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3209                      else
3210                         Compiler_Error($"overriding class expected to be derived from method class\n");
3211                      return false;
3212                   }
3213                }
3214                paramSource = paramSource.next;
3215             }
3216             else
3217             {
3218                if(dest.thisClass)
3219                {
3220                   // Source thisClass must be derived from destination thisClass
3221                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3222                   {
3223                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3224                      return false;
3225                   }
3226                }
3227                else
3228                {
3229                   // THIS WAS BACKWARDS TOO??
3230                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3231                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3232                   {
3233                      //if(owningClass)
3234                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3235                      //else
3236                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3237                      return false;
3238                   }
3239                }
3240             }
3241          }
3242
3243
3244          // Source return type must be derived from destination return type
3245          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3246          {
3247             Compiler_Warning($"incompatible return type for function\n");
3248             return false;
3249          }
3250
3251          // Check parameters
3252
3253          for(; paramDest; paramDest = paramDest.next)
3254          {
3255             if(!paramSource)
3256             {
3257                //Compiler_Warning($"not enough parameters\n");
3258                Compiler_Error($"not enough parameters\n");
3259                return false;
3260             }
3261             {
3262                Type paramDestType = paramDest;
3263                Type paramSourceType = paramSource;
3264                Type type = paramDestType;
3265
3266                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3267                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3268                   paramSource.kind != templateType)
3269                {
3270                   int id = 0;
3271                   ClassTemplateParameter curParam = null;
3272                   Class sClass;
3273                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3274                   {
3275                      id = 0;
3276                      if(sClass.templateClass) sClass = sClass.templateClass;
3277                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3278                      {
3279                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3280                         {
3281                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3282                            {
3283                               if(sClass.templateClass) sClass = sClass.templateClass;
3284                               id += sClass.templateParams.count;
3285                            }
3286                            break;
3287                         }
3288                         id++;
3289                      }
3290                      if(curParam) break;
3291                   }
3292
3293                   if(curParam)
3294                   {
3295                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3296                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3297                   }
3298                }
3299
3300                // paramDest must be derived from paramSource
3301                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3302                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3303                {
3304                   char type[1024];
3305                   type[0] = 0;
3306                   PrintType(paramDest, type, false, true);
3307                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3308
3309                   if(paramDestType != paramDest)
3310                      FreeType(paramDestType);
3311                   return false;
3312                }
3313                if(paramDestType != paramDest)
3314                   FreeType(paramDestType);
3315             }
3316
3317             paramSource = paramSource.next;
3318          }
3319          if(paramSource)
3320          {
3321             Compiler_Error($"too many parameters\n");
3322             return false;
3323          }
3324          return true;
3325       }
3326       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3327       {
3328          return true;
3329       }
3330       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3331          (source.kind == arrayType || source.kind == pointerType))
3332       {
3333          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3334             return true;
3335       }
3336    }
3337    return false;
3338 }
3339
3340 static void FreeConvert(Conversion convert)
3341 {
3342    if(convert.resultType)
3343       FreeType(convert.resultType);
3344 }
3345
3346 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3347                               char * string, OldList conversions)
3348 {
3349    BTNamedLink link;
3350
3351    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3352    {
3353       Class _class = link.data;
3354       if(_class.type == enumClass)
3355       {
3356          OldList converts { };
3357          Type type { };
3358          type.kind = classType;
3359
3360          if(!_class.symbol)
3361             _class.symbol = FindClass(_class.fullName);
3362          type._class = _class.symbol;
3363
3364          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3365          {
3366             NamedLink value;
3367             Class enumClass = eSystem_FindClass(privateModule, "enum");
3368             if(enumClass)
3369             {
3370                Class baseClass;
3371                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3372                {
3373                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3374                   for(value = e.values.first; value; value = value.next)
3375                   {
3376                      if(!strcmp(value.name, string))
3377                         break;
3378                   }
3379                   if(value)
3380                   {
3381                      FreeExpContents(sourceExp);
3382                      FreeType(sourceExp.expType);
3383
3384                      sourceExp.isConstant = true;
3385                      sourceExp.expType = MkClassType(baseClass.fullName);
3386                      //if(inCompiler)
3387                      {
3388                         char constant[256];
3389                         sourceExp.type = constantExp;
3390                         if(!strcmp(baseClass.dataTypeString, "int"))
3391                            sprintf(constant, "%d",(int)value.data);
3392                         else
3393                            sprintf(constant, "0x%X",(int)value.data);
3394                         sourceExp.constant = CopyString(constant);
3395                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3396                      }
3397
3398                      while(converts.first)
3399                      {
3400                         Conversion convert = converts.first;
3401                         converts.Remove(convert);
3402                         conversions.Add(convert);
3403                      }
3404                      delete type;
3405                      return true;
3406                   }
3407                }
3408             }
3409          }
3410          if(converts.first)
3411             converts.Free(FreeConvert);
3412          delete type;
3413       }
3414    }
3415    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3416       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3417          return true;
3418    return false;
3419 }
3420
3421 public bool ModuleVisibility(Module searchIn, Module searchFor)
3422 {
3423    SubModule subModule;
3424
3425    if(searchFor == searchIn)
3426       return true;
3427
3428    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3429    {
3430       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3431       {
3432          if(ModuleVisibility(subModule.module, searchFor))
3433             return true;
3434       }
3435    }
3436    return false;
3437 }
3438
3439 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3440 {
3441    Module module;
3442
3443    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3444       return true;
3445    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3446       return true;
3447    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3448       return true;
3449
3450    for(module = mainModule.application.allModules.first; module; module = module.next)
3451    {
3452       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3453          return true;
3454    }
3455    return false;
3456 }
3457
3458 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3459 {
3460    Type source = sourceExp.expType;
3461    Type realDest = dest;
3462    Type backupSourceExpType = null;
3463
3464    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3465       return true;
3466
3467    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3468    {
3469        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3470        {
3471           Class sourceBase, destBase;
3472           for(sourceBase = source._class.registered;
3473               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3474               sourceBase = sourceBase.base);
3475           for(destBase = dest._class.registered;
3476               destBase && destBase.base && destBase.base.type != systemClass;
3477               destBase = destBase.base);
3478           //if(source._class.registered == dest._class.registered)
3479           if(sourceBase == destBase)
3480              return true;
3481        }
3482    }
3483
3484    if(source)
3485    {
3486       OldList * specs;
3487       bool flag = false;
3488       int64 value = MAXINT;
3489
3490       source.refCount++;
3491       dest.refCount++;
3492
3493       if(sourceExp.type == constantExp)
3494       {
3495          if(source.isSigned)
3496             value = strtoll(sourceExp.constant, null, 0);
3497          else
3498             value = strtoull(sourceExp.constant, null, 0);
3499       }
3500       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3501       {
3502          if(source.isSigned)
3503             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3504          else
3505             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3506       }
3507
3508       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3509          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3510       {
3511          FreeType(source);
3512          source = Type { kind = intType, isSigned = false, refCount = 1 };
3513       }
3514
3515       if(dest.kind == classType)
3516       {
3517          Class _class = dest._class ? dest._class.registered : null;
3518
3519          if(_class && _class.type == unitClass)
3520          {
3521             if(source.kind != classType)
3522             {
3523                Type tempType { };
3524                Type tempDest, tempSource;
3525
3526                for(; _class.base.type != systemClass; _class = _class.base);
3527                tempSource = dest;
3528                tempDest = tempType;
3529
3530                tempType.kind = classType;
3531                if(!_class.symbol)
3532                   _class.symbol = FindClass(_class.fullName);
3533
3534                tempType._class = _class.symbol;
3535                tempType.truth = dest.truth;
3536                if(tempType._class)
3537                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3538
3539                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3540                backupSourceExpType = sourceExp.expType;
3541                sourceExp.expType = dest; dest.refCount++;
3542                //sourceExp.expType = MkClassType(_class.fullName);
3543                flag = true;
3544
3545                delete tempType;
3546             }
3547          }
3548
3549
3550          // Why wasn't there something like this?
3551          if(_class && _class.type == bitClass && source.kind != classType)
3552          {
3553             if(!dest._class.registered.dataType)
3554                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3555             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3556             {
3557                FreeType(source);
3558                FreeType(sourceExp.expType);
3559                source = sourceExp.expType = MkClassType(dest._class.string);
3560                source.refCount++;
3561
3562                //source.kind = classType;
3563                //source._class = dest._class;
3564             }
3565          }
3566
3567          // Adding two enumerations
3568          /*
3569          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3570          {
3571             if(!source._class.registered.dataType)
3572                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3573             if(!dest._class.registered.dataType)
3574                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3575
3576             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3577             {
3578                FreeType(source);
3579                source = sourceExp.expType = MkClassType(dest._class.string);
3580                source.refCount++;
3581
3582                //source.kind = classType;
3583                //source._class = dest._class;
3584             }
3585          }*/
3586
3587          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3588          {
3589             OldList * specs = MkList();
3590             Declarator decl;
3591             char string[1024];
3592
3593             ReadString(string, sourceExp.string);
3594             decl = SpecDeclFromString(string, specs, null);
3595
3596             FreeExpContents(sourceExp);
3597             FreeType(sourceExp.expType);
3598
3599             sourceExp.type = classExp;
3600             sourceExp._classExp.specifiers = specs;
3601             sourceExp._classExp.decl = decl;
3602             sourceExp.expType = dest;
3603             dest.refCount++;
3604
3605             FreeType(source);
3606             FreeType(dest);
3607             if(backupSourceExpType) FreeType(backupSourceExpType);
3608             return true;
3609          }
3610       }
3611       else if(source.kind == classType)
3612       {
3613          Class _class = source._class ? source._class.registered : null;
3614
3615          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3616          {
3617             /*
3618             if(dest.kind != classType)
3619             {
3620                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3621                if(!source._class.registered.dataType)
3622                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3623
3624                FreeType(dest);
3625                dest = MkClassType(source._class.string);
3626                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3627                //   dest = MkClassType(source._class.string);
3628             }
3629             */
3630
3631             if(dest.kind != classType)
3632             {
3633                Type tempType { };
3634                Type tempDest, tempSource;
3635
3636                if(!source._class.registered.dataType)
3637                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3638
3639                for(; _class.base.type != systemClass; _class = _class.base);
3640                tempDest = source;
3641                tempSource = tempType;
3642                tempType.kind = classType;
3643                tempType._class = FindClass(_class.fullName);
3644                tempType.truth = source.truth;
3645                tempType.classObjectType = source.classObjectType;
3646
3647                if(tempType._class)
3648                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3649
3650                // PUT THIS BACK TESTING UNITS?
3651                if(conversions.last)
3652                {
3653                   ((Conversion)(conversions.last)).resultType = dest;
3654                   dest.refCount++;
3655                }
3656
3657                FreeType(sourceExp.expType);
3658                sourceExp.expType = MkClassType(_class.fullName);
3659                sourceExp.expType.truth = source.truth;
3660                sourceExp.expType.classObjectType = source.classObjectType;
3661
3662                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3663
3664                if(!sourceExp.destType)
3665                {
3666                   FreeType(sourceExp.destType);
3667                   sourceExp.destType = sourceExp.expType;
3668                   if(sourceExp.expType)
3669                      sourceExp.expType.refCount++;
3670                }
3671                //flag = true;
3672                //source = _class.dataType;
3673
3674
3675                // TOCHECK: TESTING THIS NEW CODE
3676                if(!_class.dataType)
3677                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3678                FreeType(dest);
3679                dest = MkClassType(source._class.string);
3680                dest.truth = source.truth;
3681                dest.classObjectType = source.classObjectType;
3682
3683                FreeType(source);
3684                source = _class.dataType;
3685                source.refCount++;
3686
3687                delete tempType;
3688             }
3689          }
3690       }
3691
3692       if(!flag)
3693       {
3694          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3695          {
3696             FreeType(source);
3697             FreeType(dest);
3698             return true;
3699          }
3700       }
3701
3702       // Implicit Casts
3703       /*
3704       if(source.kind == classType)
3705       {
3706          Class _class = source._class.registered;
3707          if(_class.type == unitClass)
3708          {
3709             if(!_class.dataType)
3710                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3711             source = _class.dataType;
3712          }
3713       }*/
3714
3715       if(dest.kind == classType)
3716       {
3717          Class _class = dest._class ? dest._class.registered : null;
3718          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3719             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3720          {
3721             if(_class.type == normalClass || _class.type == noHeadClass)
3722             {
3723                Expression newExp { };
3724                *newExp = *sourceExp;
3725                if(sourceExp.destType) sourceExp.destType.refCount++;
3726                if(sourceExp.expType)  sourceExp.expType.refCount++;
3727                sourceExp.type = castExp;
3728                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3729                sourceExp.cast.exp = newExp;
3730                FreeType(sourceExp.expType);
3731                sourceExp.expType = null;
3732                ProcessExpressionType(sourceExp);
3733
3734                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3735                if(!inCompiler)
3736                {
3737                   FreeType(sourceExp.expType);
3738                   sourceExp.expType = dest;
3739                }
3740
3741                FreeType(source);
3742                if(inCompiler) FreeType(dest);
3743
3744                if(backupSourceExpType) FreeType(backupSourceExpType);
3745                return true;
3746             }
3747
3748             if(!_class.dataType)
3749                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3750             FreeType(dest);
3751             dest = _class.dataType;
3752             dest.refCount++;
3753          }
3754
3755          // Accept lower precision types for units, since we want to keep the unit type
3756          if(dest.kind == doubleType &&
3757             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3758              source.kind == charType || source.kind == _BoolType))
3759          {
3760             specs = MkListOne(MkSpecifier(DOUBLE));
3761          }
3762          else if(dest.kind == floatType &&
3763             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3764             source.kind == _BoolType || source.kind == doubleType))
3765          {
3766             specs = MkListOne(MkSpecifier(FLOAT));
3767          }
3768          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3769             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3770          {
3771             specs = MkList();
3772             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3773             ListAdd(specs, MkSpecifier(INT64));
3774          }
3775          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3776             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3777          {
3778             specs = MkList();
3779             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3780             ListAdd(specs, MkSpecifier(INT));
3781          }
3782          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3783             source.kind == floatType || source.kind == doubleType))
3784          {
3785             specs = MkList();
3786             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3787             ListAdd(specs, MkSpecifier(SHORT));
3788          }
3789          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3790             source.kind == floatType || source.kind == doubleType))
3791          {
3792             specs = MkList();
3793             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3794             ListAdd(specs, MkSpecifier(CHAR));
3795          }
3796          else
3797          {
3798             FreeType(source);
3799             FreeType(dest);
3800             if(backupSourceExpType)
3801             {
3802                // Failed to convert: revert previous exp type
3803                if(sourceExp.expType) FreeType(sourceExp.expType);
3804                sourceExp.expType = backupSourceExpType;
3805             }
3806             return false;
3807          }
3808       }
3809       else if(dest.kind == doubleType &&
3810          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3811           source.kind == _BoolType || source.kind == charType))
3812       {
3813          specs = MkListOne(MkSpecifier(DOUBLE));
3814       }
3815       else if(dest.kind == floatType &&
3816          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3817       {
3818          specs = MkListOne(MkSpecifier(FLOAT));
3819       }
3820       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3821          (value == 1 || value == 0))
3822       {
3823          specs = MkList();
3824          ListAdd(specs, MkSpecifier(BOOL));
3825       }
3826       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3827          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3828       {
3829          specs = MkList();
3830          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3831          ListAdd(specs, MkSpecifier(CHAR));
3832       }
3833       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3834          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3835       {
3836          specs = MkList();
3837          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3838          ListAdd(specs, MkSpecifier(SHORT));
3839       }
3840       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3841       {
3842          specs = MkList();
3843          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3844          ListAdd(specs, MkSpecifier(INT));
3845       }
3846       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3847       {
3848          specs = MkList();
3849          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3850          ListAdd(specs, MkSpecifier(INT64));
3851       }
3852       else if(dest.kind == enumType &&
3853          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3854       {
3855          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3856       }
3857       else
3858       {
3859          FreeType(source);
3860          FreeType(dest);
3861          if(backupSourceExpType)
3862          {
3863             // Failed to convert: revert previous exp type
3864             if(sourceExp.expType) FreeType(sourceExp.expType);
3865             sourceExp.expType = backupSourceExpType;
3866          }
3867          return false;
3868       }
3869
3870       if(!flag)
3871       {
3872          Expression newExp { };
3873          *newExp = *sourceExp;
3874          newExp.prev = null;
3875          newExp.next = null;
3876          if(sourceExp.destType) sourceExp.destType.refCount++;
3877          if(sourceExp.expType)  sourceExp.expType.refCount++;
3878
3879          sourceExp.type = castExp;
3880          if(realDest.kind == classType)
3881          {
3882             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3883             FreeList(specs, FreeSpecifier);
3884          }
3885          else
3886             sourceExp.cast.typeName = MkTypeName(specs, null);
3887          if(newExp.type == opExp)
3888          {
3889             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3890          }
3891          else
3892             sourceExp.cast.exp = newExp;
3893
3894          FreeType(sourceExp.expType);
3895          sourceExp.expType = null;
3896          ProcessExpressionType(sourceExp);
3897       }
3898       else
3899          FreeList(specs, FreeSpecifier);
3900
3901       FreeType(dest);
3902       FreeType(source);
3903       if(backupSourceExpType) FreeType(backupSourceExpType);
3904
3905       return true;
3906    }
3907    else
3908    {
3909       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3910       if(sourceExp.type == identifierExp)
3911       {
3912          Identifier id = sourceExp.identifier;
3913          if(dest.kind == classType)
3914          {
3915             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3916             {
3917                Class _class = dest._class.registered;
3918                Class enumClass = eSystem_FindClass(privateModule, "enum");
3919                if(enumClass)
3920                {
3921                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3922                   {
3923                      NamedLink value;
3924                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3925                      for(value = e.values.first; value; value = value.next)
3926                      {
3927                         if(!strcmp(value.name, id.string))
3928                            break;
3929                      }
3930                      if(value)
3931                      {
3932                         FreeExpContents(sourceExp);
3933                         FreeType(sourceExp.expType);
3934
3935                         sourceExp.isConstant = true;
3936                         sourceExp.expType = MkClassType(_class.fullName);
3937                         //if(inCompiler)
3938                         {
3939                            char constant[256];
3940                            sourceExp.type = constantExp;
3941                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3942                               sprintf(constant, "%d", (int) value.data);
3943                            else
3944                               sprintf(constant, "0x%X", (int) value.data);
3945                            sourceExp.constant = CopyString(constant);
3946                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3947                         }
3948                         return true;
3949                      }
3950                   }
3951                }
3952             }
3953          }
3954
3955          // Loop through all enum classes
3956          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3957             return true;
3958       }
3959    }
3960    return false;
3961 }
3962
3963 #define TERTIARY(o, name, m, t, p) \
3964    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3965    {                                                              \
3966       exp.type = constantExp;                                    \
3967       exp.string = p(op1.m ? op2.m : op3.m);                     \
3968       if(!exp.expType) \
3969          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3970       return true;                                                \
3971    }
3972
3973 #define BINARY(o, name, m, t, p) \
3974    static bool name(Expression exp, Operand op1, Operand op2)   \
3975    {                                                              \
3976       t value2 = op2.m;                                           \
3977       exp.type = constantExp;                                    \
3978       exp.string = p(op1.m o value2);                     \
3979       if(!exp.expType) \
3980          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3981       return true;                                                \
3982    }
3983
3984 #define BINARY_DIVIDE(o, name, m, t, p) \
3985    static bool name(Expression exp, Operand op1, Operand op2)   \
3986    {                                                              \
3987       t value2 = op2.m;                                           \
3988       exp.type = constantExp;                                    \
3989       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3990       if(!exp.expType) \
3991          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3992       return true;                                                \
3993    }
3994
3995 #define UNARY(o, name, m, t, p) \
3996    static bool name(Expression exp, Operand op1)                \
3997    {                                                              \
3998       exp.type = constantExp;                                    \
3999       exp.string = p((t)(o op1.m));                                   \
4000       if(!exp.expType) \
4001          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4002       return true;                                                \
4003    }
4004
4005 #define OPERATOR_ALL(macro, o, name) \
4006    macro(o, Int##name, i, int, PrintInt) \
4007    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4008    macro(o, Int64##name, i64, int64, PrintInt64) \
4009    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4010    macro(o, Short##name, s, short, PrintShort) \
4011    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4012    macro(o, Char##name, c, char, PrintChar) \
4013    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4014    macro(o, Float##name, f, float, PrintFloat) \
4015    macro(o, Double##name, d, double, PrintDouble)
4016
4017 #define OPERATOR_INTTYPES(macro, o, name) \
4018    macro(o, Int##name, i, int, PrintInt) \
4019    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4020    macro(o, Int64##name, i64, int64, PrintInt64) \
4021    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4022    macro(o, Short##name, s, short, PrintShort) \
4023    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4024    macro(o, Char##name, c, char, PrintChar) \
4025    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4026
4027
4028 // binary arithmetic
4029 OPERATOR_ALL(BINARY, +, Add)
4030 OPERATOR_ALL(BINARY, -, Sub)
4031 OPERATOR_ALL(BINARY, *, Mul)
4032 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4033 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4034
4035 // unary arithmetic
4036 OPERATOR_ALL(UNARY, -, Neg)
4037
4038 // unary arithmetic increment and decrement
4039 OPERATOR_ALL(UNARY, ++, Inc)
4040 OPERATOR_ALL(UNARY, --, Dec)
4041
4042 // binary arithmetic assignment
4043 OPERATOR_ALL(BINARY, =, Asign)
4044 OPERATOR_ALL(BINARY, +=, AddAsign)
4045 OPERATOR_ALL(BINARY, -=, SubAsign)
4046 OPERATOR_ALL(BINARY, *=, MulAsign)
4047 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4048 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4049
4050 // binary bitwise
4051 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4052 OPERATOR_INTTYPES(BINARY, |, BitOr)
4053 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4054 OPERATOR_INTTYPES(BINARY, <<, LShift)
4055 OPERATOR_INTTYPES(BINARY, >>, RShift)
4056
4057 // unary bitwise
4058 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4059
4060 // binary bitwise assignment
4061 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4062 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4063 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4064 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4065 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4066
4067 // unary logical negation
4068 OPERATOR_INTTYPES(UNARY, !, Not)
4069
4070 // binary logical equality
4071 OPERATOR_ALL(BINARY, ==, Equ)
4072 OPERATOR_ALL(BINARY, !=, Nqu)
4073
4074 // binary logical
4075 OPERATOR_ALL(BINARY, &&, And)
4076 OPERATOR_ALL(BINARY, ||, Or)
4077
4078 // binary logical relational
4079 OPERATOR_ALL(BINARY, >, Grt)
4080 OPERATOR_ALL(BINARY, <, Sma)
4081 OPERATOR_ALL(BINARY, >=, GrtEqu)
4082 OPERATOR_ALL(BINARY, <=, SmaEqu)
4083
4084 // tertiary condition operator
4085 OPERATOR_ALL(TERTIARY, ?, Cond)
4086
4087 //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
4088 #define OPERATOR_TABLE_ALL(name, type) \
4089     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4090                           type##Neg, \
4091                           type##Inc, type##Dec, \
4092                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4093                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4094                           type##BitNot, \
4095                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4096                           type##Not, \
4097                           type##Equ, type##Nqu, \
4098                           type##And, type##Or, \
4099                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4100                         }; \
4101
4102 #define OPERATOR_TABLE_INTTYPES(name, type) \
4103     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4104                           type##Neg, \
4105                           type##Inc, type##Dec, \
4106                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4107                           null, null, null, null, null, \
4108                           null, \
4109                           null, null, null, null, null, \
4110                           null, \
4111                           type##Equ, type##Nqu, \
4112                           type##And, type##Or, \
4113                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4114                         }; \
4115
4116 OPERATOR_TABLE_ALL(int, Int)
4117 OPERATOR_TABLE_ALL(uint, UInt)
4118 OPERATOR_TABLE_ALL(int64, Int64)
4119 OPERATOR_TABLE_ALL(uint64, UInt64)
4120 OPERATOR_TABLE_ALL(short, Short)
4121 OPERATOR_TABLE_ALL(ushort, UShort)
4122 OPERATOR_TABLE_INTTYPES(float, Float)
4123 OPERATOR_TABLE_INTTYPES(double, Double)
4124 OPERATOR_TABLE_ALL(char, Char)
4125 OPERATOR_TABLE_ALL(uchar, UChar)
4126
4127 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4128 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4129 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4130 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4131 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4132 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4133 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4134 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4135
4136 public void ReadString(char * output,  char * string)
4137 {
4138    int len = strlen(string);
4139    int c,d = 0;
4140    bool quoted = false, escaped = false;
4141    for(c = 0; c<len; c++)
4142    {
4143       char ch = string[c];
4144       if(escaped)
4145       {
4146          switch(ch)
4147          {
4148             case 'n': output[d] = '\n'; break;
4149             case 't': output[d] = '\t'; break;
4150             case 'a': output[d] = '\a'; break;
4151             case 'b': output[d] = '\b'; break;
4152             case 'f': output[d] = '\f'; break;
4153             case 'r': output[d] = '\r'; break;
4154             case 'v': output[d] = '\v'; break;
4155             case '\\': output[d] = '\\'; break;
4156             case '\"': output[d] = '\"'; break;
4157             default: output[d++] = '\\'; output[d] = ch;
4158             //default: output[d] = ch;
4159          }
4160          d++;
4161          escaped = false;
4162       }
4163       else
4164       {
4165          if(ch == '\"')
4166             quoted ^= true;
4167          else if(quoted)
4168          {
4169             if(ch == '\\')
4170                escaped = true;
4171             else
4172                output[d++] = ch;
4173          }
4174       }
4175    }
4176    output[d] = '\0';
4177 }
4178
4179 public Operand GetOperand(Expression exp)
4180 {
4181    Operand op { };
4182    Type type = exp.expType;
4183    if(type)
4184    {
4185       while(type.kind == classType &&
4186          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4187       {
4188          if(!type._class.registered.dataType)
4189             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4190          type = type._class.registered.dataType;
4191
4192       }
4193       op.kind = type.kind;
4194       op.type = exp.expType;
4195       if(exp.isConstant && exp.type == constantExp)
4196       {
4197          switch(op.kind)
4198          {
4199             case _BoolType:
4200             case charType:
4201             {
4202                if(exp.constant[0] == '\'')
4203                   op.c = exp.constant[1];
4204                else if(type.isSigned)
4205                {
4206                   op.c = (char)strtol(exp.constant, null, 0);
4207                   op.ops = charOps;
4208                }
4209                else
4210                {
4211                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4212                   op.ops = ucharOps;
4213                }
4214                break;
4215             }
4216             case shortType:
4217                if(type.isSigned)
4218                {
4219                   op.s = (short)strtol(exp.constant, null, 0);
4220                   op.ops = shortOps;
4221                }
4222                else
4223                {
4224                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4225                   op.ops = ushortOps;
4226                }
4227                break;
4228             case intType:
4229             case longType:
4230                if(type.isSigned)
4231                {
4232                   op.i = (int)strtol(exp.constant, null, 0);
4233                   op.ops = intOps;
4234                }
4235                else
4236                {
4237                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4238                   op.ops = uintOps;
4239                }
4240                op.kind = intType;
4241                break;
4242             case int64Type:
4243                if(type.isSigned)
4244                {
4245                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4246                   op.ops = intOps;
4247                }
4248                else
4249                {
4250                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4251                   op.ops = uintOps;
4252                }
4253                op.kind = int64Type;
4254                break;
4255             case intPtrType:
4256                if(type.isSigned)
4257                {
4258                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4259                   op.ops = int64Ops;
4260                }
4261                else
4262                {
4263                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4264                   op.ops = uint64Ops;
4265                }
4266                op.kind = int64Type;
4267                break;
4268             case intSizeType:
4269                if(type.isSigned)
4270                {
4271                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4272                   op.ops = int64Ops;
4273                }
4274                else
4275                {
4276                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4277                   op.ops = uint64Ops;
4278                }
4279                op.kind = int64Type;
4280                break;
4281             case floatType:
4282                op.f = (float)strtod(exp.constant, null);
4283                op.ops = floatOps;
4284                break;
4285             case doubleType:
4286                op.d = (double)strtod(exp.constant, null);
4287                op.ops = doubleOps;
4288                break;
4289             //case classType:    For when we have operator overloading...
4290             // Pointer additions
4291             //case functionType:
4292             case arrayType:
4293             case pointerType:
4294             case classType:
4295                op.ui64 = _strtoui64(exp.constant, null, 0);
4296                op.kind = pointerType;
4297                op.ops = uint64Ops;
4298                // op.ptrSize =
4299                break;
4300          }
4301       }
4302    }
4303    return op;
4304 }
4305
4306 static void UnusedFunction()
4307 {
4308    int a;
4309    a.OnGetString(0,0,0);
4310 }
4311 default:
4312 extern int __ecereVMethodID_class_OnGetString;
4313 public:
4314
4315 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4316 {
4317    DataMember dataMember;
4318    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4319    {
4320       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4321          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4322       else
4323       {
4324          Expression exp { };
4325          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4326          Type type;
4327          void * ptr = inst.data + dataMember.offset + offset;
4328          char * result = null;
4329          exp.loc = member.loc = inst.loc;
4330          ((Identifier)member.identifiers->first).loc = inst.loc;
4331
4332          if(!dataMember.dataType)
4333             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4334          type = dataMember.dataType;
4335          if(type.kind == classType)
4336          {
4337             Class _class = type._class.registered;
4338             if(_class.type == enumClass)
4339             {
4340                Class enumClass = eSystem_FindClass(privateModule, "enum");
4341                if(enumClass)
4342                {
4343                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4344                   NamedLink item;
4345                   for(item = e.values.first; item; item = item.next)
4346                   {
4347                      if((int)item.data == *(int *)ptr)
4348                      {
4349                         result = item.name;
4350                         break;
4351                      }
4352                   }
4353                   if(result)
4354                   {
4355                      exp.identifier = MkIdentifier(result);
4356                      exp.type = identifierExp;
4357                      exp.destType = MkClassType(_class.fullName);
4358                      ProcessExpressionType(exp);
4359                   }
4360                }
4361             }
4362             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4363             {
4364                if(!_class.dataType)
4365                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4366                type = _class.dataType;
4367             }
4368          }
4369          if(!result)
4370          {
4371             switch(type.kind)
4372             {
4373                case floatType:
4374                {
4375                   FreeExpContents(exp);
4376
4377                   exp.constant = PrintFloat(*(float*)ptr);
4378                   exp.type = constantExp;
4379                   break;
4380                }
4381                case doubleType:
4382                {
4383                   FreeExpContents(exp);
4384
4385                   exp.constant = PrintDouble(*(double*)ptr);
4386                   exp.type = constantExp;
4387                   break;
4388                }
4389                case intType:
4390                {
4391                   FreeExpContents(exp);
4392
4393                   exp.constant = PrintInt(*(int*)ptr);
4394                   exp.type = constantExp;
4395                   break;
4396                }
4397                case int64Type:
4398                {
4399                   FreeExpContents(exp);
4400
4401                   exp.constant = PrintInt64(*(int64*)ptr);
4402                   exp.type = constantExp;
4403                   break;
4404                }
4405                case intPtrType:
4406                {
4407                   FreeExpContents(exp);
4408                   // TODO: This should probably use proper type
4409                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4410                   exp.type = constantExp;
4411                   break;
4412                }
4413                case intSizeType:
4414                {
4415                   FreeExpContents(exp);
4416                   // TODO: This should probably use proper type
4417                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4418                   exp.type = constantExp;
4419                   break;
4420                }
4421                default:
4422                   Compiler_Error($"Unhandled type populating instance\n");
4423             }
4424          }
4425          ListAdd(memberList, member);
4426       }
4427
4428       if(parentDataMember.type == unionMember)
4429          break;
4430    }
4431 }
4432
4433 void PopulateInstance(Instantiation inst)
4434 {
4435    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4436    Class _class = classSym.registered;
4437    DataMember dataMember;
4438    OldList * memberList = MkList();
4439    // Added this check and ->Add to prevent memory leaks on bad code
4440    if(!inst.members)
4441       inst.members = MkListOne(MkMembersInitList(memberList));
4442    else
4443       inst.members->Add(MkMembersInitList(memberList));
4444    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4445    {
4446       if(!dataMember.isProperty)
4447       {
4448          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4449             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4450          else
4451          {
4452             Expression exp { };
4453             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4454             Type type;
4455             void * ptr = inst.data + dataMember.offset;
4456             char * result = null;
4457
4458             exp.loc = member.loc = inst.loc;
4459             ((Identifier)member.identifiers->first).loc = inst.loc;
4460
4461             if(!dataMember.dataType)
4462                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4463             type = dataMember.dataType;
4464             if(type.kind == classType)
4465             {
4466                Class _class = type._class.registered;
4467                if(_class.type == enumClass)
4468                {
4469                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4470                   if(enumClass)
4471                   {
4472                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4473                      NamedLink item;
4474                      for(item = e.values.first; item; item = item.next)
4475                      {
4476                         if((int)item.data == *(int *)ptr)
4477                         {
4478                            result = item.name;
4479                            break;
4480                         }
4481                      }
4482                   }
4483                   if(result)
4484                   {
4485                      exp.identifier = MkIdentifier(result);
4486                      exp.type = identifierExp;
4487                      exp.destType = MkClassType(_class.fullName);
4488                      ProcessExpressionType(exp);
4489                   }
4490                }
4491                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4492                {
4493                   if(!_class.dataType)
4494                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4495                   type = _class.dataType;
4496                }
4497             }
4498             if(!result)
4499             {
4500                switch(type.kind)
4501                {
4502                   case floatType:
4503                   {
4504                      exp.constant = PrintFloat(*(float*)ptr);
4505                      exp.type = constantExp;
4506                      break;
4507                   }
4508                   case doubleType:
4509                   {
4510                      exp.constant = PrintDouble(*(double*)ptr);
4511                      exp.type = constantExp;
4512                      break;
4513                   }
4514                   case intType:
4515                   {
4516                      exp.constant = PrintInt(*(int*)ptr);
4517                      exp.type = constantExp;
4518                      break;
4519                   }
4520                   case int64Type:
4521                   {
4522                      exp.constant = PrintInt64(*(int64*)ptr);
4523                      exp.type = constantExp;
4524                      break;
4525                   }
4526                   case intPtrType:
4527                   {
4528                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4529                      exp.type = constantExp;
4530                      break;
4531                   }
4532                   default:
4533                      Compiler_Error($"Unhandled type populating instance\n");
4534                }
4535             }
4536             ListAdd(memberList, member);
4537          }
4538       }
4539    }
4540 }
4541
4542 void ComputeInstantiation(Expression exp)
4543 {
4544    Instantiation inst = exp.instance;
4545    MembersInit members;
4546    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4547    Class _class = classSym ? classSym.registered : null;
4548    DataMember curMember = null;
4549    Class curClass = null;
4550    DataMember subMemberStack[256];
4551    int subMemberStackPos = 0;
4552    uint64 bits = 0;
4553
4554    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4555    {
4556       // Don't recompute the instantiation...
4557       // Non Simple classes will have become constants by now
4558       if(inst.data)
4559          return;
4560
4561       if(_class.type == normalClass || _class.type == noHeadClass)
4562       {
4563          inst.data = (byte *)eInstance_New(_class);
4564          if(_class.type == normalClass)
4565             ((Instance)inst.data)._refCount++;
4566       }
4567       else
4568          inst.data = new0 byte[_class.structSize];
4569    }
4570
4571    if(inst.members)
4572    {
4573       for(members = inst.members->first; members; members = members.next)
4574       {
4575          switch(members.type)
4576          {
4577             case dataMembersInit:
4578             {
4579                if(members.dataMembers)
4580                {
4581                   MemberInit member;
4582                   for(member = members.dataMembers->first; member; member = member.next)
4583                   {
4584                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4585                      bool found = false;
4586
4587                      Property prop = null;
4588                      DataMember dataMember = null;
4589                      Method method = null;
4590                      uint dataMemberOffset;
4591
4592                      if(!ident)
4593                      {
4594                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4595                         if(curMember)
4596                         {
4597                            if(curMember.isProperty)
4598                               prop = (Property)curMember;
4599                            else
4600                            {
4601                               dataMember = curMember;
4602
4603                               // CHANGED THIS HERE
4604                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4605
4606                               // 2013/17/29 -- It seems that this was missing here!
4607                               if(_class.type == normalClass)
4608                                  dataMemberOffset += _class.base.structSize;
4609                               // dataMemberOffset = dataMember.offset;
4610                            }
4611                            found = true;
4612                         }
4613                      }
4614                      else
4615                      {
4616                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4617                         if(prop)
4618                         {
4619                            found = true;
4620                            if(prop.memberAccess == publicAccess)
4621                            {
4622                               curMember = (DataMember)prop;
4623                               curClass = prop._class;
4624                            }
4625                         }
4626                         else
4627                         {
4628                            DataMember _subMemberStack[256];
4629                            int _subMemberStackPos = 0;
4630
4631                            // FILL MEMBER STACK
4632                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4633
4634                            if(dataMember)
4635                            {
4636                               found = true;
4637                               if(dataMember.memberAccess == publicAccess)
4638                               {
4639                                  curMember = dataMember;
4640                                  curClass = dataMember._class;
4641                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4642                                  subMemberStackPos = _subMemberStackPos;
4643                               }
4644                            }
4645                         }
4646                      }
4647
4648                      if(found && member.initializer && member.initializer.type == expInitializer)
4649                      {
4650                         Expression value = member.initializer.exp;
4651                         Type type = null;
4652                         bool deepMember = false;
4653                         if(prop)
4654                         {
4655                            type = prop.dataType;
4656                         }
4657                         else if(dataMember)
4658                         {
4659                            if(!dataMember.dataType)
4660                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4661
4662                            type = dataMember.dataType;
4663                         }
4664
4665                         if(ident && ident.next)
4666                         {
4667                            deepMember = true;
4668
4669                            // for(; ident && type; ident = ident.next)
4670                            for(ident = ident.next; ident && type; ident = ident.next)
4671                            {
4672                               if(type.kind == classType)
4673                               {
4674                                  prop = eClass_FindProperty(type._class.registered,
4675                                     ident.string, privateModule);
4676                                  if(prop)
4677                                     type = prop.dataType;
4678                                  else
4679                                  {
4680                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4681                                        ident.string, &dataMemberOffset, privateModule, null, null);
4682                                     if(dataMember)
4683                                        type = dataMember.dataType;
4684                                  }
4685                               }
4686                               else if(type.kind == structType || type.kind == unionType)
4687                               {
4688                                  Type memberType;
4689                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4690                                  {
4691                                     if(!strcmp(memberType.name, ident.string))
4692                                     {
4693                                        type = memberType;
4694                                        break;
4695                                     }
4696                                  }
4697                               }
4698                            }
4699                         }
4700                         if(value)
4701                         {
4702                            FreeType(value.destType);
4703                            value.destType = type;
4704                            if(type) type.refCount++;
4705                            ComputeExpression(value);
4706                         }
4707                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4708                         {
4709                            if(type.kind == classType)
4710                            {
4711                               Class _class = type._class.registered;
4712                               if(_class.type == bitClass || _class.type == unitClass ||
4713                                  _class.type == enumClass)
4714                               {
4715                                  if(!_class.dataType)
4716                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4717                                  type = _class.dataType;
4718                               }
4719                            }
4720
4721                            if(dataMember)
4722                            {
4723                               void * ptr = inst.data + dataMemberOffset;
4724
4725                               if(value.type == constantExp)
4726                               {
4727                                  switch(type.kind)
4728                                  {
4729                                     case intType:
4730                                     {
4731                                        GetInt(value, (int*)ptr);
4732                                        break;
4733                                     }
4734                                     case int64Type:
4735                                     {
4736                                        GetInt64(value, (int64*)ptr);
4737                                        break;
4738                                     }
4739                                     case intPtrType:
4740                                     {
4741                                        GetIntPtr(value, (intptr*)ptr);
4742                                        break;
4743                                     }
4744                                     case intSizeType:
4745                                     {
4746                                        GetIntSize(value, (intsize*)ptr);
4747                                        break;
4748                                     }
4749                                     case floatType:
4750                                     {
4751                                        GetFloat(value, (float*)ptr);
4752                                        break;
4753                                     }
4754                                     case doubleType:
4755                                     {
4756                                        GetDouble(value, (double *)ptr);
4757                                        break;
4758                                     }
4759                                  }
4760                               }
4761                               else if(value.type == instanceExp)
4762                               {
4763                                  if(type.kind == classType)
4764                                  {
4765                                     Class _class = type._class.registered;
4766                                     if(_class.type == structClass)
4767                                     {
4768                                        ComputeTypeSize(type);
4769                                        if(value.instance.data)
4770                                           memcpy(ptr, value.instance.data, type.size);
4771                                     }
4772                                  }
4773                               }
4774                            }
4775                            else if(prop)
4776                            {
4777                               if(value.type == instanceExp && value.instance.data)
4778                               {
4779                                  if(type.kind == classType)
4780                                  {
4781                                     Class _class = type._class.registered;
4782                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4783                                     {
4784                                        void (*Set)(void *, void *) = (void *)prop.Set;
4785                                        Set(inst.data, value.instance.data);
4786                                        PopulateInstance(inst);
4787                                     }
4788                                  }
4789                               }
4790                               else if(value.type == constantExp)
4791                               {
4792                                  switch(type.kind)
4793                                  {
4794                                     case doubleType:
4795                                     {
4796                                        void (*Set)(void *, double) = (void *)prop.Set;
4797                                        Set(inst.data, strtod(value.constant, null) );
4798                                        break;
4799                                     }
4800                                     case floatType:
4801                                     {
4802                                        void (*Set)(void *, float) = (void *)prop.Set;
4803                                        Set(inst.data, (float)(strtod(value.constant, null)));
4804                                        break;
4805                                     }
4806                                     case intType:
4807                                     {
4808                                        void (*Set)(void *, int) = (void *)prop.Set;
4809                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4810                                        break;
4811                                     }
4812                                     case int64Type:
4813                                     {
4814                                        void (*Set)(void *, int64) = (void *)prop.Set;
4815                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4816                                        break;
4817                                     }
4818                                     case intPtrType:
4819                                     {
4820                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4821                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4822                                        break;
4823                                     }
4824                                     case intSizeType:
4825                                     {
4826                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4827                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4828                                        break;
4829                                     }
4830                                  }
4831                               }
4832                               else if(value.type == stringExp)
4833                               {
4834                                  char temp[1024];
4835                                  ReadString(temp, value.string);
4836                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4837                               }
4838                            }
4839                         }
4840                         else if(!deepMember && type && _class.type == unitClass)
4841                         {
4842                            if(prop)
4843                            {
4844                               // Only support converting units to units for now...
4845                               if(value.type == constantExp)
4846                               {
4847                                  if(type.kind == classType)
4848                                  {
4849                                     Class _class = type._class.registered;
4850                                     if(_class.type == unitClass)
4851                                     {
4852                                        if(!_class.dataType)
4853                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4854                                        type = _class.dataType;
4855                                     }
4856                                  }
4857                                  // TODO: Assuming same base type for units...
4858                                  switch(type.kind)
4859                                  {
4860                                     case floatType:
4861                                     {
4862                                        float fValue;
4863                                        float (*Set)(float) = (void *)prop.Set;
4864                                        GetFloat(member.initializer.exp, &fValue);
4865                                        exp.constant = PrintFloat(Set(fValue));
4866                                        exp.type = constantExp;
4867                                        break;
4868                                     }
4869                                     case doubleType:
4870                                     {
4871                                        double dValue;
4872                                        double (*Set)(double) = (void *)prop.Set;
4873                                        GetDouble(member.initializer.exp, &dValue);
4874                                        exp.constant = PrintDouble(Set(dValue));
4875                                        exp.type = constantExp;
4876                                        break;
4877                                     }
4878                                  }
4879                               }
4880                            }
4881                         }
4882                         else if(!deepMember && type && _class.type == bitClass)
4883                         {
4884                            if(prop)
4885                            {
4886                               if(value.type == instanceExp && value.instance.data)
4887                               {
4888                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4889                                  bits = Set(value.instance.data);
4890                               }
4891                               else if(value.type == constantExp)
4892                               {
4893                               }
4894                            }
4895                            else if(dataMember)
4896                            {
4897                               BitMember bitMember = (BitMember) dataMember;
4898                               Type type;
4899                               int part = 0;
4900                               GetInt(value, &part);
4901                               bits = (bits & ~bitMember.mask);
4902                               if(!bitMember.dataType)
4903                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4904
4905                               type = bitMember.dataType;
4906
4907                               if(type.kind == classType && type._class && type._class.registered)
4908                               {
4909                                  if(!type._class.registered.dataType)
4910                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4911                                  type = type._class.registered.dataType;
4912                               }
4913
4914                               switch(type.kind)
4915                               {
4916                                  case _BoolType:
4917                                  case charType:
4918                                     if(type.isSigned)
4919                                        bits |= ((char)part << bitMember.pos);
4920                                     else
4921                                        bits |= ((unsigned char)part << bitMember.pos);
4922                                     break;
4923                                  case shortType:
4924                                     if(type.isSigned)
4925                                        bits |= ((short)part << bitMember.pos);
4926                                     else
4927                                        bits |= ((unsigned short)part << bitMember.pos);
4928                                     break;
4929                                  case intType:
4930                                  case longType:
4931                                     if(type.isSigned)
4932                                        bits |= ((int)part << bitMember.pos);
4933                                     else
4934                                        bits |= ((unsigned int)part << bitMember.pos);
4935                                     break;
4936                                  case int64Type:
4937                                     if(type.isSigned)
4938                                        bits |= ((int64)part << bitMember.pos);
4939                                     else
4940                                        bits |= ((uint64)part << bitMember.pos);
4941                                     break;
4942                                  case intPtrType:
4943                                     if(type.isSigned)
4944                                     {
4945                                        bits |= ((intptr)part << bitMember.pos);
4946                                     }
4947                                     else
4948                                     {
4949                                        bits |= ((uintptr)part << bitMember.pos);
4950                                     }
4951                                     break;
4952                                  case intSizeType:
4953                                     if(type.isSigned)
4954                                     {
4955                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4956                                     }
4957                                     else
4958                                     {
4959                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4960                                     }
4961                                     break;
4962                               }
4963                            }
4964                         }
4965                      }
4966                      else
4967                      {
4968                         if(_class && _class.type == unitClass)
4969                         {
4970                            ComputeExpression(member.initializer.exp);
4971                            exp.constant = member.initializer.exp.constant;
4972                            exp.type = constantExp;
4973
4974                            member.initializer.exp.constant = null;
4975                         }
4976                      }
4977                   }
4978                }
4979                break;
4980             }
4981          }
4982       }
4983    }
4984    if(_class && _class.type == bitClass)
4985    {
4986       exp.constant = PrintHexUInt(bits);
4987       exp.type = constantExp;
4988    }
4989    if(exp.type != instanceExp)
4990    {
4991       FreeInstance(inst);
4992    }
4993 }
4994
4995 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4996 {
4997    if(exp.op.op == SIZEOF)
4998    {
4999       FreeExpContents(exp);
5000       exp.type = constantExp;
5001       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5002    }
5003    else
5004    {
5005       if(!exp.op.exp1)
5006       {
5007          switch(exp.op.op)
5008          {
5009             // unary arithmetic
5010             case '+':
5011             {
5012                // Provide default unary +
5013                Expression exp2 = exp.op.exp2;
5014                exp.op.exp2 = null;
5015                FreeExpContents(exp);
5016                FreeType(exp.expType);
5017                FreeType(exp.destType);
5018                *exp = *exp2;
5019                delete exp2;
5020                break;
5021             }
5022             case '-':
5023                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5024                break;
5025             // unary arithmetic increment and decrement
5026                   //OPERATOR_ALL(UNARY, ++, Inc)
5027                   //OPERATOR_ALL(UNARY, --, Dec)
5028             // unary bitwise
5029             case '~':
5030                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5031                break;
5032             // unary logical negation
5033             case '!':
5034                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5035                break;
5036          }
5037       }
5038       else
5039       {
5040          switch(exp.op.op)
5041          {
5042             // binary arithmetic
5043             case '+':
5044                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5045                break;
5046             case '-':
5047                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5048                break;
5049             case '*':
5050                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5051                break;
5052             case '/':
5053                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5054                break;
5055             case '%':
5056                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5057                break;
5058             // binary arithmetic assignment
5059                   //OPERATOR_ALL(BINARY, =, Asign)
5060                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5061                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5062                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5063                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5064                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5065             // binary bitwise
5066             case '&':
5067                if(exp.op.exp2)
5068                {
5069                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5070                }
5071                break;
5072             case '|':
5073                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5074                break;
5075             case '^':
5076                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5077                break;
5078             case LEFT_OP:
5079                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5080                break;
5081             case RIGHT_OP:
5082                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5083                break;
5084             // binary bitwise assignment
5085                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5086                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5087                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5088                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5089                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5090             // binary logical equality
5091             case EQ_OP:
5092                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5093                break;
5094             case NE_OP:
5095                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5096                break;
5097             // binary logical
5098             case AND_OP:
5099                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5100                break;
5101             case OR_OP:
5102                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5103                break;
5104             // binary logical relational
5105             case '>':
5106                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5107                break;
5108             case '<':
5109                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5110                break;
5111             case GE_OP:
5112                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5113                break;
5114             case LE_OP:
5115                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5116                break;
5117          }
5118       }
5119    }
5120 }
5121
5122 void ComputeExpression(Expression exp)
5123 {
5124    char expString[10240];
5125    expString[0] = '\0';
5126 #ifdef _DEBUG
5127    PrintExpression(exp, expString);
5128 #endif
5129
5130    switch(exp.type)
5131    {
5132       case instanceExp:
5133       {
5134          ComputeInstantiation(exp);
5135          break;
5136       }
5137       /*
5138       case constantExp:
5139          break;
5140       */
5141       case opExp:
5142       {
5143          Expression exp1, exp2 = null;
5144          Operand op1 { };
5145          Operand op2 { };
5146
5147          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5148          if(exp.op.exp2)
5149             ComputeExpression(exp.op.exp2);
5150          if(exp.op.exp1)
5151          {
5152             ComputeExpression(exp.op.exp1);
5153             exp1 = exp.op.exp1;
5154             exp2 = exp.op.exp2;
5155             op1 = GetOperand(exp1);
5156             if(op1.type) op1.type.refCount++;
5157             if(exp2)
5158             {
5159                op2 = GetOperand(exp2);
5160                if(op2.type) op2.type.refCount++;
5161             }
5162          }
5163          else
5164          {
5165             exp1 = exp.op.exp2;
5166             op1 = GetOperand(exp1);
5167             if(op1.type) op1.type.refCount++;
5168          }
5169
5170          CallOperator(exp, exp1, exp2, op1, op2);
5171          /*
5172          switch(exp.op.op)
5173          {
5174             // Unary operators
5175             case '&':
5176                // Also binary
5177                if(exp.op.exp1 && exp.op.exp2)
5178                {
5179                   // Binary And
5180                   if(op1.ops.BitAnd)
5181                   {
5182                      FreeExpContents(exp);
5183                      op1.ops.BitAnd(exp, op1, op2);
5184                   }
5185                }
5186                break;
5187             case '*':
5188                if(exp.op.exp1)
5189                {
5190                   if(op1.ops.Mul)
5191                   {
5192                      FreeExpContents(exp);
5193                      op1.ops.Mul(exp, op1, op2);
5194                   }
5195                }
5196                break;
5197             case '+':
5198                if(exp.op.exp1)
5199                {
5200                   if(op1.ops.Add)
5201                   {
5202                      FreeExpContents(exp);
5203                      op1.ops.Add(exp, op1, op2);
5204                   }
5205                }
5206                else
5207                {
5208                   // Provide default unary +
5209                   Expression exp2 = exp.op.exp2;
5210                   exp.op.exp2 = null;
5211                   FreeExpContents(exp);
5212                   FreeType(exp.expType);
5213                   FreeType(exp.destType);
5214
5215                   *exp = *exp2;
5216                   delete exp2;
5217                }
5218                break;
5219             case '-':
5220                if(exp.op.exp1)
5221                {
5222                   if(op1.ops.Sub)
5223                   {
5224                      FreeExpContents(exp);
5225                      op1.ops.Sub(exp, op1, op2);
5226                   }
5227                }
5228                else
5229                {
5230                   if(op1.ops.Neg)
5231                   {
5232                      FreeExpContents(exp);
5233                      op1.ops.Neg(exp, op1);
5234                   }
5235                }
5236                break;
5237             case '~':
5238                if(op1.ops.BitNot)
5239                {
5240                   FreeExpContents(exp);
5241                   op1.ops.BitNot(exp, op1);
5242                }
5243                break;
5244             case '!':
5245                if(op1.ops.Not)
5246                {
5247                   FreeExpContents(exp);
5248                   op1.ops.Not(exp, op1);
5249                }
5250                break;
5251             // Binary only operators
5252             case '/':
5253                if(op1.ops.Div)
5254                {
5255                   FreeExpContents(exp);
5256                   op1.ops.Div(exp, op1, op2);
5257                }
5258                break;
5259             case '%':
5260                if(op1.ops.Mod)
5261                {
5262                   FreeExpContents(exp);
5263                   op1.ops.Mod(exp, op1, op2);
5264                }
5265                break;
5266             case LEFT_OP:
5267                break;
5268             case RIGHT_OP:
5269                break;
5270             case '<':
5271                if(exp.op.exp1)
5272                {
5273                   if(op1.ops.Sma)
5274                   {
5275                      FreeExpContents(exp);
5276                      op1.ops.Sma(exp, op1, op2);
5277                   }
5278                }
5279                break;
5280             case '>':
5281                if(exp.op.exp1)
5282                {
5283                   if(op1.ops.Grt)
5284                   {
5285                      FreeExpContents(exp);
5286                      op1.ops.Grt(exp, op1, op2);
5287                   }
5288                }
5289                break;
5290             case LE_OP:
5291                if(exp.op.exp1)
5292                {
5293                   if(op1.ops.SmaEqu)
5294                   {
5295                      FreeExpContents(exp);
5296                      op1.ops.SmaEqu(exp, op1, op2);
5297                   }
5298                }
5299                break;
5300             case GE_OP:
5301                if(exp.op.exp1)
5302                {
5303                   if(op1.ops.GrtEqu)
5304                   {
5305                      FreeExpContents(exp);
5306                      op1.ops.GrtEqu(exp, op1, op2);
5307                   }
5308                }
5309                break;
5310             case EQ_OP:
5311                if(exp.op.exp1)
5312                {
5313                   if(op1.ops.Equ)
5314                   {
5315                      FreeExpContents(exp);
5316                      op1.ops.Equ(exp, op1, op2);
5317                   }
5318                }
5319                break;
5320             case NE_OP:
5321                if(exp.op.exp1)
5322                {
5323                   if(op1.ops.Nqu)
5324                   {
5325                      FreeExpContents(exp);
5326                      op1.ops.Nqu(exp, op1, op2);
5327                   }
5328                }
5329                break;
5330             case '|':
5331                if(op1.ops.BitOr)
5332                {
5333                   FreeExpContents(exp);
5334                   op1.ops.BitOr(exp, op1, op2);
5335                }
5336                break;
5337             case '^':
5338                if(op1.ops.BitXor)
5339                {
5340                   FreeExpContents(exp);
5341                   op1.ops.BitXor(exp, op1, op2);
5342                }
5343                break;
5344             case AND_OP:
5345                break;
5346             case OR_OP:
5347                break;
5348             case SIZEOF:
5349                FreeExpContents(exp);
5350                exp.type = constantExp;
5351                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5352                break;
5353          }
5354          */
5355          if(op1.type) FreeType(op1.type);
5356          if(op2.type) FreeType(op2.type);
5357          break;
5358       }
5359       case bracketsExp:
5360       case extensionExpressionExp:
5361       {
5362          Expression e, n;
5363          for(e = exp.list->first; e; e = n)
5364          {
5365             n = e.next;
5366             if(!n)
5367             {
5368                OldList * list = exp.list;
5369                ComputeExpression(e);
5370                //FreeExpContents(exp);
5371                FreeType(exp.expType);
5372                FreeType(exp.destType);
5373                *exp = *e;
5374                delete e;
5375                delete list;
5376             }
5377             else
5378             {
5379                FreeExpression(e);
5380             }
5381          }
5382          break;
5383       }
5384       /*
5385
5386       case ExpIndex:
5387       {
5388          Expression e;
5389          exp.isConstant = true;
5390
5391          ComputeExpression(exp.index.exp);
5392          if(!exp.index.exp.isConstant)
5393             exp.isConstant = false;
5394
5395          for(e = exp.index.index->first; e; e = e.next)
5396          {
5397             ComputeExpression(e);
5398             if(!e.next)
5399             {
5400                // Check if this type is int
5401             }
5402             if(!e.isConstant)
5403                exp.isConstant = false;
5404          }
5405          exp.expType = Dereference(exp.index.exp.expType);
5406          break;
5407       }
5408       */
5409       case memberExp:
5410       {
5411          Expression memberExp = exp.member.exp;
5412          Identifier memberID = exp.member.member;
5413
5414          Type type;
5415          ComputeExpression(exp.member.exp);
5416          type = exp.member.exp.expType;
5417          if(type)
5418          {
5419             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);
5420             Property prop = null;
5421             DataMember member = null;
5422             Class convertTo = null;
5423             if(type.kind == subClassType && exp.member.exp.type == classExp)
5424                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5425
5426             if(!_class)
5427             {
5428                char string[256];
5429                Symbol classSym;
5430                string[0] = '\0';
5431                PrintTypeNoConst(type, string, false, true);
5432                classSym = FindClass(string);
5433                _class = classSym ? classSym.registered : null;
5434             }
5435
5436             if(exp.member.member)
5437             {
5438                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5439                if(!prop)
5440                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5441             }
5442             if(!prop && !member && _class && exp.member.member)
5443             {
5444                Symbol classSym = FindClass(exp.member.member.string);
5445                convertTo = _class;
5446                _class = classSym ? classSym.registered : null;
5447                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5448             }
5449
5450             if(prop)
5451             {
5452                if(prop.compiled)
5453                {
5454                   Type type = prop.dataType;
5455                   // TODO: Assuming same base type for units...
5456                   if(_class.type == unitClass)
5457                   {
5458                      if(type.kind == classType)
5459                      {
5460                         Class _class = type._class.registered;
5461                         if(_class.type == unitClass)
5462                         {
5463                            if(!_class.dataType)
5464                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5465                            type = _class.dataType;
5466                         }
5467                      }
5468                      switch(type.kind)
5469                      {
5470                         case floatType:
5471                         {
5472                            float value;
5473                            float (*Get)(float) = (void *)prop.Get;
5474                            GetFloat(exp.member.exp, &value);
5475                            exp.constant = PrintFloat(Get ? Get(value) : value);
5476                            exp.type = constantExp;
5477                            break;
5478                         }
5479                         case doubleType:
5480                         {
5481                            double value;
5482                            double (*Get)(double);
5483                            GetDouble(exp.member.exp, &value);
5484
5485                            if(convertTo)
5486                               Get = (void *)prop.Set;
5487                            else
5488                               Get = (void *)prop.Get;
5489                            exp.constant = PrintDouble(Get ? Get(value) : value);
5490                            exp.type = constantExp;
5491                            break;
5492                         }
5493                      }
5494                   }
5495                   else
5496                   {
5497                      if(convertTo)
5498                      {
5499                         Expression value = exp.member.exp;
5500                         Type type;
5501                         if(!prop.dataType)
5502                            ProcessPropertyType(prop);
5503
5504                         type = prop.dataType;
5505                         if(!type)
5506                         {
5507                             // printf("Investigate this\n");
5508                         }
5509                         else if(_class.type == structClass)
5510                         {
5511                            switch(type.kind)
5512                            {
5513                               case classType:
5514                               {
5515                                  Class propertyClass = type._class.registered;
5516                                  if(propertyClass.type == structClass && value.type == instanceExp)
5517                                  {
5518                                     void (*Set)(void *, void *) = (void *)prop.Set;
5519                                     exp.instance = Instantiation { };
5520                                     exp.instance.data = new0 byte[_class.structSize];
5521                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5522                                     exp.instance.loc = exp.loc;
5523                                     exp.type = instanceExp;
5524                                     Set(exp.instance.data, value.instance.data);
5525                                     PopulateInstance(exp.instance);
5526                                  }
5527                                  break;
5528                               }
5529                               case intType:
5530                               {
5531                                  int intValue;
5532                                  void (*Set)(void *, int) = (void *)prop.Set;
5533
5534                                  exp.instance = Instantiation { };
5535                                  exp.instance.data = new0 byte[_class.structSize];
5536                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5537                                  exp.instance.loc = exp.loc;
5538                                  exp.type = instanceExp;
5539
5540                                  GetInt(value, &intValue);
5541
5542                                  Set(exp.instance.data, intValue);
5543                                  PopulateInstance(exp.instance);
5544                                  break;
5545                               }
5546                               case int64Type:
5547                               {
5548                                  int64 intValue;
5549                                  void (*Set)(void *, int64) = (void *)prop.Set;
5550
5551                                  exp.instance = Instantiation { };
5552                                  exp.instance.data = new0 byte[_class.structSize];
5553                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5554                                  exp.instance.loc = exp.loc;
5555                                  exp.type = instanceExp;
5556
5557                                  GetInt64(value, &intValue);
5558
5559                                  Set(exp.instance.data, intValue);
5560                                  PopulateInstance(exp.instance);
5561                                  break;
5562                               }
5563                               case intPtrType:
5564                               {
5565                                  // TOFIX:
5566                                  intptr intValue;
5567                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5568
5569                                  exp.instance = Instantiation { };
5570                                  exp.instance.data = new0 byte[_class.structSize];
5571                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5572                                  exp.instance.loc = exp.loc;
5573                                  exp.type = instanceExp;
5574
5575                                  GetIntPtr(value, &intValue);
5576
5577                                  Set(exp.instance.data, intValue);
5578                                  PopulateInstance(exp.instance);
5579                                  break;
5580                               }
5581                               case intSizeType:
5582                               {
5583                                  // TOFIX:
5584                                  intsize intValue;
5585                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5586
5587                                  exp.instance = Instantiation { };
5588                                  exp.instance.data = new0 byte[_class.structSize];
5589                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5590                                  exp.instance.loc = exp.loc;
5591                                  exp.type = instanceExp;
5592
5593                                  GetIntSize(value, &intValue);
5594
5595                                  Set(exp.instance.data, intValue);
5596                                  PopulateInstance(exp.instance);
5597                                  break;
5598                               }
5599                               case doubleType:
5600                               {
5601                                  double doubleValue;
5602                                  void (*Set)(void *, double) = (void *)prop.Set;
5603
5604                                  exp.instance = Instantiation { };
5605                                  exp.instance.data = new0 byte[_class.structSize];
5606                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5607                                  exp.instance.loc = exp.loc;
5608                                  exp.type = instanceExp;
5609
5610                                  GetDouble(value, &doubleValue);
5611
5612                                  Set(exp.instance.data, doubleValue);
5613                                  PopulateInstance(exp.instance);
5614                                  break;
5615                               }
5616                            }
5617                         }
5618                         else if(_class.type == bitClass)
5619                         {
5620                            switch(type.kind)
5621                            {
5622                               case classType:
5623                               {
5624                                  Class propertyClass = type._class.registered;
5625                                  if(propertyClass.type == structClass && value.instance.data)
5626                                  {
5627                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5628                                     unsigned int bits = Set(value.instance.data);
5629                                     exp.constant = PrintHexUInt(bits);
5630                                     exp.type = constantExp;
5631                                     break;
5632                                  }
5633                                  else if(_class.type == bitClass)
5634                                  {
5635                                     unsigned int value;
5636                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5637                                     unsigned int bits;
5638
5639                                     GetUInt(exp.member.exp, &value);
5640                                     bits = Set(value);
5641                                     exp.constant = PrintHexUInt(bits);
5642                                     exp.type = constantExp;
5643                                  }
5644                               }
5645                            }
5646                         }
5647                      }
5648                      else
5649                      {
5650                         if(_class.type == bitClass)
5651                         {
5652                            unsigned int value;
5653                            GetUInt(exp.member.exp, &value);
5654
5655                            switch(type.kind)
5656                            {
5657                               case classType:
5658                               {
5659                                  Class _class = type._class.registered;
5660                                  if(_class.type == structClass)
5661                                  {
5662                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5663
5664                                     exp.instance = Instantiation { };
5665                                     exp.instance.data = new0 byte[_class.structSize];
5666                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5667                                     exp.instance.loc = exp.loc;
5668                                     //exp.instance.fullSet = true;
5669                                     exp.type = instanceExp;
5670                                     Get(value, exp.instance.data);
5671                                     PopulateInstance(exp.instance);
5672                                  }
5673                                  else if(_class.type == bitClass)
5674                                  {
5675                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5676                                     uint64 bits = Get(value);
5677                                     exp.constant = PrintHexUInt64(bits);
5678                                     exp.type = constantExp;
5679                                  }
5680                                  break;
5681                               }
5682                            }
5683                         }
5684                         else if(_class.type == structClass)
5685                         {
5686                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5687                            switch(type.kind)
5688                            {
5689                               case classType:
5690                               {
5691                                  Class _class = type._class.registered;
5692                                  if(_class.type == structClass && value)
5693                                  {
5694                                     void (*Get)(void *, void *) = (void *)prop.Get;
5695
5696                                     exp.instance = Instantiation { };
5697                                     exp.instance.data = new0 byte[_class.structSize];
5698                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5699                                     exp.instance.loc = exp.loc;
5700                                     //exp.instance.fullSet = true;
5701                                     exp.type = instanceExp;
5702                                     Get(value, exp.instance.data);
5703                                     PopulateInstance(exp.instance);
5704                                  }
5705                                  break;
5706                               }
5707                            }
5708                         }
5709                         /*else
5710                         {
5711                            char * value = exp.member.exp.instance.data;
5712                            switch(type.kind)
5713                            {
5714                               case classType:
5715                               {
5716                                  Class _class = type._class.registered;
5717                                  if(_class.type == normalClass)
5718                                  {
5719                                     void *(*Get)(void *) = (void *)prop.Get;
5720
5721                                     exp.instance = Instantiation { };
5722                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5723                                     exp.type = instanceExp;
5724                                     exp.instance.data = Get(value, exp.instance.data);
5725                                  }
5726                                  break;
5727                               }
5728                            }
5729                         }
5730                         */
5731                      }
5732                   }
5733                }
5734                else
5735                {
5736                   exp.isConstant = false;
5737                }
5738             }
5739             else if(member)
5740             {
5741             }
5742          }
5743
5744          if(exp.type != ExpressionType::memberExp)
5745          {
5746             FreeExpression(memberExp);
5747             FreeIdentifier(memberID);
5748          }
5749          break;
5750       }
5751       case typeSizeExp:
5752       {
5753          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5754          FreeExpContents(exp);
5755          exp.constant = PrintUInt(ComputeTypeSize(type));
5756          exp.type = constantExp;
5757          FreeType(type);
5758          break;
5759       }
5760       case classSizeExp:
5761       {
5762          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5763          if(classSym && classSym.registered)
5764          {
5765             if(classSym.registered.fixed)
5766             {
5767                FreeSpecifier(exp._class);
5768                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5769                exp.type = constantExp;
5770             }
5771             else
5772             {
5773                char className[1024];
5774                strcpy(className, "__ecereClass_");
5775                FullClassNameCat(className, classSym.string, true);
5776                MangleClassName(className);
5777
5778                DeclareClass(classSym, className);
5779
5780                FreeExpContents(exp);
5781                exp.type = pointerExp;
5782                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5783                exp.member.member = MkIdentifier("structSize");
5784             }
5785          }
5786          break;
5787       }
5788       case castExp:
5789       //case constantExp:
5790       {
5791          Type type;
5792          Expression e = exp;
5793          if(exp.type == castExp)
5794          {
5795             if(exp.cast.exp)
5796                ComputeExpression(exp.cast.exp);
5797             e = exp.cast.exp;
5798          }
5799          if(e && exp.expType)
5800          {
5801             /*if(exp.destType)
5802                type = exp.destType;
5803             else*/
5804                type = exp.expType;
5805             if(type.kind == classType)
5806             {
5807                Class _class = type._class.registered;
5808                if(_class && (_class.type == unitClass || _class.type == bitClass))
5809                {
5810                   if(!_class.dataType)
5811                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5812                   type = _class.dataType;
5813                }
5814             }
5815
5816             switch(type.kind)
5817             {
5818                case _BoolType:
5819                case charType:
5820                   if(type.isSigned)
5821                   {
5822                      char value;
5823                      GetChar(e, &value);
5824                      FreeExpContents(exp);
5825                      exp.constant = PrintChar(value);
5826                      exp.type = constantExp;
5827                   }
5828                   else
5829                   {
5830                      unsigned char value;
5831                      GetUChar(e, &value);
5832                      FreeExpContents(exp);
5833                      exp.constant = PrintUChar(value);
5834                      exp.type = constantExp;
5835                   }
5836                   break;
5837                case shortType:
5838                   if(type.isSigned)
5839                   {
5840                      short value;
5841                      GetShort(e, &value);
5842                      FreeExpContents(exp);
5843                      exp.constant = PrintShort(value);
5844                      exp.type = constantExp;
5845                   }
5846                   else
5847                   {
5848                      unsigned short value;
5849                      GetUShort(e, &value);
5850                      FreeExpContents(exp);
5851                      exp.constant = PrintUShort(value);
5852                      exp.type = constantExp;
5853                   }
5854                   break;
5855                case intType:
5856                   if(type.isSigned)
5857                   {
5858                      int value;
5859                      GetInt(e, &value);
5860                      FreeExpContents(exp);
5861                      exp.constant = PrintInt(value);
5862                      exp.type = constantExp;
5863                   }
5864                   else
5865                   {
5866                      unsigned int value;
5867                      GetUInt(e, &value);
5868                      FreeExpContents(exp);
5869                      exp.constant = PrintUInt(value);
5870                      exp.type = constantExp;
5871                   }
5872                   break;
5873                case int64Type:
5874                   if(type.isSigned)
5875                   {
5876                      int64 value;
5877                      GetInt64(e, &value);
5878                      FreeExpContents(exp);
5879                      exp.constant = PrintInt64(value);
5880                      exp.type = constantExp;
5881                   }
5882                   else
5883                   {
5884                      uint64 value;
5885                      GetUInt64(e, &value);
5886                      FreeExpContents(exp);
5887                      exp.constant = PrintUInt64(value);
5888                      exp.type = constantExp;
5889                   }
5890                   break;
5891                case intPtrType:
5892                   if(type.isSigned)
5893                   {
5894                      intptr value;
5895                      GetIntPtr(e, &value);
5896                      FreeExpContents(exp);
5897                      exp.constant = PrintInt64((int64)value);
5898                      exp.type = constantExp;
5899                   }
5900                   else
5901                   {
5902                      uintptr value;
5903                      GetUIntPtr(e, &value);
5904                      FreeExpContents(exp);
5905                      exp.constant = PrintUInt64((uint64)value);
5906                      exp.type = constantExp;
5907                   }
5908                   break;
5909                case intSizeType:
5910                   if(type.isSigned)
5911                   {
5912                      intsize value;
5913                      GetIntSize(e, &value);
5914                      FreeExpContents(exp);
5915                      exp.constant = PrintInt64((int64)value);
5916                      exp.type = constantExp;
5917                   }
5918                   else
5919                   {
5920                      uintsize value;
5921                      GetUIntSize(e, &value);
5922                      FreeExpContents(exp);
5923                      exp.constant = PrintUInt64((uint64)value);
5924                      exp.type = constantExp;
5925                   }
5926                   break;
5927                case floatType:
5928                {
5929                   float value;
5930                   GetFloat(e, &value);
5931                   FreeExpContents(exp);
5932                   exp.constant = PrintFloat(value);
5933                   exp.type = constantExp;
5934                   break;
5935                }
5936                case doubleType:
5937                {
5938                   double value;
5939                   GetDouble(e, &value);
5940                   FreeExpContents(exp);
5941                   exp.constant = PrintDouble(value);
5942                   exp.type = constantExp;
5943                   break;
5944                }
5945             }
5946          }
5947          break;
5948       }
5949       case conditionExp:
5950       {
5951          Operand op1 { };
5952          Operand op2 { };
5953          Operand op3 { };
5954
5955          if(exp.cond.exp)
5956             // Caring only about last expression for now...
5957             ComputeExpression(exp.cond.exp->last);
5958          if(exp.cond.elseExp)
5959             ComputeExpression(exp.cond.elseExp);
5960          if(exp.cond.cond)
5961             ComputeExpression(exp.cond.cond);
5962
5963          op1 = GetOperand(exp.cond.cond);
5964          if(op1.type) op1.type.refCount++;
5965          op2 = GetOperand(exp.cond.exp->last);
5966          if(op2.type) op2.type.refCount++;
5967          op3 = GetOperand(exp.cond.elseExp);
5968          if(op3.type) op3.type.refCount++;
5969
5970          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5971          if(op1.type) FreeType(op1.type);
5972          if(op2.type) FreeType(op2.type);
5973          if(op3.type) FreeType(op3.type);
5974          break;
5975       }
5976    }
5977 }
5978
5979 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5980 {
5981    bool result = true;
5982    if(destType)
5983    {
5984       OldList converts { };
5985       Conversion convert;
5986
5987       if(destType.kind == voidType)
5988          return false;
5989
5990       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5991          result = false;
5992       if(converts.count)
5993       {
5994          // for(convert = converts.last; convert; convert = convert.prev)
5995          for(convert = converts.first; convert; convert = convert.next)
5996          {
5997             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5998             if(!empty)
5999             {
6000                Expression newExp { };
6001                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6002
6003                // TODO: Check this...
6004                *newExp = *exp;
6005                newExp.destType = null;
6006
6007                if(convert.isGet)
6008                {
6009                   // [exp].ColorRGB
6010                   exp.type = memberExp;
6011                   exp.addedThis = true;
6012                   exp.member.exp = newExp;
6013                   FreeType(exp.member.exp.expType);
6014
6015                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6016                   exp.member.exp.expType.classObjectType = objectType;
6017                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6018                   exp.member.memberType = propertyMember;
6019                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6020                   // TESTING THIS... for (int)degrees
6021                   exp.needCast = true;
6022                   if(exp.expType) exp.expType.refCount++;
6023                   ApplyAnyObjectLogic(exp.member.exp);
6024                }
6025                else
6026                {
6027
6028                   /*if(exp.isConstant)
6029                   {
6030                      // Color { ColorRGB = [exp] };
6031                      exp.type = instanceExp;
6032                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6033                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6034                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6035                   }
6036                   else*/
6037                   {
6038                      // If not constant, don't turn it yet into an instantiation
6039                      // (Go through the deep members system first)
6040                      exp.type = memberExp;
6041                      exp.addedThis = true;
6042                      exp.member.exp = newExp;
6043
6044                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6045                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6046                         newExp.expType._class.registered.type == noHeadClass)
6047                      {
6048                         newExp.byReference = true;
6049                      }
6050
6051                      FreeType(exp.member.exp.expType);
6052                      /*exp.member.exp.expType = convert.convert.dataType;
6053                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6054                      exp.member.exp.expType = null;
6055                      if(convert.convert.dataType)
6056                      {
6057                         exp.member.exp.expType = { };
6058                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6059                         exp.member.exp.expType.refCount = 1;
6060                         exp.member.exp.expType.classObjectType = objectType;
6061                         ApplyAnyObjectLogic(exp.member.exp);
6062                      }
6063
6064                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6065                      exp.member.memberType = reverseConversionMember;
6066                      exp.expType = convert.resultType ? convert.resultType :
6067                         MkClassType(convert.convert._class.fullName);
6068                      exp.needCast = true;
6069                      if(convert.resultType) convert.resultType.refCount++;
6070                   }
6071                }
6072             }
6073             else
6074             {
6075                FreeType(exp.expType);
6076                if(convert.isGet)
6077                {
6078                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6079                   exp.needCast = true;
6080                   if(exp.expType) exp.expType.refCount++;
6081                }
6082                else
6083                {
6084                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6085                   exp.needCast = true;
6086                   if(convert.resultType)
6087                      convert.resultType.refCount++;
6088                }
6089             }
6090          }
6091          if(exp.isConstant && inCompiler)
6092             ComputeExpression(exp);
6093
6094          converts.Free(FreeConvert);
6095       }
6096
6097       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6098       {
6099          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6100       }
6101       if(!result && exp.expType && exp.destType)
6102       {
6103          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6104              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6105             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6106             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6107             result = true;
6108       }
6109    }
6110    // if(result) CheckTemplateTypes(exp);
6111    return result;
6112 }
6113
6114 void CheckTemplateTypes(Expression exp)
6115 {
6116    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6117    {
6118       Expression newExp { };
6119       Statement compound;
6120       Context context;
6121       *newExp = *exp;
6122       if(exp.destType) exp.destType.refCount++;
6123       if(exp.expType)  exp.expType.refCount++;
6124       newExp.prev = null;
6125       newExp.next = null;
6126
6127       switch(exp.expType.kind)
6128       {
6129          case doubleType:
6130             if(exp.destType.classObjectType)
6131             {
6132                // We need to pass the address, just pass it along (Undo what was done above)
6133                if(exp.destType) exp.destType.refCount--;
6134                if(exp.expType)  exp.expType.refCount--;
6135                delete newExp;
6136             }
6137             else
6138             {
6139                // If we're looking for value:
6140                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6141                OldList * specs;
6142                OldList * unionDefs = MkList();
6143                OldList * statements = MkList();
6144                context = PushContext();
6145                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6146                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6147                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6148                exp.type = extensionCompoundExp;
6149                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6150                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6151                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6152                exp.compound.compound.context = context;
6153                PopContext(context);
6154             }
6155             break;
6156          default:
6157             exp.type = castExp;
6158             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6159             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6160             break;
6161       }
6162    }
6163    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6164    {
6165       Expression newExp { };
6166       Statement compound;
6167       Context context;
6168       *newExp = *exp;
6169       if(exp.destType) exp.destType.refCount++;
6170       if(exp.expType)  exp.expType.refCount++;
6171       newExp.prev = null;
6172       newExp.next = null;
6173
6174       switch(exp.expType.kind)
6175       {
6176          case doubleType:
6177             if(exp.destType.classObjectType)
6178             {
6179                // We need to pass the address, just pass it along (Undo what was done above)
6180                if(exp.destType) exp.destType.refCount--;
6181                if(exp.expType)  exp.expType.refCount--;
6182                delete newExp;
6183             }
6184             else
6185             {
6186                // If we're looking for value:
6187                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6188                OldList * specs;
6189                OldList * unionDefs = MkList();
6190                OldList * statements = MkList();
6191                context = PushContext();
6192                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6193                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6194                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6195                exp.type = extensionCompoundExp;
6196                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6197                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6198                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6199                exp.compound.compound.context = context;
6200                PopContext(context);
6201             }
6202             break;
6203          case classType:
6204          {
6205             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6206             {
6207                exp.type = bracketsExp;
6208                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6209                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6210                ProcessExpressionType(exp.list->first);
6211                break;
6212             }
6213             else
6214             {
6215                exp.type = bracketsExp;
6216                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6217                newExp.needCast = true;
6218                ProcessExpressionType(exp.list->first);
6219                break;
6220             }
6221          }
6222          default:
6223          {
6224             if(exp.expType.kind == templateType)
6225             {
6226                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6227                if(type)
6228                {
6229                   FreeType(exp.destType);
6230                   FreeType(exp.expType);
6231                   delete newExp;
6232                   break;
6233                }
6234             }
6235             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6236             {
6237                exp.type = opExp;
6238                exp.op.op = '*';
6239                exp.op.exp1 = null;
6240                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6241                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6242             }
6243             else
6244             {
6245                char typeString[1024];
6246                Declarator decl;
6247                OldList * specs = MkList();
6248                typeString[0] = '\0';
6249                PrintType(exp.expType, typeString, false, false);
6250                decl = SpecDeclFromString(typeString, specs, null);
6251
6252                exp.type = castExp;
6253                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6254                exp.cast.typeName = MkTypeName(specs, decl);
6255                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6256                exp.cast.exp.needCast = true;
6257             }
6258             break;
6259          }
6260       }
6261    }
6262 }
6263 // TODO: The Symbol tree should be reorganized by namespaces
6264 // Name Space:
6265 //    - Tree of all symbols within (stored without namespace)
6266 //    - Tree of sub-namespaces
6267
6268 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6269 {
6270    int nsLen = strlen(nameSpace);
6271    Symbol symbol;
6272    // Start at the name space prefix
6273    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6274    {
6275       char * s = symbol.string;
6276       if(!strncmp(s, nameSpace, nsLen))
6277       {
6278          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6279          int c;
6280          char * namePart;
6281          for(c = strlen(s)-1; c >= 0; c--)
6282             if(s[c] == ':')
6283                break;
6284
6285          namePart = s+c+1;
6286          if(!strcmp(namePart, name))
6287          {
6288             // TODO: Error on ambiguity
6289             return symbol;
6290          }
6291       }
6292       else
6293          break;
6294    }
6295    return null;
6296 }
6297
6298 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6299 {
6300    int c;
6301    char nameSpace[1024];
6302    char * namePart;
6303    bool gotColon = false;
6304
6305    nameSpace[0] = '\0';
6306    for(c = strlen(name)-1; c >= 0; c--)
6307       if(name[c] == ':')
6308       {
6309          gotColon = true;
6310          break;
6311       }
6312
6313    namePart = name+c+1;
6314    while(c >= 0 && name[c] == ':') c--;
6315    if(c >= 0)
6316    {
6317       // Try an exact match first
6318       Symbol symbol = (Symbol)tree.FindString(name);
6319       if(symbol)
6320          return symbol;
6321
6322       // Namespace specified
6323       memcpy(nameSpace, name, c + 1);
6324       nameSpace[c+1] = 0;
6325
6326       return ScanWithNameSpace(tree, nameSpace, namePart);
6327    }
6328    else if(gotColon)
6329    {
6330       // Looking for a global symbol, e.g. ::Sleep()
6331       Symbol symbol = (Symbol)tree.FindString(namePart);
6332       return symbol;
6333    }
6334    else
6335    {
6336       // Name only (no namespace specified)
6337       Symbol symbol = (Symbol)tree.FindString(namePart);
6338       if(symbol)
6339          return symbol;
6340       return ScanWithNameSpace(tree, "", namePart);
6341    }
6342    return null;
6343 }
6344
6345 static void ProcessDeclaration(Declaration decl);
6346
6347 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6348 {
6349 #ifdef _DEBUG
6350    //Time startTime = GetTime();
6351 #endif
6352    // Optimize this later? Do this before/less?
6353    Context ctx;
6354    Symbol symbol = null;
6355    // First, check if the identifier is declared inside the function
6356    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6357
6358    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6359    {
6360       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6361       {
6362          symbol = null;
6363          if(thisNameSpace)
6364          {
6365             char curName[1024];
6366             strcpy(curName, thisNameSpace);
6367             strcat(curName, "::");
6368             strcat(curName, name);
6369             // Try to resolve in current namespace first
6370             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6371          }
6372          if(!symbol)
6373             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6374       }
6375       else
6376          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6377
6378       if(symbol || ctx == endContext) break;
6379    }
6380    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6381    {
6382       if(symbol.pointerExternal.type == functionExternal)
6383       {
6384          FunctionDefinition function = symbol.pointerExternal.function;
6385
6386          // Modified this recently...
6387          Context tmpContext = curContext;
6388          curContext = null;
6389          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6390          curContext = tmpContext;
6391
6392          symbol.pointerExternal.symbol = symbol;
6393
6394          // TESTING THIS:
6395          DeclareType(symbol.type, true, true);
6396
6397          ast->Insert(curExternal.prev, symbol.pointerExternal);
6398
6399          symbol.id = curExternal.symbol.idCode;
6400
6401       }
6402       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6403       {
6404          ast->Move(symbol.pointerExternal, curExternal.prev);
6405          symbol.id = curExternal.symbol.idCode;
6406       }
6407    }
6408 #ifdef _DEBUG
6409    //findSymbolTotalTime += GetTime() - startTime;
6410 #endif
6411    return symbol;
6412 }
6413
6414 static void GetTypeSpecs(Type type, OldList * specs)
6415 {
6416    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6417    switch(type.kind)
6418    {
6419       case classType:
6420       {
6421          if(type._class.registered)
6422          {
6423             if(!type._class.registered.dataType)
6424                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6425             GetTypeSpecs(type._class.registered.dataType, specs);
6426          }
6427          break;
6428       }
6429       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6430       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6431       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6432       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6433       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6434       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6435       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6436       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6437       case intType:
6438       default:
6439          ListAdd(specs, MkSpecifier(INT)); break;
6440    }
6441 }
6442
6443 static void PrintArraySize(Type arrayType, char * string)
6444 {
6445    char size[256];
6446    size[0] = '\0';
6447    strcat(size, "[");
6448    if(arrayType.enumClass)
6449       strcat(size, arrayType.enumClass.string);
6450    else if(arrayType.arraySizeExp)
6451       PrintExpression(arrayType.arraySizeExp, size);
6452    strcat(size, "]");
6453    strcat(string, size);
6454 }
6455
6456 // WARNING : This function expects a null terminated string since it recursively concatenate...
6457 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6458 {
6459    if(type)
6460    {
6461       if(printConst && type.constant)
6462          strcat(string, "const ");
6463       switch(type.kind)
6464       {
6465          case classType:
6466          {
6467             Symbol c = type._class;
6468             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6469             //       look into merging with thisclass ?
6470             if(type.classObjectType == typedObject)
6471                strcat(string, "typed_object");
6472             else if(type.classObjectType == anyObject)
6473                strcat(string, "any_object");
6474             else
6475             {
6476                if(c && c.string)
6477                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6478             }
6479             if(type.byReference)
6480                strcat(string, " &");
6481             break;
6482          }
6483          case voidType: strcat(string, "void"); break;
6484          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6485          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6486          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6487          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6488          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6489          case _BoolType: strcat(string, "_Bool"); break;
6490          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6491          case floatType: strcat(string, "float"); break;
6492          case doubleType: strcat(string, "double"); break;
6493          case structType:
6494             if(type.enumName)
6495             {
6496                strcat(string, "struct ");
6497                strcat(string, type.enumName);
6498             }
6499             else if(type.typeName)
6500                strcat(string, type.typeName);
6501             else
6502             {
6503                Type member;
6504                strcat(string, "struct { ");
6505                for(member = type.members.first; member; member = member.next)
6506                {
6507                   PrintType(member, string, true, fullName);
6508                   strcat(string,"; ");
6509                }
6510                strcat(string,"}");
6511             }
6512             break;
6513          case unionType:
6514             if(type.enumName)
6515             {
6516                strcat(string, "union ");
6517                strcat(string, type.enumName);
6518             }
6519             else if(type.typeName)
6520                strcat(string, type.typeName);
6521             else
6522             {
6523                strcat(string, "union ");
6524                strcat(string,"(unnamed)");
6525             }
6526             break;
6527          case enumType:
6528             if(type.enumName)
6529             {
6530                strcat(string, "enum ");
6531                strcat(string, type.enumName);
6532             }
6533             else if(type.typeName)
6534                strcat(string, type.typeName);
6535             else
6536                strcat(string, "int"); // "enum");
6537             break;
6538          case ellipsisType:
6539             strcat(string, "...");
6540             break;
6541          case subClassType:
6542             strcat(string, "subclass(");
6543             strcat(string, type._class ? type._class.string : "int");
6544             strcat(string, ")");
6545             break;
6546          case templateType:
6547             strcat(string, type.templateParameter.identifier.string);
6548             break;
6549          case thisClassType:
6550             strcat(string, "thisclass");
6551             break;
6552          case vaListType:
6553             strcat(string, "__builtin_va_list");
6554             break;
6555       }
6556    }
6557 }
6558
6559 static void PrintName(Type type, char * string, bool fullName)
6560 {
6561    if(type.name && type.name[0])
6562    {
6563       if(fullName)
6564          strcat(string, type.name);
6565       else
6566       {
6567          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6568          if(name) name += 2; else name = type.name;
6569          strcat(string, name);
6570       }
6571    }
6572 }
6573
6574 static void PrintAttribs(Type type, char * string)
6575 {
6576    if(type)
6577    {
6578       if(type.dllExport)   strcat(string, "dllexport ");
6579       if(type.attrStdcall) strcat(string, "stdcall ");
6580    }
6581 }
6582
6583 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6584 {
6585    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6586    {
6587       Type attrType = null;
6588       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6589          PrintAttribs(type, string);
6590       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6591          strcat(string, " const");
6592       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6593       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6594          strcat(string, " (");
6595       if(type.kind == pointerType)
6596       {
6597          if(type.type.kind == functionType || type.type.kind == methodType)
6598             PrintAttribs(type.type, string);
6599       }
6600       if(type.kind == pointerType)
6601       {
6602          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6603             strcat(string, "*");
6604          else
6605             strcat(string, " *");
6606       }
6607       if(printConst && type.constant && type.kind == pointerType)
6608          strcat(string, " const");
6609    }
6610    else
6611       PrintTypeSpecs(type, string, fullName, printConst);
6612 }
6613
6614 static void PostPrintType(Type type, char * string, bool fullName)
6615 {
6616    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6617       strcat(string, ")");
6618    if(type.kind == arrayType)
6619       PrintArraySize(type, string);
6620    else if(type.kind == functionType)
6621    {
6622       Type param;
6623       strcat(string, "(");
6624       for(param = type.params.first; param; param = param.next)
6625       {
6626          PrintType(param, string, true, fullName);
6627          if(param.next) strcat(string, ", ");
6628       }
6629       strcat(string, ")");
6630    }
6631    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6632       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6633 }
6634
6635 // *****
6636 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6637 // *****
6638 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6639 {
6640    PrePrintType(type, string, fullName, null, printConst);
6641
6642    if(type.thisClass || (printName && type.name && type.name[0]))
6643       strcat(string, " ");
6644    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6645    {
6646       Symbol _class = type.thisClass;
6647       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6648       {
6649          if(type.classObjectType == classPointer)
6650             strcat(string, "class");
6651          else
6652             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6653       }
6654       else if(_class && _class.string)
6655       {
6656          String s = _class.string;
6657          if(fullName)
6658             strcat(string, s);
6659          else
6660          {
6661             char * name = RSearchString(s, "::", strlen(s), true, false);
6662             if(name) name += 2; else name = s;
6663             strcat(string, name);
6664          }
6665       }
6666       strcat(string, "::");
6667    }
6668
6669    if(printName && type.name)
6670       PrintName(type, string, fullName);
6671    PostPrintType(type, string, fullName);
6672    if(type.bitFieldCount)
6673    {
6674       char count[100];
6675       sprintf(count, ":%d", type.bitFieldCount);
6676       strcat(string, count);
6677    }
6678 }
6679
6680 void PrintType(Type type, char * string, bool printName, bool fullName)
6681 {
6682    _PrintType(type, string, printName, fullName, true);
6683 }
6684
6685 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6686 {
6687    _PrintType(type, string, printName, fullName, false);
6688 }
6689
6690 static Type FindMember(Type type, char * string)
6691 {
6692    Type memberType;
6693    for(memberType = type.members.first; memberType; memberType = memberType.next)
6694    {
6695       if(!memberType.name)
6696       {
6697          Type subType = FindMember(memberType, string);
6698          if(subType)
6699             return subType;
6700       }
6701       else if(!strcmp(memberType.name, string))
6702          return memberType;
6703    }
6704    return null;
6705 }
6706
6707 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6708 {
6709    Type memberType;
6710    for(memberType = type.members.first; memberType; memberType = memberType.next)
6711    {
6712       if(!memberType.name)
6713       {
6714          Type subType = FindMember(memberType, string);
6715          if(subType)
6716          {
6717             *offset += memberType.offset;
6718             return subType;
6719          }
6720       }
6721       else if(!strcmp(memberType.name, string))
6722       {
6723          *offset += memberType.offset;
6724          return memberType;
6725       }
6726    }
6727    return null;
6728 }
6729
6730 public bool GetParseError() { return parseError; }
6731
6732 Expression ParseExpressionString(char * expression)
6733 {
6734    parseError = false;
6735
6736    fileInput = TempFile { };
6737    fileInput.Write(expression, 1, strlen(expression));
6738    fileInput.Seek(0, start);
6739
6740    echoOn = false;
6741    parsedExpression = null;
6742    resetScanner();
6743    expression_yyparse();
6744    delete fileInput;
6745
6746    return parsedExpression;
6747 }
6748
6749 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6750 {
6751    Identifier id = exp.identifier;
6752    Method method = null;
6753    Property prop = null;
6754    DataMember member = null;
6755    ClassProperty classProp = null;
6756
6757    if(_class && _class.type == enumClass)
6758    {
6759       NamedLink value = null;
6760       Class enumClass = eSystem_FindClass(privateModule, "enum");
6761       if(enumClass)
6762       {
6763          Class baseClass;
6764          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6765          {
6766             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6767             for(value = e.values.first; value; value = value.next)
6768             {
6769                if(!strcmp(value.name, id.string))
6770                   break;
6771             }
6772             if(value)
6773             {
6774                char constant[256];
6775
6776                FreeExpContents(exp);
6777
6778                exp.type = constantExp;
6779                exp.isConstant = true;
6780                if(!strcmp(baseClass.dataTypeString, "int"))
6781                   sprintf(constant, "%d",(int)value.data);
6782                else
6783                   sprintf(constant, "0x%X",(int)value.data);
6784                exp.constant = CopyString(constant);
6785                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6786                exp.expType = MkClassType(baseClass.fullName);
6787                break;
6788             }
6789          }
6790       }
6791       if(value)
6792          return true;
6793    }
6794    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6795    {
6796       ProcessMethodType(method);
6797       exp.expType = Type
6798       {
6799          refCount = 1;
6800          kind = methodType;
6801          method = method;
6802          // Crash here?
6803          // TOCHECK: Put it back to what it was...
6804          // methodClass = _class;
6805          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6806       };
6807       //id._class = null;
6808       return true;
6809    }
6810    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6811    {
6812       if(!prop.dataType)
6813          ProcessPropertyType(prop);
6814       exp.expType = prop.dataType;
6815       if(prop.dataType) prop.dataType.refCount++;
6816       return true;
6817    }
6818    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6819    {
6820       if(!member.dataType)
6821          member.dataType = ProcessTypeString(member.dataTypeString, false);
6822       exp.expType = member.dataType;
6823       if(member.dataType) member.dataType.refCount++;
6824       return true;
6825    }
6826    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6827    {
6828       if(!classProp.dataType)
6829          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6830
6831       if(classProp.constant)
6832       {
6833          FreeExpContents(exp);
6834
6835          exp.isConstant = true;
6836          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6837          {
6838             //char constant[256];
6839             exp.type = stringExp;
6840             exp.constant = QMkString((char *)classProp.Get(_class));
6841          }
6842          else
6843          {
6844             char constant[256];
6845             exp.type = constantExp;
6846             sprintf(constant, "%d", (int)classProp.Get(_class));
6847             exp.constant = CopyString(constant);
6848          }
6849       }
6850       else
6851       {
6852          // TO IMPLEMENT...
6853       }
6854
6855       exp.expType = classProp.dataType;
6856       if(classProp.dataType) classProp.dataType.refCount++;
6857       return true;
6858    }
6859    return false;
6860 }
6861
6862 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6863 {
6864    BinaryTree * tree = &nameSpace.functions;
6865    GlobalData data = (GlobalData)tree->FindString(name);
6866    NameSpace * child;
6867    if(!data)
6868    {
6869       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6870       {
6871          data = ScanGlobalData(child, name);
6872          if(data)
6873             break;
6874       }
6875    }
6876    return data;
6877 }
6878
6879 static GlobalData FindGlobalData(char * name)
6880 {
6881    int start = 0, c;
6882    NameSpace * nameSpace;
6883    nameSpace = globalData;
6884    for(c = 0; name[c]; c++)
6885    {
6886       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6887       {
6888          NameSpace * newSpace;
6889          char * spaceName = new char[c - start + 1];
6890          strncpy(spaceName, name + start, c - start);
6891          spaceName[c-start] = '\0';
6892          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6893          delete spaceName;
6894          if(!newSpace)
6895             return null;
6896          nameSpace = newSpace;
6897          if(name[c] == ':') c++;
6898          start = c+1;
6899       }
6900    }
6901    if(c - start)
6902    {
6903       return ScanGlobalData(nameSpace, name + start);
6904    }
6905    return null;
6906 }
6907
6908 static int definedExpStackPos;
6909 static void * definedExpStack[512];
6910
6911 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6912 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6913 {
6914    Expression prev = checkedExp.prev, next = checkedExp.next;
6915
6916    FreeExpContents(checkedExp);
6917    FreeType(checkedExp.expType);
6918    FreeType(checkedExp.destType);
6919
6920    *checkedExp = *newExp;
6921
6922    delete newExp;
6923
6924    checkedExp.prev = prev;
6925    checkedExp.next = next;
6926 }
6927
6928 void ApplyAnyObjectLogic(Expression e)
6929 {
6930    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6931 #ifdef _DEBUG
6932    char debugExpString[4096];
6933    debugExpString[0] = '\0';
6934    PrintExpression(e, debugExpString);
6935 #endif
6936
6937    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6938    {
6939       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6940       //ellipsisDestType = destType;
6941       if(e && e.expType)
6942       {
6943          Type type = e.expType;
6944          Class _class = null;
6945          //Type destType = e.destType;
6946
6947          if(type.kind == classType && type._class && type._class.registered)
6948          {
6949             _class = type._class.registered;
6950          }
6951          else if(type.kind == subClassType)
6952          {
6953             _class = FindClass("ecere::com::Class").registered;
6954          }
6955          else
6956          {
6957             char string[1024] = "";
6958             Symbol classSym;
6959
6960             PrintTypeNoConst(type, string, false, true);
6961             classSym = FindClass(string);
6962             if(classSym) _class = classSym.registered;
6963          }
6964
6965          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...
6966             (!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))) ||
6967             destType.byReference)))
6968          {
6969             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6970             {
6971                Expression checkedExp = e, newExp;
6972
6973                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6974                {
6975                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6976                   {
6977                      if(checkedExp.type == extensionCompoundExp)
6978                      {
6979                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6980                      }
6981                      else
6982                         checkedExp = checkedExp.list->last;
6983                   }
6984                   else if(checkedExp.type == castExp)
6985                      checkedExp = checkedExp.cast.exp;
6986                }
6987
6988                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6989                {
6990                   newExp = checkedExp.op.exp2;
6991                   checkedExp.op.exp2 = null;
6992                   FreeExpContents(checkedExp);
6993
6994                   if(e.expType && e.expType.passAsTemplate)
6995                   {
6996                      char size[100];
6997                      ComputeTypeSize(e.expType);
6998                      sprintf(size, "%d", e.expType.size);
6999                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7000                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7001                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7002                   }
7003
7004                   ReplaceExpContents(checkedExp, newExp);
7005                   e.byReference = true;
7006                }
7007                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7008                {
7009                   Expression checkedExp, newExp;
7010
7011                   {
7012                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7013                      bool hasAddress =
7014                         e.type == identifierExp ||
7015                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7016                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7017                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7018                         e.type == indexExp;
7019
7020                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7021                      {
7022                         Context context = PushContext();
7023                         Declarator decl;
7024                         OldList * specs = MkList();
7025                         char typeString[1024];
7026                         Expression newExp { };
7027
7028                         typeString[0] = '\0';
7029                         *newExp = *e;
7030
7031                         //if(e.destType) e.destType.refCount++;
7032                         // if(exp.expType) exp.expType.refCount++;
7033                         newExp.prev = null;
7034                         newExp.next = null;
7035                         newExp.expType = null;
7036
7037                         PrintTypeNoConst(e.expType, typeString, false, true);
7038                         decl = SpecDeclFromString(typeString, specs, null);
7039                         newExp.destType = ProcessType(specs, decl);
7040
7041                         curContext = context;
7042
7043                         // We need a current compound for this
7044                         if(curCompound)
7045                         {
7046                            char name[100];
7047                            OldList * stmts = MkList();
7048                            e.type = extensionCompoundExp;
7049                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7050                            if(!curCompound.compound.declarations)
7051                               curCompound.compound.declarations = MkList();
7052                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7053                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7054                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7055                            e.compound = MkCompoundStmt(null, stmts);
7056                         }
7057                         else
7058                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7059
7060                         /*
7061                         e.compound = MkCompoundStmt(
7062                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7063                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7064
7065                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7066                         */
7067
7068                         {
7069                            Type type = e.destType;
7070                            e.destType = { };
7071                            CopyTypeInto(e.destType, type);
7072                            e.destType.refCount = 1;
7073                            e.destType.classObjectType = none;
7074                            FreeType(type);
7075                         }
7076
7077                         e.compound.compound.context = context;
7078                         PopContext(context);
7079                         curContext = context.parent;
7080                      }
7081                   }
7082
7083                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7084                   checkedExp = e;
7085                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7086                   {
7087                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7088                      {
7089                         if(checkedExp.type == extensionCompoundExp)
7090                         {
7091                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7092                         }
7093                         else
7094                            checkedExp = checkedExp.list->last;
7095                      }
7096                      else if(checkedExp.type == castExp)
7097                         checkedExp = checkedExp.cast.exp;
7098                   }
7099                   {
7100                      Expression operand { };
7101                      operand = *checkedExp;
7102                      checkedExp.destType = null;
7103                      checkedExp.expType = null;
7104                      checkedExp.Clear();
7105                      checkedExp.type = opExp;
7106                      checkedExp.op.op = '&';
7107                      checkedExp.op.exp1 = null;
7108                      checkedExp.op.exp2 = operand;
7109
7110                      //newExp = MkExpOp(null, '&', checkedExp);
7111                   }
7112                   //ReplaceExpContents(checkedExp, newExp);
7113                }
7114             }
7115          }
7116       }
7117    }
7118    {
7119       // If expression type is a simple class, make it an address
7120       // FixReference(e, true);
7121    }
7122 //#if 0
7123    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7124       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7125          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7126    {
7127       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"))
7128       {
7129          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7130       }
7131       else
7132       {
7133          Expression thisExp { };
7134
7135          *thisExp = *e;
7136          thisExp.prev = null;
7137          thisExp.next = null;
7138          e.Clear();
7139
7140          e.type = bracketsExp;
7141          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7142          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7143             ((Expression)e.list->first).byReference = true;
7144
7145          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7146          {
7147             e.expType = thisExp.expType;
7148             e.expType.refCount++;
7149          }
7150          else*/
7151          {
7152             e.expType = { };
7153             CopyTypeInto(e.expType, thisExp.expType);
7154             e.expType.byReference = false;
7155             e.expType.refCount = 1;
7156
7157             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7158                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7159             {
7160                e.expType.classObjectType = none;
7161             }
7162          }
7163       }
7164    }
7165 // TOFIX: Try this for a nice IDE crash!
7166 //#endif
7167    // The other way around
7168    else
7169 //#endif
7170    if(destType && e.expType &&
7171          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7172          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7173          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7174    {
7175       if(destType.kind == ellipsisType)
7176       {
7177          Compiler_Error($"Unspecified type\n");
7178       }
7179       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7180       {
7181          bool byReference = e.expType.byReference;
7182          Expression thisExp { };
7183          Declarator decl;
7184          OldList * specs = MkList();
7185          char typeString[1024]; // Watch buffer overruns
7186          Type type;
7187          ClassObjectType backupClassObjectType;
7188          bool backupByReference;
7189
7190          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7191             type = e.expType;
7192          else
7193             type = destType;
7194
7195          backupClassObjectType = type.classObjectType;
7196          backupByReference = type.byReference;
7197
7198          type.classObjectType = none;
7199          type.byReference = false;
7200
7201          typeString[0] = '\0';
7202          PrintType(type, typeString, false, true);
7203          decl = SpecDeclFromString(typeString, specs, null);
7204
7205          type.classObjectType = backupClassObjectType;
7206          type.byReference = backupByReference;
7207
7208          *thisExp = *e;
7209          thisExp.prev = null;
7210          thisExp.next = null;
7211          e.Clear();
7212
7213          if( ( type.kind == classType && type._class && type._class.registered &&
7214                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7215                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7216              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7217              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7218          {
7219             e.type = opExp;
7220             e.op.op = '*';
7221             e.op.exp1 = null;
7222             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7223
7224             e.expType = { };
7225             CopyTypeInto(e.expType, type);
7226             e.expType.byReference = false;
7227             e.expType.refCount = 1;
7228          }
7229          else
7230          {
7231             e.type = castExp;
7232             e.cast.typeName = MkTypeName(specs, decl);
7233             e.cast.exp = thisExp;
7234             e.byReference = true;
7235             e.expType = type;
7236             type.refCount++;
7237          }
7238          e.destType = destType;
7239          destType.refCount++;
7240       }
7241    }
7242 }
7243
7244 void ProcessExpressionType(Expression exp)
7245 {
7246    bool unresolved = false;
7247    Location oldyylloc = yylloc;
7248    bool notByReference = false;
7249 #ifdef _DEBUG
7250    char debugExpString[4096];
7251    debugExpString[0] = '\0';
7252    PrintExpression(exp, debugExpString);
7253 #endif
7254    if(!exp || exp.expType)
7255       return;
7256
7257    //eSystem_Logf("%s\n", expString);
7258
7259    // Testing this here
7260    yylloc = exp.loc;
7261    switch(exp.type)
7262    {
7263       case identifierExp:
7264       {
7265          Identifier id = exp.identifier;
7266          if(!id || !topContext) return;
7267
7268          // DOING THIS LATER NOW...
7269          if(id._class && id._class.name)
7270          {
7271             id.classSym = id._class.symbol; // FindClass(id._class.name);
7272             /* TODO: Name Space Fix ups
7273             if(!id.classSym)
7274                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7275             */
7276          }
7277
7278          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7279          {
7280             exp.expType = ProcessTypeString("Module", true);
7281             break;
7282          }
7283          else */if(strstr(id.string, "__ecereClass") == id.string)
7284          {
7285             exp.expType = ProcessTypeString("ecere::com::Class", true);
7286             break;
7287          }
7288          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7289          {
7290             // Added this here as well
7291             ReplaceClassMembers(exp, thisClass);
7292             if(exp.type != identifierExp)
7293             {
7294                ProcessExpressionType(exp);
7295                break;
7296             }
7297
7298             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7299                break;
7300          }
7301          else
7302          {
7303             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7304             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7305             if(!symbol/* && exp.destType*/)
7306             {
7307                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7308                   break;
7309                else
7310                {
7311                   if(thisClass)
7312                   {
7313                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7314                      if(exp.type != identifierExp)
7315                      {
7316                         ProcessExpressionType(exp);
7317                         break;
7318                      }
7319                   }
7320                   // Static methods called from inside the _class
7321                   else if(currentClass && !id._class)
7322                   {
7323                      if(ResolveIdWithClass(exp, currentClass, true))
7324                         break;
7325                   }
7326                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7327                }
7328             }
7329
7330             // If we manage to resolve this symbol
7331             if(symbol)
7332             {
7333                Type type = symbol.type;
7334                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7335
7336                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7337                {
7338                   Context context = SetupTemplatesContext(_class);
7339                   type = ReplaceThisClassType(_class);
7340                   FinishTemplatesContext(context);
7341                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7342                }
7343
7344                FreeSpecifier(id._class);
7345                id._class = null;
7346                delete id.string;
7347                id.string = CopyString(symbol.string);
7348
7349                id.classSym = null;
7350                exp.expType = type;
7351                if(type)
7352                   type.refCount++;
7353                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7354                   // Add missing cases here... enum Classes...
7355                   exp.isConstant = true;
7356
7357                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7358                if(symbol.isParam || !strcmp(id.string, "this"))
7359                {
7360                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7361                      exp.byReference = true;
7362
7363                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7364                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7365                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7366                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7367                   {
7368                      Identifier id = exp.identifier;
7369                      exp.type = bracketsExp;
7370                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7371                   }*/
7372                }
7373
7374                if(symbol.isIterator)
7375                {
7376                   if(symbol.isIterator == 3)
7377                   {
7378                      exp.type = bracketsExp;
7379                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7380                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7381                      exp.expType = null;
7382                      ProcessExpressionType(exp);
7383                   }
7384                   else if(symbol.isIterator != 4)
7385                   {
7386                      exp.type = memberExp;
7387                      exp.member.exp = MkExpIdentifier(exp.identifier);
7388                      exp.member.exp.expType = exp.expType;
7389                      /*if(symbol.isIterator == 6)
7390                         exp.member.member = MkIdentifier("key");
7391                      else*/
7392                         exp.member.member = MkIdentifier("data");
7393                      exp.expType = null;
7394                      ProcessExpressionType(exp);
7395                   }
7396                }
7397                break;
7398             }
7399             else
7400             {
7401                DefinedExpression definedExp = null;
7402                if(thisNameSpace && !(id._class && !id._class.name))
7403                {
7404                   char name[1024];
7405                   strcpy(name, thisNameSpace);
7406                   strcat(name, "::");
7407                   strcat(name, id.string);
7408                   definedExp = eSystem_FindDefine(privateModule, name);
7409                }
7410                if(!definedExp)
7411                   definedExp = eSystem_FindDefine(privateModule, id.string);
7412                if(definedExp)
7413                {
7414                   int c;
7415                   for(c = 0; c<definedExpStackPos; c++)
7416                      if(definedExpStack[c] == definedExp)
7417                         break;
7418                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7419                   {
7420                      Location backupYylloc = yylloc;
7421                      File backInput = fileInput;
7422                      definedExpStack[definedExpStackPos++] = definedExp;
7423
7424                      fileInput = TempFile { };
7425                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7426                      fileInput.Seek(0, start);
7427
7428                      echoOn = false;
7429                      parsedExpression = null;
7430                      resetScanner();
7431                      expression_yyparse();
7432                      delete fileInput;
7433                      if(backInput)
7434                         fileInput = backInput;
7435
7436                      yylloc = backupYylloc;
7437
7438                      if(parsedExpression)
7439                      {
7440                         FreeIdentifier(id);
7441                         exp.type = bracketsExp;
7442                         exp.list = MkListOne(parsedExpression);
7443                         parsedExpression.loc = yylloc;
7444                         ProcessExpressionType(exp);
7445                         definedExpStackPos--;
7446                         return;
7447                      }
7448                      definedExpStackPos--;
7449                   }
7450                   else
7451                   {
7452                      if(inCompiler)
7453                      {
7454                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7455                      }
7456                   }
7457                }
7458                else
7459                {
7460                   GlobalData data = null;
7461                   if(thisNameSpace && !(id._class && !id._class.name))
7462                   {
7463                      char name[1024];
7464                      strcpy(name, thisNameSpace);
7465                      strcat(name, "::");
7466                      strcat(name, id.string);
7467                      data = FindGlobalData(name);
7468                   }
7469                   if(!data)
7470                      data = FindGlobalData(id.string);
7471                   if(data)
7472                   {
7473                      DeclareGlobalData(data);
7474                      exp.expType = data.dataType;
7475                      if(data.dataType) data.dataType.refCount++;
7476
7477                      delete id.string;
7478                      id.string = CopyString(data.fullName);
7479                      FreeSpecifier(id._class);
7480                      id._class = null;
7481
7482                      break;
7483                   }
7484                   else
7485                   {
7486                      GlobalFunction function = null;
7487                      if(thisNameSpace && !(id._class && !id._class.name))
7488                      {
7489                         char name[1024];
7490                         strcpy(name, thisNameSpace);
7491                         strcat(name, "::");
7492                         strcat(name, id.string);
7493                         function = eSystem_FindFunction(privateModule, name);
7494                      }
7495                      if(!function)
7496                         function = eSystem_FindFunction(privateModule, id.string);
7497                      if(function)
7498                      {
7499                         char name[1024];
7500                         delete id.string;
7501                         id.string = CopyString(function.name);
7502                         name[0] = 0;
7503
7504                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7505                            strcpy(name, "__ecereFunction_");
7506                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7507                         if(DeclareFunction(function, name))
7508                         {
7509                            delete id.string;
7510                            id.string = CopyString(name);
7511                         }
7512                         exp.expType = function.dataType;
7513                         if(function.dataType) function.dataType.refCount++;
7514
7515                         FreeSpecifier(id._class);
7516                         id._class = null;
7517
7518                         break;
7519                      }
7520                   }
7521                }
7522             }
7523          }
7524          unresolved = true;
7525          break;
7526       }
7527       case instanceExp:
7528       {
7529          Class _class;
7530          // Symbol classSym;
7531
7532          if(!exp.instance._class)
7533          {
7534             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7535             {
7536                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7537             }
7538          }
7539
7540          //classSym = FindClass(exp.instance._class.fullName);
7541          //_class = classSym ? classSym.registered : null;
7542
7543          ProcessInstantiationType(exp.instance);
7544          exp.isConstant = exp.instance.isConstant;
7545
7546          /*
7547          if(_class.type == unitClass && _class.base.type != systemClass)
7548          {
7549             {
7550                Type destType = exp.destType;
7551
7552                exp.destType = MkClassType(_class.base.fullName);
7553                exp.expType = MkClassType(_class.fullName);
7554                CheckExpressionType(exp, exp.destType, true);
7555
7556                exp.destType = destType;
7557             }
7558             exp.expType = MkClassType(_class.fullName);
7559          }
7560          else*/
7561          if(exp.instance._class)
7562          {
7563             exp.expType = MkClassType(exp.instance._class.name);
7564             /*if(exp.expType._class && exp.expType._class.registered &&
7565                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7566                exp.expType.byReference = true;*/
7567          }
7568          break;
7569       }
7570       case constantExp:
7571       {
7572          if(!exp.expType)
7573          {
7574             char * constant = exp.constant;
7575             Type type
7576             {
7577                refCount = 1;
7578                constant = true;
7579             };
7580             exp.expType = type;
7581
7582             if(constant[0] == '\'')
7583             {
7584                if((int)((byte *)constant)[1] > 127)
7585                {
7586                   int nb;
7587                   unichar ch = UTF8GetChar(constant + 1, &nb);
7588                   if(nb < 2) ch = constant[1];
7589                   delete constant;
7590                   exp.constant = PrintUInt(ch);
7591                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7592                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7593                   type._class = FindClass("unichar");
7594
7595                   type.isSigned = false;
7596                }
7597                else
7598                {
7599                   type.kind = charType;
7600                   type.isSigned = true;
7601                }
7602             }
7603             else
7604             {
7605                char * dot = strchr(constant, '.');
7606                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7607                char * exponent;
7608                if(isHex)
7609                {
7610                   exponent = strchr(constant, 'p');
7611                   if(!exponent) exponent = strchr(constant, 'P');
7612                }
7613                else
7614                {
7615                   exponent = strchr(constant, 'e');
7616                   if(!exponent) exponent = strchr(constant, 'E');
7617                }
7618
7619                if(dot || exponent)
7620                {
7621                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7622                      type.kind = floatType;
7623                   else
7624                      type.kind = doubleType;
7625                   type.isSigned = true;
7626                }
7627                else
7628                {
7629                   bool isSigned = constant[0] == '-';
7630                   int64 i64 = strtoll(constant, null, 0);
7631                   uint64 ui64 = strtoull(constant, null, 0);
7632                   bool is64Bit = false;
7633                   if(isSigned)
7634                   {
7635                      if(i64 < MININT)
7636                         is64Bit = true;
7637                   }
7638                   else
7639                   {
7640                      if(ui64 > MAXINT)
7641                      {
7642                         if(ui64 > MAXDWORD)
7643                         {
7644                            is64Bit = true;
7645                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7646                               isSigned = true;
7647                         }
7648                      }
7649                      else if(constant[0] != '0' || !constant[1])
7650                         isSigned = true;
7651                   }
7652                   type.kind = is64Bit ? int64Type : intType;
7653                   type.isSigned = isSigned;
7654                }
7655             }
7656             exp.isConstant = true;
7657             if(exp.destType && exp.destType.kind == doubleType)
7658                type.kind = doubleType;
7659             else if(exp.destType && exp.destType.kind == floatType)
7660                type.kind = floatType;
7661             else if(exp.destType && exp.destType.kind == int64Type)
7662                type.kind = int64Type;
7663          }
7664          break;
7665       }
7666       case stringExp:
7667       {
7668          exp.isConstant = true;      // Why wasn't this constant?
7669          exp.expType = Type
7670          {
7671             refCount = 1;
7672             kind = pointerType;
7673             type = Type
7674             {
7675                refCount = 1;
7676                kind = charType;
7677                constant = true;
7678                isSigned = true;
7679             }
7680          };
7681          break;
7682       }
7683       case newExp:
7684       case new0Exp:
7685          ProcessExpressionType(exp._new.size);
7686          exp.expType = Type
7687          {
7688             refCount = 1;
7689             kind = pointerType;
7690             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7691          };
7692          DeclareType(exp.expType.type, false, false);
7693          break;
7694       case renewExp:
7695       case renew0Exp:
7696          ProcessExpressionType(exp._renew.size);
7697          ProcessExpressionType(exp._renew.exp);
7698          exp.expType = Type
7699          {
7700             refCount = 1;
7701             kind = pointerType;
7702             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7703          };
7704          DeclareType(exp.expType.type, false, false);
7705          break;
7706       case opExp:
7707       {
7708          bool assign = false, boolResult = false, boolOps = false;
7709          Type type1 = null, type2 = null;
7710          bool useDestType = false, useSideType = false;
7711          Location oldyylloc = yylloc;
7712          bool useSideUnit = false;
7713
7714          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7715          Type dummy
7716          {
7717             count = 1;
7718             refCount = 1;
7719          };
7720
7721          switch(exp.op.op)
7722          {
7723             // Assignment Operators
7724             case '=':
7725             case MUL_ASSIGN:
7726             case DIV_ASSIGN:
7727             case MOD_ASSIGN:
7728             case ADD_ASSIGN:
7729             case SUB_ASSIGN:
7730             case LEFT_ASSIGN:
7731             case RIGHT_ASSIGN:
7732             case AND_ASSIGN:
7733             case XOR_ASSIGN:
7734             case OR_ASSIGN:
7735                assign = true;
7736                break;
7737             // boolean Operators
7738             case '!':
7739                // Expect boolean operators
7740                //boolOps = true;
7741                //boolResult = true;
7742                break;
7743             case AND_OP:
7744             case OR_OP:
7745                // Expect boolean operands
7746                boolOps = true;
7747                boolResult = true;
7748                break;
7749             // Comparisons
7750             case EQ_OP:
7751             case '<':
7752             case '>':
7753             case LE_OP:
7754             case GE_OP:
7755             case NE_OP:
7756                // Gives boolean result
7757                boolResult = true;
7758                useSideType = true;
7759                break;
7760             case '+':
7761             case '-':
7762                useSideUnit = true;
7763
7764                // Just added these... testing
7765             case '|':
7766             case '&':
7767             case '^':
7768
7769             // DANGER: Verify units
7770             case '/':
7771             case '%':
7772             case '*':
7773
7774                if(exp.op.op != '*' || exp.op.exp1)
7775                {
7776                   useSideType = true;
7777                   useDestType = true;
7778                }
7779                break;
7780
7781             /*// Implement speed etc.
7782             case '*':
7783             case '/':
7784                break;
7785             */
7786          }
7787          if(exp.op.op == '&')
7788          {
7789             // Added this here earlier for Iterator address as key
7790             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7791             {
7792                Identifier id = exp.op.exp2.identifier;
7793                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7794                if(symbol && symbol.isIterator == 2)
7795                {
7796                   exp.type = memberExp;
7797                   exp.member.exp = exp.op.exp2;
7798                   exp.member.member = MkIdentifier("key");
7799                   exp.expType = null;
7800                   exp.op.exp2.expType = symbol.type;
7801                   symbol.type.refCount++;
7802                   ProcessExpressionType(exp);
7803                   FreeType(dummy);
7804                   break;
7805                }
7806                // exp.op.exp2.usage.usageRef = true;
7807             }
7808          }
7809
7810          //dummy.kind = TypeDummy;
7811
7812          if(exp.op.exp1)
7813          {
7814             if(exp.destType && exp.destType.kind == classType &&
7815                exp.destType._class && exp.destType._class.registered && useDestType &&
7816
7817               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7818                exp.destType._class.registered.type == enumClass ||
7819                exp.destType._class.registered.type == bitClass
7820                ))
7821
7822               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7823             {
7824                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7825                exp.op.exp1.destType = exp.destType;
7826                if(exp.destType)
7827                   exp.destType.refCount++;
7828             }
7829             else if(!assign)
7830             {
7831                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7832                exp.op.exp1.destType = dummy;
7833                dummy.refCount++;
7834             }
7835
7836             // TESTING THIS HERE...
7837             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7838             ProcessExpressionType(exp.op.exp1);
7839             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7840
7841             if(exp.op.exp1.destType == dummy)
7842             {
7843                FreeType(dummy);
7844                exp.op.exp1.destType = null;
7845             }
7846             type1 = exp.op.exp1.expType;
7847          }
7848
7849          if(exp.op.exp2)
7850          {
7851             char expString[10240];
7852             expString[0] = '\0';
7853             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7854             {
7855                if(exp.op.exp1)
7856                {
7857                   exp.op.exp2.destType = exp.op.exp1.expType;
7858                   if(exp.op.exp1.expType)
7859                      exp.op.exp1.expType.refCount++;
7860                }
7861                else
7862                {
7863                   exp.op.exp2.destType = exp.destType;
7864                   if(exp.destType)
7865                      exp.destType.refCount++;
7866                }
7867
7868                if(type1) type1.refCount++;
7869                exp.expType = type1;
7870             }
7871             else if(assign)
7872             {
7873                if(inCompiler)
7874                   PrintExpression(exp.op.exp2, expString);
7875
7876                if(type1 && type1.kind == pointerType)
7877                {
7878                   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 ||
7879                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7880                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7881                   else if(exp.op.op == '=')
7882                   {
7883                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7884                      exp.op.exp2.destType = type1;
7885                      if(type1)
7886                         type1.refCount++;
7887                   }
7888                }
7889                else
7890                {
7891                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
7892                   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/* ||
7893                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7894                   else
7895                   {
7896                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7897                      exp.op.exp2.destType = type1;
7898                      if(type1)
7899                         type1.refCount++;
7900                   }
7901                }
7902                if(type1) type1.refCount++;
7903                exp.expType = type1;
7904             }
7905             else if(exp.destType && exp.destType.kind == classType &&
7906                exp.destType._class && exp.destType._class.registered &&
7907
7908                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
7909                   (exp.destType._class.registered.type == enumClass && useDestType))
7910                   )
7911             {
7912                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7913                exp.op.exp2.destType = exp.destType;
7914                if(exp.destType)
7915                   exp.destType.refCount++;
7916             }
7917             else
7918             {
7919                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7920                exp.op.exp2.destType = dummy;
7921                dummy.refCount++;
7922             }
7923
7924             // TESTING THIS HERE... (DANGEROUS)
7925             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
7926                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7927             {
7928                FreeType(exp.op.exp2.destType);
7929                exp.op.exp2.destType = type1;
7930                type1.refCount++;
7931             }
7932             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7933             ProcessExpressionType(exp.op.exp2);
7934             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7935
7936             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7937             {
7938                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)
7939                {
7940                   if(exp.op.op != '=' && type1.type.kind == voidType)
7941                      Compiler_Error($"void *: unknown size\n");
7942                }
7943                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||
7944                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7945                               (exp.op.exp2.expType._class.registered.type == normalClass ||
7946                               exp.op.exp2.expType._class.registered.type == structClass ||
7947                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7948                {
7949                   if(exp.op.op == ADD_ASSIGN)
7950                      Compiler_Error($"cannot add two pointers\n");
7951                }
7952                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
7953                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7954                {
7955                   if(exp.op.op == ADD_ASSIGN)
7956                      Compiler_Error($"cannot add two pointers\n");
7957                }
7958                else if(inCompiler)
7959                {
7960                   char type1String[1024];
7961                   char type2String[1024];
7962                   type1String[0] = '\0';
7963                   type2String[0] = '\0';
7964
7965                   PrintType(exp.op.exp2.expType, type1String, false, true);
7966                   PrintType(type1, type2String, false, true);
7967                   ChangeCh(expString, '\n', ' ');
7968                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7969                }
7970             }
7971
7972             if(exp.op.exp2.destType == dummy)
7973             {
7974                FreeType(dummy);
7975                exp.op.exp2.destType = null;
7976             }
7977
7978             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
7979             {
7980                type2 = { };
7981                type2.refCount = 1;
7982                CopyTypeInto(type2, exp.op.exp2.expType);
7983                type2.isSigned = true;
7984             }
7985             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
7986             {
7987                type2 = { kind = intType };
7988                type2.refCount = 1;
7989                type2.isSigned = true;
7990             }
7991             else
7992             {
7993                type2 = exp.op.exp2.expType;
7994                if(type2) type2.refCount++;
7995             }
7996          }
7997
7998          dummy.kind = voidType;
7999
8000          if(exp.op.op == SIZEOF)
8001          {
8002             exp.expType = Type
8003             {
8004                refCount = 1;
8005                kind = intType;
8006             };
8007             exp.isConstant = true;
8008          }
8009          // Get type of dereferenced pointer
8010          else if(exp.op.op == '*' && !exp.op.exp1)
8011          {
8012             exp.expType = Dereference(type2);
8013             if(type2 && type2.kind == classType)
8014                notByReference = true;
8015          }
8016          else if(exp.op.op == '&' && !exp.op.exp1)
8017             exp.expType = Reference(type2);
8018          else if(!assign)
8019          {
8020             if(boolOps)
8021             {
8022                if(exp.op.exp1)
8023                {
8024                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8025                   exp.op.exp1.destType = MkClassType("bool");
8026                   exp.op.exp1.destType.truth = true;
8027                   if(!exp.op.exp1.expType)
8028                      ProcessExpressionType(exp.op.exp1);
8029                   else
8030                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8031                   FreeType(exp.op.exp1.expType);
8032                   exp.op.exp1.expType = MkClassType("bool");
8033                   exp.op.exp1.expType.truth = true;
8034                }
8035                if(exp.op.exp2)
8036                {
8037                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8038                   exp.op.exp2.destType = MkClassType("bool");
8039                   exp.op.exp2.destType.truth = true;
8040                   if(!exp.op.exp2.expType)
8041                      ProcessExpressionType(exp.op.exp2);
8042                   else
8043                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8044                   FreeType(exp.op.exp2.expType);
8045                   exp.op.exp2.expType = MkClassType("bool");
8046                   exp.op.exp2.expType.truth = true;
8047                }
8048             }
8049             else if(exp.op.exp1 && exp.op.exp2 &&
8050                ((useSideType /*&&
8051                      (useSideUnit ||
8052                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8053                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8054                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8055                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8056             {
8057                if(type1 && type2 &&
8058                   // If either both are class or both are not class
8059                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8060                {
8061                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8062                   exp.op.exp2.destType = type1;
8063                   type1.refCount++;
8064                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8065                   exp.op.exp1.destType = type2;
8066                   type2.refCount++;
8067                   // Warning here for adding Radians + Degrees with no destination type
8068                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8069                      type1._class.registered && type1._class.registered.type == unitClass &&
8070                      type2._class.registered && type2._class.registered.type == unitClass &&
8071                      type1._class.registered != type2._class.registered)
8072                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8073                         type1._class.string, type2._class.string, type1._class.string);
8074
8075                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8076                   {
8077                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8078                      if(argExp)
8079                      {
8080                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8081
8082                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8083                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8084                            exp.op.exp1)));
8085
8086                         ProcessExpressionType(exp.op.exp1);
8087
8088                         if(type2.kind != pointerType)
8089                         {
8090                            ProcessExpressionType(classExp);
8091
8092                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8093                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8094                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8095                                  // noHeadClass
8096                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8097                                     OR_OP,
8098                                  // normalClass
8099                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8100                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8101                                        MkPointer(null, null), null)))),
8102                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8103
8104                            if(!exp.op.exp2.expType)
8105                            {
8106                               if(type2)
8107                                  FreeType(type2);
8108                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8109                               type2.refCount++;
8110                            }
8111
8112                            ProcessExpressionType(exp.op.exp2);
8113                         }
8114                      }
8115                   }
8116
8117                   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)))
8118                   {
8119                      if(type1.kind != classType && type1.type.kind == voidType)
8120                         Compiler_Error($"void *: unknown size\n");
8121                      exp.expType = type1;
8122                      if(type1) type1.refCount++;
8123                   }
8124                   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)))
8125                   {
8126                      if(type2.kind != classType && type2.type.kind == voidType)
8127                         Compiler_Error($"void *: unknown size\n");
8128                      exp.expType = type2;
8129                      if(type2) type2.refCount++;
8130                   }
8131                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8132                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8133                   {
8134                      Compiler_Warning($"different levels of indirection\n");
8135                   }
8136                   else
8137                   {
8138                      bool success = false;
8139                      if(type1.kind == pointerType && type2.kind == pointerType)
8140                      {
8141                         if(exp.op.op == '+')
8142                            Compiler_Error($"cannot add two pointers\n");
8143                         else if(exp.op.op == '-')
8144                         {
8145                            // Pointer Subtraction gives integer
8146                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8147                            {
8148                               exp.expType = Type
8149                               {
8150                                  kind = intType;
8151                                  refCount = 1;
8152                               };
8153                               success = true;
8154
8155                               if(type1.type.kind == templateType)
8156                               {
8157                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8158                                  if(argExp)
8159                                  {
8160                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8161
8162                                     ProcessExpressionType(classExp);
8163
8164                                     exp.type = bracketsExp;
8165                                     exp.list = MkListOne(MkExpOp(
8166                                        MkExpBrackets(MkListOne(MkExpOp(
8167                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8168                                              , exp.op.op,
8169                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8170
8171                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8172
8173                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8174                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8175                                                 // noHeadClass
8176                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8177                                                    OR_OP,
8178                                                 // normalClass
8179                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8180                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8181                                                       MkPointer(null, null), null)))),
8182                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8183
8184
8185                                              ));
8186
8187                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8188                                     FreeType(dummy);
8189                                     return;
8190                                  }
8191                               }
8192                            }
8193                         }
8194                      }
8195
8196                      if(!success && exp.op.exp1.type == constantExp)
8197                      {
8198                         // If first expression is constant, try to match that first
8199                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8200                         {
8201                            if(exp.expType) FreeType(exp.expType);
8202                            exp.expType = exp.op.exp1.destType;
8203                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8204                            success = true;
8205                         }
8206                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8207                         {
8208                            if(exp.expType) FreeType(exp.expType);
8209                            exp.expType = exp.op.exp2.destType;
8210                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8211                            success = true;
8212                         }
8213                      }
8214                      else if(!success)
8215                      {
8216                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8217                         {
8218                            if(exp.expType) FreeType(exp.expType);
8219                            exp.expType = exp.op.exp2.destType;
8220                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8221                            success = true;
8222                         }
8223                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8224                         {
8225                            if(exp.expType) FreeType(exp.expType);
8226                            exp.expType = exp.op.exp1.destType;
8227                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8228                            success = true;
8229                         }
8230                      }
8231                      if(!success)
8232                      {
8233                         char expString1[10240];
8234                         char expString2[10240];
8235                         char type1[1024];
8236                         char type2[1024];
8237                         expString1[0] = '\0';
8238                         expString2[0] = '\0';
8239                         type1[0] = '\0';
8240                         type2[0] = '\0';
8241                         if(inCompiler)
8242                         {
8243                            PrintExpression(exp.op.exp1, expString1);
8244                            ChangeCh(expString1, '\n', ' ');
8245                            PrintExpression(exp.op.exp2, expString2);
8246                            ChangeCh(expString2, '\n', ' ');
8247                            PrintType(exp.op.exp1.expType, type1, false, true);
8248                            PrintType(exp.op.exp2.expType, type2, false, true);
8249                         }
8250
8251                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8252                      }
8253                   }
8254                }
8255                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8256                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8257                {
8258                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8259                   // Convert e.g. / 4 into / 4.0
8260                   exp.op.exp1.destType = type2._class.registered.dataType;
8261                   if(type2._class.registered.dataType)
8262                      type2._class.registered.dataType.refCount++;
8263                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8264                   exp.expType = type2;
8265                   if(type2) type2.refCount++;
8266                }
8267                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8268                {
8269                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8270                   // Convert e.g. / 4 into / 4.0
8271                   exp.op.exp2.destType = type1._class.registered.dataType;
8272                   if(type1._class.registered.dataType)
8273                      type1._class.registered.dataType.refCount++;
8274                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8275                   exp.expType = type1;
8276                   if(type1) type1.refCount++;
8277                }
8278                else if(type1)
8279                {
8280                   bool valid = false;
8281
8282                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8283                   {
8284                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8285
8286                      if(!type1._class.registered.dataType)
8287                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8288                      exp.op.exp2.destType = type1._class.registered.dataType;
8289                      exp.op.exp2.destType.refCount++;
8290
8291                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8292                      if(type2)
8293                         FreeType(type2);
8294                      type2 = exp.op.exp2.destType;
8295                      if(type2) type2.refCount++;
8296
8297                      exp.expType = type2;
8298                      type2.refCount++;
8299                   }
8300
8301                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8302                   {
8303                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8304
8305                      if(!type2._class.registered.dataType)
8306                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8307                      exp.op.exp1.destType = type2._class.registered.dataType;
8308                      exp.op.exp1.destType.refCount++;
8309
8310                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8311                      type1 = exp.op.exp1.destType;
8312                      exp.expType = type1;
8313                      type1.refCount++;
8314                   }
8315
8316                   // TESTING THIS NEW CODE
8317                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8318                   {
8319                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8320                      {
8321                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8322                         {
8323                            if(exp.expType) FreeType(exp.expType);
8324                            exp.expType = exp.op.exp1.expType;
8325                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8326                            valid = true;
8327                         }
8328                      }
8329
8330                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8331                      {
8332                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8333                         {
8334                            if(exp.expType) FreeType(exp.expType);
8335                            exp.expType = exp.op.exp2.expType;
8336                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8337                            valid = true;
8338                         }
8339                      }
8340                   }
8341
8342                   if(!valid)
8343                   {
8344                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8345                      exp.op.exp2.destType = type1;
8346                      type1.refCount++;
8347
8348                      /*
8349                      // Maybe this was meant to be an enum...
8350                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8351                      {
8352                         Type oldType = exp.op.exp2.expType;
8353                         exp.op.exp2.expType = null;
8354                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8355                            FreeType(oldType);
8356                         else
8357                            exp.op.exp2.expType = oldType;
8358                      }
8359                      */
8360
8361                      /*
8362                      // TESTING THIS HERE... LATEST ADDITION
8363                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8364                      {
8365                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8366                         exp.op.exp2.destType = type2._class.registered.dataType;
8367                         if(type2._class.registered.dataType)
8368                            type2._class.registered.dataType.refCount++;
8369                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8370
8371                         //exp.expType = type2._class.registered.dataType; //type2;
8372                         //if(type2) type2.refCount++;
8373                      }
8374
8375                      // TESTING THIS HERE... LATEST ADDITION
8376                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8377                      {
8378                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8379                         exp.op.exp1.destType = type1._class.registered.dataType;
8380                         if(type1._class.registered.dataType)
8381                            type1._class.registered.dataType.refCount++;
8382                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8383                         exp.expType = type1._class.registered.dataType; //type1;
8384                         if(type1) type1.refCount++;
8385                      }
8386                      */
8387
8388                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8389                      {
8390                         if(exp.expType) FreeType(exp.expType);
8391                         exp.expType = exp.op.exp2.destType;
8392                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8393                      }
8394                      else if(type1 && type2)
8395                      {
8396                         char expString1[10240];
8397                         char expString2[10240];
8398                         char type1String[1024];
8399                         char type2String[1024];
8400                         expString1[0] = '\0';
8401                         expString2[0] = '\0';
8402                         type1String[0] = '\0';
8403                         type2String[0] = '\0';
8404                         if(inCompiler)
8405                         {
8406                            PrintExpression(exp.op.exp1, expString1);
8407                            ChangeCh(expString1, '\n', ' ');
8408                            PrintExpression(exp.op.exp2, expString2);
8409                            ChangeCh(expString2, '\n', ' ');
8410                            PrintType(exp.op.exp1.expType, type1String, false, true);
8411                            PrintType(exp.op.exp2.expType, type2String, false, true);
8412                         }
8413
8414                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8415
8416                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8417                         {
8418                            exp.expType = exp.op.exp1.expType;
8419                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8420                         }
8421                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8422                         {
8423                            exp.expType = exp.op.exp2.expType;
8424                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8425                         }
8426                      }
8427                   }
8428                }
8429                else if(type2)
8430                {
8431                   // Maybe this was meant to be an enum...
8432                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8433                   {
8434                      Type oldType = exp.op.exp1.expType;
8435                      exp.op.exp1.expType = null;
8436                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8437                         FreeType(oldType);
8438                      else
8439                         exp.op.exp1.expType = oldType;
8440                   }
8441
8442                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8443                   exp.op.exp1.destType = type2;
8444                   type2.refCount++;
8445                   /*
8446                   // TESTING THIS HERE... LATEST ADDITION
8447                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8448                   {
8449                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8450                      exp.op.exp1.destType = type1._class.registered.dataType;
8451                      if(type1._class.registered.dataType)
8452                         type1._class.registered.dataType.refCount++;
8453                   }
8454
8455                   // TESTING THIS HERE... LATEST ADDITION
8456                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8457                   {
8458                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8459                      exp.op.exp2.destType = type2._class.registered.dataType;
8460                      if(type2._class.registered.dataType)
8461                         type2._class.registered.dataType.refCount++;
8462                   }
8463                   */
8464
8465                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8466                   {
8467                      if(exp.expType) FreeType(exp.expType);
8468                      exp.expType = exp.op.exp1.destType;
8469                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8470                   }
8471                }
8472             }
8473             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8474             {
8475                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8476                {
8477                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8478                   // Convert e.g. / 4 into / 4.0
8479                   exp.op.exp1.destType = type2._class.registered.dataType;
8480                   if(type2._class.registered.dataType)
8481                      type2._class.registered.dataType.refCount++;
8482                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8483                }
8484                if(exp.op.op == '!')
8485                {
8486                   exp.expType = MkClassType("bool");
8487                   exp.expType.truth = true;
8488                }
8489                else
8490                {
8491                   exp.expType = type2;
8492                   if(type2) type2.refCount++;
8493                }
8494             }
8495             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8496             {
8497                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8498                {
8499                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8500                   // Convert e.g. / 4 into / 4.0
8501                   exp.op.exp2.destType = type1._class.registered.dataType;
8502                   if(type1._class.registered.dataType)
8503                      type1._class.registered.dataType.refCount++;
8504                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8505                }
8506                exp.expType = type1;
8507                if(type1) type1.refCount++;
8508             }
8509          }
8510
8511          yylloc = exp.loc;
8512          if(exp.op.exp1 && !exp.op.exp1.expType)
8513          {
8514             char expString[10000];
8515             expString[0] = '\0';
8516             if(inCompiler)
8517             {
8518                PrintExpression(exp.op.exp1, expString);
8519                ChangeCh(expString, '\n', ' ');
8520             }
8521             if(expString[0])
8522                Compiler_Error($"couldn't determine type of %s\n", expString);
8523          }
8524          if(exp.op.exp2 && !exp.op.exp2.expType)
8525          {
8526             char expString[10240];
8527             expString[0] = '\0';
8528             if(inCompiler)
8529             {
8530                PrintExpression(exp.op.exp2, expString);
8531                ChangeCh(expString, '\n', ' ');
8532             }
8533             if(expString[0])
8534                Compiler_Error($"couldn't determine type of %s\n", expString);
8535          }
8536
8537          if(boolResult)
8538          {
8539             FreeType(exp.expType);
8540             exp.expType = MkClassType("bool");
8541             exp.expType.truth = true;
8542          }
8543
8544          if(exp.op.op != SIZEOF)
8545             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8546                (!exp.op.exp2 || exp.op.exp2.isConstant);
8547
8548          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8549          {
8550             DeclareType(exp.op.exp2.expType, false, false);
8551          }
8552
8553          yylloc = oldyylloc;
8554
8555          FreeType(dummy);
8556          if(type2)
8557             FreeType(type2);
8558          break;
8559       }
8560       case bracketsExp:
8561       case extensionExpressionExp:
8562       {
8563          Expression e;
8564          exp.isConstant = true;
8565          for(e = exp.list->first; e; e = e.next)
8566          {
8567             bool inced = false;
8568             if(!e.next)
8569             {
8570                FreeType(e.destType);
8571                e.destType = exp.destType;
8572                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8573             }
8574             ProcessExpressionType(e);
8575             if(inced)
8576                exp.destType.count--;
8577             if(!exp.expType && !e.next)
8578             {
8579                exp.expType = e.expType;
8580                if(e.expType) e.expType.refCount++;
8581             }
8582             if(!e.isConstant)
8583                exp.isConstant = false;
8584          }
8585
8586          // In case a cast became a member...
8587          e = exp.list->first;
8588          if(!e.next && e.type == memberExp)
8589          {
8590             // Preserve prev, next
8591             Expression next = exp.next, prev = exp.prev;
8592
8593
8594             FreeType(exp.expType);
8595             FreeType(exp.destType);
8596             delete exp.list;
8597
8598             *exp = *e;
8599
8600             exp.prev = prev;
8601             exp.next = next;
8602
8603             delete e;
8604
8605             ProcessExpressionType(exp);
8606          }
8607          break;
8608       }
8609       case indexExp:
8610       {
8611          Expression e;
8612          exp.isConstant = true;
8613
8614          ProcessExpressionType(exp.index.exp);
8615          if(!exp.index.exp.isConstant)
8616             exp.isConstant = false;
8617
8618          if(exp.index.exp.expType)
8619          {
8620             Type source = exp.index.exp.expType;
8621             if(source.kind == classType && source._class && source._class.registered)
8622             {
8623                Class _class = source._class.registered;
8624                Class c = _class.templateClass ? _class.templateClass : _class;
8625                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8626                {
8627                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8628
8629                   if(exp.index.index && exp.index.index->last)
8630                   {
8631                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8632                   }
8633                }
8634             }
8635          }
8636
8637          for(e = exp.index.index->first; e; e = e.next)
8638          {
8639             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8640             {
8641                if(e.destType) FreeType(e.destType);
8642                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8643             }
8644             ProcessExpressionType(e);
8645             if(!e.next)
8646             {
8647                // Check if this type is int
8648             }
8649             if(!e.isConstant)
8650                exp.isConstant = false;
8651          }
8652
8653          if(!exp.expType)
8654             exp.expType = Dereference(exp.index.exp.expType);
8655          if(exp.expType)
8656             DeclareType(exp.expType, false, false);
8657          break;
8658       }
8659       case callExp:
8660       {
8661          Expression e;
8662          Type functionType;
8663          Type methodType = null;
8664          char name[1024];
8665          name[0] = '\0';
8666
8667          if(inCompiler)
8668          {
8669             PrintExpression(exp.call.exp,  name);
8670             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8671             {
8672                //exp.call.exp.expType = null;
8673                PrintExpression(exp.call.exp,  name);
8674             }
8675          }
8676          if(exp.call.exp.type == identifierExp)
8677          {
8678             Expression idExp = exp.call.exp;
8679             Identifier id = idExp.identifier;
8680             if(!strcmp(id.string, "__builtin_frame_address"))
8681             {
8682                exp.expType = ProcessTypeString("void *", true);
8683                if(exp.call.arguments && exp.call.arguments->first)
8684                   ProcessExpressionType(exp.call.arguments->first);
8685                break;
8686             }
8687             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8688             {
8689                exp.expType = ProcessTypeString("int", true);
8690                if(exp.call.arguments && exp.call.arguments->first)
8691                   ProcessExpressionType(exp.call.arguments->first);
8692                break;
8693             }
8694             else if(!strcmp(id.string, "Max") ||
8695                !strcmp(id.string, "Min") ||
8696                !strcmp(id.string, "Sgn") ||
8697                !strcmp(id.string, "Abs"))
8698             {
8699                Expression a = null;
8700                Expression b = null;
8701                Expression tempExp1 = null, tempExp2 = null;
8702                if((!strcmp(id.string, "Max") ||
8703                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8704                {
8705                   a = exp.call.arguments->first;
8706                   b = exp.call.arguments->last;
8707                   tempExp1 = a;
8708                   tempExp2 = b;
8709                }
8710                else if(exp.call.arguments->count == 1)
8711                {
8712                   a = exp.call.arguments->first;
8713                   tempExp1 = a;
8714                }
8715
8716                if(a)
8717                {
8718                   exp.call.arguments->Clear();
8719                   idExp.identifier = null;
8720
8721                   FreeExpContents(exp);
8722
8723                   ProcessExpressionType(a);
8724                   if(b)
8725                      ProcessExpressionType(b);
8726
8727                   exp.type = bracketsExp;
8728                   exp.list = MkList();
8729
8730                   if(a.expType && (!b || b.expType))
8731                   {
8732                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8733                      {
8734                         // Use the simpleStruct name/ids for now...
8735                         if(inCompiler)
8736                         {
8737                            OldList * specs = MkList();
8738                            OldList * decls = MkList();
8739                            Declaration decl;
8740                            char temp1[1024], temp2[1024];
8741
8742                            GetTypeSpecs(a.expType, specs);
8743
8744                            if(a && !a.isConstant && a.type != identifierExp)
8745                            {
8746                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8747                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8748                               tempExp1 = QMkExpId(temp1);
8749                               tempExp1.expType = a.expType;
8750                               if(a.expType)
8751                                  a.expType.refCount++;
8752                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8753                            }
8754                            if(b && !b.isConstant && b.type != identifierExp)
8755                            {
8756                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8757                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8758                               tempExp2 = QMkExpId(temp2);
8759                               tempExp2.expType = b.expType;
8760                               if(b.expType)
8761                                  b.expType.refCount++;
8762                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8763                            }
8764
8765                            decl = MkDeclaration(specs, decls);
8766                            if(!curCompound.compound.declarations)
8767                               curCompound.compound.declarations = MkList();
8768                            curCompound.compound.declarations->Insert(null, decl);
8769                         }
8770                      }
8771                   }
8772
8773                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8774                   {
8775                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8776                      ListAdd(exp.list,
8777                         MkExpCondition(MkExpBrackets(MkListOne(
8778                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8779                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8780                      exp.expType = a.expType;
8781                      if(a.expType)
8782                         a.expType.refCount++;
8783                   }
8784                   else if(!strcmp(id.string, "Abs"))
8785                   {
8786                      ListAdd(exp.list,
8787                         MkExpCondition(MkExpBrackets(MkListOne(
8788                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8789                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8790                      exp.expType = a.expType;
8791                      if(a.expType)
8792                         a.expType.refCount++;
8793                   }
8794                   else if(!strcmp(id.string, "Sgn"))
8795                   {
8796                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8797                      ListAdd(exp.list,
8798                         MkExpCondition(MkExpBrackets(MkListOne(
8799                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8800                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8801                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8802                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8803                      exp.expType = ProcessTypeString("int", false);
8804                   }
8805
8806                   FreeExpression(tempExp1);
8807                   if(tempExp2) FreeExpression(tempExp2);
8808
8809                   FreeIdentifier(id);
8810                   break;
8811                }
8812             }
8813          }
8814
8815          {
8816             Type dummy
8817             {
8818                count = 1;
8819                refCount = 1;
8820             };
8821             if(!exp.call.exp.destType)
8822             {
8823                exp.call.exp.destType = dummy;
8824                dummy.refCount++;
8825             }
8826             ProcessExpressionType(exp.call.exp);
8827             if(exp.call.exp.destType == dummy)
8828             {
8829                FreeType(dummy);
8830                exp.call.exp.destType = null;
8831             }
8832             FreeType(dummy);
8833          }
8834
8835          // Check argument types against parameter types
8836          functionType = exp.call.exp.expType;
8837
8838          if(functionType && functionType.kind == TypeKind::methodType)
8839          {
8840             methodType = functionType;
8841             functionType = methodType.method.dataType;
8842
8843             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8844             // TOCHECK: Instead of doing this here could this be done per param?
8845             if(exp.call.exp.expType.usedClass)
8846             {
8847                char typeString[1024];
8848                typeString[0] = '\0';
8849                {
8850                   Symbol back = functionType.thisClass;
8851                   // Do not output class specifier here (thisclass was added to this)
8852                   functionType.thisClass = null;
8853                   PrintType(functionType, typeString, true, true);
8854                   functionType.thisClass = back;
8855                }
8856                if(strstr(typeString, "thisclass"))
8857                {
8858                   OldList * specs = MkList();
8859                   Declarator decl;
8860                   {
8861                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8862
8863                      decl = SpecDeclFromString(typeString, specs, null);
8864
8865                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8866                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8867                         exp.call.exp.expType.usedClass))
8868                         thisClassParams = false;
8869
8870                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8871                      {
8872                         Class backupThisClass = thisClass;
8873                         thisClass = exp.call.exp.expType.usedClass;
8874                         ProcessDeclarator(decl);
8875                         thisClass = backupThisClass;
8876                      }
8877
8878                      thisClassParams = true;
8879
8880                      functionType = ProcessType(specs, decl);
8881                      functionType.refCount = 0;
8882                      FinishTemplatesContext(context);
8883                   }
8884
8885                   FreeList(specs, FreeSpecifier);
8886                   FreeDeclarator(decl);
8887                 }
8888             }
8889          }
8890          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8891          {
8892             Type type = functionType.type;
8893             if(!functionType.refCount)
8894             {
8895                functionType.type = null;
8896                FreeType(functionType);
8897             }
8898             //methodType = functionType;
8899             functionType = type;
8900          }
8901          if(functionType && functionType.kind != TypeKind::functionType)
8902          {
8903             Compiler_Error($"called object %s is not a function\n", name);
8904          }
8905          else if(functionType)
8906          {
8907             bool emptyParams = false, noParams = false;
8908             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8909             Type type = functionType.params.first;
8910             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8911             int extra = 0;
8912             Location oldyylloc = yylloc;
8913
8914             if(!type) emptyParams = true;
8915
8916             // WORKING ON THIS:
8917             if(functionType.extraParam && e && functionType.thisClass)
8918             {
8919                e.destType = MkClassType(functionType.thisClass.string);
8920                e = e.next;
8921             }
8922
8923             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8924             // Fixed #141 by adding '&& !functionType.extraParam'
8925             if(!functionType.staticMethod && !functionType.extraParam)
8926             {
8927                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
8928                   memberExp.member.exp.expType._class)
8929                {
8930                   type = MkClassType(memberExp.member.exp.expType._class.string);
8931                   if(e)
8932                   {
8933                      e.destType = type;
8934                      e = e.next;
8935                      type = functionType.params.first;
8936                   }
8937                   else
8938                      type.refCount = 0;
8939                }
8940                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8941                {
8942                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8943                   type.byReference = functionType.byReference;
8944                   type.typedByReference = functionType.typedByReference;
8945                   if(e)
8946                   {
8947                      // Allow manually passing a class for typed object
8948                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
8949                         e = e.next;
8950                      e.destType = type;
8951                      e = e.next;
8952                      type = functionType.params.first;
8953                   }
8954                   else
8955                      type.refCount = 0;
8956                   //extra = 1;
8957                }
8958             }
8959
8960             if(type && type.kind == voidType)
8961             {
8962                noParams = true;
8963                if(!type.refCount) FreeType(type);
8964                type = null;
8965             }
8966
8967             for( ; e; e = e.next)
8968             {
8969                if(!type && !emptyParams)
8970                {
8971                   yylloc = e.loc;
8972                   if(methodType && methodType.methodClass)
8973                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8974                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8975                         noParams ? 0 : functionType.params.count);
8976                   else
8977                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8978                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8979                         noParams ? 0 : functionType.params.count);
8980                   break;
8981                }
8982
8983                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8984                {
8985                   Type templatedType = null;
8986                   Class _class = methodType.usedClass;
8987                   ClassTemplateParameter curParam = null;
8988                   int id = 0;
8989                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8990                   {
8991                      Class sClass;
8992                      for(sClass = _class; sClass; sClass = sClass.base)
8993                      {
8994                         if(sClass.templateClass) sClass = sClass.templateClass;
8995                         id = 0;
8996                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8997                         {
8998                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8999                            {
9000                               Class nextClass;
9001                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9002                               {
9003                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9004                                  id += nextClass.templateParams.count;
9005                               }
9006                               break;
9007                            }
9008                            id++;
9009                         }
9010                         if(curParam) break;
9011                      }
9012                   }
9013                   if(curParam && _class.templateArgs[id].dataTypeString)
9014                   {
9015                      ClassTemplateArgument arg = _class.templateArgs[id];
9016                      {
9017                         Context context = SetupTemplatesContext(_class);
9018
9019                         /*if(!arg.dataType)
9020                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9021                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9022                         FinishTemplatesContext(context);
9023                      }
9024                      e.destType = templatedType;
9025                      if(templatedType)
9026                      {
9027                         templatedType.passAsTemplate = true;
9028                         // templatedType.refCount++;
9029                      }
9030                   }
9031                   else
9032                   {
9033                      e.destType = type;
9034                      if(type) type.refCount++;
9035                   }
9036                }
9037                else
9038                {
9039                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9040                   {
9041                      e.destType = type.prev;
9042                      e.destType.refCount++;
9043                   }
9044                   else
9045                   {
9046                      e.destType = type;
9047                      if(type) type.refCount++;
9048                   }
9049                }
9050                // Don't reach the end for the ellipsis
9051                if(type && type.kind != ellipsisType)
9052                {
9053                   Type next = type.next;
9054                   if(!type.refCount) FreeType(type);
9055                   type = next;
9056                }
9057             }
9058
9059             if(type && type.kind != ellipsisType)
9060             {
9061                if(methodType && methodType.methodClass)
9062                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9063                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9064                      functionType.params.count + extra);
9065                else
9066                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9067                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9068                      functionType.params.count + extra);
9069             }
9070             yylloc = oldyylloc;
9071             if(type && !type.refCount) FreeType(type);
9072          }
9073          else
9074          {
9075             functionType = Type
9076             {
9077                refCount = 0;
9078                kind = TypeKind::functionType;
9079             };
9080
9081             if(exp.call.exp.type == identifierExp)
9082             {
9083                char * string = exp.call.exp.identifier.string;
9084                if(inCompiler)
9085                {
9086                   Symbol symbol;
9087                   Location oldyylloc = yylloc;
9088
9089                   yylloc = exp.call.exp.identifier.loc;
9090                   if(strstr(string, "__builtin_") == string)
9091                   {
9092                      if(exp.destType)
9093                      {
9094                         functionType.returnType = exp.destType;
9095                         exp.destType.refCount++;
9096                      }
9097                   }
9098                   else
9099                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9100                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9101                   globalContext.symbols.Add((BTNode)symbol);
9102                   if(strstr(symbol.string, "::"))
9103                      globalContext.hasNameSpace = true;
9104
9105                   yylloc = oldyylloc;
9106                }
9107             }
9108             else if(exp.call.exp.type == memberExp)
9109             {
9110                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9111                   exp.call.exp.member.member.string);*/
9112             }
9113             else
9114                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9115
9116             if(!functionType.returnType)
9117             {
9118                functionType.returnType = Type
9119                {
9120                   refCount = 1;
9121                   kind = intType;
9122                };
9123             }
9124          }
9125          if(functionType && functionType.kind == TypeKind::functionType)
9126          {
9127             exp.expType = functionType.returnType;
9128
9129             if(functionType.returnType)
9130                functionType.returnType.refCount++;
9131
9132             if(!functionType.refCount)
9133                FreeType(functionType);
9134          }
9135
9136          if(exp.call.arguments)
9137          {
9138             for(e = exp.call.arguments->first; e; e = e.next)
9139             {
9140                Type destType = e.destType;
9141                ProcessExpressionType(e);
9142             }
9143          }
9144          break;
9145       }
9146       case memberExp:
9147       {
9148          Type type;
9149          Location oldyylloc = yylloc;
9150          bool thisPtr;
9151          Expression checkExp = exp.member.exp;
9152          while(checkExp)
9153          {
9154             if(checkExp.type == castExp)
9155                checkExp = checkExp.cast.exp;
9156             else if(checkExp.type == bracketsExp)
9157                checkExp = checkExp.list ? checkExp.list->first : null;
9158             else
9159                break;
9160          }
9161
9162          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9163          exp.thisPtr = thisPtr;
9164
9165          // DOING THIS LATER NOW...
9166          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9167          {
9168             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9169             /* TODO: Name Space Fix ups
9170             if(!exp.member.member.classSym)
9171                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9172             */
9173          }
9174
9175          ProcessExpressionType(exp.member.exp);
9176          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9177             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9178          {
9179             exp.isConstant = false;
9180          }
9181          else
9182             exp.isConstant = exp.member.exp.isConstant;
9183          type = exp.member.exp.expType;
9184
9185          yylloc = exp.loc;
9186
9187          if(type && (type.kind == templateType))
9188          {
9189             Class _class = thisClass ? thisClass : currentClass;
9190             ClassTemplateParameter param = null;
9191             if(_class)
9192             {
9193                for(param = _class.templateParams.first; param; param = param.next)
9194                {
9195                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9196                      break;
9197                }
9198             }
9199             if(param && param.defaultArg.member)
9200             {
9201                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9202                if(argExp)
9203                {
9204                   Expression expMember = exp.member.exp;
9205                   Declarator decl;
9206                   OldList * specs = MkList();
9207                   char thisClassTypeString[1024];
9208
9209                   FreeIdentifier(exp.member.member);
9210
9211                   ProcessExpressionType(argExp);
9212
9213                   {
9214                      char * colon = strstr(param.defaultArg.memberString, "::");
9215                      if(colon)
9216                      {
9217                         char className[1024];
9218                         Class sClass;
9219
9220                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9221                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9222                      }
9223                      else
9224                         strcpy(thisClassTypeString, _class.fullName);
9225                   }
9226
9227                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9228
9229                   exp.expType = ProcessType(specs, decl);
9230                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9231                   {
9232                      Class expClass = exp.expType._class.registered;
9233                      Class cClass = null;
9234                      int c;
9235                      int paramCount = 0;
9236                      int lastParam = -1;
9237
9238                      char templateString[1024];
9239                      ClassTemplateParameter param;
9240                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9241                      for(cClass = expClass; cClass; cClass = cClass.base)
9242                      {
9243                         int p = 0;
9244                         for(param = cClass.templateParams.first; param; param = param.next)
9245                         {
9246                            int id = p;
9247                            Class sClass;
9248                            ClassTemplateArgument arg;
9249                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9250                            arg = expClass.templateArgs[id];
9251
9252                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9253                            {
9254                               ClassTemplateParameter cParam;
9255                               //int p = numParams - sClass.templateParams.count;
9256                               int p = 0;
9257                               Class nextClass;
9258                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9259
9260                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9261                               {
9262                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9263                                  {
9264                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9265                                     {
9266                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9267                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9268                                        break;
9269                                     }
9270                                  }
9271                               }
9272                            }
9273
9274                            {
9275                               char argument[256];
9276                               argument[0] = '\0';
9277                               /*if(arg.name)
9278                               {
9279                                  strcat(argument, arg.name.string);
9280                                  strcat(argument, " = ");
9281                               }*/
9282                               switch(param.type)
9283                               {
9284                                  case expression:
9285                                  {
9286                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9287                                     char expString[1024];
9288                                     OldList * specs = MkList();
9289                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9290                                     Expression exp;
9291                                     char * string = PrintHexUInt64(arg.expression.ui64);
9292                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9293                                     delete string;
9294
9295                                     ProcessExpressionType(exp);
9296                                     ComputeExpression(exp);
9297                                     expString[0] = '\0';
9298                                     PrintExpression(exp, expString);
9299                                     strcat(argument, expString);
9300                                     // delete exp;
9301                                     FreeExpression(exp);
9302                                     break;
9303                                  }
9304                                  case identifier:
9305                                  {
9306                                     strcat(argument, arg.member.name);
9307                                     break;
9308                                  }
9309                                  case TemplateParameterType::type:
9310                                  {
9311                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9312                                     {
9313                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9314                                           strcat(argument, thisClassTypeString);
9315                                        else
9316                                           strcat(argument, arg.dataTypeString);
9317                                     }
9318                                     break;
9319                                  }
9320                               }
9321                               if(argument[0])
9322                               {
9323                                  if(paramCount) strcat(templateString, ", ");
9324                                  if(lastParam != p - 1)
9325                                  {
9326                                     strcat(templateString, param.name);
9327                                     strcat(templateString, " = ");
9328                                  }
9329                                  strcat(templateString, argument);
9330                                  paramCount++;
9331                                  lastParam = p;
9332                               }
9333                               p++;
9334                            }
9335                         }
9336                      }
9337                      {
9338                         int len = strlen(templateString);
9339                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9340                         templateString[len++] = '>';
9341                         templateString[len++] = '\0';
9342                      }
9343                      {
9344                         Context context = SetupTemplatesContext(_class);
9345                         FreeType(exp.expType);
9346                         exp.expType = ProcessTypeString(templateString, false);
9347                         FinishTemplatesContext(context);
9348                      }
9349                   }
9350
9351                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9352                   exp.type = bracketsExp;
9353                   exp.list = MkListOne(MkExpOp(null, '*',
9354                   /*opExp;
9355                   exp.op.op = '*';
9356                   exp.op.exp1 = null;
9357                   exp.op.exp2 = */
9358                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9359                      MkExpBrackets(MkListOne(
9360                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9361                            '+',
9362                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9363                            '+',
9364                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9365
9366                            ));
9367                }
9368             }
9369             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9370                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9371             {
9372                type = ProcessTemplateParameterType(type.templateParameter);
9373             }
9374          }
9375          // TODO: *** This seems to be where we should add method support for all basic types ***
9376          if(type && (type.kind == templateType));
9377          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9378                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9379                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9380                           (type.kind == pointerType && type.type.kind == charType)))
9381          {
9382             Identifier id = exp.member.member;
9383             TypeKind typeKind = type.kind;
9384             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9385             if(typeKind == subClassType && exp.member.exp.type == classExp)
9386             {
9387                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9388                typeKind = classType;
9389             }
9390
9391             if(id)
9392             {
9393                if(typeKind == intType || typeKind == enumType)
9394                   _class = eSystem_FindClass(privateModule, "int");
9395                else if(!_class)
9396                {
9397                   if(type.kind == classType && type._class && type._class.registered)
9398                   {
9399                      _class = type._class.registered;
9400                   }
9401                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9402                   {
9403                      _class = FindClass("char *").registered;
9404                   }
9405                   else if(type.kind == pointerType)
9406                   {
9407                      _class = eSystem_FindClass(privateModule, "uintptr");
9408                      FreeType(exp.expType);
9409                      exp.expType = ProcessTypeString("uintptr", false);
9410                      exp.byReference = true;
9411                   }
9412                   else
9413                   {
9414                      char string[1024] = "";
9415                      Symbol classSym;
9416                      PrintTypeNoConst(type, string, false, true);
9417                      classSym = FindClass(string);
9418                      if(classSym) _class = classSym.registered;
9419                   }
9420                }
9421             }
9422
9423             if(_class && id)
9424             {
9425                /*bool thisPtr =
9426                   (exp.member.exp.type == identifierExp &&
9427                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9428                Property prop = null;
9429                Method method = null;
9430                DataMember member = null;
9431                Property revConvert = null;
9432                ClassProperty classProp = null;
9433
9434                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9435                   exp.member.memberType = propertyMember;
9436
9437                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9438                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9439
9440                if(typeKind != subClassType)
9441                {
9442                   // Prioritize data members over properties for "this"
9443                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9444                   {
9445                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9446                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9447                      {
9448                         prop = eClass_FindProperty(_class, id.string, privateModule);
9449                         if(prop)
9450                            member = null;
9451                      }
9452                      if(!member && !prop)
9453                         prop = eClass_FindProperty(_class, id.string, privateModule);
9454                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9455                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9456                         exp.member.thisPtr = true;
9457                   }
9458                   // Prioritize properties over data members otherwise
9459                   else
9460                   {
9461                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9462                      if(!id.classSym)
9463                      {
9464                         prop = eClass_FindProperty(_class, id.string, null);
9465                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9466                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9467                      }
9468
9469                      if(!prop && !member)
9470                      {
9471                         method = eClass_FindMethod(_class, id.string, null);
9472                         if(!method)
9473                         {
9474                            prop = eClass_FindProperty(_class, id.string, privateModule);
9475                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9476                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9477                         }
9478                      }
9479
9480                      if(member && prop)
9481                      {
9482                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9483                            prop = null;
9484                         else
9485                            member = null;
9486                      }
9487                   }
9488                }
9489                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9490                   method = eClass_FindMethod(_class, id.string, privateModule);
9491                if(!prop && !member && !method)
9492                {
9493                   if(typeKind == subClassType)
9494                   {
9495                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9496                      if(classProp)
9497                      {
9498                         exp.member.memberType = classPropertyMember;
9499                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9500                      }
9501                      else
9502                      {
9503                         // Assume this is a class_data member
9504                         char structName[1024];
9505                         Identifier id = exp.member.member;
9506                         Expression classExp = exp.member.exp;
9507                         type.refCount++;
9508
9509                         FreeType(classExp.expType);
9510                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9511
9512                         strcpy(structName, "__ecereClassData_");
9513                         FullClassNameCat(structName, type._class.string, false);
9514                         exp.type = pointerExp;
9515                         exp.member.member = id;
9516
9517                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9518                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9519                               MkExpBrackets(MkListOne(MkExpOp(
9520                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9521                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9522                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9523                                  )));
9524
9525                         FreeType(type);
9526
9527                         ProcessExpressionType(exp);
9528                         return;
9529                      }
9530                   }
9531                   else
9532                   {
9533                      // Check for reverse conversion
9534                      // (Convert in an instantiation later, so that we can use
9535                      //  deep properties system)
9536                      Symbol classSym = FindClass(id.string);
9537                      if(classSym)
9538                      {
9539                         Class convertClass = classSym.registered;
9540                         if(convertClass)
9541                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9542                      }
9543                   }
9544                }
9545
9546                if(prop)
9547                {
9548                   exp.member.memberType = propertyMember;
9549                   if(!prop.dataType)
9550                      ProcessPropertyType(prop);
9551                   exp.expType = prop.dataType;
9552                   if(prop.dataType) prop.dataType.refCount++;
9553                }
9554                else if(member)
9555                {
9556                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9557                   {
9558                      FreeExpContents(exp);
9559                      exp.type = identifierExp;
9560                      exp.identifier = MkIdentifier("class");
9561                      ProcessExpressionType(exp);
9562                      return;
9563                   }
9564
9565                   exp.member.memberType = dataMember;
9566                   DeclareStruct(_class.fullName, false);
9567                   if(!member.dataType)
9568                   {
9569                      Context context = SetupTemplatesContext(_class);
9570                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9571                      FinishTemplatesContext(context);
9572                   }
9573                   exp.expType = member.dataType;
9574                   if(member.dataType) member.dataType.refCount++;
9575                }
9576                else if(revConvert)
9577                {
9578                   exp.member.memberType = reverseConversionMember;
9579                   exp.expType = MkClassType(revConvert._class.fullName);
9580                }
9581                else if(method)
9582                {
9583                   //if(inCompiler)
9584                   {
9585                      /*if(id._class)
9586                      {
9587                         exp.type = identifierExp;
9588                         exp.identifier = exp.member.member;
9589                      }
9590                      else*/
9591                         exp.member.memberType = methodMember;
9592                   }
9593                   if(!method.dataType)
9594                      ProcessMethodType(method);
9595                   exp.expType = Type
9596                   {
9597                      refCount = 1;
9598                      kind = methodType;
9599                      method = method;
9600                   };
9601
9602                   // Tricky spot here... To use instance versus class virtual table
9603                   // Put it back to what it was... What did we break?
9604
9605                   // Had to put it back for overriding Main of Thread global instance
9606
9607                   //exp.expType.methodClass = _class;
9608                   exp.expType.methodClass = (id && id._class) ? _class : null;
9609
9610                   // Need the actual class used for templated classes
9611                   exp.expType.usedClass = _class;
9612                }
9613                else if(!classProp)
9614                {
9615                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9616                   {
9617                      FreeExpContents(exp);
9618                      exp.type = identifierExp;
9619                      exp.identifier = MkIdentifier("class");
9620                      ProcessExpressionType(exp);
9621                      return;
9622                   }
9623                   yylloc = exp.member.member.loc;
9624                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9625                   if(inCompiler)
9626                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9627                }
9628
9629                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9630                {
9631                   Class tClass;
9632
9633                   tClass = _class;
9634                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9635
9636                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9637                   {
9638                      int id = 0;
9639                      ClassTemplateParameter curParam = null;
9640                      Class sClass;
9641
9642                      for(sClass = tClass; sClass; sClass = sClass.base)
9643                      {
9644                         id = 0;
9645                         if(sClass.templateClass) sClass = sClass.templateClass;
9646                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9647                         {
9648                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9649                            {
9650                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9651                                  id += sClass.templateParams.count;
9652                               break;
9653                            }
9654                            id++;
9655                         }
9656                         if(curParam) break;
9657                      }
9658
9659                      if(curParam && tClass.templateArgs[id].dataTypeString)
9660                      {
9661                         ClassTemplateArgument arg = tClass.templateArgs[id];
9662                         Context context = SetupTemplatesContext(tClass);
9663                         /*if(!arg.dataType)
9664                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9665                         FreeType(exp.expType);
9666                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9667                         if(exp.expType)
9668                         {
9669                            if(exp.expType.kind == thisClassType)
9670                            {
9671                               FreeType(exp.expType);
9672                               exp.expType = ReplaceThisClassType(_class);
9673                            }
9674
9675                            if(tClass.templateClass)
9676                               exp.expType.passAsTemplate = true;
9677                            //exp.expType.refCount++;
9678                            if(!exp.destType)
9679                            {
9680                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9681                               //exp.destType.refCount++;
9682
9683                               if(exp.destType.kind == thisClassType)
9684                               {
9685                                  FreeType(exp.destType);
9686                                  exp.destType = ReplaceThisClassType(_class);
9687                               }
9688                            }
9689                         }
9690                         FinishTemplatesContext(context);
9691                      }
9692                   }
9693                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9694                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9695                   {
9696                      int id = 0;
9697                      ClassTemplateParameter curParam = null;
9698                      Class sClass;
9699
9700                      for(sClass = tClass; sClass; sClass = sClass.base)
9701                      {
9702                         id = 0;
9703                         if(sClass.templateClass) sClass = sClass.templateClass;
9704                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9705                         {
9706                            if(curParam.type == TemplateParameterType::type &&
9707                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9708                            {
9709                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9710                                  id += sClass.templateParams.count;
9711                               break;
9712                            }
9713                            id++;
9714                         }
9715                         if(curParam) break;
9716                      }
9717
9718                      if(curParam)
9719                      {
9720                         ClassTemplateArgument arg = tClass.templateArgs[id];
9721                         Context context = SetupTemplatesContext(tClass);
9722                         Type basicType;
9723                         /*if(!arg.dataType)
9724                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9725
9726                         basicType = ProcessTypeString(arg.dataTypeString, false);
9727                         if(basicType)
9728                         {
9729                            if(basicType.kind == thisClassType)
9730                            {
9731                               FreeType(basicType);
9732                               basicType = ReplaceThisClassType(_class);
9733                            }
9734
9735                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9736                            if(tClass.templateClass)
9737                               basicType.passAsTemplate = true;
9738                            */
9739
9740                            FreeType(exp.expType);
9741
9742                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9743                            //exp.expType.refCount++;
9744                            if(!exp.destType)
9745                            {
9746                               exp.destType = exp.expType;
9747                               exp.destType.refCount++;
9748                            }
9749
9750                            {
9751                               Expression newExp { };
9752                               OldList * specs = MkList();
9753                               Declarator decl;
9754                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9755                               *newExp = *exp;
9756                               if(exp.destType) exp.destType.refCount++;
9757                               if(exp.expType)  exp.expType.refCount++;
9758                               exp.type = castExp;
9759                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9760                               exp.cast.exp = newExp;
9761                               //FreeType(exp.expType);
9762                               //exp.expType = null;
9763                               //ProcessExpressionType(sourceExp);
9764                            }
9765                         }
9766                         FinishTemplatesContext(context);
9767                      }
9768                   }
9769                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9770                   {
9771                      Class expClass = exp.expType._class.registered;
9772                      if(expClass)
9773                      {
9774                         Class cClass = null;
9775                         int c;
9776                         int p = 0;
9777                         int paramCount = 0;
9778                         int lastParam = -1;
9779                         char templateString[1024];
9780                         ClassTemplateParameter param;
9781                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9782                         while(cClass != expClass)
9783                         {
9784                            Class sClass;
9785                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9786                            cClass = sClass;
9787
9788                            for(param = cClass.templateParams.first; param; param = param.next)
9789                            {
9790                               Class cClassCur = null;
9791                               int c;
9792                               int cp = 0;
9793                               ClassTemplateParameter paramCur = null;
9794                               ClassTemplateArgument arg;
9795                               while(cClassCur != tClass && !paramCur)
9796                               {
9797                                  Class sClassCur;
9798                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9799                                  cClassCur = sClassCur;
9800
9801                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9802                                  {
9803                                     if(!strcmp(paramCur.name, param.name))
9804                                     {
9805
9806                                        break;
9807                                     }
9808                                     cp++;
9809                                  }
9810                               }
9811                               if(paramCur && paramCur.type == TemplateParameterType::type)
9812                                  arg = tClass.templateArgs[cp];
9813                               else
9814                                  arg = expClass.templateArgs[p];
9815
9816                               {
9817                                  char argument[256];
9818                                  argument[0] = '\0';
9819                                  /*if(arg.name)
9820                                  {
9821                                     strcat(argument, arg.name.string);
9822                                     strcat(argument, " = ");
9823                                  }*/
9824                                  switch(param.type)
9825                                  {
9826                                     case expression:
9827                                     {
9828                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9829                                        char expString[1024];
9830                                        OldList * specs = MkList();
9831                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9832                                        Expression exp;
9833                                        char * string = PrintHexUInt64(arg.expression.ui64);
9834                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9835                                        delete string;
9836
9837                                        ProcessExpressionType(exp);
9838                                        ComputeExpression(exp);
9839                                        expString[0] = '\0';
9840                                        PrintExpression(exp, expString);
9841                                        strcat(argument, expString);
9842                                        // delete exp;
9843                                        FreeExpression(exp);
9844                                        break;
9845                                     }
9846                                     case identifier:
9847                                     {
9848                                        strcat(argument, arg.member.name);
9849                                        break;
9850                                     }
9851                                     case TemplateParameterType::type:
9852                                     {
9853                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9854                                           strcat(argument, arg.dataTypeString);
9855                                        break;
9856                                     }
9857                                  }
9858                                  if(argument[0])
9859                                  {
9860                                     if(paramCount) strcat(templateString, ", ");
9861                                     if(lastParam != p - 1)
9862                                     {
9863                                        strcat(templateString, param.name);
9864                                        strcat(templateString, " = ");
9865                                     }
9866                                     strcat(templateString, argument);
9867                                     paramCount++;
9868                                     lastParam = p;
9869                                  }
9870                               }
9871                               p++;
9872                            }
9873                         }
9874                         {
9875                            int len = strlen(templateString);
9876                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9877                            templateString[len++] = '>';
9878                            templateString[len++] = '\0';
9879                         }
9880
9881                         FreeType(exp.expType);
9882                         {
9883                            Context context = SetupTemplatesContext(tClass);
9884                            exp.expType = ProcessTypeString(templateString, false);
9885                            FinishTemplatesContext(context);
9886                         }
9887                      }
9888                   }
9889                }
9890             }
9891             else
9892                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9893          }
9894          else if(type && (type.kind == structType || type.kind == unionType))
9895          {
9896             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9897             if(memberType)
9898             {
9899                exp.expType = memberType;
9900                if(memberType)
9901                   memberType.refCount++;
9902             }
9903          }
9904          else
9905          {
9906             char expString[10240];
9907             expString[0] = '\0';
9908             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9909             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9910          }
9911
9912          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9913          {
9914             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9915             {
9916                Identifier id = exp.member.member;
9917                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9918                if(_class)
9919                {
9920                   FreeType(exp.expType);
9921                   exp.expType = ReplaceThisClassType(_class);
9922                }
9923             }
9924          }
9925          yylloc = oldyylloc;
9926          break;
9927       }
9928       // Convert x->y into (*x).y
9929       case pointerExp:
9930       {
9931          Type destType = exp.destType;
9932
9933          // DOING THIS LATER NOW...
9934          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9935          {
9936             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9937             /* TODO: Name Space Fix ups
9938             if(!exp.member.member.classSym)
9939                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9940             */
9941          }
9942
9943          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9944          exp.type = memberExp;
9945          if(destType)
9946             destType.count++;
9947          ProcessExpressionType(exp);
9948          if(destType)
9949             destType.count--;
9950          break;
9951       }
9952       case classSizeExp:
9953       {
9954          //ComputeExpression(exp);
9955
9956          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9957          if(classSym && classSym.registered)
9958          {
9959             if(classSym.registered.type == noHeadClass)
9960             {
9961                char name[1024];
9962                name[0] = '\0';
9963                DeclareStruct(classSym.string, false);
9964                FreeSpecifier(exp._class);
9965                exp.type = typeSizeExp;
9966                FullClassNameCat(name, classSym.string, false);
9967                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9968             }
9969             else
9970             {
9971                if(classSym.registered.fixed)
9972                {
9973                   FreeSpecifier(exp._class);
9974                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9975                   exp.type = constantExp;
9976                }
9977                else
9978                {
9979                   char className[1024];
9980                   strcpy(className, "__ecereClass_");
9981                   FullClassNameCat(className, classSym.string, true);
9982                   MangleClassName(className);
9983
9984                   DeclareClass(classSym, className);
9985
9986                   FreeExpContents(exp);
9987                   exp.type = pointerExp;
9988                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9989                   exp.member.member = MkIdentifier("structSize");
9990                }
9991             }
9992          }
9993
9994          exp.expType = Type
9995          {
9996             refCount = 1;
9997             kind = intType;
9998          };
9999          // exp.isConstant = true;
10000          break;
10001       }
10002       case typeSizeExp:
10003       {
10004          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10005
10006          exp.expType = Type
10007          {
10008             refCount = 1;
10009             kind = intType;
10010          };
10011          exp.isConstant = true;
10012
10013          DeclareType(type, false, false);
10014          FreeType(type);
10015          break;
10016       }
10017       case castExp:
10018       {
10019          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10020          type.count = 1;
10021          FreeType(exp.cast.exp.destType);
10022          exp.cast.exp.destType = type;
10023          type.refCount++;
10024          ProcessExpressionType(exp.cast.exp);
10025          type.count = 0;
10026          exp.expType = type;
10027          //type.refCount++;
10028
10029          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10030          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10031          {
10032             void * prev = exp.prev, * next = exp.next;
10033             Type expType = exp.cast.exp.destType;
10034             Expression castExp = exp.cast.exp;
10035             Type destType = exp.destType;
10036
10037             if(expType) expType.refCount++;
10038
10039             //FreeType(exp.destType);
10040             FreeType(exp.expType);
10041             FreeTypeName(exp.cast.typeName);
10042
10043             *exp = *castExp;
10044             FreeType(exp.expType);
10045             FreeType(exp.destType);
10046
10047             exp.expType = expType;
10048             exp.destType = destType;
10049
10050             delete castExp;
10051
10052             exp.prev = prev;
10053             exp.next = next;
10054
10055          }
10056          else
10057          {
10058             exp.isConstant = exp.cast.exp.isConstant;
10059          }
10060          //FreeType(type);
10061          break;
10062       }
10063       case extensionInitializerExp:
10064       {
10065          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10066          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10067          // ProcessInitializer(exp.initializer.initializer, type);
10068          exp.expType = type;
10069          break;
10070       }
10071       case vaArgExp:
10072       {
10073          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10074          ProcessExpressionType(exp.vaArg.exp);
10075          exp.expType = type;
10076          break;
10077       }
10078       case conditionExp:
10079       {
10080          Expression e;
10081          exp.isConstant = true;
10082
10083          FreeType(exp.cond.cond.destType);
10084          exp.cond.cond.destType = MkClassType("bool");
10085          exp.cond.cond.destType.truth = true;
10086          ProcessExpressionType(exp.cond.cond);
10087          if(!exp.cond.cond.isConstant)
10088             exp.isConstant = false;
10089          for(e = exp.cond.exp->first; e; e = e.next)
10090          {
10091             if(!e.next)
10092             {
10093                FreeType(e.destType);
10094                e.destType = exp.destType;
10095                if(e.destType) e.destType.refCount++;
10096             }
10097             ProcessExpressionType(e);
10098             if(!e.next)
10099             {
10100                exp.expType = e.expType;
10101                if(e.expType) e.expType.refCount++;
10102             }
10103             if(!e.isConstant)
10104                exp.isConstant = false;
10105          }
10106
10107          FreeType(exp.cond.elseExp.destType);
10108          // Added this check if we failed to find an expType
10109          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10110
10111          // Reversed it...
10112          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10113
10114          if(exp.cond.elseExp.destType)
10115             exp.cond.elseExp.destType.refCount++;
10116          ProcessExpressionType(exp.cond.elseExp);
10117
10118          // FIXED THIS: Was done before calling process on elseExp
10119          if(!exp.cond.elseExp.isConstant)
10120             exp.isConstant = false;
10121          break;
10122       }
10123       case extensionCompoundExp:
10124       {
10125          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10126          {
10127             Statement last = exp.compound.compound.statements->last;
10128             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10129             {
10130                ((Expression)last.expressions->last).destType = exp.destType;
10131                if(exp.destType)
10132                   exp.destType.refCount++;
10133             }
10134             ProcessStatement(exp.compound);
10135             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10136             if(exp.expType)
10137                exp.expType.refCount++;
10138          }
10139          break;
10140       }
10141       case classExp:
10142       {
10143          Specifier spec = exp._classExp.specifiers->first;
10144          if(spec && spec.type == nameSpecifier)
10145          {
10146             exp.expType = MkClassType(spec.name);
10147             exp.expType.kind = subClassType;
10148             exp.byReference = true;
10149          }
10150          else
10151          {
10152             exp.expType = MkClassType("ecere::com::Class");
10153             exp.byReference = true;
10154          }
10155          break;
10156       }
10157       case classDataExp:
10158       {
10159          Class _class = thisClass ? thisClass : currentClass;
10160          if(_class)
10161          {
10162             Identifier id = exp.classData.id;
10163             char structName[1024];
10164             Expression classExp;
10165             strcpy(structName, "__ecereClassData_");
10166             FullClassNameCat(structName, _class.fullName, false);
10167             exp.type = pointerExp;
10168             exp.member.member = id;
10169             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10170                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10171             else
10172                classExp = MkExpIdentifier(MkIdentifier("class"));
10173
10174             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10175                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10176                   MkExpBrackets(MkListOne(MkExpOp(
10177                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10178                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10179                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10180                      )));
10181
10182             ProcessExpressionType(exp);
10183             return;
10184          }
10185          break;
10186       }
10187       case arrayExp:
10188       {
10189          Type type = null;
10190          char * typeString = null;
10191          char typeStringBuf[1024];
10192          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10193             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10194          {
10195             Class templateClass = exp.destType._class.registered;
10196             typeString = templateClass.templateArgs[2].dataTypeString;
10197          }
10198          else if(exp.list)
10199          {
10200             // Guess type from expressions in the array
10201             Expression e;
10202             for(e = exp.list->first; e; e = e.next)
10203             {
10204                ProcessExpressionType(e);
10205                if(e.expType)
10206                {
10207                   if(!type) { type = e.expType; type.refCount++; }
10208                   else
10209                   {
10210                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10211                      if(!MatchTypeExpression(e, type, null, false))
10212                      {
10213                         FreeType(type);
10214                         type = e.expType;
10215                         e.expType = null;
10216
10217                         e = exp.list->first;
10218                         ProcessExpressionType(e);
10219                         if(e.expType)
10220                         {
10221                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10222                            if(!MatchTypeExpression(e, type, null, false))
10223                            {
10224                               FreeType(e.expType);
10225                               e.expType = null;
10226                               FreeType(type);
10227                               type = null;
10228                               break;
10229                            }
10230                         }
10231                      }
10232                   }
10233                   if(e.expType)
10234                   {
10235                      FreeType(e.expType);
10236                      e.expType = null;
10237                   }
10238                }
10239             }
10240             if(type)
10241             {
10242                typeStringBuf[0] = '\0';
10243                PrintTypeNoConst(type, typeStringBuf, false, true);
10244                typeString = typeStringBuf;
10245                FreeType(type);
10246                type = null;
10247             }
10248          }
10249          if(typeString)
10250          {
10251             /*
10252             (Container)& (struct BuiltInContainer)
10253             {
10254                ._vTbl = class(BuiltInContainer)._vTbl,
10255                ._class = class(BuiltInContainer),
10256                .refCount = 0,
10257                .data = (int[]){ 1, 7, 3, 4, 5 },
10258                .count = 5,
10259                .type = class(int),
10260             }
10261             */
10262             char templateString[1024];
10263             OldList * initializers = MkList();
10264             OldList * structInitializers = MkList();
10265             OldList * specs = MkList();
10266             Expression expExt;
10267             Declarator decl = SpecDeclFromString(typeString, specs, null);
10268             sprintf(templateString, "Container<%s>", typeString);
10269
10270             if(exp.list)
10271             {
10272                Expression e;
10273                type = ProcessTypeString(typeString, false);
10274                while(e = exp.list->first)
10275                {
10276                   exp.list->Remove(e);
10277                   e.destType = type;
10278                   type.refCount++;
10279                   ProcessExpressionType(e);
10280                   ListAdd(initializers, MkInitializerAssignment(e));
10281                }
10282                FreeType(type);
10283                delete exp.list;
10284             }
10285
10286             DeclareStruct("ecere::com::BuiltInContainer", false);
10287
10288             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10289                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10290             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10291                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10292             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10293                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10294             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10295                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10296                MkInitializerList(initializers))));
10297                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10298             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10299                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10300             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10301                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10302             exp.expType = ProcessTypeString(templateString, false);
10303             exp.type = bracketsExp;
10304             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10305                MkExpOp(null, '&',
10306                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10307                   MkInitializerList(structInitializers)))));
10308             ProcessExpressionType(expExt);
10309          }
10310          else
10311          {
10312             exp.expType = ProcessTypeString("Container", false);
10313             Compiler_Error($"Couldn't determine type of array elements\n");
10314          }
10315          break;
10316       }
10317    }
10318
10319    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10320    {
10321       FreeType(exp.expType);
10322       exp.expType = ReplaceThisClassType(thisClass);
10323    }
10324
10325    // Resolve structures here
10326    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10327    {
10328       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10329       // TODO: Fix members reference...
10330       if(symbol)
10331       {
10332          if(exp.expType.kind != enumType)
10333          {
10334             Type member;
10335             String enumName = CopyString(exp.expType.enumName);
10336
10337             // Fixed a memory leak on self-referencing C structs typedefs
10338             // by instantiating a new type rather than simply copying members
10339             // into exp.expType
10340             FreeType(exp.expType);
10341             exp.expType = Type { };
10342             exp.expType.kind = symbol.type.kind;
10343             exp.expType.refCount++;
10344             exp.expType.enumName = enumName;
10345
10346             exp.expType.members = symbol.type.members;
10347             for(member = symbol.type.members.first; member; member = member.next)
10348                member.refCount++;
10349          }
10350          else
10351          {
10352             NamedLink member;
10353             for(member = symbol.type.members.first; member; member = member.next)
10354             {
10355                NamedLink value { name = CopyString(member.name) };
10356                exp.expType.members.Add(value);
10357             }
10358          }
10359       }
10360    }
10361
10362    yylloc = exp.loc;
10363    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10364    else if(exp.destType && !exp.destType.keepCast)
10365    {
10366       if(!CheckExpressionType(exp, exp.destType, false))
10367       {
10368          if(!exp.destType.count || unresolved)
10369          {
10370             if(!exp.expType)
10371             {
10372                yylloc = exp.loc;
10373                if(exp.destType.kind != ellipsisType)
10374                {
10375                   char type2[1024];
10376                   type2[0] = '\0';
10377                   if(inCompiler)
10378                   {
10379                      char expString[10240];
10380                      expString[0] = '\0';
10381
10382                      PrintType(exp.destType, type2, false, true);
10383
10384                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10385                      if(unresolved)
10386                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10387                      else if(exp.type != dummyExp)
10388                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10389                   }
10390                }
10391                else
10392                {
10393                   char expString[10240] ;
10394                   expString[0] = '\0';
10395                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10396
10397                   if(unresolved)
10398                      Compiler_Error($"unresolved identifier %s\n", expString);
10399                   else if(exp.type != dummyExp)
10400                      Compiler_Error($"couldn't determine type of %s\n", expString);
10401                }
10402             }
10403             else
10404             {
10405                char type1[1024];
10406                char type2[1024];
10407                type1[0] = '\0';
10408                type2[0] = '\0';
10409                if(inCompiler)
10410                {
10411                   PrintType(exp.expType, type1, false, true);
10412                   PrintType(exp.destType, type2, false, true);
10413                }
10414
10415                //CheckExpressionType(exp, exp.destType, false);
10416
10417                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10418                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10419                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10420                else
10421                {
10422                   char expString[10240];
10423                   expString[0] = '\0';
10424                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10425
10426 #ifdef _DEBUG
10427                   CheckExpressionType(exp, exp.destType, false);
10428 #endif
10429                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10430                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10431                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10432
10433                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10434                   FreeType(exp.expType);
10435                   exp.destType.refCount++;
10436                   exp.expType = exp.destType;
10437                }
10438             }
10439          }
10440       }
10441       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10442       {
10443          Expression newExp { };
10444          char typeString[1024];
10445          OldList * specs = MkList();
10446          Declarator decl;
10447
10448          typeString[0] = '\0';
10449
10450          *newExp = *exp;
10451
10452          if(exp.expType)  exp.expType.refCount++;
10453          if(exp.expType)  exp.expType.refCount++;
10454          exp.type = castExp;
10455          newExp.destType = exp.expType;
10456
10457          PrintType(exp.expType, typeString, false, false);
10458          decl = SpecDeclFromString(typeString, specs, null);
10459
10460          exp.cast.typeName = MkTypeName(specs, decl);
10461          exp.cast.exp = newExp;
10462       }
10463    }
10464    else if(unresolved)
10465    {
10466       if(exp.identifier._class && exp.identifier._class.name)
10467          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10468       else if(exp.identifier.string && exp.identifier.string[0])
10469          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10470    }
10471    else if(!exp.expType && exp.type != dummyExp)
10472    {
10473       char expString[10240];
10474       expString[0] = '\0';
10475       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10476       Compiler_Error($"couldn't determine type of %s\n", expString);
10477    }
10478
10479    // Let's try to support any_object & typed_object here:
10480    if(inCompiler)
10481       ApplyAnyObjectLogic(exp);
10482
10483    // Mark nohead classes as by reference, unless we're casting them to an integral type
10484    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10485       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10486          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10487           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10488    {
10489       exp.byReference = true;
10490    }
10491    yylloc = oldyylloc;
10492 }
10493
10494 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10495 {
10496    // THIS CODE WILL FIND NEXT MEMBER...
10497    if(*curMember)
10498    {
10499       *curMember = (*curMember).next;
10500
10501       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10502       {
10503          *curMember = subMemberStack[--(*subMemberStackPos)];
10504          *curMember = (*curMember).next;
10505       }
10506
10507       // SKIP ALL PROPERTIES HERE...
10508       while((*curMember) && (*curMember).isProperty)
10509          *curMember = (*curMember).next;
10510
10511       if(subMemberStackPos)
10512       {
10513          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10514          {
10515             subMemberStack[(*subMemberStackPos)++] = *curMember;
10516
10517             *curMember = (*curMember).members.first;
10518             while(*curMember && (*curMember).isProperty)
10519                *curMember = (*curMember).next;
10520          }
10521       }
10522    }
10523    while(!*curMember)
10524    {
10525       if(!*curMember)
10526       {
10527          if(subMemberStackPos && *subMemberStackPos)
10528          {
10529             *curMember = subMemberStack[--(*subMemberStackPos)];
10530             *curMember = (*curMember).next;
10531          }
10532          else
10533          {
10534             Class lastCurClass = *curClass;
10535
10536             if(*curClass == _class) break;     // REACHED THE END
10537
10538             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10539             *curMember = (*curClass).membersAndProperties.first;
10540          }
10541
10542          while((*curMember) && (*curMember).isProperty)
10543             *curMember = (*curMember).next;
10544          if(subMemberStackPos)
10545          {
10546             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10547             {
10548                subMemberStack[(*subMemberStackPos)++] = *curMember;
10549
10550                *curMember = (*curMember).members.first;
10551                while(*curMember && (*curMember).isProperty)
10552                   *curMember = (*curMember).next;
10553             }
10554          }
10555       }
10556    }
10557 }
10558
10559
10560 static void ProcessInitializer(Initializer init, Type type)
10561 {
10562    switch(init.type)
10563    {
10564       case expInitializer:
10565          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10566          {
10567             // TESTING THIS FOR SHUTTING = 0 WARNING
10568             if(init.exp && !init.exp.destType)
10569             {
10570                FreeType(init.exp.destType);
10571                init.exp.destType = type;
10572                if(type) type.refCount++;
10573             }
10574             if(init.exp)
10575             {
10576                ProcessExpressionType(init.exp);
10577                init.isConstant = init.exp.isConstant;
10578             }
10579             break;
10580          }
10581          else
10582          {
10583             Expression exp = init.exp;
10584             Instantiation inst = exp.instance;
10585             MembersInit members;
10586
10587             init.type = listInitializer;
10588             init.list = MkList();
10589
10590             if(inst.members)
10591             {
10592                for(members = inst.members->first; members; members = members.next)
10593                {
10594                   if(members.type == dataMembersInit)
10595                   {
10596                      MemberInit member;
10597                      for(member = members.dataMembers->first; member; member = member.next)
10598                      {
10599                         ListAdd(init.list, member.initializer);
10600                         member.initializer = null;
10601                      }
10602                   }
10603                   // Discard all MembersInitMethod
10604                }
10605             }
10606             FreeExpression(exp);
10607          }
10608       case listInitializer:
10609       {
10610          Initializer i;
10611          Type initializerType = null;
10612          Class curClass = null;
10613          DataMember curMember = null;
10614          DataMember subMemberStack[256];
10615          int subMemberStackPos = 0;
10616
10617          if(type && type.kind == arrayType)
10618             initializerType = Dereference(type);
10619          else if(type && (type.kind == structType || type.kind == unionType))
10620             initializerType = type.members.first;
10621
10622          for(i = init.list->first; i; i = i.next)
10623          {
10624             if(type && type.kind == classType && type._class && type._class.registered)
10625             {
10626                // 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)
10627                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10628                // TODO: Generate error on initializing a private data member this way from another module...
10629                if(curMember)
10630                {
10631                   if(!curMember.dataType)
10632                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10633                   initializerType = curMember.dataType;
10634                }
10635             }
10636             ProcessInitializer(i, initializerType);
10637             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10638                initializerType = initializerType.next;
10639             if(!i.isConstant)
10640                init.isConstant = false;
10641          }
10642
10643          if(type && type.kind == arrayType)
10644             FreeType(initializerType);
10645
10646          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10647          {
10648             Compiler_Error($"Assigning list initializer to non list\n");
10649          }
10650          break;
10651       }
10652    }
10653 }
10654
10655 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10656 {
10657    switch(spec.type)
10658    {
10659       case baseSpecifier:
10660       {
10661          if(spec.specifier == THISCLASS)
10662          {
10663             if(thisClass)
10664             {
10665                spec.type = nameSpecifier;
10666                spec.name = ReplaceThisClass(thisClass);
10667                spec.symbol = FindClass(spec.name);
10668                ProcessSpecifier(spec, declareStruct);
10669             }
10670          }
10671          break;
10672       }
10673       case nameSpecifier:
10674       {
10675          Symbol symbol = FindType(curContext, spec.name);
10676          if(symbol)
10677             DeclareType(symbol.type, true, true);
10678          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10679             DeclareStruct(spec.name, false);
10680          break;
10681       }
10682       case enumSpecifier:
10683       {
10684          Enumerator e;
10685          if(spec.list)
10686          {
10687             for(e = spec.list->first; e; e = e.next)
10688             {
10689                if(e.exp)
10690                   ProcessExpressionType(e.exp);
10691             }
10692          }
10693          break;
10694       }
10695       case structSpecifier:
10696       case unionSpecifier:
10697       {
10698          if(spec.definitions)
10699          {
10700             ClassDef def;
10701             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10702             //if(symbol)
10703                ProcessClass(spec.definitions, symbol);
10704             /*else
10705             {
10706                for(def = spec.definitions->first; def; def = def.next)
10707                {
10708                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10709                      ProcessDeclaration(def.decl);
10710                }
10711             }*/
10712          }
10713          break;
10714       }
10715       /*
10716       case classSpecifier:
10717       {
10718          Symbol classSym = FindClass(spec.name);
10719          if(classSym && classSym.registered && classSym.registered.type == structClass)
10720             DeclareStruct(spec.name, false);
10721          break;
10722       }
10723       */
10724    }
10725 }
10726
10727
10728 static void ProcessDeclarator(Declarator decl)
10729 {
10730    switch(decl.type)
10731    {
10732       case identifierDeclarator:
10733          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10734          {
10735             FreeSpecifier(decl.identifier._class);
10736             decl.identifier._class = null;
10737          }
10738          break;
10739       case arrayDeclarator:
10740          if(decl.array.exp)
10741             ProcessExpressionType(decl.array.exp);
10742       case structDeclarator:
10743       case bracketsDeclarator:
10744       case functionDeclarator:
10745       case pointerDeclarator:
10746       case extendedDeclarator:
10747       case extendedDeclaratorEnd:
10748          if(decl.declarator)
10749             ProcessDeclarator(decl.declarator);
10750          if(decl.type == functionDeclarator)
10751          {
10752             Identifier id = GetDeclId(decl);
10753             if(id && id._class)
10754             {
10755                TypeName param
10756                {
10757                   qualifiers = MkListOne(id._class);
10758                   declarator = null;
10759                };
10760                if(!decl.function.parameters)
10761                   decl.function.parameters = MkList();
10762                decl.function.parameters->Insert(null, param);
10763                id._class = null;
10764             }
10765             if(decl.function.parameters)
10766             {
10767                TypeName param;
10768
10769                for(param = decl.function.parameters->first; param; param = param.next)
10770                {
10771                   if(param.qualifiers && param.qualifiers->first)
10772                   {
10773                      Specifier spec = param.qualifiers->first;
10774                      if(spec && spec.specifier == TYPED_OBJECT)
10775                      {
10776                         Declarator d = param.declarator;
10777                         TypeName newParam
10778                         {
10779                            qualifiers = MkListOne(MkSpecifier(VOID));
10780                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10781                         };
10782
10783                         FreeList(param.qualifiers, FreeSpecifier);
10784
10785                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10786                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10787
10788                         decl.function.parameters->Insert(param, newParam);
10789                         param = newParam;
10790                      }
10791                      else if(spec && spec.specifier == ANY_OBJECT)
10792                      {
10793                         Declarator d = param.declarator;
10794
10795                         FreeList(param.qualifiers, FreeSpecifier);
10796
10797                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10798                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10799                      }
10800                      else if(spec.specifier == THISCLASS)
10801                      {
10802                         if(thisClass)
10803                         {
10804                            spec.type = nameSpecifier;
10805                            spec.name = ReplaceThisClass(thisClass);
10806                            spec.symbol = FindClass(spec.name);
10807                            ProcessSpecifier(spec, false);
10808                         }
10809                      }
10810                   }
10811
10812                   if(param.declarator)
10813                      ProcessDeclarator(param.declarator);
10814                }
10815             }
10816          }
10817          break;
10818    }
10819 }
10820
10821 static void ProcessDeclaration(Declaration decl)
10822 {
10823    yylloc = decl.loc;
10824    switch(decl.type)
10825    {
10826       case initDeclaration:
10827       {
10828          bool declareStruct = false;
10829          /*
10830          lineNum = decl.pos.line;
10831          column = decl.pos.col;
10832          */
10833
10834          if(decl.declarators)
10835          {
10836             InitDeclarator d;
10837
10838             for(d = decl.declarators->first; d; d = d.next)
10839             {
10840                Type type, subType;
10841                ProcessDeclarator(d.declarator);
10842
10843                type = ProcessType(decl.specifiers, d.declarator);
10844
10845                if(d.initializer)
10846                {
10847                   ProcessInitializer(d.initializer, type);
10848
10849                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
10850
10851                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10852                      d.initializer.exp.type == instanceExp)
10853                   {
10854                      if(type.kind == classType && type._class ==
10855                         d.initializer.exp.expType._class)
10856                      {
10857                         Instantiation inst = d.initializer.exp.instance;
10858                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10859
10860                         d.initializer.exp.instance = null;
10861                         if(decl.specifiers)
10862                            FreeList(decl.specifiers, FreeSpecifier);
10863                         FreeList(decl.declarators, FreeInitDeclarator);
10864
10865                         d = null;
10866
10867                         decl.type = instDeclaration;
10868                         decl.inst = inst;
10869                      }
10870                   }
10871                }
10872                for(subType = type; subType;)
10873                {
10874                   if(subType.kind == classType)
10875                   {
10876                      declareStruct = true;
10877                      break;
10878                   }
10879                   else if(subType.kind == pointerType)
10880                      break;
10881                   else if(subType.kind == arrayType)
10882                      subType = subType.arrayType;
10883                   else
10884                      break;
10885                }
10886
10887                FreeType(type);
10888                if(!d) break;
10889             }
10890          }
10891
10892          if(decl.specifiers)
10893          {
10894             Specifier s;
10895             for(s = decl.specifiers->first; s; s = s.next)
10896             {
10897                ProcessSpecifier(s, declareStruct);
10898             }
10899          }
10900          break;
10901       }
10902       case instDeclaration:
10903       {
10904          ProcessInstantiationType(decl.inst);
10905          break;
10906       }
10907       case structDeclaration:
10908       {
10909          Specifier spec;
10910          Declarator d;
10911          bool declareStruct = false;
10912
10913          if(decl.declarators)
10914          {
10915             for(d = decl.declarators->first; d; d = d.next)
10916             {
10917                Type type = ProcessType(decl.specifiers, d.declarator);
10918                Type subType;
10919                ProcessDeclarator(d);
10920                for(subType = type; subType;)
10921                {
10922                   if(subType.kind == classType)
10923                   {
10924                      declareStruct = true;
10925                      break;
10926                   }
10927                   else if(subType.kind == pointerType)
10928                      break;
10929                   else if(subType.kind == arrayType)
10930                      subType = subType.arrayType;
10931                   else
10932                      break;
10933                }
10934                FreeType(type);
10935             }
10936          }
10937          if(decl.specifiers)
10938          {
10939             for(spec = decl.specifiers->first; spec; spec = spec.next)
10940                ProcessSpecifier(spec, declareStruct);
10941          }
10942          break;
10943       }
10944    }
10945 }
10946
10947 static FunctionDefinition curFunction;
10948
10949 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10950 {
10951    char propName[1024], propNameM[1024];
10952    char getName[1024], setName[1024];
10953    OldList * args;
10954
10955    DeclareProperty(prop, setName, getName);
10956
10957    // eInstance_FireWatchers(object, prop);
10958    strcpy(propName, "__ecereProp_");
10959    FullClassNameCat(propName, prop._class.fullName, false);
10960    strcat(propName, "_");
10961    // strcat(propName, prop.name);
10962    FullClassNameCat(propName, prop.name, true);
10963    MangleClassName(propName);
10964
10965    strcpy(propNameM, "__ecerePropM_");
10966    FullClassNameCat(propNameM, prop._class.fullName, false);
10967    strcat(propNameM, "_");
10968    // strcat(propNameM, prop.name);
10969    FullClassNameCat(propNameM, prop.name, true);
10970    MangleClassName(propNameM);
10971
10972    if(prop.isWatchable)
10973    {
10974       args = MkList();
10975       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10976       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10977       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10978
10979       args = MkList();
10980       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10981       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10982       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10983    }
10984
10985
10986    {
10987       args = MkList();
10988       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10989       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10990       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10991
10992       args = MkList();
10993       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10994       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10995       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10996    }
10997
10998    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
10999       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11000       curFunction.propSet.fireWatchersDone = true;
11001 }
11002
11003 static void ProcessStatement(Statement stmt)
11004 {
11005    yylloc = stmt.loc;
11006    /*
11007    lineNum = stmt.pos.line;
11008    column = stmt.pos.col;
11009    */
11010    switch(stmt.type)
11011    {
11012       case labeledStmt:
11013          ProcessStatement(stmt.labeled.stmt);
11014          break;
11015       case caseStmt:
11016          // This expression should be constant...
11017          if(stmt.caseStmt.exp)
11018          {
11019             FreeType(stmt.caseStmt.exp.destType);
11020             stmt.caseStmt.exp.destType = curSwitchType;
11021             if(curSwitchType) curSwitchType.refCount++;
11022             ProcessExpressionType(stmt.caseStmt.exp);
11023             ComputeExpression(stmt.caseStmt.exp);
11024          }
11025          if(stmt.caseStmt.stmt)
11026             ProcessStatement(stmt.caseStmt.stmt);
11027          break;
11028       case compoundStmt:
11029       {
11030          if(stmt.compound.context)
11031          {
11032             Declaration decl;
11033             Statement s;
11034
11035             Statement prevCompound = curCompound;
11036             Context prevContext = curContext;
11037
11038             if(!stmt.compound.isSwitch)
11039                curCompound = stmt;
11040             curContext = stmt.compound.context;
11041
11042             if(stmt.compound.declarations)
11043             {
11044                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11045                   ProcessDeclaration(decl);
11046             }
11047             if(stmt.compound.statements)
11048             {
11049                for(s = stmt.compound.statements->first; s; s = s.next)
11050                   ProcessStatement(s);
11051             }
11052
11053             curContext = prevContext;
11054             curCompound = prevCompound;
11055          }
11056          break;
11057       }
11058       case expressionStmt:
11059       {
11060          Expression exp;
11061          if(stmt.expressions)
11062          {
11063             for(exp = stmt.expressions->first; exp; exp = exp.next)
11064                ProcessExpressionType(exp);
11065          }
11066          break;
11067       }
11068       case ifStmt:
11069       {
11070          Expression exp;
11071
11072          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11073          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11074          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11075          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11076          {
11077             ProcessExpressionType(exp);
11078          }
11079          if(stmt.ifStmt.stmt)
11080             ProcessStatement(stmt.ifStmt.stmt);
11081          if(stmt.ifStmt.elseStmt)
11082             ProcessStatement(stmt.ifStmt.elseStmt);
11083          break;
11084       }
11085       case switchStmt:
11086       {
11087          Type oldSwitchType = curSwitchType;
11088          if(stmt.switchStmt.exp)
11089          {
11090             Expression exp;
11091             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11092             {
11093                if(!exp.next)
11094                {
11095                   /*
11096                   Type destType
11097                   {
11098                      kind = intType;
11099                      refCount = 1;
11100                   };
11101                   e.exp.destType = destType;
11102                   */
11103
11104                   ProcessExpressionType(exp);
11105                }
11106                if(!exp.next)
11107                   curSwitchType = exp.expType;
11108             }
11109          }
11110          ProcessStatement(stmt.switchStmt.stmt);
11111          curSwitchType = oldSwitchType;
11112          break;
11113       }
11114       case whileStmt:
11115       {
11116          if(stmt.whileStmt.exp)
11117          {
11118             Expression exp;
11119
11120             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11121             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11122             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11123             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11124             {
11125                ProcessExpressionType(exp);
11126             }
11127          }
11128          if(stmt.whileStmt.stmt)
11129             ProcessStatement(stmt.whileStmt.stmt);
11130          break;
11131       }
11132       case doWhileStmt:
11133       {
11134          if(stmt.doWhile.exp)
11135          {
11136             Expression exp;
11137
11138             if(stmt.doWhile.exp->last)
11139             {
11140                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11141                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11142                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11143             }
11144             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11145             {
11146                ProcessExpressionType(exp);
11147             }
11148          }
11149          if(stmt.doWhile.stmt)
11150             ProcessStatement(stmt.doWhile.stmt);
11151          break;
11152       }
11153       case forStmt:
11154       {
11155          Expression exp;
11156          if(stmt.forStmt.init)
11157             ProcessStatement(stmt.forStmt.init);
11158
11159          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11160          {
11161             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11162             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11163             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11164          }
11165
11166          if(stmt.forStmt.check)
11167             ProcessStatement(stmt.forStmt.check);
11168          if(stmt.forStmt.increment)
11169          {
11170             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11171                ProcessExpressionType(exp);
11172          }
11173
11174          if(stmt.forStmt.stmt)
11175             ProcessStatement(stmt.forStmt.stmt);
11176          break;
11177       }
11178       case forEachStmt:
11179       {
11180          Identifier id = stmt.forEachStmt.id;
11181          OldList * exp = stmt.forEachStmt.exp;
11182          OldList * filter = stmt.forEachStmt.filter;
11183          Statement block = stmt.forEachStmt.stmt;
11184          char iteratorType[1024];
11185          Type source;
11186          Expression e;
11187          bool isBuiltin = exp && exp->last &&
11188             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11189               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11190          Expression arrayExp;
11191          char * typeString = null;
11192          int builtinCount = 0;
11193
11194          for(e = exp ? exp->first : null; e; e = e.next)
11195          {
11196             if(!e.next)
11197             {
11198                FreeType(e.destType);
11199                e.destType = ProcessTypeString("Container", false);
11200             }
11201             if(!isBuiltin || e.next)
11202                ProcessExpressionType(e);
11203          }
11204
11205          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11206          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11207             eClass_IsDerived(source._class.registered, containerClass)))
11208          {
11209             Class _class = source ? source._class.registered : null;
11210             Symbol symbol;
11211             Expression expIt = null;
11212             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11213             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11214             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11215             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11216             stmt.type = compoundStmt;
11217
11218             stmt.compound.context = Context { };
11219             stmt.compound.context.parent = curContext;
11220             curContext = stmt.compound.context;
11221
11222             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11223             {
11224                Class mapClass = eSystem_FindClass(privateModule, "Map");
11225                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11226                isCustomAVLTree = true;
11227                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11228                   isAVLTree = true;
11229                else if(eClass_IsDerived(source._class.registered, mapClass))
11230                   isMap = true;
11231             }
11232             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11233             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11234             {
11235                Class listClass = eSystem_FindClass(privateModule, "List");
11236                isLinkList = true;
11237                isList = eClass_IsDerived(source._class.registered, listClass);
11238             }
11239
11240             if(isArray)
11241             {
11242                Declarator decl;
11243                OldList * specs = MkList();
11244                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11245                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11246                stmt.compound.declarations = MkListOne(
11247                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11248                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11249                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11250                      MkInitializerAssignment(MkExpBrackets(exp))))));
11251             }
11252             else if(isBuiltin)
11253             {
11254                Type type = null;
11255                char typeStringBuf[1024];
11256
11257                // TODO: Merge this code?
11258                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11259                if(((Expression)exp->last).type == castExp)
11260                {
11261                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11262                   if(typeName)
11263                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11264                }
11265
11266                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11267                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11268                   arrayExp.destType._class.registered.templateArgs)
11269                {
11270                   Class templateClass = arrayExp.destType._class.registered;
11271                   typeString = templateClass.templateArgs[2].dataTypeString;
11272                }
11273                else if(arrayExp.list)
11274                {
11275                   // Guess type from expressions in the array
11276                   Expression e;
11277                   for(e = arrayExp.list->first; e; e = e.next)
11278                   {
11279                      ProcessExpressionType(e);
11280                      if(e.expType)
11281                      {
11282                         if(!type) { type = e.expType; type.refCount++; }
11283                         else
11284                         {
11285                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11286                            if(!MatchTypeExpression(e, type, null, false))
11287                            {
11288                               FreeType(type);
11289                               type = e.expType;
11290                               e.expType = null;
11291
11292                               e = arrayExp.list->first;
11293                               ProcessExpressionType(e);
11294                               if(e.expType)
11295                               {
11296                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11297                                  if(!MatchTypeExpression(e, type, null, false))
11298                                  {
11299                                     FreeType(e.expType);
11300                                     e.expType = null;
11301                                     FreeType(type);
11302                                     type = null;
11303                                     break;
11304                                  }
11305                               }
11306                            }
11307                         }
11308                         if(e.expType)
11309                         {
11310                            FreeType(e.expType);
11311                            e.expType = null;
11312                         }
11313                      }
11314                   }
11315                   if(type)
11316                   {
11317                      typeStringBuf[0] = '\0';
11318                      PrintType(type, typeStringBuf, false, true);
11319                      typeString = typeStringBuf;
11320                      FreeType(type);
11321                   }
11322                }
11323                if(typeString)
11324                {
11325                   OldList * initializers = MkList();
11326                   Declarator decl;
11327                   OldList * specs = MkList();
11328                   if(arrayExp.list)
11329                   {
11330                      Expression e;
11331
11332                      builtinCount = arrayExp.list->count;
11333                      type = ProcessTypeString(typeString, false);
11334                      while(e = arrayExp.list->first)
11335                      {
11336                         arrayExp.list->Remove(e);
11337                         e.destType = type;
11338                         type.refCount++;
11339                         ProcessExpressionType(e);
11340                         ListAdd(initializers, MkInitializerAssignment(e));
11341                      }
11342                      FreeType(type);
11343                      delete arrayExp.list;
11344                   }
11345                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11346                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11347                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11348
11349                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11350                      PlugDeclarator(
11351                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11352                         ), MkInitializerList(initializers)))));
11353                   FreeList(exp, FreeExpression);
11354                }
11355                else
11356                {
11357                   arrayExp.expType = ProcessTypeString("Container", false);
11358                   Compiler_Error($"Couldn't determine type of array elements\n");
11359                }
11360
11361                /*
11362                Declarator decl;
11363                OldList * specs = MkList();
11364
11365                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11366                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11367                stmt.compound.declarations = MkListOne(
11368                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11369                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11370                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11371                      MkInitializerAssignment(MkExpBrackets(exp))))));
11372                */
11373             }
11374             else if(isLinkList && !isList)
11375             {
11376                Declarator decl;
11377                OldList * specs = MkList();
11378                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11379                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11380                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11381                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11382                      MkInitializerAssignment(MkExpBrackets(exp))))));
11383             }
11384             /*else if(isCustomAVLTree)
11385             {
11386                Declarator decl;
11387                OldList * specs = MkList();
11388                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11389                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11390                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11391                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11392                      MkInitializerAssignment(MkExpBrackets(exp))))));
11393             }*/
11394             else if(_class.templateArgs)
11395             {
11396                if(isMap)
11397                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11398                else
11399                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11400
11401                stmt.compound.declarations = MkListOne(
11402                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11403                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11404                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11405             }
11406             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11407
11408             if(block)
11409             {
11410                // Reparent sub-contexts in this statement
11411                switch(block.type)
11412                {
11413                   case compoundStmt:
11414                      if(block.compound.context)
11415                         block.compound.context.parent = stmt.compound.context;
11416                      break;
11417                   case ifStmt:
11418                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11419                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11420                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11421                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11422                      break;
11423                   case switchStmt:
11424                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11425                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11426                      break;
11427                   case whileStmt:
11428                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11429                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11430                      break;
11431                   case doWhileStmt:
11432                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11433                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11434                      break;
11435                   case forStmt:
11436                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11437                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11438                      break;
11439                   case forEachStmt:
11440                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11441                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11442                      break;
11443                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11444                   case labeledStmt:
11445                   case caseStmt
11446                   case expressionStmt:
11447                   case gotoStmt:
11448                   case continueStmt:
11449                   case breakStmt
11450                   case returnStmt:
11451                   case asmStmt:
11452                   case badDeclarationStmt:
11453                   case fireWatchersStmt:
11454                   case stopWatchingStmt:
11455                   case watchStmt:
11456                   */
11457                }
11458             }
11459             if(filter)
11460             {
11461                block = MkIfStmt(filter, block, null);
11462             }
11463             if(isArray)
11464             {
11465                stmt.compound.statements = MkListOne(MkForStmt(
11466                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11467                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11468                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11469                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11470                   block));
11471               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11472               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11473               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11474             }
11475             else if(isBuiltin)
11476             {
11477                char count[128];
11478                //OldList * specs = MkList();
11479                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11480
11481                sprintf(count, "%d", builtinCount);
11482
11483                stmt.compound.statements = MkListOne(MkForStmt(
11484                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11485                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11486                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11487                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11488                   block));
11489
11490                /*
11491                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11492                stmt.compound.statements = MkListOne(MkForStmt(
11493                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11494                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11495                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11496                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11497                   block));
11498               */
11499               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11500               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11501               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11502             }
11503             else if(isLinkList && !isList)
11504             {
11505                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11506                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11507                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11508                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11509                {
11510                   stmt.compound.statements = MkListOne(MkForStmt(
11511                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11512                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11513                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11514                      block));
11515                }
11516                else
11517                {
11518                   OldList * specs = MkList();
11519                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11520                   stmt.compound.statements = MkListOne(MkForStmt(
11521                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11522                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11523                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11524                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11525                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11526                      block));
11527                }
11528                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11529                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11530                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11531             }
11532             /*else if(isCustomAVLTree)
11533             {
11534                stmt.compound.statements = MkListOne(MkForStmt(
11535                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11536                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11537                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11538                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11539                   block));
11540
11541                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11542                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11543                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11544             }*/
11545             else
11546             {
11547                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11548                   MkIdentifier("Next")), null)), block));
11549             }
11550             ProcessExpressionType(expIt);
11551             if(stmt.compound.declarations->first)
11552                ProcessDeclaration(stmt.compound.declarations->first);
11553
11554             if(symbol)
11555                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11556
11557             ProcessStatement(stmt);
11558             curContext = stmt.compound.context.parent;
11559             break;
11560          }
11561          else
11562          {
11563             Compiler_Error($"Expression is not a container\n");
11564          }
11565          break;
11566       }
11567       case gotoStmt:
11568          break;
11569       case continueStmt:
11570          break;
11571       case breakStmt:
11572          break;
11573       case returnStmt:
11574       {
11575          Expression exp;
11576          if(stmt.expressions)
11577          {
11578             for(exp = stmt.expressions->first; exp; exp = exp.next)
11579             {
11580                if(!exp.next)
11581                {
11582                   if(curFunction && !curFunction.type)
11583                      curFunction.type = ProcessType(
11584                         curFunction.specifiers, curFunction.declarator);
11585                   FreeType(exp.destType);
11586                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11587                   if(exp.destType) exp.destType.refCount++;
11588                }
11589                ProcessExpressionType(exp);
11590             }
11591          }
11592          break;
11593       }
11594       case badDeclarationStmt:
11595       {
11596          ProcessDeclaration(stmt.decl);
11597          break;
11598       }
11599       case asmStmt:
11600       {
11601          AsmField field;
11602          if(stmt.asmStmt.inputFields)
11603          {
11604             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11605                if(field.expression)
11606                   ProcessExpressionType(field.expression);
11607          }
11608          if(stmt.asmStmt.outputFields)
11609          {
11610             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11611                if(field.expression)
11612                   ProcessExpressionType(field.expression);
11613          }
11614          if(stmt.asmStmt.clobberedFields)
11615          {
11616             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11617             {
11618                if(field.expression)
11619                   ProcessExpressionType(field.expression);
11620             }
11621          }
11622          break;
11623       }
11624       case watchStmt:
11625       {
11626          PropertyWatch propWatch;
11627          OldList * watches = stmt._watch.watches;
11628          Expression object = stmt._watch.object;
11629          Expression watcher = stmt._watch.watcher;
11630          if(watcher)
11631             ProcessExpressionType(watcher);
11632          if(object)
11633             ProcessExpressionType(object);
11634
11635          if(inCompiler)
11636          {
11637             if(watcher || thisClass)
11638             {
11639                External external = curExternal;
11640                Context context = curContext;
11641
11642                stmt.type = expressionStmt;
11643                stmt.expressions = MkList();
11644
11645                curExternal = external.prev;
11646
11647                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11648                {
11649                   ClassFunction func;
11650                   char watcherName[1024];
11651                   Class watcherClass = watcher ?
11652                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11653                   External createdExternal;
11654
11655                   // Create a declaration above
11656                   External externalDecl = MkExternalDeclaration(null);
11657                   ast->Insert(curExternal.prev, externalDecl);
11658
11659                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11660                   if(propWatch.deleteWatch)
11661                      strcat(watcherName, "_delete");
11662                   else
11663                   {
11664                      Identifier propID;
11665                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11666                      {
11667                         strcat(watcherName, "_");
11668                         strcat(watcherName, propID.string);
11669                      }
11670                   }
11671
11672                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11673                   {
11674                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11675                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11676                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11677                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11678                      ProcessClassFunctionBody(func, propWatch.compound);
11679                      propWatch.compound = null;
11680
11681                      //afterExternal = afterExternal ? afterExternal : curExternal;
11682
11683                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11684                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11685                      // TESTING THIS...
11686                      createdExternal.symbol.idCode = external.symbol.idCode;
11687
11688                      curExternal = createdExternal;
11689                      ProcessFunction(createdExternal.function);
11690
11691
11692                      // Create a declaration above
11693                      {
11694                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11695                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11696                         externalDecl.declaration = decl;
11697                         if(decl.symbol && !decl.symbol.pointerExternal)
11698                            decl.symbol.pointerExternal = externalDecl;
11699                      }
11700
11701                      if(propWatch.deleteWatch)
11702                      {
11703                         OldList * args = MkList();
11704                         ListAdd(args, CopyExpression(object));
11705                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11706                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11707                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11708                      }
11709                      else
11710                      {
11711                         Class _class = object.expType._class.registered;
11712                         Identifier propID;
11713
11714                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11715                         {
11716                            char propName[1024];
11717                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11718                            if(prop)
11719                            {
11720                               char getName[1024], setName[1024];
11721                               OldList * args = MkList();
11722
11723                               DeclareProperty(prop, setName, getName);
11724
11725                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11726                               strcpy(propName, "__ecereProp_");
11727                               FullClassNameCat(propName, prop._class.fullName, false);
11728                               strcat(propName, "_");
11729                               // strcat(propName, prop.name);
11730                               FullClassNameCat(propName, prop.name, true);
11731
11732                               ListAdd(args, CopyExpression(object));
11733                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11734                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11735                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11736
11737                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11738                            }
11739                            else
11740                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11741                         }
11742                      }
11743                   }
11744                   else
11745                      Compiler_Error($"Invalid watched object\n");
11746                }
11747
11748                curExternal = external;
11749                curContext = context;
11750
11751                if(watcher)
11752                   FreeExpression(watcher);
11753                if(object)
11754                   FreeExpression(object);
11755                FreeList(watches, FreePropertyWatch);
11756             }
11757             else
11758                Compiler_Error($"No observer specified and not inside a _class\n");
11759          }
11760          else
11761          {
11762             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11763             {
11764                ProcessStatement(propWatch.compound);
11765             }
11766
11767          }
11768          break;
11769       }
11770       case fireWatchersStmt:
11771       {
11772          OldList * watches = stmt._watch.watches;
11773          Expression object = stmt._watch.object;
11774          Class _class;
11775          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11776          // printf("%X\n", watches);
11777          // printf("%X\n", stmt._watch.watches);
11778          if(object)
11779             ProcessExpressionType(object);
11780
11781          if(inCompiler)
11782          {
11783             _class = object ?
11784                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11785
11786             if(_class)
11787             {
11788                Identifier propID;
11789
11790                stmt.type = expressionStmt;
11791                stmt.expressions = MkList();
11792
11793                // Check if we're inside a property set
11794                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11795                {
11796                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11797                }
11798                else if(!watches)
11799                {
11800                   //Compiler_Error($"No property specified and not inside a property set\n");
11801                }
11802                if(watches)
11803                {
11804                   for(propID = watches->first; propID; propID = propID.next)
11805                   {
11806                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11807                      if(prop)
11808                      {
11809                         CreateFireWatcher(prop, object, stmt);
11810                      }
11811                      else
11812                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11813                   }
11814                }
11815                else
11816                {
11817                   // Fire all properties!
11818                   Property prop;
11819                   Class base;
11820                   for(base = _class; base; base = base.base)
11821                   {
11822                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11823                      {
11824                         if(prop.isProperty && prop.isWatchable)
11825                         {
11826                            CreateFireWatcher(prop, object, stmt);
11827                         }
11828                      }
11829                   }
11830                }
11831
11832                if(object)
11833                   FreeExpression(object);
11834                FreeList(watches, FreeIdentifier);
11835             }
11836             else
11837                Compiler_Error($"Invalid object specified and not inside a class\n");
11838          }
11839          break;
11840       }
11841       case stopWatchingStmt:
11842       {
11843          OldList * watches = stmt._watch.watches;
11844          Expression object = stmt._watch.object;
11845          Expression watcher = stmt._watch.watcher;
11846          Class _class;
11847          if(object)
11848             ProcessExpressionType(object);
11849          if(watcher)
11850             ProcessExpressionType(watcher);
11851          if(inCompiler)
11852          {
11853             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11854
11855             if(watcher || thisClass)
11856             {
11857                if(_class)
11858                {
11859                   Identifier propID;
11860
11861                   stmt.type = expressionStmt;
11862                   stmt.expressions = MkList();
11863
11864                   if(!watches)
11865                   {
11866                      OldList * args;
11867                      // eInstance_StopWatching(object, null, watcher);
11868                      args = MkList();
11869                      ListAdd(args, CopyExpression(object));
11870                      ListAdd(args, MkExpConstant("0"));
11871                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11872                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11873                   }
11874                   else
11875                   {
11876                      for(propID = watches->first; propID; propID = propID.next)
11877                      {
11878                         char propName[1024];
11879                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11880                         if(prop)
11881                         {
11882                            char getName[1024], setName[1024];
11883                            OldList * args = MkList();
11884
11885                            DeclareProperty(prop, setName, getName);
11886
11887                            // eInstance_StopWatching(object, prop, watcher);
11888                            strcpy(propName, "__ecereProp_");
11889                            FullClassNameCat(propName, prop._class.fullName, false);
11890                            strcat(propName, "_");
11891                            // strcat(propName, prop.name);
11892                            FullClassNameCat(propName, prop.name, true);
11893                            MangleClassName(propName);
11894
11895                            ListAdd(args, CopyExpression(object));
11896                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11897                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11898                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11899                         }
11900                         else
11901                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11902                      }
11903                   }
11904
11905                   if(object)
11906                      FreeExpression(object);
11907                   if(watcher)
11908                      FreeExpression(watcher);
11909                   FreeList(watches, FreeIdentifier);
11910                }
11911                else
11912                   Compiler_Error($"Invalid object specified and not inside a class\n");
11913             }
11914             else
11915                Compiler_Error($"No observer specified and not inside a class\n");
11916          }
11917          break;
11918       }
11919    }
11920 }
11921
11922 static void ProcessFunction(FunctionDefinition function)
11923 {
11924    Identifier id = GetDeclId(function.declarator);
11925    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11926    Type type = symbol ? symbol.type : null;
11927    Class oldThisClass = thisClass;
11928    Context oldTopContext = topContext;
11929
11930    yylloc = function.loc;
11931    // Process thisClass
11932
11933    if(type && type.thisClass)
11934    {
11935       Symbol classSym = type.thisClass;
11936       Class _class = type.thisClass.registered;
11937       char className[1024];
11938       char structName[1024];
11939       Declarator funcDecl;
11940       Symbol thisSymbol;
11941
11942       bool typedObject = false;
11943
11944       if(_class && !_class.base)
11945       {
11946          _class = currentClass;
11947          if(_class && !_class.symbol)
11948             _class.symbol = FindClass(_class.fullName);
11949          classSym = _class ? _class.symbol : null;
11950          typedObject = true;
11951       }
11952
11953       thisClass = _class;
11954
11955       if(inCompiler && _class)
11956       {
11957          if(type.kind == functionType)
11958          {
11959             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11960             {
11961                //TypeName param = symbol.type.params.first;
11962                Type param = symbol.type.params.first;
11963                symbol.type.params.Remove(param);
11964                //FreeTypeName(param);
11965                FreeType(param);
11966             }
11967             if(type.classObjectType != classPointer)
11968             {
11969                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11970                symbol.type.staticMethod = true;
11971                symbol.type.thisClass = null;
11972
11973                // HIGH DANGER: VERIFYING THIS...
11974                symbol.type.extraParam = false;
11975             }
11976          }
11977
11978          strcpy(className, "__ecereClass_");
11979          FullClassNameCat(className, _class.fullName, true);
11980
11981          MangleClassName(className);
11982
11983          structName[0] = 0;
11984          FullClassNameCat(structName, _class.fullName, false);
11985
11986          // [class] this
11987
11988
11989          funcDecl = GetFuncDecl(function.declarator);
11990          if(funcDecl)
11991          {
11992             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11993             {
11994                TypeName param = funcDecl.function.parameters->first;
11995                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11996                {
11997                   funcDecl.function.parameters->Remove(param);
11998                   FreeTypeName(param);
11999                }
12000             }
12001
12002             // DANGER: Watch for this... Check if it's a Conversion?
12003             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12004
12005             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12006             if(!function.propertyNoThis)
12007             {
12008                TypeName thisParam;
12009
12010                if(type.classObjectType != classPointer)
12011                {
12012                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12013                   if(!funcDecl.function.parameters)
12014                      funcDecl.function.parameters = MkList();
12015                   funcDecl.function.parameters->Insert(null, thisParam);
12016                }
12017
12018                if(typedObject)
12019                {
12020                   if(type.classObjectType != classPointer)
12021                   {
12022                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12023                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12024                   }
12025
12026                   thisParam = TypeName
12027                   {
12028                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12029                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12030                   };
12031                   funcDecl.function.parameters->Insert(null, thisParam);
12032                }
12033             }
12034          }
12035
12036          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12037          {
12038             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12039             funcDecl = GetFuncDecl(initDecl.declarator);
12040             if(funcDecl)
12041             {
12042                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12043                {
12044                   TypeName param = funcDecl.function.parameters->first;
12045                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12046                   {
12047                      funcDecl.function.parameters->Remove(param);
12048                      FreeTypeName(param);
12049                   }
12050                }
12051
12052                if(type.classObjectType != classPointer)
12053                {
12054                   // DANGER: Watch for this... Check if it's a Conversion?
12055                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12056                   {
12057                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12058
12059                      if(!funcDecl.function.parameters)
12060                         funcDecl.function.parameters = MkList();
12061                      funcDecl.function.parameters->Insert(null, thisParam);
12062                   }
12063                }
12064             }
12065          }
12066       }
12067
12068       // Add this to the context
12069       if(function.body)
12070       {
12071          if(type.classObjectType != classPointer)
12072          {
12073             thisSymbol = Symbol
12074             {
12075                string = CopyString("this");
12076                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12077             };
12078             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12079
12080             if(typedObject && thisSymbol.type)
12081             {
12082                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12083                thisSymbol.type.byReference = type.byReference;
12084                thisSymbol.type.typedByReference = type.byReference;
12085                /*
12086                thisSymbol = Symbol { string = CopyString("class") };
12087                function.body.compound.context.symbols.Add(thisSymbol);
12088                */
12089             }
12090          }
12091       }
12092
12093       // Pointer to class data
12094
12095       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12096       {
12097          DataMember member = null;
12098          {
12099             Class base;
12100             for(base = _class; base && base.type != systemClass; base = base.next)
12101             {
12102                for(member = base.membersAndProperties.first; member; member = member.next)
12103                   if(!member.isProperty)
12104                      break;
12105                if(member)
12106                   break;
12107             }
12108          }
12109          for(member = _class.membersAndProperties.first; member; member = member.next)
12110             if(!member.isProperty)
12111                break;
12112          if(member)
12113          {
12114             char pointerName[1024];
12115
12116             Declaration decl;
12117             Initializer initializer;
12118             Expression exp, bytePtr;
12119
12120             strcpy(pointerName, "__ecerePointer_");
12121             FullClassNameCat(pointerName, _class.fullName, false);
12122             {
12123                char className[1024];
12124                strcpy(className, "__ecereClass_");
12125                FullClassNameCat(className, classSym.string, true);
12126                MangleClassName(className);
12127
12128                // Testing This
12129                DeclareClass(classSym, className);
12130             }
12131
12132             // ((byte *) this)
12133             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12134
12135             if(_class.fixed)
12136             {
12137                char string[256];
12138                sprintf(string, "%d", _class.offset);
12139                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12140             }
12141             else
12142             {
12143                // ([bytePtr] + [className]->offset)
12144                exp = QBrackets(MkExpOp(bytePtr, '+',
12145                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12146             }
12147
12148             // (this ? [exp] : 0)
12149             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12150             exp.expType = Type
12151             {
12152                refCount = 1;
12153                kind = pointerType;
12154                type = Type { refCount = 1, kind = voidType };
12155             };
12156
12157             if(function.body)
12158             {
12159                yylloc = function.body.loc;
12160                // ([structName] *) [exp]
12161                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12162                initializer = MkInitializerAssignment(
12163                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12164
12165                // [structName] * [pointerName] = [initializer];
12166                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12167
12168                {
12169                   Context prevContext = curContext;
12170                   curContext = function.body.compound.context;
12171
12172                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12173                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12174
12175                   curContext = prevContext;
12176                }
12177
12178                // WHY?
12179                decl.symbol = null;
12180
12181                if(!function.body.compound.declarations)
12182                   function.body.compound.declarations = MkList();
12183                function.body.compound.declarations->Insert(null, decl);
12184             }
12185          }
12186       }
12187
12188
12189       // Loop through the function and replace undeclared identifiers
12190       // which are a member of the class (methods, properties or data)
12191       // by "this.[member]"
12192    }
12193    else
12194       thisClass = null;
12195
12196    if(id)
12197    {
12198       FreeSpecifier(id._class);
12199       id._class = null;
12200
12201       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12202       {
12203          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12204          id = GetDeclId(initDecl.declarator);
12205
12206          FreeSpecifier(id._class);
12207          id._class = null;
12208       }
12209    }
12210    if(function.body)
12211       topContext = function.body.compound.context;
12212    {
12213       FunctionDefinition oldFunction = curFunction;
12214       curFunction = function;
12215       if(function.body)
12216          ProcessStatement(function.body);
12217
12218       // If this is a property set and no firewatchers has been done yet, add one here
12219       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12220       {
12221          Statement prevCompound = curCompound;
12222          Context prevContext = curContext;
12223
12224          Statement fireWatchers = MkFireWatchersStmt(null, null);
12225          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12226          ListAdd(function.body.compound.statements, fireWatchers);
12227
12228          curCompound = function.body;
12229          curContext = function.body.compound.context;
12230
12231          ProcessStatement(fireWatchers);
12232
12233          curContext = prevContext;
12234          curCompound = prevCompound;
12235
12236       }
12237
12238       curFunction = oldFunction;
12239    }
12240
12241    if(function.declarator)
12242    {
12243       ProcessDeclarator(function.declarator);
12244    }
12245
12246    topContext = oldTopContext;
12247    thisClass = oldThisClass;
12248 }
12249
12250 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12251 static void ProcessClass(OldList definitions, Symbol symbol)
12252 {
12253    ClassDef def;
12254    External external = curExternal;
12255    Class regClass = symbol ? symbol.registered : null;
12256
12257    // Process all functions
12258    for(def = definitions.first; def; def = def.next)
12259    {
12260       if(def.type == functionClassDef)
12261       {
12262          if(def.function.declarator)
12263             curExternal = def.function.declarator.symbol.pointerExternal;
12264          else
12265             curExternal = external;
12266
12267          ProcessFunction((FunctionDefinition)def.function);
12268       }
12269       else if(def.type == declarationClassDef)
12270       {
12271          if(def.decl.type == instDeclaration)
12272          {
12273             thisClass = regClass;
12274             ProcessInstantiationType(def.decl.inst);
12275             thisClass = null;
12276          }
12277          // Testing this
12278          else
12279          {
12280             Class backThisClass = thisClass;
12281             if(regClass) thisClass = regClass;
12282             ProcessDeclaration(def.decl);
12283             thisClass = backThisClass;
12284          }
12285       }
12286       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12287       {
12288          MemberInit defProperty;
12289
12290          // Add this to the context
12291          Symbol thisSymbol = Symbol
12292          {
12293             string = CopyString("this");
12294             type = regClass ? MkClassType(regClass.fullName) : null;
12295          };
12296          globalContext.symbols.Add((BTNode)thisSymbol);
12297
12298          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12299          {
12300             thisClass = regClass;
12301             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12302             thisClass = null;
12303          }
12304
12305          globalContext.symbols.Remove((BTNode)thisSymbol);
12306          FreeSymbol(thisSymbol);
12307       }
12308       else if(def.type == propertyClassDef && def.propertyDef)
12309       {
12310          PropertyDef prop = def.propertyDef;
12311
12312          // Add this to the context
12313          /*
12314          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12315          globalContext.symbols.Add(thisSymbol);
12316          */
12317
12318          thisClass = regClass;
12319          if(prop.setStmt)
12320          {
12321             if(regClass)
12322             {
12323                Symbol thisSymbol
12324                {
12325                   string = CopyString("this");
12326                   type = MkClassType(regClass.fullName);
12327                };
12328                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12329             }
12330
12331             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12332             ProcessStatement(prop.setStmt);
12333          }
12334          if(prop.getStmt)
12335          {
12336             if(regClass)
12337             {
12338                Symbol thisSymbol
12339                {
12340                   string = CopyString("this");
12341                   type = MkClassType(regClass.fullName);
12342                };
12343                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12344             }
12345
12346             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12347             ProcessStatement(prop.getStmt);
12348          }
12349          if(prop.issetStmt)
12350          {
12351             if(regClass)
12352             {
12353                Symbol thisSymbol
12354                {
12355                   string = CopyString("this");
12356                   type = MkClassType(regClass.fullName);
12357                };
12358                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12359             }
12360
12361             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12362             ProcessStatement(prop.issetStmt);
12363          }
12364
12365          thisClass = null;
12366
12367          /*
12368          globalContext.symbols.Remove(thisSymbol);
12369          FreeSymbol(thisSymbol);
12370          */
12371       }
12372       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12373       {
12374          PropertyWatch propertyWatch = def.propertyWatch;
12375
12376          thisClass = regClass;
12377          if(propertyWatch.compound)
12378          {
12379             Symbol thisSymbol
12380             {
12381                string = CopyString("this");
12382                type = regClass ? MkClassType(regClass.fullName) : null;
12383             };
12384
12385             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12386
12387             curExternal = null;
12388             ProcessStatement(propertyWatch.compound);
12389          }
12390          thisClass = null;
12391       }
12392    }
12393 }
12394
12395 void DeclareFunctionUtil(String s)
12396 {
12397    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12398    if(function)
12399    {
12400       char name[1024];
12401       name[0] = 0;
12402       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12403          strcpy(name, "__ecereFunction_");
12404       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12405       DeclareFunction(function, name);
12406    }
12407 }
12408
12409 void ComputeDataTypes()
12410 {
12411    External external;
12412    External temp { };
12413    External after = null;
12414
12415    currentClass = null;
12416
12417    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12418
12419    for(external = ast->first; external; external = external.next)
12420    {
12421       if(external.type == declarationExternal)
12422       {
12423          Declaration decl = external.declaration;
12424          if(decl)
12425          {
12426             OldList * decls = decl.declarators;
12427             if(decls)
12428             {
12429                InitDeclarator initDecl = decls->first;
12430                if(initDecl)
12431                {
12432                   Declarator declarator = initDecl.declarator;
12433                   if(declarator && declarator.type == identifierDeclarator)
12434                   {
12435                      Identifier id = declarator.identifier;
12436                      if(id && id.string)
12437                      {
12438                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12439                         {
12440                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12441                            after = external;
12442                         }
12443                      }
12444                   }
12445                }
12446             }
12447          }
12448        }
12449    }
12450
12451    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12452    ast->Insert(after, temp);
12453    curExternal = temp;
12454
12455    DeclareFunctionUtil("eSystem_New");
12456    DeclareFunctionUtil("eSystem_New0");
12457    DeclareFunctionUtil("eSystem_Renew");
12458    DeclareFunctionUtil("eSystem_Renew0");
12459    DeclareFunctionUtil("eSystem_Delete");
12460    DeclareFunctionUtil("eClass_GetProperty");
12461    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12462
12463    DeclareStruct("ecere::com::Class", false);
12464    DeclareStruct("ecere::com::Instance", false);
12465    DeclareStruct("ecere::com::Property", false);
12466    DeclareStruct("ecere::com::DataMember", false);
12467    DeclareStruct("ecere::com::Method", false);
12468    DeclareStruct("ecere::com::SerialBuffer", false);
12469    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12470
12471    ast->Remove(temp);
12472
12473    for(external = ast->first; external; external = external.next)
12474    {
12475       afterExternal = curExternal = external;
12476       if(external.type == functionExternal)
12477       {
12478          currentClass = external.function._class;
12479          ProcessFunction(external.function);
12480       }
12481       // There shouldn't be any _class member access here anyways...
12482       else if(external.type == declarationExternal)
12483       {
12484          currentClass = null;
12485          ProcessDeclaration(external.declaration);
12486       }
12487       else if(external.type == classExternal)
12488       {
12489          ClassDefinition _class = external._class;
12490          currentClass = external.symbol.registered;
12491          if(_class.definitions)
12492          {
12493             ProcessClass(_class.definitions, _class.symbol);
12494          }
12495          if(inCompiler)
12496          {
12497             // Free class data...
12498             ast->Remove(external);
12499             delete external;
12500          }
12501       }
12502       else if(external.type == nameSpaceExternal)
12503       {
12504          thisNameSpace = external.id.string;
12505       }
12506    }
12507    currentClass = null;
12508    thisNameSpace = null;
12509
12510    delete temp.symbol;
12511    delete temp;
12512 }