1/* Check that protocol qualifiers are compiled and encoded properly.  */
2/* Author: Ziemowit Laski <zlaski@apple.com>  */
3
4/* { dg-do run } */
5/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include "../objc-obj-c++-shared/runtime.h"
10#include <objc/Protocol.h>
11
12/* The encoded parameter sizes will be rounded up to match pointer alignment.  */
13#define ROUND(s,a) (a * ((s + a - 1) / a))
14#define aligned_sizeof(T) ROUND(sizeof(T),__alignof(void *))
15
16#define CHECK_IF(expr) if(!(expr)) abort()
17
18@protocol Retain
19+ (oneway void)retainArgument:(out bycopy id)arg1 with:(in signed char **)arg2;
20- (bycopy) address:(byref inout id)location with:(out short unsigned **)arg2;
21@end
22
23@interface Foo <Retain>
24+ (oneway void)retainArgument:(out bycopy id)arg with:(in signed char **)arg2;
25@end
26
27@implementation Foo
28+ (oneway void)retainArgument:(out bycopy id)arg1 with:(in signed char **)arg2 { }
29- (bycopy) address:(byref inout id)location with:(out short unsigned **)arg2 { return nil; }
30@end
31
32Protocol *proto;
33struct objc_method_description *meth;
34struct objc_method_description meth_object;
35unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
36
37static void scan_initial(const char *pattern) {
38  totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
39  sscanf(meth->types, pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
40      &offs4, &offs5, &offs6, &offs7);
41  CHECK_IF(!offs0 && offs1 == aligned_sizeof(id) && offs2 == offs1 + aligned_sizeof(SEL) && totsize >= offs2);
42}
43
44int main(void) {
45  proto = @protocol(Retain);
46
47  meth_object = protocol_getMethodDescription (proto,
48		  @selector(address:with:), YES, YES);
49  meth = &meth_object;
50
51  scan_initial("O@%u@%u:%uNR@%uo^^S%u");
52  CHECK_IF(offs3 == offs2 + aligned_sizeof(id) && totsize == offs3 + aligned_sizeof(unsigned));
53
54  meth_object = protocol_getMethodDescription (proto,
55		  @selector(retainArgument:with:), YES, NO);
56  meth = &meth_object;
57
58  scan_initial("Vv%u@%u:%uOo@%un^*%u");
59  CHECK_IF(offs3 == offs2 + aligned_sizeof(id) && totsize == offs3 + aligned_sizeof(char **));
60  return 0;
61}
62