mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 07:21:14 +00:00
The size is in bits, so 8 is an unlikely value. Also, don't hardcode a size, ask the data layout for it.
222 lines
11 KiB
Plaintext
222 lines
11 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/class/field.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/class/field.carbon
|
|
|
|
// --- basic.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class C {
|
|
var a: i32;
|
|
var b: C*;
|
|
}
|
|
|
|
fn F(c: C) -> i32 {
|
|
return (*c.b).a;
|
|
}
|
|
|
|
fn Run() -> i32 {
|
|
var c: C;
|
|
c.a = 1;
|
|
c.b = &c;
|
|
return F(c);
|
|
}
|
|
|
|
// --- inplace_init_with_nonconst.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Other { }
|
|
|
|
class Use {
|
|
var a: Other*;
|
|
var b: Other;
|
|
}
|
|
|
|
fn Run() {
|
|
var o: Other;
|
|
// .a is initialized with a non-constant expression
|
|
// .b is initialized with in-place class_init
|
|
var _: Use = {.a = &o, .b = {}};
|
|
}
|
|
|
|
// --- implicit_init_with_nonempty_nonconst.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Other {
|
|
var v: i32;
|
|
}
|
|
|
|
class Use {
|
|
var a: Other*;
|
|
var b: Other;
|
|
}
|
|
|
|
fn Run() {
|
|
var o: Other;
|
|
// .a is initialized with a non-constant expression
|
|
// .b is initialized with in-place class_init
|
|
var _: Use = {.a = &o, .b = {.v = 42}};
|
|
}
|
|
|
|
// CHECK:STDOUT: ; ModuleID = 'basic.carbon'
|
|
// CHECK:STDOUT: source_filename = "basic.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define i32 @_CF.Main(ptr %c) #0 !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %.loc10_13.1.b = getelementptr inbounds nuw { i32, ptr }, ptr %c, i32 0, i32 1, !dbg !11
|
|
// CHECK:STDOUT: %.loc10_13.2 = load ptr, ptr %.loc10_13.1.b, align 8, !dbg !11
|
|
// CHECK:STDOUT: %.loc10_16.1.a = getelementptr inbounds nuw { i32, ptr }, ptr %.loc10_13.2, i32 0, i32 0, !dbg !12
|
|
// CHECK:STDOUT: %.loc10_16.2 = load i32, ptr %.loc10_16.1.a, align 4, !dbg !12
|
|
// CHECK:STDOUT: ret i32 %.loc10_16.2, !dbg !13
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define i32 @main() #0 !dbg !14 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %c.var = alloca { i32, ptr }, align 8, !dbg !17
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %c.var), !dbg !17
|
|
// CHECK:STDOUT: %.loc15_4.a = getelementptr inbounds nuw { i32, ptr }, ptr %c.var, i32 0, i32 0, !dbg !18
|
|
// CHECK:STDOUT: store i32 1, ptr %.loc15_4.a, align 4, !dbg !18
|
|
// CHECK:STDOUT: %.loc16.b = getelementptr inbounds nuw { i32, ptr }, ptr %c.var, i32 0, i32 1, !dbg !19
|
|
// CHECK:STDOUT: store ptr %c.var, ptr %.loc16.b, align 8, !dbg !19
|
|
// CHECK:STDOUT: %F.call = call i32 @_CF.Main(ptr %c.var), !dbg !20
|
|
// CHECK:STDOUT: ret i32 %F.call, !dbg !21
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: attributes #0 = { nounwind }
|
|
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
|
|
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
|
|
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
|
|
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "basic.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main", scope: null, file: !3, line: 9, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !9)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{!7, !8}
|
|
// CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
|
// CHECK:STDOUT: !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
|
|
// CHECK:STDOUT: !9 = !{!10}
|
|
// CHECK:STDOUT: !10 = !DILocalVariable(arg: 1, scope: !4, type: !8)
|
|
// CHECK:STDOUT: !11 = !DILocation(line: 10, column: 12, scope: !4)
|
|
// CHECK:STDOUT: !12 = !DILocation(line: 10, column: 10, scope: !4)
|
|
// CHECK:STDOUT: !13 = !DILocation(line: 10, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !14 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !15, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !15 = !DISubroutineType(types: !16)
|
|
// CHECK:STDOUT: !16 = !{!7}
|
|
// CHECK:STDOUT: !17 = !DILocation(line: 14, column: 3, scope: !14)
|
|
// CHECK:STDOUT: !18 = !DILocation(line: 15, column: 3, scope: !14)
|
|
// CHECK:STDOUT: !19 = !DILocation(line: 16, column: 3, scope: !14)
|
|
// CHECK:STDOUT: !20 = !DILocation(line: 17, column: 10, scope: !14)
|
|
// CHECK:STDOUT: !21 = !DILocation(line: 17, column: 3, scope: !14)
|
|
// CHECK:STDOUT: ; ModuleID = 'inplace_init_with_nonconst.carbon'
|
|
// CHECK:STDOUT: source_filename = "inplace_init_with_nonconst.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: @Other.val.loc15_33.5 = internal constant {} zeroinitializer
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define void @main() #0 !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %o.var = alloca {}, align 8, !dbg !7
|
|
// CHECK:STDOUT: %_.var = alloca { ptr, {} }, align 8, !dbg !8
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %o.var), !dbg !7
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %_.var), !dbg !8
|
|
// CHECK:STDOUT: %.loc15_33.2.a = getelementptr inbounds nuw { ptr, {} }, ptr %_.var, i32 0, i32 0, !dbg !9
|
|
// CHECK:STDOUT: store ptr %o.var, ptr %.loc15_33.2.a, align 8, !dbg !9
|
|
// CHECK:STDOUT: %.loc15_33.4.b = getelementptr inbounds nuw { ptr, {} }, ptr %_.var, i32 0, i32 1, !dbg !9
|
|
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.loc15_33.4.b, ptr align 1 @Other.val.loc15_33.5, i64 0, i1 false), !dbg !9
|
|
// CHECK:STDOUT: ret void, !dbg !10
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; uselistorder directives
|
|
// CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: attributes #0 = { nounwind }
|
|
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT: attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
|
|
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
|
|
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
|
|
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "inplace_init_with_nonconst.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 11, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{null}
|
|
// CHECK:STDOUT: !7 = !DILocation(line: 12, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !8 = !DILocation(line: 15, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !9 = !DILocation(line: 15, column: 16, scope: !4)
|
|
// CHECK:STDOUT: !10 = !DILocation(line: 11, column: 1, scope: !4)
|
|
// CHECK:STDOUT: ; ModuleID = 'implicit_init_with_nonempty_nonconst.carbon'
|
|
// CHECK:STDOUT: source_filename = "implicit_init_with_nonempty_nonconst.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: @Other.val.loc17_40.5 = internal constant { i32 } { i32 42 }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define void @main() #0 !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %o.var = alloca { i32 }, align 8, !dbg !7
|
|
// CHECK:STDOUT: %_.var = alloca { ptr, { i32 } }, align 8, !dbg !8
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %o.var), !dbg !7
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %_.var), !dbg !8
|
|
// CHECK:STDOUT: %.loc17_40.2.a = getelementptr inbounds nuw { ptr, { i32 } }, ptr %_.var, i32 0, i32 0, !dbg !9
|
|
// CHECK:STDOUT: store ptr %o.var, ptr %.loc17_40.2.a, align 8, !dbg !9
|
|
// CHECK:STDOUT: %.loc17_40.4.b = getelementptr inbounds nuw { ptr, { i32 } }, ptr %_.var, i32 0, i32 1, !dbg !9
|
|
// CHECK:STDOUT: %.loc17_39.3.v = getelementptr inbounds nuw { i32 }, ptr %.loc17_40.4.b, i32 0, i32 0, !dbg !10
|
|
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %.loc17_40.4.b, ptr align 4 @Other.val.loc17_40.5, i64 4, i1 false), !dbg !9
|
|
// CHECK:STDOUT: ret void, !dbg !11
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; uselistorder directives
|
|
// CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: attributes #0 = { nounwind }
|
|
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT: attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
|
|
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
|
|
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
|
|
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "implicit_init_with_nonempty_nonconst.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{null}
|
|
// CHECK:STDOUT: !7 = !DILocation(line: 14, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !8 = !DILocation(line: 17, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !9 = !DILocation(line: 17, column: 16, scope: !4)
|
|
// CHECK:STDOUT: !10 = !DILocation(line: 17, column: 31, scope: !4)
|
|
// CHECK:STDOUT: !11 = !DILocation(line: 13, column: 1, scope: !4)
|