source: src/ResolvExpr/CommonType.cc @ f1149ac

Last change on this file since f1149ac was f1149ac, checked in by JiadaL <j82liang@…>, 3 months ago

Remove unused variables

  • Property mode set to 100644
File size: 51.2 KB
RevLine 
[a32b204]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[a436947]7// CommonType.cc --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 06:59:27 2015
11// Last Modified By : Peter A. Buhr
[ada4575]12// Last Modified On : Thu Feb 14 17:10:10 2019
13// Update Count     : 24
[a32b204]14//
[51b7345]15
[5bf3976]16#include "CommonType.hpp"
17
[e3e16bc]18#include <cassert>                       // for strict_dynamic_cast
[ea6332d]19#include <map>                           // for _Rb_tree_const_iterator
20#include <utility>                       // for pair
21
[ee574a2]22#include "AST/Decl.hpp"
[bccd70a]23#include "AST/Pass.hpp"
[ee574a2]24#include "AST/Type.hpp"
[d286cf68]25#include "Unify.h"                       // for unifyExact, WidenMode
[ea6332d]26#include "typeops.h"                     // for isFtype
[ef1da0e2]27#include "Tuples/Tuples.h"
[51b7345]28
[ba89e9b7]29// #define DEBUG
[9d1e3f7]30#ifdef DEBUG
31#define PRINT(x) x
32#else
33#define PRINT(x)
34#endif
[51b7345]35
36namespace ResolvExpr {
[cdcddfe1]37
[e15853c]38        // GENERATED START, DO NOT EDIT
[6fd1955]39        // GENERATED BY BasicTypes-gen.cc
[7d55e4d]40        #define BT ast::BasicType::
41        static const BT Kind commonTypes[BT NUMBER_OF_BASIC_TYPES][BT NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor
[96df1329]42                /*                                      B                       C                      SC                      UC                      SI                     SUI
43                                                        I                      UI                      LI                     LUI                     LLI                    LLUI
44                                                       IB                     UIB                     _FH                     _FH                      _F                     _FC
45                                                        F                      FC                     _FX                    _FXC                      FD                    _FDC
46                                                        D                      DC                    F80X                   _FDXC                     F80                     _FB
47                                                    _FLDC                      FB                      LD                     LDC                    _FBX                  _FLDXC
48                                 */
49                                  {
[3a55d9f]50                /*      B */                BT Bool,                BT Char,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
[96df1329]51                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
52                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
53                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
54                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
55                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
56                                  },
57                                  {
[3a55d9f]58                /*      C */                BT Char,                BT Char,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
[96df1329]59                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
60                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
61                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
62                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
63                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
64                                  },
65                                  {
[3a55d9f]66                /*     SC */          BT SignedChar,          BT SignedChar,          BT SignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
[96df1329]67                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
68                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
69                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
70                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
71                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
72                                  },
73                                  {
[3a55d9f]74                /*     UC */        BT UnsignedChar,        BT UnsignedChar,        BT UnsignedChar,        BT UnsignedChar,      BT ShortSignedInt,    BT ShortUnsignedInt,
[96df1329]75                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
76                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
77                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
78                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
79                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
80                                  },
81                                  {
[3a55d9f]82                /*     SI */      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,      BT ShortSignedInt,    BT ShortUnsignedInt,
[96df1329]83                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
84                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
85                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
86                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
87                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
88                                  },
89                                  {
[3a55d9f]90                /*    SUI */    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,    BT ShortUnsignedInt,
[96df1329]91                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
92                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
93                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
94                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
95                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
96                                  },
97                                  {
[3a55d9f]98                /*      I */           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,           BT SignedInt,
[96df1329]99                                             BT SignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
100                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
101                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
102                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
103                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
104                                  },
105                                  {
[3a55d9f]106                /*     UI */         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,         BT UnsignedInt,
[96df1329]107                                           BT UnsignedInt,         BT UnsignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
108                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
109                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
110                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
111                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
112                                  },
113                                  {
[3a55d9f]114                /*     LI */       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,
[96df1329]115                                         BT LongSignedInt,       BT LongSignedInt,       BT LongSignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
116                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
117                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
118                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
119                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
120                                  },
121                                  {
[3a55d9f]122                /*    LUI */     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,
[96df1329]123                                       BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,     BT LongUnsignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
124                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
125                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
126                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
127                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
128                                  },
129                                  {
[3a55d9f]130                /*    LLI */   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,
[96df1329]131                                     BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt,   BT LongLongSignedInt, BT LongLongUnsignedInt,
132                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
133                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
134                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
135                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
136                                  },
137                                  {
[3a55d9f]138                /*   LLUI */ BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
[96df1329]139                                   BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
140                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
141                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
142                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
143                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
144                                  },
145                                  {
[3a55d9f]146                /*     IB */        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,
[96df1329]147                                          BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,        BT SignedInt128,
148                                          BT SignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
149                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
150                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
151                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
152                                  },
153                                  {
[3a55d9f]154                /*    UIB */      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,
[96df1329]155                                        BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,      BT UnsignedInt128,
156                                        BT UnsignedInt128,      BT UnsignedInt128,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
157                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
158                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
159                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
160                                  },
161                                  {
[3a55d9f]162                /*    _FH */            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,
[96df1329]163                                              BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,            BT uFloat16,
164                                              BT uFloat16,            BT uFloat16,            BT uFloat16,     BT uFloat16Complex,            BT uFloat32,     BT uFloat32Complex,
165                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
166                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
167                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
168                                  },
169                                  {
[3a55d9f]170                /*    _FH */     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,
[96df1329]171                                       BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,
172                                       BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat16Complex,     BT uFloat32Complex,     BT uFloat32Complex,
173                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
174                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
175                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
176                                  },
177                                  {
[3a55d9f]178                /*     _F */            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,
[96df1329]179                                              BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,            BT uFloat32,
180                                              BT uFloat32,            BT uFloat32,            BT uFloat32,     BT uFloat32Complex,            BT uFloat32,     BT uFloat32Complex,
181                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
182                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
183                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
184                                  },
185                                  {
[3a55d9f]186                /*    _FC */     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
[96df1329]187                                       BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
188                                       BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,     BT uFloat32Complex,
189                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
190                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
191                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
192                                  },
193                                  {
[3a55d9f]194                /*      F */               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,
[96df1329]195                                                 BT Float,               BT Float,               BT Float,               BT Float,               BT Float,               BT Float,
196                                                 BT Float,               BT Float,               BT Float,        BT FloatComplex,               BT Float,        BT FloatComplex,
197                                                 BT Float,        BT FloatComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
198                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
199                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
200                                  },
201                                  {
[3a55d9f]202                /*     FC */        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
[96df1329]203                                          BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
204                                          BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,        BT FloatComplex,
205                                          BT FloatComplex,        BT FloatComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
206                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
207                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
208                                  },
209                                  {
[3a55d9f]210                /*    _FX */           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,
[96df1329]211                                             BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,           BT uFloat32x,
212                                             BT uFloat32x,           BT uFloat32x,           BT uFloat32x,    BT uFloat32xComplex,           BT uFloat32x,    BT uFloat32xComplex,
213                                             BT uFloat32x,    BT uFloat32xComplex,           BT uFloat32x,    BT uFloat32xComplex,            BT uFloat64,     BT uFloat64Complex,
214                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
215                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
216                                  },
217                                  {
[3a55d9f]218                /*   _FXC */    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
[96df1329]219                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
220                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,
221                                      BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,    BT uFloat32xComplex,     BT uFloat64Complex,     BT uFloat64Complex,
222                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
223                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
224                                  },
225                                  {
[3a55d9f]226                /*     FD */            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,
[96df1329]227                                              BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,            BT uFloat64,
228                                              BT uFloat64,            BT uFloat64,            BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,
229                                              BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,            BT uFloat64,     BT uFloat64Complex,
230                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
231                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
232                                  },
233                                  {
[3a55d9f]234                /*   _FDC */     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
[96df1329]235                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
236                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
237                                       BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,     BT uFloat64Complex,
238                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
239                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
240                                  },
241                                  {
[3a55d9f]242                /*      D */              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,
[96df1329]243                                                BT Double,              BT Double,              BT Double,              BT Double,              BT Double,              BT Double,
244                                                BT Double,              BT Double,              BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,
245                                                BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,              BT Double,       BT DoubleComplex,
246                                                BT Double,       BT DoubleComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
247                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
248                                  },
249                                  {
[3a55d9f]250                /*     DC */       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
[96df1329]251                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
252                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
253                                         BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,       BT DoubleComplex,
254                                         BT DoubleComplex,       BT DoubleComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
255                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
256                                  },
257                                  {
[3a55d9f]258                /*   F80X */           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,
[96df1329]259                                             BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,           BT uFloat64x,
260                                             BT uFloat64x,           BT uFloat64x,           BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,
261                                             BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,
262                                             BT uFloat64x,    BT uFloat64xComplex,           BT uFloat64x,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
263                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
264                                  },
265                                  {
[3a55d9f]266                /*  _FDXC */    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
[96df1329]267                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
268                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
269                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,
270                                      BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat64xComplex,    BT uFloat128Complex,
271                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
272                                  },
273                                  {
[3a55d9f]274                /*    F80 */           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,
[96df1329]275                                             BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,           BT uuFloat80,
276                                             BT uuFloat80,           BT uuFloat80,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,
277                                             BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,
278                                             BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,    BT uFloat64xComplex,           BT uuFloat80,           BT uFloat128,
279                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
280                                  },
281                                  {
[3a55d9f]282                /*    _FB */           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,
[96df1329]283                                             BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,           BT uFloat128,
284                                             BT uFloat128,           BT uFloat128,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,
285                                             BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,
286                                             BT uFloat128,    BT uFloat128Complex,           BT uFloat128,    BT uFloat128Complex,           BT uFloat128,           BT uFloat128,
287                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
288                                  },
289                                  {
[3a55d9f]290                /*  _FLDC */    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
[96df1329]291                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
292                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
293                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
294                                      BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,    BT uFloat128Complex,
295                                      BT uFloat128Complex,    BT uFloat128Complex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
296                                  },
297                                  {
[3a55d9f]298                /*     FB */          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,
[96df1329]299                                            BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,          BT uuFloat128,
300                                            BT uuFloat128,          BT uuFloat128,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,
301                                            BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,
302                                            BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,    BT uFloat128Complex,          BT uuFloat128,          BT uuFloat128,
303                                      BT uFloat128Complex,          BT uuFloat128,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
304                                  },
305                                  {
[3a55d9f]306                /*     LD */          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,
[96df1329]307                                            BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,          BT LongDouble,
308                                            BT LongDouble,          BT LongDouble,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,
309                                            BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,
310                                            BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,   BT LongDoubleComplex,          BT LongDouble,          BT LongDouble,
311                                     BT LongDoubleComplex,          BT LongDouble,          BT LongDouble,   BT LongDoubleComplex,          BT uFloat128x,   BT uFloat128xComplex,
312                                  },
313                                  {
[3a55d9f]314                /*    LDC */   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
[96df1329]315                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
316                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
317                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
318                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,
319                                     BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT LongDoubleComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
320                                  },
321                                  {
[3a55d9f]322                /*   _FBX */          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,
[96df1329]323                                            BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,          BT uFloat128x,
324                                            BT uFloat128x,          BT uFloat128x,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
325                                            BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
326                                            BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,          BT uFloat128x,
327                                     BT uFloat128xComplex,          BT uFloat128x,          BT uFloat128x,   BT uFloat128xComplex,          BT uFloat128x,   BT uFloat128xComplex,
328                                  },
329                                  {
[ef5b828]330                /* _FLDXC */   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
[96df1329]331                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
332                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
333                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
334                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
335                                     BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,   BT uFloat128xComplex,
336                                  },
[fd9ae1d]337        }; // commonTypes
[e15853c]338        #undef BT
339        // GENERATED END
[4ee3b0c1]340        static_assert(
[4ac402d]341                sizeof(commonTypes)/sizeof(commonTypes[0][0]) == ast::BasicType::NUMBER_OF_BASIC_TYPES * ast::BasicType::NUMBER_OF_BASIC_TYPES,
[4ee3b0c1]342                "Each basic type kind should have a corresponding row in the combined type matrix"
343        );
[a32b204]344
[2908f08]345class CommonType final : public ast::WithShortCircuiting {
346        const ast::Type * type2;
347        WidenMode widen;
348        ast::TypeEnvironment & tenv;
349        const ast::OpenVarSet & open;
350        ast::AssertionSet & need;
351        ast::AssertionSet & have;
352public:
353        static size_t traceId;
354        ast::ptr< ast::Type > result;
355
356        CommonType(
357                const ast::Type * t2, WidenMode w,
358                ast::TypeEnvironment & env, const ast::OpenVarSet & o,
359                ast::AssertionSet & need, ast::AssertionSet & have )
360        : type2( t2 ), widen( w ), tenv( env ), open( o ), need (need), have (have) ,result() {}
361
362        void previsit( const ast::Node * ) { visit_children = false; }
363
364        void postvisit( const ast::VoidType * ) {}
365
366        void postvisit( const ast::BasicType * basic ) {
367                if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
368                        ast::BasicType::Kind kind;
369                        if (basic->kind != basic2->kind && !widen.first && !widen.second) return;
370                        else if (!widen.first) kind = basic->kind; // widen.second
371                        else if (!widen.second) kind = basic2->kind;
372                        else kind = commonTypes[ basic->kind ][ basic2->kind ];
373                        // xxx - what does qualifiers even do here??
374                        if ( (basic->qualifiers >= basic2->qualifiers || widen.first)
375                                && (basic->qualifiers <= basic2->qualifiers || widen.second) ) {
376                                result = new ast::BasicType{ kind, basic->qualifiers | basic2->qualifiers };
377                        }
378                } else if (
379                        dynamic_cast< const ast::ZeroType * >( type2 )
380                        || dynamic_cast< const ast::OneType * >( type2 )
381                ) {
382                        if (widen.second) {
383                                result = new ast::BasicType{ basic->kind, basic->qualifiers | type2->qualifiers };
384                        }
385                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
386                        const ast::EnumDecl* enumDecl = enumInst->base;
387                        if ( enumDecl->base ) {
388                                result = enumDecl->base.get();
389                        } else {
390                                ast::BasicType::Kind kind = commonTypes[ basic->kind ][ ast::BasicType::SignedInt ];
391                                if (
392                                        ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
393                                                || widen.first )
394                                        && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
395                                                || widen.second )
396                                ) {
397                                        result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
[fc134a48]398                                }
[ee574a2]399                        }
[f1149ac]400                } else if ( dynamic_cast< const ast::EnumPosType * >( type2 ) ) {
[0522ebe]401                        ast::BasicType::Kind kind = commonTypes[ basic->kind ][ ast::BasicType::SignedInt ];
402                        if (
403                                ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
404                                        || widen.first )
405                                && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
406                                        || widen.second )
407                        ) {
408                                result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
409                        }
[ee574a2]410                }
[2908f08]411        }
[ee574a2]412
[2908f08]413private:
414        template< typename Pointer >
415        void getCommonWithVoidPointer( const Pointer * voidPtr, const Pointer * oPtr ) {
416                const ast::Type * base = oPtr->base;
417                if ( auto var = dynamic_cast< const ast::TypeInstType * >( base ) ) {
418                        auto entry = open.find( *var );
419                        if ( entry != open.end() ) {
420                                ast::AssertionSet need, have;
421                                if ( ! tenv.bindVar(
422                                        var, voidPtr->base, entry->second, need, have, open, widen )
423                                ) return;
[ee574a2]424                        }
425                }
[2908f08]426                result = voidPtr;
427                add_qualifiers( result, oPtr->qualifiers );
428        }
[ee574a2]429
[2908f08]430        // For a typed enum, we want to unify type1 with the base type of the enum
431        bool tryResolveWithTypedEnum( const ast::Type * type1 ) {
432                if (auto enumInst = dynamic_cast<const ast::EnumInstType *> (type2) ) {
433                        ast::OpenVarSet newOpen{ open };
434                        if (enumInst->base->base
[251ce80]435                                && unifyExact(type1, enumInst->base->base, tenv, need, have, newOpen, widen)) {
[fc134a48]436                                        result = type1;
[2908f08]437                                return true;
[fc134a48]438                        }
439                }
[2908f08]440                return false;
441        }
[fc134a48]442
[2908f08]443public:
444        void postvisit( const ast::PointerType * pointer ) {
445                if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
446                        if (
447                                widen.first
448                                && pointer2->base.as< ast::VoidType >()
449                                && ! ast::isFtype( pointer->base )
450                        ) {
451                                getCommonWithVoidPointer( pointer2, pointer );
452                        } else if (
453                                widen.second
454                                && pointer->base.as< ast::VoidType >()
455                                && ! ast::isFtype( pointer2->base )
456                        ) {
457                                getCommonWithVoidPointer( pointer, pointer2 );
458                        } else if (
459                                ( pointer->base->qualifiers >= pointer2->base->qualifiers || widen.first )
460                                && ( pointer->base->qualifiers <= pointer2->base->qualifiers || widen.second )
461                        ) {
462                                ast::CV::Qualifiers q1 = pointer->base->qualifiers;
463                                ast::CV::Qualifiers q2 = pointer2->base->qualifiers;
[ee574a2]464
[2908f08]465                                // force t{1,2} to be cloned if their qualifiers must be stripped, so that
466                                // pointer{,2}->base are unchanged
467                                ast::ptr< ast::Type > t1{ pointer->base }, t2{ pointer2->base };
468                                reset_qualifiers( t1 );
469                                reset_qualifiers( t2 );
[ef5b828]470
[2908f08]471                                ast::OpenVarSet newOpen{ open };
472                                if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
473                                        result = pointer;
474                                        if ( q1.val != q2.val ) {
475                                                // reset result->base->qualifiers to be union of two base qualifiers
476                                                strict_dynamic_cast< ast::PointerType * >(
477                                                        result.get_and_mutate()
478                                                )->base.get_and_mutate()->qualifiers = q1 | q2;
[ee574a2]479                                        }
[2908f08]480                                } else if ( isFtype (t1) && isFtype (t2) ) {
481                                        auto f1 = t1.as<ast::FunctionType>();
482                                        if (!f1) return;
483                                        auto f2 = t2.strict_as<ast::FunctionType>();
[ef1da0e2]484
[2908f08]485                                        assertf(f1->returns.size() <= 1, "Function return should not be a list");
486                                        assertf(f2->returns.size() <= 1, "Function return should not be a list");
[ef1da0e2]487
[2908f08]488                                        if (
489                                                ( f1->params.size() != f2->params.size() || f1->returns.size() != f2->returns.size() )
490                                                && ! f1->isTtype()
491                                                && ! f2->isTtype()
492                                        ) return;
[ef1da0e2]493
[2908f08]494                                        auto params1 = flattenList( f1->params, tenv );
495                                        auto params2 = flattenList( f2->params, tenv );
496
497                                        auto crnt1 = params1.begin();
498                                        auto crnt2 = params2.begin();
499                                        auto end1 = params1.end();
500                                        auto end2 = params2.end();
501
502                                        while (crnt1 != end1 && crnt2 != end2 ) {
503                                                const ast::Type * arg1 = *crnt1;
504                                                const ast::Type * arg2 = *crnt2;
505
506                                                bool isTuple1 = Tuples::isTtype( t1 );
507                                                bool isTuple2 = Tuples::isTtype( t2 );
508
509                                                // assumes here that ttype *must* be last parameter
510                                                if ( isTuple1 && ! isTuple2 ) {
511                                                        // combine remainder of list2, then unify
512                                                        if (unifyExact(
513                                                                arg1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
514                                                                noWiden() )) {
515                                                                        break;
516                                                        } else return;
517                                                } else if ( ! isTuple1 && isTuple2 ) {
518                                                        // combine remainder of list1, then unify
519                                                        if (unifyExact(
520                                                                tupleFromTypes( crnt1, end1 ), arg2, tenv, need, have, open,
521                                                                noWiden() )) {
522                                                                        break;
523                                                        } else return;
524                                                }
[ef1da0e2]525
[2908f08]526                                                // allow qualifiers of pointer and reference base to become more specific
527                                                if (auto ref1 = dynamic_cast<const ast::ReferenceType *> (arg1)) {
528                                                        if (auto ref2 = dynamic_cast<const ast::ReferenceType *> (arg2)) {
529                                                                ast::ptr<ast::Type> base1 = ref1->base;
530                                                                ast::ptr<ast::Type> base2 = ref2->base;
[ef1da0e2]531
[2908f08]532                                                                // xxx - assume LHS is always the target type
[ef1da0e2]533
[2908f08]534                                                                if ( ! ((widen.second && ref2->qualifiers.is_mutex) 
535                                                                || (ref1->qualifiers.is_mutex == ref2->qualifiers.is_mutex ))) return;
[ef1da0e2]536
[2908f08]537                                                                if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
[ef1da0e2]538
[2908f08]539                                                                        reset_qualifiers(base1);
540                                                                        reset_qualifiers(base2);
541
542                                                                        if ( !unifyExact(
543                                                                                base1, base2, tenv, need, have, open, noWiden() )
544                                                                        ) return;
[ef1da0e2]545                                                                }
[2908f08]546                                                        } else return;
547                                                } else if (auto ptr1 = dynamic_cast<const ast::PointerType *> (arg1)) {
548                                                        if (auto ptr2 = dynamic_cast<const ast::PointerType *> (arg2)) {
549                                                                ast::ptr<ast::Type> base1 = ptr1->base;
550                                                                ast::ptr<ast::Type> base2 = ptr2->base;
[ef1da0e2]551
[2908f08]552                                                                // xxx - assume LHS is always the target type
553                                                                // a function accepting const can always be called by non-const arg
[ef1da0e2]554
[2908f08]555                                                                if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
[ef1da0e2]556
[2908f08]557                                                                        reset_qualifiers(base1);
558                                                                        reset_qualifiers(base2);
[ef1da0e2]559
[2908f08]560                                                                        if ( ! unifyExact(
561                                                                                base1, base2, tenv, need, have, open, noWiden() )
562                                                                        ) return;
[ef1da0e2]563                                                                }
[2908f08]564                                                        } else return;
565                                                } else if (! unifyExact(
566                                                                arg1, arg2, tenv, need, have, open, noWiden() )) {
567                                                        return;
[ef1da0e2]568                                                }
[2908f08]569                                                ++crnt1; ++crnt2;
570                                        }
571                                        if ( crnt1 != end1 ) {
572                                                // try unifying empty tuple with ttype
573                                                const ast::Type * t1 = *crnt1;
574                                                if (! Tuples::isTtype( t1 ) ) return;
575                                                if (! unifyExact(
576                                                        t1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
577                                                        noWiden() )) return;
578                                        } else if ( crnt2 != end2 ) {
579                                                // try unifying empty tuple with ttype
580                                                const ast::Type * t2 = *crnt2;
581                                                if ( !Tuples::isTtype( t2 ) ) return;
582                                                if ( !unifyExact(
583                                                        tupleFromTypes( crnt1, end1 ), t2, tenv, need, have, open,
584                                                        noWiden() )) return;
585                                        }
586                                        if ((f1->returns.size() == 0 && f2->returns.size() == 0)
587                                          || (f1->returns.size() == 1 && f2->returns.size() == 1 && unifyExact(f1->returns[0], f2->returns[0], tenv, need, have, open, noWiden()))) {
588                                                result = pointer;
589
590                                                for (auto & assn : f1->assertions) {
591                                                        auto i = need.find(assn);
592                                                        if (i != need.end()) i->second.isUsed = true;
593                                                        auto j = have.find(assn);
594                                                        if (j != have.end()) j->second.isUsed = true;
[ef1da0e2]595                                                }
596
[2908f08]597                                                for (auto & assn : f2->assertions) {
598                                                        auto i = need.find(assn);
599                                                        if (i != need.end()) i->second.isUsed = true;
600                                                        auto j = have.find(assn);
601                                                        if (j != have.end()) j->second.isUsed = true;
[ef1da0e2]602                                                }
[2908f08]603                                        }
604                                } // if ftype
[ee574a2]605                        }
[2908f08]606                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
607                        result = pointer;
608                        add_qualifiers( result, type2->qualifiers );
609                } else {
610                        tryResolveWithTypedEnum( pointer );
[ee574a2]611                }
[2908f08]612        }
[ee574a2]613
[2908f08]614        void postvisit( const ast::ArrayType * arr ) {
615                // xxx - does it make sense?
616                tryResolveWithTypedEnum( arr );
617        }
[ee574a2]618
[2908f08]619        void postvisit( const ast::ReferenceType * ref ) {
620                if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
621                        if (
622                                widen.first && ref2->base.as< ast::VoidType >() && ! ast::isFtype( ref->base )
623                        ) {
624                                getCommonWithVoidPointer( ref2, ref );
625                        } else if (
626                                widen.second && ref->base.as< ast::VoidType>() && ! ast::isFtype( ref2->base )
627                        ) {
628                                getCommonWithVoidPointer( ref, ref2 );
629                        } else if (
630                                ( ref->base->qualifiers >= ref2->base->qualifiers || widen.first )
631                                && ( ref->base->qualifiers <= ref2->base->qualifiers || widen.second )
632                        ) {
633                                ast::CV::Qualifiers q1 = ref->base->qualifiers, q2 = ref2->base->qualifiers;
634
635                                // force t{1,2} to be cloned if their qualifiers must be stripped, so that
636                                // ref{,2}->base are unchanged
637                                ast::ptr< ast::Type > t1{ ref->base }, t2{ ref2->base };
638                                reset_qualifiers( t1 );
639                                reset_qualifiers( t2 );
640
641                                ast::OpenVarSet newOpen{ open };
642                                if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
643                                        result = ref;
644                                        if ( q1.val != q2.val ) {
645                                                // reset result->base->qualifiers to be union of two base qualifiers
646                                                strict_dynamic_cast< ast::ReferenceType * >(
647                                                        result.get_and_mutate()
648                                                )->base.get_and_mutate()->qualifiers = q1 | q2;
[ee574a2]649                                        }
650                                }
651                        }
[2908f08]652                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
653                        result = ref;
654                        add_qualifiers( result, type2->qualifiers );
655                } else {
656                        if (!dynamic_cast<const ast::EnumInstType *>(type2))
657                                result = commonType( type2, ref, tenv, need, have, open, widen );
[ee574a2]658                }
[2908f08]659        }
[ee574a2]660
[2908f08]661        void postvisit( const ast::FunctionType * func) {
662                tryResolveWithTypedEnum( func );
663        }
[ee574a2]664
[2908f08]665        void postvisit( const ast::StructInstType * inst ) {
666                tryResolveWithTypedEnum( inst );
667        }
[ee574a2]668
[2908f08]669        void postvisit( const ast::UnionInstType * inst ) {
670                tryResolveWithTypedEnum( inst );
671        }
[ee574a2]672
[2908f08]673        void postvisit( const ast::EnumInstType * enumInst ) {
674                if (!dynamic_cast<const ast::EnumInstType *>(type2))
675                        result = commonType( type2, enumInst, tenv, need, have, open, widen);
[ee574a2]676                }
677
[2908f08]678        void postvisit( const ast::TraitInstType * ) {}
[ee574a2]679
[2908f08]680        void postvisit( const ast::TypeInstType * ) {}
[ee574a2]681
[2908f08]682        void postvisit( const ast::TupleType * tuple ) {
683                tryResolveWithTypedEnum( tuple );
684        }
[ee574a2]685
[2908f08]686        void postvisit( const ast::VarArgsType * ) {}
[ee574a2]687
[2908f08]688        void postvisit( const ast::ZeroType * zero ) {
689                if ( !widen.first ) return;
690                if ( dynamic_cast< const ast::BasicType * >( type2 )
[fc134a48]691                                || dynamic_cast< const ast::PointerType * >( type2 ) ) {
[2908f08]692                        if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
693                                result = type2;
694                                add_qualifiers( result, zero->qualifiers );
695                        }
696                } else if ( widen.second && dynamic_cast< const ast::OneType * >( type2 ) ) {
697                        result = new ast::BasicType{
698                                ast::BasicType::SignedInt, zero->qualifiers | type2->qualifiers };
699                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
700                        const ast::EnumDecl * enumDecl = enumInst->base;
701                        if ( enumDecl->base ) {
702                                if ( tryResolveWithTypedEnum( zero ) )
703                                        add_qualifiers( result, zero->qualifiers );
704                        } else {
[ee574a2]705                                if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
706                                        result = type2;
707                                        add_qualifiers( result, zero->qualifiers );
708                                }
709                        }
710                }
[2908f08]711        }
[ee574a2]712
[2908f08]713        void postvisit( const ast::OneType * one ) {
714                if ( !widen.first ) return;
715                if ( dynamic_cast< const ast::BasicType * >( type2 ) ) {
716                        if ( widen.second || one->qualifiers <= type2->qualifiers ) {
717                                result = type2;
718                                add_qualifiers( result, one->qualifiers );
719                        }
720                } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
721                        result = new ast::BasicType{
722                                ast::BasicType::SignedInt, one->qualifiers | type2->qualifiers };
723                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
724                        const ast::EnumDecl * enumBase = enumInst->base;
725                        if ( enumBase->base ) {
726                                if ( tryResolveWithTypedEnum( one ))
727                                        add_qualifiers( result, one->qualifiers );
728                        } else {
[ee574a2]729                                if ( widen.second || one->qualifiers <= type2->qualifiers ) {
730                                        result = type2;
731                                        add_qualifiers( result, one->qualifiers );
732                                }
733                        }
734                }
735        }
[2908f08]736};
[ee574a2]737
[2908f08]738// size_t CommonType::traceId = Stats::Heap::new_stacktrace_id("CommonType");
739
740namespace {
741        ast::ptr< ast::Type > handleReference(
742                const ast::ptr< ast::Type > & t1, const ast::ptr< ast::Type > & t2, WidenMode widen,
743                ast::TypeEnvironment & env,
744                const ast::OpenVarSet & open
[ee574a2]745        ) {
[2908f08]746                ast::ptr<ast::Type> common;
747                ast::AssertionSet have, need;
748                ast::OpenVarSet newOpen{ open };
[ee574a2]749
[2908f08]750                // need unify to bind type variables
751                if ( unify( t1, t2, env, have, need, newOpen, common ) ) {
752                        ast::CV::Qualifiers q1 = t1->qualifiers, q2 = t2->qualifiers;
[ee574a2]753                        PRINT(
[2908f08]754                                std::cerr << "unify success: " << widenFirst << " " << widenSecond << std::endl;
[ee574a2]755                        )
[2908f08]756                        if ( ( widen.first || q2 <= q1 ) && ( widen.second || q1 <= q2 ) ) {
[ee574a2]757                                PRINT(
[2908f08]758                                        std::cerr << "widen okay" << std::endl;
[ee574a2]759                                )
[2908f08]760                                add_qualifiers( common, q1 | q2 );
761                                return common;
[ee574a2]762                        }
[2908f08]763                }
764
765                PRINT(
766                        std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl;
767                )
768                return { nullptr };
769        }
770}
771
772ast::ptr< ast::Type > commonType(
773        const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2,
774        ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
775        const ast::OpenVarSet & open, WidenMode widen
776) {
777        unsigned depth1 = type1->referenceDepth();
778        unsigned depth2 = type2->referenceDepth();
779
780        if ( depth1 != depth2 ) {  // implies depth1 > 0 || depth2 > 0
781                PRINT(
782                        std::cerr << "reference depth diff: " << (depth1-depth2) << std::endl;
783                )
784                ast::ptr< ast::Type > result;
785                const ast::ReferenceType * ref1 = type1.as< ast::ReferenceType >();
786                const ast::ReferenceType * ref2 = type2.as< ast::ReferenceType >();
787
788                if ( depth1 > depth2 ) {
789                        assert( ref1 );
790                        result = handleReference( ref1->base, type2, widen, env, open );
791                } else {  // Implies depth1 < depth2
792                        assert( ref2 );
793                        result = handleReference( type1, ref2->base, widen, env, open );
794                }
[ee574a2]795
[2908f08]796                if ( result && ref1 ) {
797                        // formal is reference, so result should be reference
[ee574a2]798                        PRINT(
[2908f08]799                                std::cerr << "formal is reference; result should be reference" << std::endl;
[ee574a2]800                        )
[2908f08]801                        result = new ast::ReferenceType{ result, ref1->qualifiers };
[ee574a2]802                }
803
[2908f08]804                PRINT(
805                        std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is "
806                        "[" << result << "]" << std::endl;
807                )
808                return result;
[ee574a2]809        }
[2908f08]810        // otherwise both are reference types of the same depth and this is handled by the visitor
811        ast::Pass<CommonType> visitor{ type2, widen, env, open, need, have };
812        type1->accept( visitor );
813        // ast::ptr< ast::Type > result = visitor.core.result;
814
815        return visitor.core.result;
816}
[ee574a2]817
[51b7345]818} // namespace ResolvExpr
[a32b204]819
820// Local Variables: //
821// tab-width: 4 //
822// mode: c++ //
823// compile-command: "make install" //
824// End: //
Note: See TracBrowser for help on using the repository browser.