mojoshader_compiler.c
changeset 995 7e7826e191f5
parent 993 426ab31f27d1
child 996 164238a438e1
equal deleted inserted replaced
994:561ee5138f4e 995:7e7826e191f5
  2269     //   after possible type promotion via the following rules.
  2269     //   after possible type promotion via the following rules.
  2270     // - Scalars can be promoted to vectors to make a parameter match.
  2270     // - Scalars can be promoted to vectors to make a parameter match.
  2271     // - Scalars can promote to other scalars (short to int, etc).
  2271     // - Scalars can promote to other scalars (short to int, etc).
  2272     // - Vectors may NOT be promoted (a float2 can't extend to a float4).
  2272     // - Vectors may NOT be promoted (a float2 can't extend to a float4).
  2273     // - Vectors with the same elements can promote (a half2 can become a float2...I _think_ it can't downcast here.).
  2273     // - Vectors with the same elements can promote (a half2 can become a float2...I _think_ it can't downcast here.).
       
  2274     // - A perfect match of all params will be favored over any functions
       
  2275     //   that only match if type promotion is applied.
  2274     // - If more than one function matches after this (all params that
  2276     // - If more than one function matches after this (all params that
  2275     //   would be different between two functions are passed scalars)
  2277     //   would be different between two functions are passed scalars)
  2276     //   then fail().
  2278     //   then fail().
  2277 
  2279 
  2278     if (datatypes_match(arg, param))
  2280     if (datatypes_match(arg, param))
  2337         // there's a locally-scoped symbol with this name? It takes precedence.
  2339         // there's a locally-scoped symbol with this name? It takes precedence.
  2338         if (dt->type != MOJOSHADER_AST_DATATYPE_FUNCTION)
  2340         if (dt->type != MOJOSHADER_AST_DATATYPE_FUNCTION)
  2339             return dt;
  2341             return dt;
  2340 
  2342 
  2341         const MOJOSHADER_astDataTypeFunction *dtfn = (MOJOSHADER_astDataTypeFunction *) dt;
  2343         const MOJOSHADER_astDataTypeFunction *dtfn = (MOJOSHADER_astDataTypeFunction *) dt;
  2342         int this_match = 1;
  2344         int this_match = 2;  // 2 == perfect, 1 == compatible, 0 == not.
  2343         int i;
  2345         int i;
  2344 
  2346 
  2345         if (argcount != dtfn->num_params)  // !!! FIXME: default args.
  2347         if (argcount != dtfn->num_params)  // !!! FIXME: default args.
  2346             this_match = 0;
  2348             this_match = 0;
  2347         else
  2349         else
  2350             for (i = 0; i < argcount; i++)
  2352             for (i = 0; i < argcount; i++)
  2351             {
  2353             {
  2352                 assert(args != NULL);
  2354                 assert(args != NULL);
  2353                 dt = args->argument->datatype;
  2355                 dt = args->argument->datatype;
  2354                 args = args->next;
  2356                 args = args->next;
  2355                 if (!compatible_arg_datatype(ctx, dt, dtfn->params[i]))
  2357                 if (datatypes_match(dt, dtfn->params[i]))
  2356                 {
  2358                     continue;
  2357                     this_match = 0;  // can't be perfect match.
  2359 
       
  2360                 // not perfect, but maybe compatible?
       
  2361                 this_match = compatible_arg_datatype(ctx, dt, dtfn->params[i]);
       
  2362                 if (!this_match)
  2358                     break;
  2363                     break;
  2359                 } // if
       
  2360             } // for
  2364             } // for
  2361 
  2365 
  2362             if (args != NULL)
  2366             if (args != NULL)
  2363                 this_match = 0;  // too many arguments supplied. No match.
  2367                 this_match = 0;  // too many arguments supplied. No match.
  2364         } // else
  2368         } // else
  2365 
  2369 
  2366         if (this_match)
  2370         if (this_match == 2)  // perfect match.
       
  2371         {
       
  2372             match = 1;  // ignore all other compatible matches.
       
  2373             best = item;
       
  2374             break;
       
  2375         } // if
       
  2376 
       
  2377         else if (this_match == 1)  // compatible, but not perfect, match.
  2367         {
  2378         {
  2368             match++;
  2379             match++;
  2369             if (match == 1)
  2380             if (match == 1)
  2370                 best = item;
  2381                 best = item;
  2371             else
  2382             else
  2372             {
  2383             {
  2373                 if (match == 2)
  2384                 // !!! FIXME: list each possible function in a fail(),
  2374                     failf(ctx, "Ambiguous function call to '%s'", sym);
  2385                 // !!! FIXME:  but you can't actually fail() here, since
  2375                 // !!! FIXME: list each possible function in a fail() here.
  2386                 // !!! FIXME:  this will cease to be ambiguous if we get
       
  2387                 // !!! FIXME:  a perfect match on a later overload.
  2376             } // else
  2388             } // else
  2377         } // if
  2389         } // else if
  2378     } // while
  2390     } // while
  2379 
  2391 
       
  2392     if (match > 1)
       
  2393     {
       
  2394         assert(best != NULL);
       
  2395         failf(ctx, "Ambiguous function call to '%s'", sym);
       
  2396     } // if
       
  2397 
  2380     if (best == NULL)
  2398     if (best == NULL)
       
  2399     {
       
  2400         assert(best == NULL);
  2381         failf(ctx, "No matching function named '%s'", sym);
  2401         failf(ctx, "No matching function named '%s'", sym);
       
  2402     } // if
  2382     else
  2403     else
  2383     {
  2404     {
  2384         ident->datatype = reduce_datatype(ctx, best->datatype);
  2405         ident->datatype = reduce_datatype(ctx, best->datatype);
  2385         ident->index = best->index;
  2406         ident->index = best->index;
  2386     } // else
  2407     } // else